def test_change_of_objective_is_reflected_in_low_level_solver(self): x = Variable('x', lb=-83.3, ub=1324422.) y = Variable('y', lb=-181133.3, ub=12000.) objective = Objective(0.3 * x + 0.4 * y, name='test', direction='max') self.model.objective = objective self.assertEqual((self.model.objective.expression - (0.4 * y + 0.3 * x)).expand() - 0, 0) self.assertEqual(self.model.objective.direction, "max") self.assertEqual(glp_get_obj_coef(self.model.problem, x._index), 0.3) self.assertEqual(glp_get_obj_coef(self.model.problem, y._index), 0.4) for i in range(1, glp_get_num_cols(self.model.problem) + 1): if i != x._index and i != y._index: self.assertEqual(glp_get_obj_coef(self.model.problem, i), 0) z = Variable('z', lb=4, ub=4, type='integer') self.model.objective += 77. * z self.assertEqual((self.model.objective.expression - (0.4 * y + 0.3 * x + 77.0 * z)).expand() - 0, 0) self.assertEqual(self.model.objective.direction, "max") self.assertEqual(glp_get_obj_coef(self.model.problem, x._index), 0.3) self.assertEqual(glp_get_obj_coef(self.model.problem, y._index), 0.4) self.assertEqual(glp_get_obj_coef(self.model.problem, z._index), 77.) for i in range(1, glp_get_num_cols(self.model.problem) + 1): if i != x._index and i != y._index and i != z._index: self.assertEqual(glp_get_obj_coef(self.model.problem, i), 0)
def test_constraint_set_problem_to_None_caches_the_latest_expression_from_solver_instance( self): x = Variable('x', lb=-83.3, ub=1324422.) y = Variable('y', lb=-181133.3, ub=12000.) constraint = Constraint(0.3 * x + 0.4 * y, lb=-100, name='test') self.model.add(constraint) z = Variable('z', lb=2, ub=5, type='integer') constraint += 77. * z self.model.remove(constraint) self.assertEqual(constraint.__str__(), 'test: -100 <= 0.4*y + 0.3*x + 77.0*z')
def setUp(self): self.var1 = var1 = Variable("var1", lb=0, ub=1, type="continuous") self.var2 = var2 = Variable("var2", lb=0, ub=1, type="continuous") self.const1 = const1 = Constraint(0.5 * var1, lb=0, ub=1, name="c1") self.const2 = const2 = Constraint(0.1 * var2 + 0.4 * var1, name="c2") self.model = model = Model() model.add([var1, var2]) model.add([const1, const2]) model.objective = Objective(var1 + var2) model.update() self.json_string = json.dumps(model.to_json())
def _GetDualVariablesAndConstants(self): # Define and apply the constraints on the concentrations ln_conc_lb, ln_conc_ub = self._MakeLnConcentratonBounds() # Create the driving force variable and add the relevant constraints A, b, c = self._MakeDrivingForceConstraints(ln_conc_lb, ln_conc_ub) w = [Variable('w%d' % i, lb=0) for i in range(self.Nr_active)] g = [Variable('g%d' % i, lb=0) for i in range(2 * self.Nr)] z = [Variable('z%d' % i, lb=0) for i in range(self.Nc)] u = [Variable('u%d' % i, lb=0) for i in range(self.Nc)] return A, b, c, w, g, z, u
def _GetPrimalVariablesAndConstants(self): # Define and apply the constraints on the concentrations ln_conc_lb, ln_conc_ub = self._MakeLnConcentratonBounds() # Create the driving force variable and add the relevant constraints A, b, c = self._MakeDrivingForceConstraints(ln_conc_lb, ln_conc_ub) # the dG'0 covariance eigenvariables y = [Variable("y%d" % i) for i in range(self.Nr)] # ln-concentration variables l = [Variable("l%d" % i) for i in range(self.Nc)] return A, b, c, y, l
def _MakeMDFProblem(self): """Create a CVXOPT problem for finding the Maximal Thermodynamic Driving Force (MDF). Does not set the objective function... leaves that to the caller. Returns: the linear problem object, and the three types of variables as arrays """ A, b, c, y, l = self._GetPrimalVariablesAndConstants() B = Variable('mdf') x = y + l + [B] lp = Model(name="MDF_PRIMAL") cnstr_names = ["driving_force_%02d" % j for j in range(self.Nr_active)] + \ ["covariance_var_ub_%02d" % j for j in range(self.Nr)] + \ ["covariance_var_lb_%02d" % j for j in range(self.Nr)] + \ ["log_conc_ub_%02d" % j for j in range(self.Nc)] + \ ["log_conc_lb_%02d" % j for j in range(self.Nc)] constraints = [] for j in range(A.shape[0]): row = [A[j, i] * x[i] for i in range(A.shape[1])] constraints.append( Constraint(sum(row), ub=b[j, 0], name=cnstr_names[j])) lp.add(constraints) row = [c[i, 0] * x[i] for i in range(c.shape[0])] lp.objective = Objective(sum(row), direction='max') return lp, y, l, B
def test_change_of_constraint_is_reflected_in_low_level_solver(self): x = Variable('x', lb=-83.3, ub=1324422.) y = Variable('y', lb=-181133.3, ub=12000.) constraint = Constraint(0.3 * x + 0.4 * y, lb=-100, name='test') self.assertEqual(constraint.index, None) self.model.add(constraint) self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x') self.assertEqual(constraint.index, 73) z = Variable('z', lb=3, ub=10, type='integer') self.assertEqual(z.index, None) constraint += 77. * z self.assertEqual(z.index, 98) self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x + 77.0*z') print(self.model) self.assertEqual(constraint.index, 73)
def test_change_of_constraint_is_reflected_in_low_level_solver(self): x = Variable('x', lb=-83.3, ub=1324422.) y = Variable('y', lb=-181133.3, ub=12000.) constraint = Constraint(0.3 * x + 0.4 * y, lb=-100, name='test') self.assertEqual(constraint._index, None) self.model.add(constraint) self.assertEqual((self.model.constraints["test"].expression - (0.4 * y + 0.3 * x)).expand(), 0) self.assertEqual(self.model.constraints["test"].lb, -100) self.assertEqual(constraint._index, 73) z = Variable('z', lb=3, ub=10, type='integer') self.assertEqual(z._index, None) constraint += 77. * z self.assertEqual(z._index, 98) self.assertEqual((self.model.constraints["test"].expression - (0.4 * y + 0.3 * x + 77.0 * z)).expand(), 0) self.assertEqual(self.model.constraints["test"].lb, -100) self.assertEqual(constraint._index, 73)
def test_add_non_cplex_conform_variable(self): var = Variable('12x!!@#5_3', lb=-666, ub=666) self.assertEqual(var._index, None) self.model.add(var) self.assertTrue(var in self.model.variables.values()) self.assertEqual(var.name, glp_get_col_name(self.model.problem, var._index)) self.assertEqual(self.model.variables['12x!!@#5_3'].lb, -666) self.assertEqual(self.model.variables['12x!!@#5_3'].ub, 666) repickled = pickle.loads(pickle.dumps(self.model)) var_from_pickle = repickled.variables['12x!!@#5_3'] self.assertEqual( var_from_pickle.name, glp_get_col_name(repickled.problem, var_from_pickle._index))
def test_add_constraints(self): x = Variable('x', lb=0, ub=1, type='binary') y = Variable('y', lb=-181133.3, ub=12000., type='continuous') z = Variable('z', lb=0., ub=10., type='integer') constr1 = Constraint(0.3 * x + 0.4 * y + 66. * z, lb=-100, ub=0., name='test') constr2 = Constraint(2.333 * x + y + 3.333, ub=100.33, name='test2') constr3 = Constraint(2.333 * x + y + z, lb=-300) constr4 = Constraint(x, lb=-300, ub=-300) constr5 = Constraint(3 * x) self.model.add(constr1) self.model.add(constr2) self.model.add(constr3) self.model.add([constr4, constr5]) self.assertIn(constr1.name, self.model.constraints) self.assertIn(constr2.name, self.model.constraints) self.assertIn(constr3.name, self.model.constraints) self.assertIn(constr4.name, self.model.constraints) self.assertIn(constr5.name, self.model.constraints) # constr1 ia = intArray(glp_get_num_rows(self.model.problem) + 1) da = doubleArray(glp_get_num_rows(self.model.problem) + 1) nnz = glp_get_mat_row(self.model.problem, constr1._index, ia, da) coeff_dict = dict() for i in range(1, nnz + 1): coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i] self.assertDictEqual(coeff_dict, {'x': 0.3, 'y': 0.4, 'z': 66.}) self.assertEqual(glp_get_row_type(self.model.problem, constr1._index), GLP_DB) self.assertEqual(glp_get_row_lb(self.model.problem, constr1._index), -100) self.assertEqual(glp_get_row_ub(self.model.problem, constr1._index), 0) # constr2 ia = intArray(glp_get_num_rows(self.model.problem) + 1) da = doubleArray(glp_get_num_rows(self.model.problem) + 1) nnz = glp_get_mat_row(self.model.problem, constr2._index, ia, da) coeff_dict = dict() for i in range(1, nnz + 1): coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i] self.assertDictEqual(coeff_dict, {'x': 2.333, 'y': 1.}) self.assertEqual(glp_get_row_type(self.model.problem, constr2._index), GLP_UP) self.assertEqual(glp_get_row_lb(self.model.problem, constr2._index), -1.7976931348623157e+308) self.assertEqual(glp_get_row_ub(self.model.problem, constr2._index), 96.997) # constr3 ia = intArray(glp_get_num_rows(self.model.problem) + 1) da = doubleArray(glp_get_num_rows(self.model.problem) + 1) nnz = glp_get_mat_row(self.model.problem, constr3._index, ia, da) coeff_dict = dict() for i in range(1, nnz + 1): coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i] self.assertDictEqual(coeff_dict, {'x': 2.333, 'y': 1., 'z': 1.}) self.assertEqual(glp_get_row_type(self.model.problem, constr3._index), GLP_LO) self.assertEqual(glp_get_row_lb(self.model.problem, constr3._index), -300) self.assertEqual(glp_get_row_ub(self.model.problem, constr3._index), 1.7976931348623157e+308) # constr4 ia = intArray(glp_get_num_rows(self.model.problem) + 1) da = doubleArray(glp_get_num_rows(self.model.problem) + 1) nnz = glp_get_mat_row(self.model.problem, constr4._index, ia, da) coeff_dict = dict() for i in range(1, nnz + 1): coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i] self.assertDictEqual(coeff_dict, {'x': 1}) self.assertEqual(glp_get_row_type(self.model.problem, constr4._index), GLP_FX) self.assertEqual(glp_get_row_lb(self.model.problem, constr4._index), -300) self.assertEqual(glp_get_row_ub(self.model.problem, constr4._index), -300) # constr5 ia = intArray(glp_get_num_rows(self.model.problem) + 1) da = doubleArray(glp_get_num_rows(self.model.problem) + 1) nnz = glp_get_mat_row(self.model.problem, constr5._index, ia, da) coeff_dict = dict() for i in range(1, nnz + 1): coeff_dict[glp_get_col_name(self.model.problem, ia[i])] = da[i] self.assertDictEqual(coeff_dict, {'x': 3}) self.assertEqual(glp_get_row_type(self.model.problem, constr5._index), GLP_FR) self.assertLess(glp_get_row_lb(self.model.problem, constr5._index), -1e30) self.assertGreater(glp_get_row_ub(self.model.problem, constr5._index), 1e30)