コード例 #1
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')
コード例 #2
0
 def test_gurobi_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)
     self.model.add(constr1)
     self.model.add(constr2)
     self.model.add(constr3)
     self.model.add(constr4)
     self.model.problem.update()
     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)
     # constr1
     coeff_dict = dict()
     internal_constraint = self.model.problem.getConstrByName(constr1.name)
     row = self.model.problem.getRow(internal_constraint)
     for i in range(row.size()):
         coeff_dict[row.getVar(i).VarName] = row.getCoeff(i)
     self.assertDictEqual(coeff_dict, {'x': 0.3, 'y': 0.4, 'z': 66., 'test_aux': -1.0})
     self.assertEqual(internal_constraint.RHS, constr1.lb)
     self.assertEqual(self.model.problem.getVarByName(internal_constraint.getAttr('ConstrName') + '_aux'), 100)
     # constr2
     coeff_dict = dict()
     internal_constraint = self.model.problem.getConstrByName(constr2.name)
     row = self.model.problem.getRow(internal_constraint)
     for i in range(row.size()):
         coeff_dict[row.getVar(i).VarName] = row.getCoeff(i)
     self.assertDictEqual(coeff_dict, {'x': 2.333, 'y': 1.})
     self.assertEqual(internal_constraint.RHS, constr2.ub)
     self.assertEqual(internal_constraint.Sense, '<')
     # constr3
     coeff_dict = dict()
     internal_constraint = self.model.problem.getConstrByName(constr3.name)
     print(internal_constraint)
     row = self.model.problem.getRow(internal_constraint)
     for i in range(row.size()):
         coeff_dict[row.getVar(i).VarName] = row.getCoeff(i)
     self.assertDictEqual(coeff_dict, {'x': 2.333, 'y': 1., 'z': 1.})
     self.assertEqual(internal_constraint.RHS, constr3.lb)
     self.assertEqual(internal_constraint.Sense, '>')
     # constr4
     coeff_dict = dict()
     internal_constraint = self.model.problem.getConstrByName(constr4.name)
     print(internal_constraint)
     row = self.model.problem.getRow(internal_constraint)
     for i in range(row.size()):
         coeff_dict[row.getVar(i).VarName] = row.getCoeff(i)
     self.assertDictEqual(coeff_dict, {'x': 1})
     self.assertEqual(internal_constraint.RHS, constr4.lb)
     self.assertEqual(internal_constraint.Sense, '=')
コード例 #3
0
 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._internal_constraint, None)
     self.model.add(constraint)
     self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x')
     z = Variable('z', lb=3, ub=10, type='integer')
     self.assertEqual(z._internal_variable, None)
     constraint += 77. * z
     self.assertEqual(z._internal_variable, self.model.problem.getVarByName('z'))
     self.assertEqual(self.model.constraints['test'].__str__(), 'test: -100 <= 0.4*y + 0.3*x + 77.0*z')
コード例 #4
0
 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])