Class CpModel
public final class CpModel
extends java.lang.Object
Proposes a factory to create all modeling objects understood by the SAT solver.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCpModel.MismatchedArrayLengthsException thrown when parallel arrays have mismatched lengths.static classCpModel.WrongLengthException thrown when an array has a wrong length. -
Constructor Summary
Constructors Constructor Description CpModel() -
Method Summary
Modifier and Type Method Description ConstraintaddAbsEquality(IntVar target, IntVar var)Addstarget == Abs(var).ConstraintaddAllDifferent(IntVar[] variables)AddsAllDifferent(variables).ConstraintaddAllowedAssignments(IntVar[] variables, int[][] tuplesList)AddsAllowedAssignments(variables, tuplesList).ConstraintaddAllowedAssignments(IntVar[] variables, long[][] tuplesList)AddsAllowedAssignments(variables, tuplesList).voidaddAssumption(Literal lit)Adds a literal to the model as assumptionvoidaddAssumptions(Literal[] literals)Adds multiple literals to the model as assumptionsConstraintaddAutomaton(IntVar[] transitionVariables, long startingState, long[] finalStates, long[][] transitions)Adds an automaton constraint.ConstraintaddBoolAnd(Literal[] literals)AddsAnd(literals) == true.ConstraintaddBoolOr(Literal[] literals)AddsOr(literals) == true.ConstraintaddBoolXor(Literal[] literals)AddsXOr(literals) == true.ConstraintaddCircuit(int[] tails, int[] heads, Literal[] literals)AddsCircuit(tails, heads, literals).ConstraintaddCumulative(IntervalVar[] intervals, int[] demands, long capacity)AddsCumulative(intervals, demands, capacity)with fixed demands and fixed capacity.ConstraintaddCumulative(IntervalVar[] intervals, int[] demands, IntVar capacity)AddsCumulative(intervals, demands, capacity)with fixed demands.ConstraintaddCumulative(IntervalVar[] intervals, long[] demands, long capacity)AddsCumulative(intervals, demands, capacity)with fixed demands and fixed capacity.ConstraintaddCumulative(IntervalVar[] intervals, long[] demands, IntVar capacity)AddsCumulative(intervals, demands, capacity)with fixed demands.ConstraintaddCumulative(IntervalVar[] intervals, IntVar[] demands, long capacity)AddsCumulative(intervals, demands, capacity)with fixed capacity.ConstraintaddCumulative(IntervalVar[] intervals, IntVar[] demands, IntVar capacity)AddsCumulative(intervals, demands, capacity).voidaddDecisionStrategy(IntVar[] variables, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr)AddsDecisionStrategy(variables, varStr, domStr).ConstraintaddDifferent(IntVar left, IntVar right)Addsleft != right.ConstraintaddDifferent(LinearExpr expr, long value)Addsexpr != value.ConstraintaddDifferentWithOffset(IntVar left, IntVar right, long offset)Addsleft + offset != right.ConstraintaddDivisionEquality(IntVar target, IntVar num, IntVar denom)Addstarget == num / denom, rounded towards 0.ConstraintaddElement(IntVar index, int[] values, IntVar target)Adds the element constraint:values[index] == target.ConstraintaddElement(IntVar index, long[] values, IntVar target)Adds the element constraint:values[index] == target.ConstraintaddElement(IntVar index, IntVar[] variables, IntVar target)Adds the element constraint:variables[index] == target.ConstraintaddEquality(LinearExpr expr, long value)Addsexpr == value.ConstraintaddEquality(LinearExpr left, LinearExpr right)Addsleft == right.ConstraintaddEqualityWithOffset(LinearExpr left, LinearExpr right, long offset)Addsleft + offset == right.ConstraintaddForbiddenAssignments(IntVar[] variables, int[][] tuplesList)AddsForbiddenAssignments(variables, tuplesList).ConstraintaddForbiddenAssignments(IntVar[] variables, long[][] tuplesList)AddsForbiddenAssignments(variables, tuplesList).ConstraintaddGreaterOrEqual(LinearExpr expr, long value)Addsexpr >= value.ConstraintaddGreaterOrEqual(LinearExpr left, LinearExpr right)Addsleft >= right.ConstraintaddGreaterOrEqualWithOffset(LinearExpr left, LinearExpr right, long offset)Addsleft + offset >= right.ConstraintaddGreaterThan(LinearExpr expr, long value)Addsexpr > value.ConstraintaddGreaterThan(LinearExpr left, LinearExpr right)Addsleft > right.voidaddHint(IntVar var, long value)Adds hinting to a variableConstraintaddImplication(Literal a, Literal b)Addsa => b.ConstraintaddInverse(IntVar[] variables, IntVar[] inverseVariables)AddsInverse(variables, inverseVariables).ConstraintaddLessOrEqual(LinearExpr expr, long value)Addsexpr <= value.ConstraintaddLessOrEqual(LinearExpr left, LinearExpr right)Addsleft <= right.ConstraintaddLessOrEqualWithOffset(LinearExpr left, LinearExpr right, long offset)Addsleft + offset <= right.ConstraintaddLessThan(LinearExpr expr, long value)Addsexpr < value.ConstraintaddLessThan(LinearExpr left, LinearExpr right)Addsleft < right.ConstraintaddLinearConstraint(LinearExpr expr, long lb, long ub)Addslb <= expr <= ub.ConstraintaddLinearExpressionInDomain(LinearExpr expr, Domain domain)Addsexpr in domain.voidaddMapDomain(IntVar var, Literal[] booleans, long offset)Addsvar == i + offset <=> booleans[i] == true for all i in [0, booleans.length).ConstraintaddMaxEquality(IntVar target, IntVar[] vars)Addstarget == Max(vars).ConstraintaddMinEquality(IntVar target, IntVar[] vars)Addstarget == Min(vars).ConstraintaddModuloEquality(IntVar target, IntVar var, long mod)Addstarget == var % mod.ConstraintaddModuloEquality(IntVar target, IntVar var, IntVar mod)Addstarget == var % mod.ConstraintaddNoOverlap(IntervalVar[] intervalVars)AddsNoOverlap(intervalVars).ConstraintaddNoOverlap2D(IntervalVar[] xIntervals, IntervalVar[] yIntervals)AddsNoOverlap2D(xIntervals, yIntervals).ConstraintaddProductEquality(IntVar target, IntVar[] vars)Addstarget == Product(vars).ConstraintaddReservoirConstraint(IntVar[] times, int[] demands, long minLevel, long maxLevel)AddsReservoir(times, demands, minLevel, maxLevel).ConstraintaddReservoirConstraint(IntVar[] times, long[] demands, long minLevel, long maxLevel)AddsReservoir(times, demands, minLevel, maxLevel).ConstraintaddReservoirConstraintWithActive(IntVar[] times, int[] demands, IntVar[] actives, long minLevel, long maxLevel)AddsReservoir(times, demands, actives, minLevel, maxLevel).ConstraintaddReservoirConstraintWithActive(IntVar[] times, long[] demands, IntVar[] actives, long minLevel, long maxLevel)AddsReservoir(times, demands, actives, minLevel, maxLevel).voidclearAssumptions()Remove all assumptions from the modelvoidclearHints()Remove all solution hintsjava.lang.BooleanexportToFile(java.lang.String file)Write the model as a protocol buffer to 'file'.LiteralfalseLiteral()Returns the false literal.CpModelProto.BuildergetBuilder()Returns the model builder.voidmaximize(LinearExpr expr)Adds a maximization objective of a linear expression.voidminimize(LinearExpr expr)Adds a minimization objective of a linear expression.CpModelProtomodel()java.lang.StringmodelStats()Returns some statistics on model as a string.intnegated(int index)IntVarnewBoolVar(java.lang.String name)Creates a Boolean variable with the given name.IntVarnewConstant(long value)Creates a constant variable.IntervalVarnewFixedInterval(long start, long size, java.lang.String name)Creates a fixed interval from its start and its size.IntervalVarnewIntervalVar(long start, IntVar size, IntVar end, java.lang.String name)Creates an interval variable with a fixed start.IntervalVarnewIntervalVar(IntVar start, long size, IntVar end, java.lang.String name)Creates an interval variable with a fixed size.IntervalVarnewIntervalVar(IntVar start, IntVar size, long end, java.lang.String name)Creates an interval variable with a fixed end.IntervalVarnewIntervalVar(IntVar start, IntVar size, IntVar end, java.lang.String name)Creates an interval variable from start, size, and end.IntVarnewIntVar(long lb, long ub, java.lang.String name)Creates an integer variable with domain [lb, ub].IntVarnewIntVarFromDomain(Domain domain, java.lang.String name)Creates an integer variable with given domain.IntervalVarnewOptionalFixedInterval(long start, long size, Literal isPresent, java.lang.String name)Creates an optional fixed interval from start and size.IntervalVarnewOptionalIntervalVar(long start, IntVar size, IntVar end, Literal isPresent, java.lang.String name)Creates an optional interval with a fixed start.IntervalVarnewOptionalIntervalVar(IntVar start, long size, IntVar end, Literal isPresent, java.lang.String name)Creates an optional interval with a fixed size.IntervalVarnewOptionalIntervalVar(IntVar start, IntVar size, long end, Literal isPresent, java.lang.String name)Creates an optional interval with a fixed end.IntervalVarnewOptionalIntervalVar(IntVar start, IntVar size, IntVar end, Literal isPresent, java.lang.String name)Creates an optional interval variable from start, size, end, and isPresent.LiteraltrueLiteral()Returns the true literal.java.lang.Stringvalidate()Returns a non empty string explaining the issue if the model is invalid.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
CpModel
public CpModel()
-
-
Method Details
-
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. -
addBoolOr
AddsOr(literals) == true. -
addBoolAnd
AddsAnd(literals) == true. -
addBoolXor
AddsXOr(literals) == true. -
addImplication
Addsa => b. -
addLinearExpressionInDomain
Addsexpr in domain. -
addLinearConstraint
Addslb <= expr <= ub. -
addEquality
Addsexpr == value. -
addEquality
Addsleft == right. -
addEqualityWithOffset
Addsleft + offset == right. -
addLessOrEqual
Addsexpr <= value. -
addLessOrEqual
Addsleft <= right. -
addLessThan
Addsexpr < value. -
addLessThan
Addsleft < right. -
addLessOrEqualWithOffset
Addsleft + offset <= right. -
addGreaterOrEqual
Addsexpr >= value. -
addGreaterOrEqual
Addsleft >= right. -
addGreaterThan
Addsexpr > value. -
addGreaterThan
Addsleft > right. -
addGreaterOrEqualWithOffset
Addsleft + offset >= right. -
addDifferent
Addsexpr != value. -
addDifferent
Addsleft != right. -
addDifferentWithOffset
Addsleft + offset != right. -
addAllDifferent
AddsAllDifferent(variables).This constraint forces all variables to have different values.
- Parameters:
variables- a list of integer variables- Returns:
- an instance of the Constraint class
-
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(tails, heads, literals).Adds a circuit constraint from a sparse list of arcs that encode the graph.
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.- Parameters:
tails- the tails of all arcsheads- the heads of all arcsliterals- the literals that control whether an arc is selected or not- Returns:
- an instance of the Constraint class
- Throws:
CpModel.MismatchedArrayLengths- if the arrays have different sizes
-
addAllowedAssignments
AddsAllowedAssignments(variables, tuplesList).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 variablestuplesList- a list of admissible tuples. Each tuple must have the same length as the variables, and the ith value of a tuple corresponds to the ith variable.- Returns:
- an instance of the Constraint class
- Throws:
CpModel.WrongLength- if one tuple does not have the same length as the variables
-
addAllowedAssignments
AddsAllowedAssignments(variables, tuplesList).- See Also:
addAllowedAssignments
-
addForbiddenAssignments
AddsForbiddenAssignments(variables, tuplesList).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 variablestuplesList- a list of forbidden tuples. Each tuple must have the same length as the variables, and the ith value of a tuple corresponds to the ith variable.- Returns:
- an instance of the Constraint class
- Throws:
CpModel.WrongLength- if one tuple does not have the same length as the variables
-
addForbiddenAssignments
AddsForbiddenAssignments(variables, tuplesList).- See Also:
addForbiddenAssignments
-
addAutomaton
public Constraint addAutomaton(IntVar[] transitionVariables, long startingState, long[] finalStates, long[][] transitions)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. 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 statestransitions- a list of transition for the automaton, in the following format (currentState, variableValue, nextState)- Returns:
- an instance of the Constraint class
- Throws:
CpModel.WrongLength- if one transition does not have a length of 3
-
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
public Constraint addReservoirConstraint(IntVar[] times, long[] demands, long minLevel, long maxLevel)AddsReservoir(times, demands, minLevel, maxLevel).Maintains a reservoir level within bounds. The water level starts at 0, and at any times, it must be between minLevel and maxLevel. If the variable
times[i]is assigned a value t, and ifactives[i]is true, then the current level changes bydemands[i](which is constant) at the time t.Note that
minLevelmust be less than 0, andmaxLevelmust be greater than 0. Therefore,forall t : minLevel <= sum(demands[i] if times[i] <= t) <= maxLevel.- Parameters:
times- a list of integer variables which specify the time of the filling or emptying the reservoirdemands- a list of integer values that specifies the amount of the emptying or feelingminLevel- 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 Constraint class
- Throws:
CpModel.MismatchedArrayLengths- if times and demands have different lengthjava.lang.IllegalArgumentException- if minLevel > 0java.lang.IllegalArgumentException- if maxLevel < 0
-
addReservoirConstraint
public Constraint addReservoirConstraint(IntVar[] times, int[] demands, long minLevel, long maxLevel)AddsReservoir(times, demands, minLevel, maxLevel).- See Also:
Reservoir
-
addReservoirConstraintWithActive
public Constraint addReservoirConstraintWithActive(IntVar[] times, long[] demands, IntVar[] actives, long minLevel, long maxLevel)AddsReservoir(times, demands, actives, minLevel, maxLevel).Maintains a reservoir level within bounds. The water level starts at 0, and at any time, it must be between minLevel and maxLevel. If the variable
times[i]is assigned a value t, then the current level changes bydemands[i](which is constant) at the time t.Note that
minLevelmust be less than 0, andmaxLevelmust be greater than 0. Therefore,forall t : minLevel <= sum(demands[i] * actives[i] if times[i] <= t) <= maxLevel.- Parameters:
times- a list of integer variables which specify the time of the filling or emptying the reservoirdemands- a list of integer values that specifies the amount of the emptying or feelingminLevel- 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 Constraint class
- Throws:
CpModel.MismatchedArrayLengths- if times, demands, or actives have different lengthjava.lang.IllegalArgumentException- if minLevel > 0java.lang.IllegalArgumentException- if maxLevel < 0
-
addReservoirConstraintWithActive
public Constraint addReservoirConstraintWithActive(IntVar[] times, int[] demands, IntVar[] actives, long minLevel, long maxLevel)AddsReservoir(times, demands, actives, minLevel, maxLevel).- See Also:
Reservoir
-
addMapDomain
Addsvar == i + offset <=> booleans[i] == true for all i in [0, booleans.length). -
addMinEquality
Addstarget == Min(vars). -
addMaxEquality
Addstarget == Max(vars). -
addDivisionEquality
Addstarget == num / denom, rounded towards 0. -
addAbsEquality
Addstarget == Abs(var). -
addModuloEquality
Addstarget == var % mod. -
addModuloEquality
Addstarget == var % mod. -
addProductEquality
Addstarget == Product(vars). -
newIntervalVar
Creates an interval variable from 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 intervalsize- the size of the intervalend- the end of the intervalname- the name of the interval variable- Returns:
- An IntervalVar object
-
newIntervalVar
Creates an interval variable with a fixed end.- See Also:
newIntervalVar
-
newIntervalVar
Creates an interval variable with a fixed size.- See Also:
newIntervalVar
-
newIntervalVar
Creates an interval variable with a fixed start.- See Also:
newIntervalVar
-
newFixedInterval
Creates a fixed interval from its start and its size. -
newOptionalIntervalVar
public IntervalVar newOptionalIntervalVar(IntVar start, IntVar size, IntVar end, Literal isPresent, java.lang.String name)Creates an optional interval variable from 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
isPresentliteral that indicates if it is active or not.Internally, it ensures that
isPresent => start + size == end.- Parameters:
start- the start of the interval. It can be an integer value, or an integer variable.size- the size of the interval. It can be an integer value, or an integer variable.end- the end of the interval. It can be an integer value, or an integer variable.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
-
newOptionalIntervalVar
public IntervalVar newOptionalIntervalVar(IntVar start, IntVar size, long end, Literal isPresent, java.lang.String name)Creates an optional interval with a fixed end.- See Also:
newOptionalIntervalVar
-
newOptionalIntervalVar
public IntervalVar newOptionalIntervalVar(IntVar start, long size, IntVar end, Literal isPresent, java.lang.String name)Creates an optional interval with a fixed size.- See Also:
newOptionalIntervalVar
-
newOptionalIntervalVar
public IntervalVar newOptionalIntervalVar(long start, IntVar size, IntVar end, Literal isPresent, java.lang.String name)Creates an optional interval with a fixed start. -
newOptionalFixedInterval
public IntervalVar newOptionalFixedInterval(long start, long size, Literal isPresent, java.lang.String name)Creates an optional fixed interval from start and size.- See Also:
newOptionalIntervalVar
-
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
-
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.
- Parameters:
xIntervals- the X coordinates of the rectanglesyIntervals- the Y coordinates of the rectangles- Returns:
- an instance of the Constraint class
-
addCumulative
AddsCumulative(intervals, demands, capacity).This constraint enforces that:
forall t: sum(demands[i] if (start(intervals[t]) <= t < end(intervals[t])) and (t is present)) <= capacity.- Parameters:
intervals- the list of intervalsdemands- the list of demands for each interval. Each demand must be a positive integer variable.capacity- the maximum capacity of the cumulative constraint. It must be a positive integer variable.- Returns:
- an instance of the Constraint class
-
addCumulative
AddsCumulative(intervals, demands, capacity)with fixed demands.- See Also:
AddCumulative
-
addCumulative
AddsCumulative(intervals, demands, capacity)with fixed demands.- See Also:
AddCumulative
-
addCumulative
AddsCumulative(intervals, demands, capacity)with fixed capacity.- See Also:
AddCumulative
-
addCumulative
AddsCumulative(intervals, demands, capacity)with fixed demands and fixed capacity.- See Also:
AddCumulative
-
addCumulative
AddsCumulative(intervals, demands, capacity)with fixed demands and fixed capacity.- See Also:
AddCumulative
-
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. -
maximize
Adds a maximization objective of a linear expression. -
addDecisionStrategy
public void addDecisionStrategy(IntVar[] variables, DecisionStrategyProto.VariableSelectionStrategy varStr, DecisionStrategyProto.DomainReductionStrategy domStr)AddsDecisionStrategy(variables, varStr, domStr). -
modelStats
public java.lang.String modelStats()Returns some statistics on model as a string. -
validate
public java.lang.String validate()Returns a non empty string explaining the issue if the model is invalid. -
exportToFile
public java.lang.Boolean exportToFile(java.lang.String file)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.
-