Ejemplo n.º 1
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, 0)
     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, 96.997)
     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, -300)
     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, -300)
     self.assertEqual(internal_constraint.Sense, '=')
Ejemplo n.º 2
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])