- java.lang.Object
-
- org.chocosolver.solver.constraints.Constraint
-
- Direct Known Subclasses:
AllDifferent
,Arithmetic
,BooleanConstraint
,ClauseConstraint
,CondAllDifferent
,CostRegular
,Cumulative
,GlobalCardinality
,ImpliedConstraint
,LocalConstructiveDisjunction
,Member
,NotMember
,Opposite
,RealConstraint
,ReificationConstraint
,SatConstraint
,SumConstraint
,SumWithLongConstraint
public class Constraint extends java.lang.Object
A Constraint is basically a set ofPropagator
. It can either be posted or reified- Since:
- 0.01
- Version:
- major revision 13/01/2014
- Author:
- Jean-Guillaume Fages, Xavier Lorca, Charles Prud'homme
- See Also:
Variable
,Propagator
,PropagationEngine
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Constraint.Status
Status of this constraint wrt the model
-
Field Summary
Fields Modifier and Type Field Description protected BoolVar
boolReif
BoolVar that reifies this constraint, unique.protected Propagator<?>[]
propagators
Propagators of the constraint (they will filter domains and eventually check solutions)
-
Constructor Summary
Constructors Constructor Description Constraint(java.lang.String name, Propagator<?>... propagators)
Make a new constraint defined as a set of given propagators
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkNewStatus(Constraint.Status aStatus)
Check if the new status is not in conflict with the current onePropagatorPriority
computeMaxPriority()
void
declareAs(Constraint.Status aStatus, int idx)
For internal usage only, declare the status of this constraint in the model and, if need be, its position in the constraint list.int
getCidxInModel()
java.lang.String
getName()
Constraint
getOpposite()
Get the opposite constraint of this constraint.Propagator<?>
getPropagator(int i)
Propagator[]
getPropagators()
Return an array which contains the propagators declared inthis
.Constraint.Status
getStatus()
void
ignore()
When a constraint has been declared but neither posted or reified, a call toignore()
ensures this constraint will be ignored when declared constraints are checked.void
impliedBy(BoolVar r)
Encapsulate this constraint in an implication relationship:void
implies(BoolVar r)
Encapsulate this constraint in an implication relationship:boolean
isEnabled()
A constraint, when disabled, is prevented from execute propagation during search and from participate in the solution feasibility check.boolean
isReified()
ESat
isSatisfied()
Test if thisConstraint
object is satisfied, regarding itsPropagators
and itsVariable
current domains.protected Constraint
makeOpposite()
Make the opposite constraint of this.static Constraint
merge(java.lang.String name, Constraint... toMerges)
Creates a new constraint with all propagators of toMerge0, toMerge1 and toMerges.void
post()
Posts the constraint to its model so that the constraint must be satisfied.BoolVar
reify()
Get/make the boolean variable indicating whether the constraint is satisfied or not This should not be posted.void
reifyWith(BoolVar bool)
Reifies the constraint with a boolean variable If the reified boolean variable already exists, an additional (equality) constraint is automatically posted.void
setEnabled(boolean enabled)
Disable a constraint from being propagated during search and from feasibility check (Solver.isSatisfied()
).void
setName(java.lang.String newName)
Changes the name ofthis
constraintprotected void
setOpposite(Constraint opp)
java.lang.String
toString()
-
-
-
Field Detail
-
propagators
protected final Propagator<?>[] propagators
Propagators of the constraint (they will filter domains and eventually check solutions)
-
boolReif
protected BoolVar boolReif
BoolVar that reifies this constraint, unique.
-
-
Constructor Detail
-
Constraint
public Constraint(java.lang.String name, Propagator<?>... propagators)
Make a new constraint defined as a set of given propagators- Parameters:
name
- name of the constraintpropagators
- set of propagators defining the constraint
-
-
Method Detail
-
getPropagators
public Propagator[] getPropagators()
Return an array which contains the propagators declared inthis
.- Returns:
- an array of
Propagator
.
-
getPropagator
public Propagator<?> getPropagator(int i)
-
isSatisfied
public ESat isSatisfied()
Test if thisConstraint
object is satisfied, regarding itsPropagators
and itsVariable
current domains. This method is called on each solution as a checker when assertions are enabled (-ea in VM parameters) It is also called for constraint reification (to state whether or not a constraint is satisfied) The method calls entailment checks ofthis
propagators- Returns:
ESat.FALSE
if the constraint cannot be satisfied (from domain consideration),ESat.TRUE
if whatever future decisions are, the constraint will be satisfied for sure (without propagating domain modifications)ESat.UNDIFINED
otherwise (more decisions/filtering must be made before concluding about constraint satisfaction)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isReified
public final boolean isReified()
- Returns:
- true iff this constraint has been reified
-
reifyWith
public void reifyWith(BoolVar bool)
Reifies the constraint with a boolean variable If the reified boolean variable already exists, an additional (equality) constraint is automatically posted.- Parameters:
bool
- the variable to reify with
-
reify
public final BoolVar reify()
Get/make the boolean variable indicating whether the constraint is satisfied or not This should not be posted.- Returns:
- the boolean reifying the constraint
-
implies
public final void implies(BoolVar r)
Encapsulate this constraint in an implication relationship:c ⇒ r
where 'c' is this constraint and 'r' is a boolean variable.After a call to this method, this constraint 'c' can be posted, but a better option would be to set 'r' to true. 'c' can also be reified, but a better option would be to link the reifying boolean to 'r' directly.
- Parameters:
r
- a boolean variable
-
impliedBy
public final void impliedBy(BoolVar r)
Encapsulate this constraint in an implication relationship:r ⇒ c
where 'r' is a boolean variable and 'c' is this constraint.
After a call to this method, this constraint 'c' can be posted, but then 'r' can take any value. 'c' can also be reified, but a better option would be to link the reifying boolean to 'r' directly.
- Parameters:
r
- a boolean variable
-
post
public final void post()
Posts the constraint to its model so that the constraint must be satisfied. This should not be reified.
-
ignore
public final void ignore()
When a constraint has been declared but neither posted or reified, a call toignore()
ensures this constraint will be ignored when declared constraints are checked.
-
declareAs
public final void declareAs(Constraint.Status aStatus, int idx) throws SolverException
For internal usage only, declare the status of this constraint in the model and, if need be, its position in the constraint list.- Parameters:
aStatus
- status of this constraint in the modelidx
- position of this constraint in the constraint list.- Throws:
SolverException
- if the constraint a incoherent status is declared
-
checkNewStatus
public final void checkNewStatus(Constraint.Status aStatus) throws SolverException
Check if the new status is not in conflict with the current one- Parameters:
aStatus
- new status of the constraint- Throws:
SolverException
- if the constraint a incoherent status is declared
-
getStatus
public final Constraint.Status getStatus()
- Returns:
- the
Constraint.Status
of this constraint
-
getCidxInModel
public int getCidxInModel()
- Returns:
- the position of this constraint in the model
-
getOpposite
public Constraint getOpposite()
Get the opposite constraint of this constraint. At first call, it creates the opposite constraint, links them together (the opposite constraint of this opposite constraint is this constraint) and returns the opposite. Next calls will return the previously created opposite constraint. In other words, there can be only one opposite per instance of constraint. The default opposite constraint does not filter domains but fails if this constraint is satisfied.- Returns:
- the opposite constraint of this
-
setOpposite
protected void setOpposite(Constraint opp)
-
makeOpposite
protected Constraint makeOpposite()
Make the opposite constraint of this. BEWARE: this method should never be called by the user but it can be overridden to provide better constraint negations
-
setName
public void setName(java.lang.String newName)
Changes the name ofthis
constraint- Parameters:
newName
- the name of the constraint
-
getName
public java.lang.String getName()
- Returns:
- the name of
this
constraint
-
computeMaxPriority
public PropagatorPriority computeMaxPriority()
- Returns:
- the maximum priority of a propagator of this constraint
-
merge
public static Constraint merge(java.lang.String name, Constraint... toMerges)
Creates a new constraint with all propagators of toMerge0, toMerge1 and toMerges. Some constraints can be null but at least one propagator must be present.- Parameters:
name
- name of the new constrainttoMerges
- a set of constraints to merge in this (null are accepted)- Returns:
- a new constraint with all propagators of toMerge
- Throws:
java.lang.IllegalArgumentException
- when no propagator can be extracted
-
isEnabled
public boolean isEnabled()
A constraint, when disabled, is prevented from execute propagation during search and from participate in the solution feasibility check. It's handy to disable constraints for algorithms like (QuickXPlain
, that execute massive search to find a minimum conflicting set of constraints, and to do this needs to alternate constraints execution by enabling and disabling it.- Returns:
- enabled if the constraint is available to the solver
-
setEnabled
public void setEnabled(boolean enabled)
Disable a constraint from being propagated during search and from feasibility check (Solver.isSatisfied()
). A constraint shouldn't swap between enabled/disabled during solver execution (branching, filtering, etc...) because there is not control of the side effects it can cause (e.g.: when at node n, if a constraint becomes disabled, it doesn't undo filtering it has done at n-1). It means that, constraint should be disabled only before any interaction with the (Solver
) class to prevent side-effects.- Parameters:
enabled
- a boolean- Throws:
SolverException
- when setEnabled is called during solving
-
-