Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper translation of count to minizinc #462

Merged
merged 2 commits into from
May 2, 2024
Merged
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
20 changes: 6 additions & 14 deletions cpmpy/solvers/minizinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,20 +498,6 @@ def zero_based(array):
args_str = [self._convert_expression(e) for e in expr.args]
return "alldifferent_except_0({})".format(args_str)

# count: we need the lhs and rhs together
if isinstance(expr, Comparison) and expr.args[0].name == 'count':
name = expr.name
lhs, rhs = expr.args
c = self._convert_expression(rhs) # count
x = [self._convert_expression(countable) for countable in lhs.args[0]] # array
y = self._convert_expression(lhs.args[1]) # value to count in array
functionmap = {'==': 'count_eq', '!=': 'count_neq',
'<=': 'count_geq', '>=': 'count_leq',
'>': 'count_lt', '<': 'count_gt'}
if name in functionmap:
name = functionmap[name]
return "{}({},{},{})".format(name, x, y, c)

args_str = [self._convert_expression(e) for e in expr.args]
# standard expressions: comparison, operator, element
if isinstance(expr, Comparison):
Expand Down Expand Up @@ -608,6 +594,12 @@ def zero_based(array):
elif expr.name == "abs":
return "abs({})".format(args_str[0])

elif expr.name == "count":
vars, val = expr.args
vars = self._convert_expression(vars)
val = self._convert_expression(val)
return "count({},{})".format(vars, val)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. So, now we should not need the one at line 502 right?

# a direct constraint, treat differently for MiniZinc, a text-based language
# use the name as, unpack the arguments from the argument tuple
elif isinstance(expr, DirectConstraint):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,18 @@ 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_minizinc.supported(),
reason="Minizinc not installed")
def test_count_mzn(self):
# bug #461
from cpmpy.expressions.core import Operator

iv = cp.intvar(0,10, shape=3)
x = cp.intvar(0,1)
y = cp.intvar(0,1)
wsum = Operator("wsum", [[1,2,3],[x,y,cp.Count(iv,3)]])

m = cp.Model([x + y == 2, wsum == 9])
self.assertTrue(m.solve(solver="minizinc"))
Loading