Skip to content

Commit

Permalink
Constraint Precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
lecoutre committed Dec 27, 2021
1 parent 0dc39a4 commit 590b4f7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
7 changes: 3 additions & 4 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from pycsp3.functions import variant, subvariant, Var, VarArray, satisfy, minimize, maximize, annotate
from pycsp3.functions import And, Or, Not, Xor, IfThen, IfThenElse, Iff, Slide
from pycsp3.functions import protect, col, abs, min, max, xor, iff, imply, ift, expr, conjunction, disjunction
from pycsp3.functions import (AllDifferent, AllDifferentList, AllEqual, Increasing, Decreasing, LexIncreasing, LexDecreasing, Sum, Count, NValues, Cardinality,
Maximum, Minimum, Channel, NoOverlap, Cumulative, BinPacking, Circuit, Clause)
from pycsp3.functions import (AllDifferent, AllDifferentList, AllEqual, Increasing, Decreasing, LexIncreasing, LexDecreasing, Precedence, Sum, Count, NValues,
Cardinality, Maximum, Minimum, Channel, NoOverlap, Cumulative, BinPacking, Circuit, Clause)
from pycsp3.functions import posted, objective, unpost, value, values

from pycsp3.tools.curser import columns, diagonal_down, diagonals_down, diagonal_up, diagonals_up, cp_array
Expand Down Expand Up @@ -49,7 +49,6 @@
CHOCO = TypeSolver.CHOCO
""" Solver Choco """


if sys.argv:
if len(sys.argv) == 1 and sys.argv[0] == "-m": # copy of models
import shutil
Expand Down Expand Up @@ -188,7 +187,7 @@ def solve(*, solver=ACE, options="", filename=None, verbose=-1, sols=None, extra
if sols == ALL or isinstance(sols, int) and sols > 1:
options += " -xe -xc=false"
elif solver == CHOCO:
#options += " -v=" + str(verbose)
# options += " -v=" + str(verbose)
if sols == ALL or isinstance(sols, int) and sols > 1:
options += " -a "
_solver.setting(options)
Expand Down
3 changes: 2 additions & 1 deletion classes/auxiliary/ptypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class TypeCtrArg(AbstractType):
SIZES, WEIGTHS, PROFITS = auto(3)
SIZE, ROOT, IMAGE, GRAPH, ROW, EXPRESSION, TYPE = auto(7)
START_INDEX, START_ROW_INDEX, START_COL_INDEX = auto(3)
COVERED = auto()


@unique
Expand Down Expand Up @@ -206,4 +207,4 @@ class TypeStatus(Enum):
UNSAT, SAT, OPTIMUM, CORE, UNKNOWN = auto(5)

def __str__(self):
return self.name
return self.name
7 changes: 7 additions & 0 deletions classes/main/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ def __init__(self, lst, operator):
self.arg(TypeCtrArg.OPERATOR, operator)


class ConstraintPrecedence(Constraint):
def __init__(self, lst, values, covered=False):
super().__init__(TypeCtr.PRECEDENCE)
self.arg(TypeCtrArg.LIST, lst, content_ordered=True)
self.arg(TypeCtrArg.VALUES, values, attributes=[(TypeCtrArg.COVERED, "true")] if covered else [], content_ordered=True)


''' Counting and Summing Constraints '''


Expand Down
16 changes: 14 additions & 2 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
AnnotationRestarts)
from pycsp3.classes.main.constraints import (
ConstraintIntension, ConstraintExtension, ConstraintRegular, ConstraintMdd, ConstraintAllDifferent,
ConstraintAllDifferentList, ConstraintAllDifferentMatrix, ConstraintAllEqual, ConstraintOrdered, ConstraintLex, ConstraintLexMatrix, ConstraintSum,
ConstraintCount, ConstraintNValues, ConstraintCardinality, ConstraintMaximum, ConstraintMinimum, ConstraintChannel, ConstraintNoOverlap,
ConstraintAllDifferentList, ConstraintAllDifferentMatrix, ConstraintAllEqual, ConstraintOrdered, ConstraintLex, ConstraintLexMatrix, ConstraintPrecedence,
ConstraintSum, ConstraintCount, ConstraintNValues, ConstraintCardinality, ConstraintMaximum, ConstraintMinimum, ConstraintChannel, ConstraintNoOverlap,
ConstraintCumulative, ConstraintBinPacking, ConstraintCircuit, ConstraintClause, PartialConstraint, ScalarProduct, auxiliary, manage_global_indirection)
from pycsp3.classes.main.domains import Domain
from pycsp3.classes.main.objectives import ObjectiveExpression, ObjectivePartial
Expand Down Expand Up @@ -767,6 +767,18 @@ def LexDecreasing(term, *others, strict=False, matrix=False):
return _lex(term, others, TypeOrderedOperator.DECREASING if not strict else TypeOrderedOperator.STRICTLY_DECREASING, matrix)


def Precedence(scope, *, values, covered=False):
"""
Builds and returns a constraint Precedence.
:param scope: the scope of the constraint
:param values: the values such that the ith value must precede the i+1th value in the scope
:param covered: if True, all specified values must be assigned to the variables of the scope
:return: a constraint Precedence
"""
return ECtr(ConstraintPrecedence(scope, values, covered))


''' Method for handling complete/partial constraints '''


Expand Down

0 comments on commit 590b4f7

Please sign in to comment.