Skip to content
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
3 changes: 2 additions & 1 deletion cpmpy/expressions/globalconstraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ def __init__(self, *args: ExprLike | ListLike[ExprLike]):
Arguments:
args (ListLike[ExprLike]): List of expressions or constants to be different from each other, except those equal to 0
"""
super().__init__(flatlist(args), 0)
super().__init__(flatlist(args), [0])
self.name = "alldifferent_except_0"

class AllEqual(GlobalConstraint):
"""
Expand Down
10 changes: 6 additions & 4 deletions cpmpy/solvers/choco.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CPM_choco(SolverInterface):

"""

supported_global_constraints = frozenset({"alldifferent", "alldifferent_except0", "allequal",
supported_global_constraints = frozenset({"alldifferent", "alldifferent_except_0", "allequal",
"table", 'negative_table', "short_table", "regular", "indomain",
"cumulative", "no_overlap", "circuit", "gcc", "inverse", "precedence",
"increasing", "decreasing", "strictly_increasing", "strictly_decreasing",
Expand Down Expand Up @@ -569,12 +569,10 @@ def _get_constraint(self, cpm_expr):
elif isinstance(cpm_expr, GlobalConstraint):

# many globals require all variables as arguments
if cpm_expr.name in {"alldifferent", "alldifferent_except0", "allequal", "circuit", "inverse","increasing","decreasing","strictly_increasing","strictly_decreasing","lex_lesseq","lex_less"}:
if cpm_expr.name in {"alldifferent", "allequal", "circuit", "inverse","increasing","decreasing","strictly_increasing","strictly_decreasing","lex_lesseq","lex_less"}:
chc_args = self._to_vars(cpm_expr.args)
if cpm_expr.name == 'alldifferent':
return self.chc_model.all_different(chc_args)
elif cpm_expr.name == 'alldifferent_except0':
return self.chc_model.all_different_except_0(chc_args)
elif cpm_expr.name == 'allequal':
return self.chc_model.all_equal(chc_args)
elif cpm_expr.name == "circuit":
Expand All @@ -598,6 +596,10 @@ def _get_constraint(self, cpm_expr):
# return self.chc_model.lex_chain_less(chc_args)

# but not all
elif cpm_expr.name == 'alldifferent_except_0':
assert (len(cpm_expr.args) == 2) # args = [array, [0]]
assert (cpm_expr.args[1] == [0])
return self.chc_model.all_different_except_0(self._to_vars(cpm_expr.args[0]))
elif cpm_expr.name == 'table':
assert (len(cpm_expr.args) == 2) # args = [array, table]
array, table = self.solver_vars(cpm_expr.args)
Expand Down
13 changes: 9 additions & 4 deletions cpmpy/solvers/minizinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CPM_minizinc(SolverInterface):
https://minizinc-python.readthedocs.io/
"""

supported_global_constraints = frozenset({"alldifferent", "alldifferent_except0", "allequal",
supported_global_constraints = frozenset({"alldifferent", "alldifferent_except_0", "alldifferent_except_n", "allequal",
"inverse", "ite", "xor", "table", "indomain", "negative_table", "mdd", "regular", "cumulative", "circuit", "gcc",
"increasing", "decreasing",
"strictly_increasing", "strictly_decreasing", "lex_lesseq", "lex_less",
Expand Down Expand Up @@ -720,9 +720,14 @@ def zero_based(array):
str_rev = zero_based(expr.args[1])
return "inverse({}, {})".format(str_fwd, str_rev)

if expr.name == "alldifferent_except0":
args_str = [self._convert_expression(e) for e in expr.args]
return "alldifferent_except_0([{}])".format(",".join(args_str))
if expr.name == "alldifferent_except_0":
return "alldifferent_except_0({})".format(self._convert_expression(expr.args[0]))

if expr.name == "alldifferent_except_n":
arr, n = expr.args
arr_str = self._convert_expression(arr)
str_n = "{" + ",".join(self._convert_expression(v) for v in n) + "}"
return "alldifferent_except({}, {})".format(arr_str, str_n)

if expr.name in ["lex_lesseq", "lex_less"]:
X = [self._convert_expression(e) for e in expr.args[0]]
Expand Down
Loading