Global Constraints (cpmpy.expressions.globalconstraints)
Global constraints conveniently express non-primitive constraints.
Using global constraints
Solvers can have specialised implementations for global constraints. CPMpy has GlobalConstraint
expressions so that they can be passed to the solver as is when supported.
If a solver does not support a global constraint (see Solver Interfaces) then it will be automatically
decomposed by calling its decompose() function.
The decompose() function returns two arguments:
a list of simpler constraints replacing the global constraint
- if the decomposition introduces new variables, then the second argument has to be a list
of constraints that (totally) define those new variables
As a user you should almost never subclass GlobalConstraint() unless you know of a solver that supports that specific global constraint, and that you will update its solver interface to support it.
For all other use cases, it sufficies to write your own helper function that immediately returns the decomposition, e.g.:
def alldifferent_except0(args):
return [ ((var1!= 0) & (var2 != 0)).implies(var1 != var2) for var1, var2 in all_pairs(args)]
Numeric global constraints
CPMpy also implements Numeric Global Constraints. For these, the CPMpy GlobalConstraint does not
exactly match what is implemented in the solver, but for good reason!!
For example solvers may implement the global constraint Minimum(iv1, iv2, iv3) == iv4 through an API
call addMinimumEquals([iv1,iv2,iv3], iv4).
However, CPMpy also wishes to support the expressions Minimum(iv1, iv2, iv3) > iv4 as well as
iv4 + Minimum(iv1, iv2, iv3).
Hence, the CPMpy global constraint only captures the Minimum(iv1, iv2, iv3) part, whose return type
is numeric and can be used in any other CPMpy expression. Only at the time of transforming the CPMpy
model to the solver API, will the expressions be decomposed and auxiliary variables introduced as needed
such that the solver only receives Minimum(iv1, iv2, iv3) == ivX expressions.
This is the burden of the CPMpy framework, not of the user who wants to express a problem formulation.
Subclassing GlobalConstraint
If you do wish to add a GlobalConstraint, because it is supported by solvers or because you will do
advanced analysis and rewriting on it, then preferably define it with a standard decomposition, e.g.:
class my_global(GlobalConstraint):
def __init__(self, args):
super().__init__("my_global", args)
def decompose(self):
return [self.args[0] != self.args[1]] # your decomposition
You can also implement a .negate() method if the global constraint has a better way to negate it than negating the decomposition.
If it is a GlobalFunction meaning that its return type is numeric (see Minimum and Element)
then set is_bool=False in the super() constructor and preferably implement .value() accordingly.
Alternative decompositions
For advanced use cases where you want to use another decomposition than the standard decomposition
of a GlobalConstraint expression, you can overwrite the decompose() function of the class, e.g.:
def my_circuit_decomp(self):
return [self.args[0] == 1], [] # does not actually enforce circuit
circuit.decompose = my_circuit_decomp # attach it, no brackets!
vars = intvar(1,9, shape=10)
constr = circuit(vars)
Model(constr).solve()
The above will use my_circuit_decomp, if the solver does not
natively support Circuit.
List of classes
Enforces that all arguments have a different (distinct) value |
|
Enforces that all arguments, except those equal to 0, have a different (distinct) value. |
|
Enforces that all arguments, except those equal to a value in n, have a different (distinct) value. |
|
Enforces that all arguments have the same value |
|
Enforces that all arguments, except those equal to a value in n, have the same value. |
|
Enforces that the sequence of variables form a circuit, where x[i] = j means that node j is the successor of node i. |
|
Enforces that the forward and reverse arrays represent the inverse function of one another. |
|
Enforces that the values of the variables in 'array' correspond to a row in 'table' |
|
Extension of the Table constraint where the table matrix may contain wildcards (STAR), meaning there are no restrictions for the corresponding variable in that tuple. |
|
The values of the variables in 'array' do not correspond to any row in 'table' |
|
Regular-constraint (or Automaton-constraint) Takes as input a sequence of variables and a automaton representation using a transition table. |
|
Enforces a conditional expression of the form: if condition then if_true else if_false. |
|
Enforces the expression is assigned to a value in the given domain. |
|
Enforces the exclusive-or relation of the arguments. |
|
Enforces that a set of tasks is scheduled such that the capacity of the resource is never exceeded and enforces: |
|
Enforces a precedence relationship between a set of variables. |
|
Enforces that a set of tasks are scheduled without overlapping, and enforces: |
|
Enforces that the number of occurrences of each value vals[i] in the list of variables vars is equal to occ[i]. |
|
Enforces that the expressions are assigned to (non-strictly) increasing values. |
|
Enforces that the expressions are assigned to (non-strictly) decreasing values. |
|
Enforces that the expressions are assigned to strictly increasing values. |
|
Enforces that the expressions are assigned to strictly decreasing values. |
|
Enforces that the first list is lexicographically smaller than the second list. |
|
Enforces that the first list is lexicographically smaller than or equal to the second list. |
|
Enforces that all rows of the matrix are lexicographically ordered. |
|
Enforces that all rows of the matrix are lexicographically ordered (less or equal) |
|
A |
- class cpmpy.expressions.globalconstraints.AllDifferent(*args: Expression)[source]
Enforces that all arguments have a different (distinct) value
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the AllDifferent global constraint using pairwise disequality constraints.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.AllDifferentExcept0(*args: Expression)[source]
Enforces that all arguments, except those equal to 0, have a different (distinct) value.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]]
Decomposition of the AllDifferentExceptN global constraint using pairwise constraints.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- value() bool | None
- Returns:
True if the global constraint is satisfied, False otherwise, or None if any argument is not assigned
- Return type:
Optional[bool]
- class cpmpy.expressions.globalconstraints.AllDifferentExceptN(arr: Sequence[Expression], n: int | list[int])[source]
Enforces that all arguments, except those equal to a value in n, have a different (distinct) value.
- Parameters:
arr (Sequence[Expression]) – List of expressions to be different from each other, except those equal to a value in n
n (int or list[int]) – Value or list of values that are excluded from satisfying the alldifferent condition
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the AllDifferentExceptN global constraint using pairwise constraints.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.AllEqual(*args: Expression)[source]
Enforces that all arguments have the same value
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the AllEqual global constraint using cascaded equality constraints.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.AllEqualExceptN(arr: Sequence[Expression], n: int | list[int])[source]
Enforces that all arguments, except those equal to a value in n, have the same value.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the AllEqualExceptN global constraint using pairwise constraints.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Circuit(*args: Expression)[source]
Enforces that the sequence of variables form a circuit, where x[i] = j means that node j is the successor of node i.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the Circuit global constraint using auxiliary variables to reprsent the order in which we visit all the nodes. Auxiliary variables are defined in the defining part of the decomposition, which is alwasy enforced top-level.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Cumulative(start: Sequence[Expression], duration: Sequence[Expression], end: Sequence[Expression] | None = None, demand: Sequence[Expression] | Expression | None = None, capacity: Expression | None = None)[source]
- Enforces that a set of tasks is scheduled such that the capacity of the resource is never exceeded and enforces:
duration >= 0
demand >= 0
start + duration == end
Equivalent to
NoOverlapwhen demand and capacity are equal to 1. Supports both varying demand across tasks or equal demand for all jobs.- property args
- decompose(how: str = 'auto') tuple[Sequence[Expression], Sequence[Expression]][source]
Decompose the Cumulative constraint Support time-based decomposition or task-based decomposition. By default, we heuristically select the best decomposition based on the number of tasks and the horizon.
- Parameters:
how (str) – how the cumulative constraint should be decomposed, can be “time”, “task”, or “auto” (default)
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Decreasing(*args: Expression)[source]
Enforces that the expressions are assigned to (non-strictly) decreasing values.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the Decreasing constraint.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.DecreasingStrict(*args: Expression)[source]
Enforces that the expressions are assigned to strictly decreasing values.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the DecreasingStrict constraint.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.DirectConstraint(name: str, arguments: tuple[Expression, ...], novar: Sequence[int] | None = None)[source]
A
DirectConstraintwill directly call a function of the underlying solver when added to a CPMpy solverIt can not be reified, it is not flattened, it can not contain other CPMpy expressions than variables. When added to a CPMpy solver, it will literally just directly call a function on the underlying solver, replacing CPMpy variables by solver variables along the way.
See the documentation of the solver (constructor) for details on how that solver handles them.
If you want/need to use what the solver returns (e.g. an identifier for use in other constraints), then use
directvar()instead, or access the solver object from the solver interface directly.- property args
- callSolver(CPMpy_solver: SolverInterface, Native_solver: Any)[source]
Call the directname() function of the native solver, with stored arguments replacing CPMpy variables with solver variables as needed.
SolverInterfaces will call this function when this constraint is added.
- Parameters:
CPMpy_solver – a CPM_solver object, that has a solver_vars() function
Native_solver – the python interface to some specific solver
- Returns:
the response of the solver when calling the function
- deepcopy(memodict={})
- get_bounds()
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- value()
- class cpmpy.expressions.globalconstraints.GlobalCardinalityCount(vars: Sequence[Expression], vals: list[int], occ: Sequence[Expression], closed: bool = False)[source]
Enforces that the number of occurrences of each value vals[i] in the list of variables vars is equal to occ[i].
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the GlobalCardinalityCount constraint. Uses a conjunction of Count global function constraints.
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.GlobalConstraint(name, arg_list)[source]
Abstract superclass of GlobalConstraints
Like all expressions it has a
.nameand.argsproperty. Overwrites the.is_bool()method as all global constraints are Boolean.- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Returns a decomposition into (a conjunction of) smaller constraints.
The decomposition might create auxiliary variables, it can also use other global constraints as long as it does not create a circular dependency.
To ensure equivalence of decomposition, we split into constraints determining the value of the global constraint, and defining-constraints. Defining constraints (totally) define new auxiliary variables needed for the decomposition, and can always be enforced at top-level.
Tip: avoid creating auxiliary variables and use nested expressions instead! (especially, don’t create Booleans but use (iv == v) expressions instead, better for common subexpression elimination!)
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()[source]
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool[source]
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()[source]
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- value()
- class cpmpy.expressions.globalconstraints.IfThenElse(condition: Expression, if_true: Expression, if_false: Expression)[source]
Enforces a conditional expression of the form: if condition then if_true else if_false. condition, if_true and if_false are be boolean expressions.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the IfThenElse global constraint. Enforces that the condition is satisfied. ” :returns: A tuple containing the constraints representing the constraint value and the defining constraints :rtype: tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()[source]
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.InDomain(expr: Expression, arr: list[int])[source]
Enforces the expression is assigned to a value in the given domain.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the InDomain global constraint. Enforces that the expression is assigned to a value in the given domain.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()[source]
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Increasing(*args: Expression)[source]
Enforces that the expressions are assigned to (non-strictly) increasing values.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the Increasing constraint.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.IncreasingStrict(*args: Expression)[source]
Enforces that the expressions are assigned to strictly increasing values.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the IncreasingStrict constraint.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Inverse(fwd: Sequence[Expression], rev: Sequence[Expression])[source]
Enforces that the forward and reverse arrays represent the inverse function of one another. I.e., fwd[i] == x <==> rev[x] == i
Also known as channeling / assignment constraint.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the Inverse global constraint using Element global function constraints, and explicit safening.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.LexChainLess(X: Sequence[Sequence[Expression]])[source]
Enforces that all rows of the matrix are lexicographically ordered.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the LexChainLess constraint.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.LexChainLessEq(X: Sequence[Sequence[Expression]])[source]
Enforces that all rows of the matrix are lexicographically ordered (less or equal)
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decompose to a series of LexLessEq constraints between subsequent rows
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.LexLess(list1: Sequence[Expression], list2: Sequence[Expression])[source]
Enforces that the first list is lexicographically smaller than the second list.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Implementation inspired by Hakan Kjellerstrand (http://hakank.org/cpmpy/cpmpy_hakank.py)
The decomposition creates auxiliary Boolean variables and constraints that collectively ensure X is lexicographically less than Y The auxiliary boolean vars are defined to represent if the given lists are lexicographically ordered (less or equal) up to the given index. Decomposition enforces through the constraining part that the first boolean variable needs to be true, and thus through the defining part it is enforced that if it is not strictly lexicographically less in a given index, then next index must be lexicographically less or equal. It needs to be strictly less in at least one index.
The use of auxiliary Boolean variables bvar ensures that the constraints propagate immediately, maintaining arc-consistency. Each bvar[i] enforces the lexicographic ordering at each position, ensuring that every value in the domain of X[i] can be extended to a consistent value in the domain of $Y_i$ for all subsequent positions.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.LexLessEq(list1: Sequence[Expression], list2: Sequence[Expression])[source]
Enforces that the first list is lexicographically smaller than or equal to the second list.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Implementation inspired by Hakan Kjellerstrand (http://hakank.org/cpmpy/cpmpy_hakank.py)
The decomposition creates auxiliary Boolean variables and constraints that collectively ensure X is lexicographically less than Y The auxiliary boolean vars are defined to represent if the given lists are lexicographically ordered (less or equal) up to the given index. Decomposition enforces through the constraining part that the first boolean variable needs to be true, and thus through the defining part it is enforced that if it is not strictly lexicographically less in a given index, then next index must be lexicographically less or equal.
The use of auxiliary Boolean variables bvar ensures that the constraints propagate immediately, maintaining arc-consistency. Each bvar[i] enforces the lexicographic ordering at each position, ensuring that every value in the domain of X[i] can be extended to a consistent value in the domain of $Y_i$ for all subsequent positions.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.NegativeTable(array: Sequence[Expression], table: list[list[int]])[source]
The values of the variables in ‘array’ do not correspond to any row in ‘table’
- property args
- decompose()[source]
Decomposition of the NegativeTable global constraint. Enforces that the values of the variables in ‘array’ do not correspond to any row in ‘table’.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()[source]
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.NoOverlap(start: Sequence[Expression], duration: Sequence[Expression], end: Sequence[Expression] | None = None)[source]
- Enforces that a set of tasks are scheduled without overlapping, and enforces:
duration >= 0
start + duration == end
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the NoOverlap constraint, using pairwise no-overlap constraints.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Precedence(vars: Sequence[Expression], precedence: list[int])[source]
Enforces a precedence relationship between a set of variables. Given an array of variables X and a list of values P, values in P must appear in X in the order specified by P. I.e., if X[i] = P[j+1], then there exists a X[i’] = P[j] with i’ < i
Examples
X = [1,2,1,3] satisfies the precedence [1,2,3].
X = [4,1,2,1,3] also satisfies the precedence, as values not appearing in P can appear in any order.
X = [2,1,3] does not satisfy the precedence, as 1 does not appear before 2.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition based on: Law, Yat Chiu, and Jimmy HM Lee. “Global constraints for integer and set value precedence.” Principles and Practice of Constraint Programming–CP 2004: 10th International Conference, CP 2004
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Regular(array: Sequence[Expression], transitions: list[tuple[int | str, int, int | str]], start: int | str, accepting: list[int | str])[source]
Regular-constraint (or Automaton-constraint) Takes as input a sequence of variables and a automaton representation using a transition table. The constraint is satisfied if the sequence of variables corresponds to an accepting path in the automaton.
The automaton is defined by a list of transitions, a starting node and a list of accepting nodes. The transitions are represented as a list of tuples, where each tuple is of the form (id1, value, id2). An id is an integer or string representing a state in the automaton, and value is an integer representing the value of the variable in the sequence. The starting node is an integer or string representing the starting state of the automaton. The accepting nodes are a list of integers or strings representing the accepting states of the automaton.
- Example: an automaton that accepts the language 0*10* (exactly 1 variable taking value 1) is defined as:
- cp.Regular(array = cp.intvar(0,1, shape=4),
transitions = [(“A”,0,”A”), (“A”,1,”B”), (“B”,0,”C”), (“C”,0,”C”)], start = “A”, accepting = [“C”])
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the Regular global constraint. Encodes the automaton by encoding the transition table into class:cpmpy.expressions.globalconstraints.Table constraints. Then enforces that the last state is accepting.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.ShortTable(array: Sequence[Expression], table: list[list[int]])[source]
Extension of the Table constraint where the table matrix may contain wildcards (STAR), meaning there are no restrictions for the corresponding variable in that tuple.
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the ShortTable global constraint. Enforces at least one row of the table is assigned to the array. ” :returns: A tuple containing the constraints representing the constraint value and the defining constraints :rtype: tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Table(array: Sequence[Expression], table: list[list[int]])[source]
Enforces that the values of the variables in ‘array’ correspond to a row in ‘table’
- property args
- decompose() tuple[Sequence[Expression], Sequence[Expression]][source]
Decomposition of the Table global constraint. Enforces at least one row of the table is assigned to the array. ” :returns: A tuple containing the constraints representing the constraint value and the defining constraints :rtype: tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()[source]
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- class cpmpy.expressions.globalconstraints.Xor(arg_list: Sequence[Expression])[source]
Enforces the exclusive-or relation of the arguments. Supports n-ary xor-constraints, which are treated as cascaed binary xor-constraints. Equivalent to sum(args) % 2 == 1
- property args
- decompose()[source]
Decomposition of the Xor global constraint. Recursively decomposes the constraint into a chain of binary xor-constraints, represented using a sum.
- Returns:
A tuple containing the constraints representing the constraint value and the defining constraints
- Return type:
tuple[Sequence[Expression], Sequence[Expression]]
- deepcopy(memodict={})
- get_bounds()
Returns the bounds of a Boolean global constraint. Numerical global constraints should reimplement this.
- has_subexpr()
Does it contains nested
Expressions(anything other than a_NumVarImplor a constant)? Is of importance when deciding whether certain transformations are needed along particular paths of the expression tree. Results are cached for future calls and reset when the expression changes (in-place argument update).
- implies(other)
- is_bool() bool
Returns whether the global constraint is a Boolean (return type) Operator.
- Returns:
True, global constraints are Boolean
- Return type:
bool
- negate()[source]
Returns the negation of this global constraint. Defaults to ~self, but subclasses can implement a better version, > Fages, François, and Sylvain Soliman. Reifying global constraints. Diss. INRIA, 2012.
- set_description(txt, override_print=True, full_print=False)
- update_args(args)
Allows in-place update of the expression’s arguments. Resets all cached computations which depend on the expression tree.
- cpmpy.expressions.globalconstraints.alldifferent(args: Sequence[Expression])[source]
Deprecated since version 0.9.0: Please use
AllDifferentinstead.