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='obj', direction='max') self.model.objective = objective for variable in self.model.variables: coeff = self.model.problem.objective.get_linear(variable.name) if variable.name == 'x': self.assertEqual(coeff, 0.3) elif variable.name == 'y': self.assertEqual(coeff, 0.4) else: self.assertEqual(coeff, 0.) z = Variable('z', lb=0.000003, ub=0.000003, type='continuous') objective += 77. * z for variable in self.model.variables: coeff = self.model.problem.objective.get_linear(variable.name) if variable.name == 'x': self.assertEqual(coeff, 0.3) elif variable.name == 'y': self.assertEqual(coeff, 0.4) elif variable.name == 'z': self.assertEqual(coeff, 77.) else: self.assertEqual(coeff, 0.)
def test_add_quadratic_constraints(self): x = Variable('x', lb=-83.3, ub=1324422., type='binary') y = Variable('y', lb=-181133.3, ub=12000., type='continuous') z = Variable('z', lb=0.000003, ub=0.000003, type='integer') constr1 = Constraint(0.3 * x * y + 0.4 * y**2 + 66. * z, lb=-100, ub=0., name='test') constr2 = Constraint(2.333 * x * x + y + 3.333, ub=100.33, name='test2') constr3 = Constraint(2.333 * x + y**2 + z + 33, ub=100.33, lb=-300) self.model.add(constr1) self.model.add(constr2) self.model.add(constr3) self.assertIn(constr1, self.model.constraints) self.assertIn(constr2, self.model.constraints) self.assertIn(constr3, self.model.constraints) cplex_lines = [ line.strip() for line in str(self.model).split('\n') ] self.assertIn('test: 0.4 y + 66 z + 0.3 x - Rgtest = -100', cplex_lines) self.assertIn('test2: y + 2.333 x <= 96.997', cplex_lines) # Dummy_21: y + z + 2.333 x - RgDummy_21 = -300 self.assertRegexpMatches( str(self.model), '\s*Dummy_\d+:\s*y \+ z \+ 2\.333 x - .* = -300') print(self.model)
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 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.model.add(constraint) self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x') self.assertEqual(self.model.problem.linear_constraints.get_coefficients([('test', 'x'), ('test', 'y')]), [0.3, 0.4]) z = Variable('z', lb=3, ub=4, type='integer') constraint += 77. * z self.assertEqual( self.model.problem.linear_constraints.get_coefficients([('test', 'x'), ('test', 'y'), ('test', 'z')]), [0.3, 0.4, 77.]) self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x + 77.0*z') print(self.model)
def test_add_non_cplex_conform_variable(self): var = Variable('12x!!@#5_3', lb=-666, ub=666) self.model.add(var) self.assertTrue(var in self.model.variables.values()) self.assertEqual(var.name, self.model.problem.variables.get_names(len(self.model.variables) - 1)) 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)) print(repickled.variables) var_from_pickle = repickled.variables['12x!!@#5_3'] self.assertEqual(var_from_pickle.name, self.model.problem.variables.get_names()[-1])
def test_set_linear_coefficients(self): self.model.add(self.constraint) self.constraint.set_linear_coefficients({ Variable('chip'): 33., self.model.variables.R_PGK: -33 }) sparse_pair = self.model.problem.linear_constraints.get_rows( self.constraint.name) self.assertEqual( dict( zip( self.model.problem.variables.get_names( sparse_pair.ind), sparse_pair.val)), dict([('R_PGK', -33.0), ('chap', 1.0), ('chip', 33.0)]))
def _createVI(self, scaled=False, scaling_factor=1): """ Create the V_i variables for each reaction for later addition to the optimization problem. The storage of these variables as an attribute avoids its creation with every iteration of the algorithm, thus improving its performance. scaled: boolean, whether or not to use a scaling factor, which is useful with some algorithms scaling factor: Returns: dictionary, variable name(string): variable object(optlang) """ if self._VI is None: v_i = {} for r_id, r in self.model.reactions.items( ): # be it reaction_id(string), reaction object(framed reaction) v = Variable(r_id, lb=r.lb, ub=r.ub) v_i[r_id] = v self._VI = v_i if scaled: scaledV_i = deepcopy(v_i) for var in scaledV_i.values(): if var.lb is not None: nlb = var.lb * scaling_factor else: nlb = None if var.ub is not None: nub = var.ub * scaling_factor else: nub = None var.set_bounds(lb=nlb, ub=nub) self._scaledVI = scaledV_i return self._VI, self._scaledVI else: if scaled: return self._VI, self._scaledVI else: return self._VI
def setUp(self): self.model = Model() self.x1 = Variable("x1", lb=0) self.x2 = Variable("x2", lb=0) self.c1 = Constraint(self.x1 + self.x2, lb=1) self.model.add([self.x1, self.x2, self.c1])