Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout4 committed Sep 6, 2023
1 parent 731bdd5 commit a4eda3d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cpmpy/solvers/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,13 @@ def __add__(self, cpm_expr_orig):
#integer division is not the same as floordivision, so we need to use continuous variables
cont_a = self.grb_model.addVar(x.lb, x.ub, vtype=GRB.CONTINUOUS, name=str(x.name) + '_cont')
cont_rhs = self.grb_model.addVar(rhs.lb, rhs.ub, vtype=GRB.CONTINUOUS, name=str(rhs.name) + '_cont')
abs_rhs = self.grb_model.addVar(rhs.lb, rhs.ub, vtype=GRB.CONTINUOUS, name=str(rhs.name) + '_cont_abs')
self.grb_model.addLConstr(cont_a/b, GRB.EQUAL, cont_rhs)
self.grb_model.addConstr(cont_a == a)
#grbrhs is the result of integer division, so it's the rounded down version of cont_rhs
self.grb_model.addLConstr(cont_rhs - grbrhs, GRB.LESS_EQUAL, 0.999999) #closest we can get to 1
self.grb_model.addLConstr(grbrhs - cont_rhs, GRB.LESS_EQUAL, 0)
#grbrhs is the result of integer division, so it's the rounded towards 0 version of cont_rhs
self.grb_model.addGenConstrAbs(abs_rhs, cont_rhs)
self.grb_model.addLConstr(abs_rhs - grbrhs, GRB.LESS_EQUAL, 0.99999) #closest we can get to 1
self.grb_model.addLConstr(grbrhs - abs_rhs, GRB.LESS_EQUAL, 0)

else:
# General constraints
Expand Down
11 changes: 11 additions & 0 deletions tests/test_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,3 +555,14 @@ def test_gurobi_element(self):
s = cp.SolverLookup.get("gurobi", model)
self.assertTrue(s.solve())
self.assertTrue(iv.value()[idx.value(), idx2.value()] == 8)

@pytest.mark.skipif(not CPM_gurobi.supported(),
reason="Gurobi not installed")
def test_gurobi_element(self):
iv = cp.intvar(-1, 3, shape=3)
x1 = cp.intvar(0, 3, name="x1")
cons = [x1 // 2 == iv, x1 > 0]
m = cp.Model(cons)
self.assertEqual(m.solveAll('gurobi', solution_limit=90),m.solveAll)
print('___')
Model(cons).solveAll(display=[x1, iv])

0 comments on commit a4eda3d

Please sign in to comment.