def test_ransom_seed(self): """Test setting random seeds. """ np.random.seed(12345) value = helpers.randcst() self.assertNotEqual(value, helpers.randcst()) np.random.seed(12345) self.assertEqual(value, helpers.randcst()) np.random.seed(12346) self.assertNotEqual(value, helpers.randcst())
def _make_xgen(self): xl = randint(-10, 0, self.param['n']) xu = randint(1, 10, self.param['n']) self.info['xl'] = npvec(xl) self.info['xu'] = npvec(xu) self.info['xgen'] = npvec( [(xl[i] + (xu[i] - xl[i]) * randcst())[0] for i in range(self.param['n'])])
def _make_ygen(self): self._make_u() num_double_at_upper = self.info[ 'md_upp_deg'] + self.info['md_upp_nonactive'] num_not_floating = self.info['md'] - self.info['md_float'] double_at_upper = npvec(self.u[:num_double_at_upper]) double_at_lower = zeros( self.info['md_low_deg'] + self.info['md_low_nonactive']) double_floating = npvec( [randcst() * x for x in self.u[num_not_floating:]]) single_at_lower = zeros( self.info['ms_deg'] + self.info['ms_nonactive']) single_floating = rand(self.info['ms_active']) # ygen = conmat([npvec(u[:m_upp_deg+upp_nonactive]), # double_at_upper # zeros(m_low_deg+low_nonactive), # double_at_lower # v2, # double_floating # zeros(m_inf_deg+inf_nonactive), # single_at_lower # rand(m_inf-m_inf_deg-inf_nonactive)]) # single_floating self.info['ygen'] = conmat([ double_at_upper, # y=u cases double_at_lower, # y=0 cases double_floating, # 0<y<u cases single_at_lower, # y=0 cases single_floating]) # y>0 cases for yi in self.info['ygen']: assert yi >= 0 for i in range(self.info['md']): assert self.info['ygen'][i] <= self.u[i]
def test(self): """ Check outputs. This test runs choose_num NUMBER_OF_TRIALS times. """ for _ in range(NUMBER_OF_TRIALS): m = helpers.randcst() self.assertGreaterEqual(m, 0) self.assertLessEqual(m, 1)
def _make_F_pi_sigma_index(self): N = self.N M = self.M u = self.u xgen = self.info['xgen'] ygen = self.info['ygen'] m = self.param['m'] # Design q so that Nx + My + E^Tlambda + q = 0 at the solution (xgen, # ygen) q = -N * xgen - M * ygen q += conmat([ # double bounded, degenerate at upper zeros(self.info['md_upp_deg']), # double bounded, nonactive at upper -rand(self.info['md_upp_nonactive']), # double bounded, degenerate at lower zeros(self.info['md_low_deg']), # double bounded, nonactive at lower rand(self.info['md_low_nonactive']), zeros(self.info['md_float']), # double bounded, floating # single bounded, degenerate at lower zeros(self.info['ms_deg']), # single bounded, nonactive at lower rand(self.info['ms_nonactive']), zeros(self.info['ms_active'])]) # single bounded, floating ######################################### ## For later convenience ## ######################################### F = N * xgen + M * ygen + q mix_deg = self.param['mix_deg'] tol_deg = self.param['tol_deg'] # Calculate three index sets alpha, beta and gamma at (xgen, ygen). # alpha denotes the index set of i at which F(i) is active, but y(i) not. # beta_upp and beta_low denote the index sets of i at which F(i) is # active, and y(i) is active at the upper and the lower end point of # the finite interval [0, u] respectively. # beta_inf denotes the index set of i at which both F(i) and y(i) are # active for the infinite interval [0, inf). # gamma_upp and gamma_low denote the index sets of i at which F(i) is # not active, but y(i) is active at the upper and the lower point of # the finite interval [0, u] respectively. # gamma_inf denotes the index set of i at which F(i) is not active, but y(i) # is active for the infinite interval [0, inf). index = [] md = self.info['md'] for i in range(md): assert ygen[i] >= -tol_deg and ygen[i] < u[i] + tol_deg, \ "{0} not in [0, {1}]".format(ygen[i], u[i]) if abs(F[i]) <= tol_deg and ygen[i] > tol_deg and ygen[i] + tol_deg < u[i]: index.append(1) # For the index set alpha. elif abs(F[i]) <= tol_deg and abs(ygen[i] - u[i]) <= tol_deg: index.append(2) # For the index set beta_upp. elif abs(F[i]) <= tol_deg and abs(ygen[i]) <= tol_deg: index.append(3) # For the index set beta_low. elif F[i] < -tol_deg and abs(ygen[i] - u[i]) <= tol_deg: index.append(-1) # For the index set gamma_upp. elif F[i] > tol_deg and abs(ygen[i]) <= tol_deg: index.append(-1) # For the index set gamma_low. else: raise Exception(("didn't know what to do with this case: " "ygen={0}, u[i] = {1}, F[i]={2}").format( ygen[i], u[i], F[i])) for i in range(md, m): if ygen[i] > F[i] + tol_deg: index.append(1) # For the index set alpha. elif abs(ygen[i] - F[i]) <= tol_deg: index.append(4) # For the index set beta_inf. else: index.append(-1) # For the index set gamma_inf. # Generate the first level multipliers pi sigma # associated with other constraints other than the first level constraints # A*[x;y]+a<=0 in the relaxed nonlinear program. In particular, # pi is associated with F(x, y)=N*x+M*y+q, and # sigma with y. mix_upp_deg = max( mix_deg - self.info['md_low_deg'] - self.info['ms_deg'], choose_num(self.info['md_upp_deg'])) mix_upp_deg = min(mix_upp_deg, mix_deg) mix_low_deg = max( mix_deg - mix_upp_deg - self.info['ms_deg'], choose_num(self.info['md_low_deg'])) mix_low_deg = min(mix_low_deg, mix_deg - mix_upp_deg) mix_inf_deg = mix_deg - mix_upp_deg - mix_low_deg mix_inf_deg = min(mix_inf_deg, mix_deg - mix_upp_deg - mix_inf_deg) assert mix_deg >= 0 assert mix_upp_deg >= 0 assert mix_low_deg >= 0 assert mix_inf_deg >= 0 # assert self.param['second_deg'] == self.info['ms_deg'] + # self.info['md_low_deg'] + self.info['md_upp_deg'] + mix_deg k_mix_inf = 0 k_mix_upp = 0 k_mix_low = 0 pi = zeros(m, 1) sigma = zeros(m, 1) for i in range(m): if index[i] == 1: pi[i] = randcst() - randcst() sigma[i] = 0 elif index[i] == 2: if k_mix_upp < mix_upp_deg: pi[i] = 0 # The first mix_upp_deg constraints associated with F(i)<=0 # in the set beta_upp are degenerate. sigma[i] = 0 # The first mix_upp_deg constraints associated with # y(i)<=u(i) in the set beta_upp are degenerate. k_mix_upp = k_mix_upp + 1 else: pi[i] = randcst() sigma[i] = randcst() elif index[i] == 3: if k_mix_low < mix_low_deg: pi[i] = 0 # The first mix_low_deg constraints associated with F(i)>=0 # in the set beta_low are degenerate. sigma[i] = 0 # The first mix_low_deg constraints associated with # y(i)>=0 in the set beta_low are degenerate. k_mix_low = k_mix_low + 1 else: pi[i] = -randcst() sigma[i] = -randcst() elif index[i] == 4: if k_mix_inf < mix_inf_deg: pi[i] = 0 # The first mix_inf_deg constraints associated with F(i)>=0 # in the set beta_inf are degenerate. sigma[i] = 0 # The first mix_inf_deg constraints associated with # y(i)>=0 in the set beta_inf are degenerate. k_mix_inf = k_mix_inf + 1 else: pi[i] = -randcst() sigma[i] = -randcst() else: pi[i] = 0 sigma[i] = randcst() - randcst() self.q = q self.info.update({ 'F': F, 'mix_upp_deg': mix_upp_deg, 'mix_low_deg': mix_low_deg, 'mix_inf_deg': mix_inf_deg, 'pi': pi, 'sigma': sigma})