Class CpModel
Proposes a factory to create all modeling objects understood by the SAT solver.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Exception thrown when parallel arrays have mismatched lengths.static class
Exception thrown when an array has a wrong length. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAbsEquality
(LinearArgument target, LinearArgument expr) Addstarget == Abs(expr)
.addAllDifferent
(LinearArgument[] expressions) AddsAllDifferent(expressions)
.addAllDifferent
(Iterable<? extends LinearArgument> expressions) AddsAllDifferent(expressions)
.addAllowedAssignments
(IntVar[] variables) AddsAllowedAssignments(variables)
.addAllowedAssignments
(Iterable<IntVar> variables) AddsAllowedAssignments(variables)
.void
addAssumption
(Literal lit) Adds a literal to the model as assumptionvoid
addAssumptions
(Literal[] literals) Adds multiple literals to the model as assumptionsaddAtLeastOne
(Literal[] literals) Same as addBoolOr.addAtLeastOne
(Iterable<Literal> literals) Same as addBoolOr.addAtMostOne
(Literal[] literals) AddsAtMostOne(literals): Sum(literals) <= 1
.addAtMostOne
(Iterable<Literal> literals) AddsAtMostOne(literals): Sum(literals) <= 1
.addAutomaton
(IntVar[] transitionVariables, long startingState, long[] finalStates) Adds an automaton constraint.addBoolAnd
(Literal[] literals) AddsAnd(literals) == true
.addBoolAnd
(Iterable<Literal> literals) AddsAnd(literals) == true
.AddsOr(literals) == true
.AddsOr(literals) == true
.addBoolXor
(Literal[] literals) AddsXOr(literals) == true
.addBoolXor
(Iterable<Literal> literals) AddsXOr(literals) == true
.AddsCircuit()
.addCumulative
(long capacity) AddsCumulative(capacity)
.addCumulative
(LinearArgument capacity) AddsCumulative(capacity)
.void
addDecisionStrategy
(LinearArgument[] expressions, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr) AddsDecisionStrategy(expressions, varStr, domStr)
.void
addDecisionStrategy
(Iterable<? extends LinearArgument> expressions, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr) AddsDecisionStrategy(expressions, varStr, domStr)
.addDifferent
(LinearArgument expr, long value) Addsexpr != value
.addDifferent
(LinearArgument left, LinearArgument right) Addsleft != right
.addDivisionEquality
(LinearArgument target, LinearArgument num, LinearArgument denom) Addstarget == num / denom
, rounded towards 0.addElement
(IntVar index, int[] values, IntVar target) Adds the element constraint:values[index] == target
.addElement
(IntVar index, long[] values, IntVar target) Adds the element constraint:values[index] == target
.addElement
(IntVar index, IntVar[] variables, IntVar target) Adds the element constraint:variables[index] == target
.addEquality
(LinearArgument expr, long value) Addsexpr == value
.addEquality
(LinearArgument left, LinearArgument right) Addsleft == right
.addExactlyOne
(Literal[] literals) AddsExactlyOne(literals): Sum(literals) == 1
.addExactlyOne
(Iterable<Literal> literals) AddsExactlyOne(literals): Sum(literals) == 1
.addForbiddenAssignments
(IntVar[] variables) AddsForbiddenAssignments(variables)
.addForbiddenAssignments
(Iterable<IntVar> variables) AddsForbiddenAssignments(variables)
.addGreaterOrEqual
(LinearArgument expr, long value) Addsexpr >= value
.addGreaterOrEqual
(LinearArgument left, LinearArgument right) Addsleft >= right
.addGreaterThan
(LinearArgument expr, long value) Addsexpr > value
.addGreaterThan
(LinearArgument left, LinearArgument right) Addsleft > right
.void
Adds hinting to a variableaddImplication
(Literal a, Literal b) Addsa => b
.addInverse
(IntVar[] variables, IntVar[] inverseVariables) AddsInverse(variables, inverseVariables)
.addLessOrEqual
(LinearArgument expr, long value) Addsexpr <= value
.addLessOrEqual
(LinearArgument left, LinearArgument right) Addsleft <= right
.addLessThan
(LinearArgument expr, long value) Addsexpr < value
.addLessThan
(LinearArgument left, LinearArgument right) Addsleft < right
.addLinearConstraint
(LinearArgument expr, long lb, long ub) Addslb <= expr <= ub
.addLinearExpressionInDomain
(LinearArgument expr, Domain domain) Addsexpr in domain
.void
addMapDomain
(IntVar var, Literal[] booleans, long offset) Addsvar == i + offset <=> booleans[i] == true for all i in [0, booleans.length)
.addMaxEquality
(LinearArgument target, LinearArgument[] exprs) Addstarget == Max(vars)
.addMaxEquality
(LinearArgument target, Iterable<? extends LinearArgument> exprs) Addstarget == Max(exprs)
.addMinEquality
(LinearArgument target, LinearArgument[] exprs) Addstarget == Min(vars)
.addMinEquality
(LinearArgument target, Iterable<? extends LinearArgument> exprs) Addstarget == Min(exprs)
.addModuloEquality
(LinearArgument target, LinearArgument var, long mod) Addstarget == var % mod
.addModuloEquality
(LinearArgument target, LinearArgument var, LinearArgument mod) Addstarget == var % mod
.AddsMultipleCircuit()
.addMultiplicationEquality
(LinearArgument target, LinearArgument[] exprs) Addstarget == Product(exprs)
.addMultiplicationEquality
(LinearArgument target, LinearArgument left, LinearArgument right) Addstarget == left * right
.addNoOverlap
(IntervalVar[] intervalVars) AddsNoOverlap(intervalVars)
.addNoOverlap
(Iterable<IntervalVar> intervalVars) AddsNoOverlap(intervalVars)
.AddsNoOverlap2D(xIntervals, yIntervals)
.addReservoirConstraint
(long minLevel, long maxLevel) Adds a reservoir constraint with optional refill/emptying events.void
Remove all assumptions from the modelvoid
Remove all solution hintsvoid
Clears the objective.exportToFile
(String file) Write the model as a protocol buffer to 'file'.Returns the false literal.getBoolVarFromProtoIndex
(int index) Rebuilds a Boolean variable from an index.Returns the model builder.getClone()
getIntVarFromProtoIndex
(int index) Rebuilds an integer variable from an index.boolean
Checks if the model contains an objective.void
maximize
(DoubleLinearExpr expr) Adds a maximization objective of a linear expression.void
maximize
(LinearArgument expr) Adds a maximization objective of a linear expression.void
minimize
(DoubleLinearExpr expr) Adds a minimization objective of a linear expression.void
minimize
(LinearArgument expr) Adds a minimization objective of a linear expression.model()
Returns some statistics on model as a string.int
negated
(int index) newBoolVar
(String name) Creates a Boolean variable with the given name.newConstant
(long value) Creates a constant variable.newFixedInterval
(long start, long size, String name) Creates a fixed interval from its start and its size.newFixedSizeIntervalVar
(LinearArgument start, long size, String name) Creates an interval variable from an affine expression start, and a fixed size.newIntervalVar
(LinearArgument start, LinearArgument size, LinearArgument end, String name) Creates an interval variable from three affine expressions start, size, and end.Creates an integer variable with domain [lb, ub].newIntVarFromDomain
(Domain domain, String name) Creates an integer variable with given domain.newOptionalFixedInterval
(long start, long size, Literal isPresent, String name) Creates an optional fixed interval from start and size, and an isPresent literal.newOptionalFixedSizeIntervalVar
(LinearArgument start, long size, Literal isPresent, String name) Creates an optional interval variable from an affine expression start, and a fixed size.newOptionalIntervalVar
(LinearArgument start, LinearArgument size, LinearArgument end, Literal isPresent, String name) Creates an optional interval variable from three affine expressions start, size, end, and isPresent.Returns the true literal.validate()
Returns a non empty string explaining the issue if the model is invalid.
-
Constructor Details
-
CpModel
public CpModel()
-
-
Method Details
-
getClone
-
newIntVar
Creates an integer variable with domain [lb, ub]. -
newIntVarFromDomain
Creates an integer variable with given domain.- Parameters:
domain
- an instance of the Domain class.name
- the name of the variable- Returns:
- a variable with the given domain.
-
newBoolVar
Creates a Boolean variable with the given name. -
newConstant
Creates a constant variable. -
trueLiteral
Returns the true literal. -
falseLiteral
Returns the false literal. -
getBoolVarFromProtoIndex
Rebuilds a Boolean variable from an index. Useful after cloning a model. -
getIntVarFromProtoIndex
Rebuilds an integer variable from an index. Useful after cloning a model. -
addBoolOr
AddsOr(literals) == true
. -
addBoolOr
AddsOr(literals) == true
. -
addAtLeastOne
Same as addBoolOr.Sum(literals) >= 1
. -
addAtLeastOne
Same as addBoolOr.Sum(literals) >= 1
. -
addAtMostOne
AddsAtMostOne(literals): Sum(literals) <= 1
. -
addAtMostOne
AddsAtMostOne(literals): Sum(literals) <= 1
. -
addExactlyOne
AddsExactlyOne(literals): Sum(literals) == 1
. -
addExactlyOne
AddsExactlyOne(literals): Sum(literals) == 1
. -
addBoolAnd
AddsAnd(literals) == true
. -
addBoolAnd
AddsAnd(literals) == true
. -
addBoolXor
AddsXOr(literals) == true
. -
addBoolXor
AddsXOr(literals) == true
. -
addImplication
Addsa => b
. -
addLinearExpressionInDomain
Addsexpr in domain
. -
addLinearConstraint
Addslb <= expr <= ub
. -
addEquality
Addsexpr == value
. -
addEquality
Addsleft == right
. -
addLessOrEqual
Addsexpr <= value
. -
addLessOrEqual
Addsleft <= right
. -
addLessThan
Addsexpr < value
. -
addLessThan
Addsleft < right
. -
addGreaterOrEqual
Addsexpr >= value
. -
addGreaterOrEqual
Addsleft >= right
. -
addGreaterThan
Addsexpr > value
. -
addGreaterThan
Addsleft > right
. -
addDifferent
Addsexpr != value
. -
addDifferent
Addsleft != right
. -
addAllDifferent
AddsAllDifferent(expressions)
.This constraint forces all affine expressions to have different values.
- Parameters:
expressions
- a list of affine integer expressions- Returns:
- an instance of the Constraint class
-
addAllDifferent
AddsAllDifferent(expressions)
. -
addElement
Adds the element constraint:variables[index] == target
. -
addElement
Adds the element constraint:values[index] == target
. -
addElement
Adds the element constraint:values[index] == target
. -
addCircuit
AddsCircuit()
.Adds an empty circuit constraint.
A circuit is a unique Hamiltonian path in a subgraph of the total graph. In case a node 'i' is not in the path, then there must be a loop arc
'i -> i'
associated with a true literal. Otherwise this constraint will fail. -
addMultipleCircuit
AddsMultipleCircuit()
.Adds an empty multiple circuit constraint.
A multiple circuit is set of cycles in a subgraph of the total graph. The node index by 0 must be part of all cycles of length > 1. Each node with index > 0 belongs to exactly one cycle. If such node does not belong in any cycle of length > 1, then there must be a looping arc on this node attached to a literal that will be true. Otherwise, the constraint will fail.
-
addAllowedAssignments
AddsAllowedAssignments(variables)
.An AllowedAssignments constraint is a constraint on an array of variables that forces, when all variables are fixed to a single value, that the corresponding list of values is equal to one of the tuples of the tupleList.
- Parameters:
variables
- a list of variables- Returns:
- an instance of the TableConstraint class without any tuples. Tuples can be added directly to the table constraint.
-
addAllowedAssignments
AddsAllowedAssignments(variables)
. -
addForbiddenAssignments
AddsForbiddenAssignments(variables)
.A ForbiddenAssignments constraint is a constraint on an array of variables where the list of impossible combinations is provided in the tuples list.
- Parameters:
variables
- a list of variables- Returns:
- an instance of the TableConstraint class without any tuples. Tuples can be added directly to the table constraint.
-
addForbiddenAssignments
AddsForbiddenAssignments(variables)
. -
addAutomaton
public AutomatonConstraint addAutomaton(IntVar[] transitionVariables, long startingState, long[] finalStates) Adds an automaton constraint.An automaton constraint takes a list of variables (of size n), an initial state, a set of final states, and a set of transitions that will be added incrementally directly on the returned AutomatonConstraint instance. A transition is a triplet ('tail', 'transition', 'head'), where 'tail' and 'head' are states, and 'transition' is the label of an arc from 'head' to 'tail', corresponding to the value of one variable in the list of variables.
This automaton will be unrolled into a flow with n + 1 phases. Each phase contains the possible states of the automaton. The first state contains the initial state. The last phase contains the final states.
Between two consecutive phases i and i + 1, the automaton creates a set of arcs. For each transition (tail, label, head), it will add an arc from the state 'tail' of phase i and the state 'head' of phase i + 1. This arc labeled by the value 'label' of the variables 'variables[i]'. That is, this arc can only be selected if 'variables[i]' is assigned the value 'label'.
A feasible solution of this constraint is an assignment of variables such that, starting from the initial state in phase 0, there is a path labeled by the values of the variables that ends in one of the final states in the final phase.
- Parameters:
transitionVariables
- a non empty list of variables whose values correspond to the labels of the arcs traversed by the automatonstartingState
- the initial state of the automatonfinalStates
- a non empty list of admissible final states- Returns:
- an instance of the Constraint class
-
addInverse
AddsInverse(variables, inverseVariables)
.An inverse constraint enforces that if 'variables[i]' is assigned a value 'j', then inverseVariables[j] is assigned a value 'i'. And vice versa.
- Parameters:
variables
- an array of integer variablesinverseVariables
- an array of integer variables- Returns:
- an instance of the Constraint class
- Throws:
CpModel.MismatchedArrayLengths
- if variables and inverseVariables have different length
-
addReservoirConstraint
Adds a reservoir constraint with optional refill/emptying events.Maintain a reservoir level within bounds. The water level starts at 0, and at any time, it must be within [min_level, max_level].
Given an event (time, levelChange, active), if active is true, and if time is assigned a value t, then the level of the reservoir changes by levelChange (which is constant) at time t. Therefore, at any time t:
sum(levelChanges[i] * actives[i] if times[i] <= t) in [min_level, max_level]
Note that min level must be <= 0, and the max level must be >= 0. Please use fixed level_changes to simulate an initial state.
- Parameters:
minLevel
- at any time, the level of the reservoir must be greater of equal than the min level. minLevel must me <= 0.maxLevel
- at any time, the level of the reservoir must be less or equal than the max level. maxLevel must be >= 0.- Returns:
- an instance of the ReservoirConstraint class
- Throws:
IllegalArgumentException
- if minLevel > 0IllegalArgumentException
- if maxLevel < 0
-
addMapDomain
Addsvar == i + offset <=> booleans[i] == true for all i in [0, booleans.length)
. -
addMinEquality
Addstarget == Min(vars)
. -
addMinEquality
Addstarget == Min(exprs)
. -
addMaxEquality
Addstarget == Max(vars)
. -
addMaxEquality
Addstarget == Max(exprs)
. -
addDivisionEquality
public Constraint addDivisionEquality(LinearArgument target, LinearArgument num, LinearArgument denom) Addstarget == num / denom
, rounded towards 0. -
addAbsEquality
Addstarget == Abs(expr)
. -
addModuloEquality
Addstarget == var % mod
. -
addModuloEquality
Addstarget == var % mod
. -
addMultiplicationEquality
Addstarget == Product(exprs)
. -
addMultiplicationEquality
public Constraint addMultiplicationEquality(LinearArgument target, LinearArgument left, LinearArgument right) Addstarget == left * right
. -
newIntervalVar
public IntervalVar newIntervalVar(LinearArgument start, LinearArgument size, LinearArgument end, String name) Creates an interval variable from three affine expressions start, size, and end.An interval variable is a constraint, that is itself used in other constraints like NoOverlap.
Internally, it ensures that
start + size == end
.- Parameters:
start
- the start of the interval. It needs to be an affine or constant expression.size
- the size of the interval. It needs to be an affine or constant expression.end
- the end of the interval. It needs to be an affine or constant expression.name
- the name of the interval variable- Returns:
- An IntervalVar object
-
newFixedSizeIntervalVar
Creates an interval variable from an affine expression start, and a fixed size.An interval variable is a constraint, that is itself used in other constraints like NoOverlap.
- Parameters:
start
- the start of the interval. It needs to be an affine or constant expression.size
- the fixed size of the interval.name
- the name of the interval variable.- Returns:
- An IntervalVar object
-
newFixedInterval
Creates a fixed interval from its start and its size. -
newOptionalIntervalVar
public IntervalVar newOptionalIntervalVar(LinearArgument start, LinearArgument size, LinearArgument end, Literal isPresent, String name) Creates an optional interval variable from three affine expressions start, size, end, and isPresent.An optional interval variable is a constraint, that is itself used in other constraints like NoOverlap. This constraint is protected by an
isPresent
literal that indicates if it is active or not.Internally, it ensures that
isPresent => start + size == end
.- Parameters:
start
- the start of the interval. It needs to be an affine or constant expression.size
- the size of the interval. It needs to be an affine or constant expression.end
- the end of the interval. It needs to be an affine or constant expression.isPresent
- a literal that indicates if the interval is active or not. A inactive interval is simply ignored by all constraints.name
- The name of the interval variable- Returns:
- an IntervalVar object
-
newOptionalFixedSizeIntervalVar
public IntervalVar newOptionalFixedSizeIntervalVar(LinearArgument start, long size, Literal isPresent, String name) Creates an optional interval variable from an affine expression start, and a fixed size.An interval variable is a constraint, that is itself used in other constraints like NoOverlap.
- Parameters:
start
- the start of the interval. It needs to be an affine or constant expression.size
- the fixed size of the interval.isPresent
- a literal that indicates if the interval is active or not. A inactive interval is simply ignored by all constraints.name
- the name of the interval variable.- Returns:
- An IntervalVar object
-
newOptionalFixedInterval
Creates an optional fixed interval from start and size, and an isPresent literal. -
addNoOverlap
AddsNoOverlap(intervalVars)
.A NoOverlap constraint ensures that all present intervals do not overlap in time.
- Parameters:
intervalVars
- the list of interval variables to constrain- Returns:
- an instance of the Constraint class
-
addNoOverlap
AddsNoOverlap(intervalVars)
. -
addNoOverlap2D
AddsNoOverlap2D(xIntervals, yIntervals)
.A NoOverlap2D constraint ensures that all present rectangles do not overlap on a plan. Each rectangle is aligned with the X and Y axis, and is defined by two intervals which represent its projection onto the X and Y axis.
Furthermore, one box is optional if at least one of the x or y interval is optional.
- Returns:
- an instance of the NoOverlap2dConstraint class. This class allows adding rectangles incrementally.
-
addCumulative
AddsCumulative(capacity)
.This constraint enforces that:
forall t: sum(demands[i] if (start(intervals[t]) <= t < end(intervals[t])) and (t is present)) <= capacity
.- Parameters:
capacity
- the maximum capacity of the cumulative constraint. It must be a positive affine expression.- Returns:
- an instance of the CumulativeConstraint class. this class allows adding (interval, demand) pairs incrementally.
-
addCumulative
AddsCumulative(capacity)
.- See Also:
-
addHint
Adds hinting to a variable -
clearHints
public void clearHints()Remove all solution hints -
addAssumption
Adds a literal to the model as assumption -
addAssumptions
Adds multiple literals to the model as assumptions -
clearAssumptions
public void clearAssumptions()Remove all assumptions from the model -
minimize
Adds a minimization objective of a linear expression. -
minimize
Adds a minimization objective of a linear expression. -
maximize
Adds a maximization objective of a linear expression. -
maximize
Adds a maximization objective of a linear expression. -
clearObjective
public void clearObjective()Clears the objective. -
hasObjective
public boolean hasObjective()Checks if the model contains an objective. -
addDecisionStrategy
public void addDecisionStrategy(LinearArgument[] expressions, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr) AddsDecisionStrategy(expressions, varStr, domStr)
. -
addDecisionStrategy
public void addDecisionStrategy(Iterable<? extends LinearArgument> expressions, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr) AddsDecisionStrategy(expressions, varStr, domStr)
. -
modelStats
Returns some statistics on model as a string. -
validate
Returns a non empty string explaining the issue if the model is invalid. -
exportToFile
Write the model as a protocol buffer to 'file'.- Parameters:
file
- file to write the model to. If the filename ends with 'txt', the model will be written as a text file, otherwise, the binary format will be used.- Returns:
- true if the model was correctly written.
-
model
-
negated
public int negated(int index) -
getBuilder
Returns the model builder.
-