Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cpmpy/solvers/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ def solve(self, time_limit:Optional[float]=None, solution_callback=None, **kwarg
if cpm_var.is_bool():
cpm_var._value = solver_val >= 0.5
else:
cpm_var._value = int(solver_val)
cpm_var._value = round(solver_val)
# set _objective_value
if self.has_objective():
grb_obj_val = grb_objective.getValue()
if round(grb_obj_val) == grb_obj_val: # it is an integer?:
self.objective_value_ = int(grb_obj_val)
self.objective_value_ = round(grb_obj_val)
else: # can happen with DirectVar or when using floats as coefficients
self.objective_value_ = float(grb_obj_val)

Expand Down Expand Up @@ -580,7 +580,7 @@ def solveAll(self, display:Optional[Callback]=None, time_limit:Optional[float]=N
if cpm_var.is_bool():
cpm_var._value = solver_val >= 0.5
else:
cpm_var._value = int(solver_val)
cpm_var._value = round(solver_val)

# Translate objective
if self.has_objective():
Expand Down
19 changes: 19 additions & 0 deletions tests/test_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,3 +1251,22 @@ def test_objective_numexprs(solver, constraint):
assert constraint.value() > constraint.get_bounds()[0] # bounds are not always tight, but should be larger than lb for sure
except NotSupportedError:
pytest.skip(reason=f"{solver} does not support optimisation")

@pytest.mark.requires_solver("gurobi")
class TestGurobi:
def test_gurobi_read_integers_issue_858(self):
x = cp.intvar(1, 3, shape=2, name="x")
p = cp.intvar(0, 1, shape=3, name="p")
q = cp.intvar(0, 1, shape=3, name="q")
m = cp.Model()
m += x[0] == 1 # TODO without this, x[0] is assigned None because it does not occur in any constraint. This is a separate issue
m += cp.sum([p[0], p[1], p[2]]) == 1
m += cp.sum([3, 3, 1, -1] * cp.cpm_array([q[0], q[1], q[2], x[1]])) == 0
m += cp.sum([q[0], q[1], q[2]]) == 1

def check():
print(x, x.value())
assert (x[1].value() >= 1), f"{x[1]}={x.value()}"

m.solveAll(solver="gurobi", solution_limit=1000, display=check)

Loading