Skip to content

Commit

Permalink
globals to solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimosts committed May 22, 2024
1 parent 293964c commit 5a98d7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
17 changes: 8 additions & 9 deletions cpmpy/solvers/choco.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ 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","increasing_strict","decreasing_strict"}:
if cpm_expr.name in {"alldifferent", "alldifferent_except0", "allequal", "circuit", "inverse","increasing","decreasing","increasing_strict","decreasing_strict","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)
Expand All @@ -515,16 +515,15 @@ def _get_constraint(self, cpm_expr):
return self.chc_model.increasing(chc_args,1)
elif cpm_expr.name == "decreasing_strict":
return self.chc_model.decreasing(chc_args,1)
elif cpm_expr.name in ["lex_lesseq", "lex_less"]:
if cpm_expr.name == "lex_lesseq":
return self.chc_model.lex_less_eq(*chc_args)
return self.chc_model.lex_less(*chc_args)
# Ready for when it is fixed in pychoco
# elif cpm_expr.name == "lex_chain_less":
# return self.chc_model.lex_chain_less(chc_args)

# but not all
elif cpm_expr.name == "lex_lesseq":
assert (len(cpm_expr.args) == 2) # args = [X, Y]
X, Y = self.solver_vars(cpm_expr.args)
return self.chc_model.lex_less_eq(X, Y)
elif cpm_expr.name == "lex_less":
assert (len(cpm_expr.args) == 2) # args = [X, Y]
X, Y = self.solver_vars(cpm_expr.args)
return self.chc_model.lex_less(X,Y)
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: 11 additions & 2 deletions cpmpy/solvers/minizinc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from .solver_interface import SolverInterface, SolverStatus, ExitStatus
from ..exceptions import MinizincNameException, MinizincBoundsException
from ..expressions.core import Expression, Comparison, Operator, BoolVal
from ..expressions.variables import _NumVarImpl, _IntVarImpl, _BoolVarImpl, NegBoolView, intvar
from ..expressions.variables import _NumVarImpl, _IntVarImpl, _BoolVarImpl, NegBoolView, intvar, cpm_array
from ..expressions.globalconstraints import DirectConstraint
from ..expressions.utils import is_num, is_any_list, eval_comparison
from ..transformations.decompose_global import decompose_in_tree
Expand Down Expand Up @@ -418,7 +418,7 @@ def transform(self, cpm_expr):
"""
cpm_cons = toplevel_list(cpm_expr)
supported = {"min", "max", "abs", "element", "count", "nvalue", "alldifferent", "alldifferent_except0", "allequal",
"inverse", "ite" "xor", "table", "cumulative", "circuit", "gcc", "lex_lesseq", "lex_less"}
"inverse", "ite" "xor", "table", "cumulative", "circuit", "gcc", "lex_lesseq", "lex_less", "lex_chain_less", "lex_chain_lesseq"}
return decompose_in_tree(cpm_cons, supported, supported_reified=supported - {"circuit"})


Expand Down Expand Up @@ -507,6 +507,15 @@ def zero_based(array):
Y = [self._convert_expression(e) for e in expr.args[1]]
return f"{expr.name}({{}}, {{}})".format(X, Y)


if expr.name in ["lex_chain_less", "lex_chain_lesseq"]:
X = cpm_array([[self._convert_expression(e) for e in row] for row in expr.args])
str_X = "[|\n" # opening
for row in X.T: # Minizinc enforces lexicographic order on columns
str_X += ",".join(map(str, row)) + " |" # rows
str_X += "\n|]" # closing
return f"{expr.name}({{}})".format(str_X)

args_str = [self._convert_expression(e) for e in expr.args]
# standard expressions: comparison, operator, element
if isinstance(expr, Comparison):
Expand Down

0 comments on commit 5a98d7f

Please sign in to comment.