Package minicp.cp
Class Factory
java.lang.Object
minicp.cp.Factory
Factory to create
Solver, IntVar, Constraint
and some modeling utility methods.
Example for the n-queens problem:
Solver cp = Factory.makeSolver(false);
IntVar[] q = Factory.makeIntVarArray(cp, n, n);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
cp.post(Factory.notEqual(q[i], q[j]));
cp.post(Factory.notEqual(q[i], q[j], j - i));
cp.post(Factory.notEqual(q[i], q[j], i - j));
}
search.onSolution(() ->
System.out.println("solution:" + Arrays.toString(q))
);
DFSearch search = Factory.makeDfs(cp,firstFail(q));
SearchStatistics stats = search.solve();
-
Method Summary
Modifier and TypeMethodDescriptionstatic IntVarComputes a variable that is the absolute value of the given variable.static ConstraintallDifferent(IntVar[] x) Returns a binary decomposition of the allDifferent constraint.static ConstraintallDifferentDC(IntVar[] x) Returns an allDifferent constraint that enforces domain consistency.static IntVarReturns a variable representing the value in a matrix at the position specified by the two given row and column index variables This relation is enforced by theElement2Dconstraint posted by calling this method.static IntVarReturns a variable representing the value in an array at the position specified by the given index variable This relation is enforced by theElement1Dconstraint posted by calling this method.static ConstraintReturns a constraint imposing that the variable is equal to some given value.static ConstraintReturns a constraint imposing that the two different variables must take the value.static BoolVarReturns a boolean variable representing whether one variable is equal to the given constant.static BoolVarReturns a boolean variable representing whether one variable is larger than the given constant.static BoolVarisLargerOrEqual(IntVar x, int c) Returns a boolean variable representing whether one variable is larger or equal to the given constant.static BoolVarReturns a boolean variable representing whether one variable is less than the given constant.static BoolVarisLessOrEqual(IntVar x, int c) Returns a boolean variable representing whether one variable is less or equal to the given constant.static ConstraintlargerOrEqual(IntVar x, int v) Returns a constraint imposing that the variable larger or equal to some given value.static ConstraintlargerOrEqual(IntVar x, IntVar y) Returns a constraint imposing that the a first variable is larger or equal to a second one.static ConstraintlessOrEqual(IntVar x, int v) Returns a constraint imposing that the variable less or equal to some given value.static ConstraintlessOrEqual(IntVar x, IntVar y) Returns a constraint imposing that the a first variable is less or equal to a second one.static BoolVarmakeBoolVar(Solver cp) Creates a boolean variable.static DFSearchCreates a Depth First Search with custom branching heuristicstatic IntVarmakeIntVar(Solver cp, int sz) Creates a variable with a domain of specified arity.static IntVarmakeIntVar(Solver cp, int min, int max) Creates a variable with a domain equal to the specified range.static IntVarmakeIntVar(Solver cp, Set<Integer> values) Creates a variable with a domain equal to the specified set of values.static IntVar[]makeIntVarArray(int n, Function<Integer, IntVar> body) Creates an array of variables with specified lambda functionstatic IntVar[]makeIntVarArray(Solver cp, int n, int sz) Creates an array of variables with specified domain size.static IntVar[]makeIntVarArray(Solver cp, int n, int min, int max) Creates an array of variables with specified domain bounds.static SolverCreates a constraint programming solverstatic SolvermakeSolver(boolean byCopy) Creates a constraint programming solverstatic IntVarComputes a variable that is the maximum of a set of variables.static IntVarComputes a variable that is the minimum of a set of variables.static IntVarA variable that is a view of-x.static IntVarA variable that is a view ofx-v.static IntVarA variable that is a view ofx*a.static BoolVarA boolean variable that is a view of!b.static ConstraintReturns a constraint imposing that the variable is different from some given value.static ConstraintReturns a constraint imposing that the two different variables must take different values.static ConstraintReturns a constraint imposing that the the first variable differs from the second one minus a constant value.static IntVarA variable that is a view ofx+v.static ConstraintReturns a sum constraint.static IntVarReturns a variable representing the sum of a given set of variables.static ConstraintReturns a sum constraint.static ConstraintReturns a sum constraint.
-
Method Details
-
makeSolver
Creates a constraint programming solver- Returns:
- a constraint programming solver with trail-based memory management
-
makeSolver
Creates a constraint programming solver- Parameters:
byCopy- a value that should be true to specify copy-based state management or falso for a trail-based memory management- Returns:
- a constraint programming solver
-
makeIntVar
Creates a variable with a domain of specified arity.- Parameters:
cp- the solver in which the variable is createdsz- a positive value that is the size of the domain- Returns:
- a variable with domain equal to the set {0,...,sz-1}
-
makeIntVar
Creates a variable with a domain equal to the specified range.- Parameters:
cp- the solver in which the variable is createdmin- the lower bound of the domain (included)max- the upper bound of the domain (included)max > min- Returns:
- a variable with domain equal to the set {min,...,max}
-
makeIntVar
Creates a variable with a domain equal to the specified set of values.- Parameters:
cp- the solver in which the variable is createdvalues- a set of values- Returns:
- a variable with domain equal to the set of values
-
makeBoolVar
Creates a boolean variable.- Parameters:
cp- the solver in which the variable is created- Returns:
- an uninstantiated boolean variable
-
makeIntVarArray
Creates an array of variables with specified domain size.- Parameters:
cp- the solver in which the variables are createdn- the number of variables to createsz- a positive value that is the size of the domain- Returns:
- an array of n variables, each with domain equal to the set {0,...,sz-1}
-
makeIntVarArray
Creates an array of variables with specified domain bounds.- Parameters:
cp- the solver in which the variables are createdn- the number of variables to createmin- the lower bound of the domain (included)max- the upper bound of the domain (included)max > min- Returns:
- an array of n variables each with a domain equal to the set {min,...,max}
-
makeIntVarArray
Creates an array of variables with specified lambda function- Parameters:
n- the number of variables to createbody- the function that given the index i in the array creates/map the correspondingIntVar- Returns:
- an array of n variables
with variable at index i generated as
body.get(i)
-
makeDfs
Creates a Depth First Search with custom branching heuristic// Example of binary search: At each node it selects // the first free variable qi from the array q, // and creates two branches qi=v, qi!=v where v is the min value domain
DFSearch search = Factory.makeDfs(cp, () -> { IntVar qi = Arrays.stream(q).reduce(null, (a, b) -> b.size() > 1 && a == null ? b : a); if (qi == null) { return return EMPTY; } else { int v = qi.min(); Procedure left = () -> cp.post(equal(qi, v)); // left branch Procedure right = () -> cp.post(notEqual(qi, v)); // right branch return branch(left, right); } });- Parameters:
cp- the solver that will be used for the searchbranching- a generator that is called at each node of the depth first search tree to generate an array ofProcedureobjects that will be used to commit to child nodes. It should returnBranchingScheme.EMPTYwhenever the current state is a solution.- Returns:
- the depth first search object ready to execute with
DFSearch.solve()orDFSearch.optimize(Objective)using the given branching scheme - See Also:
-
mul
A variable that is a view ofx*a.- Parameters:
x- a variablea- a constant to multiply x with- Returns:
- a variable that is a view of
x*a
-
minus
A variable that is a view of-x.- Parameters:
x- a variable- Returns:
- a variable that is a view of
-x
-
plus
A variable that is a view ofx+v.- Parameters:
x- a variablev- a value- Returns:
- a variable that is a view of
x+v
-
minus
A variable that is a view ofx-v.- Parameters:
x- a variablev- a value- Returns:
- a variable that is a view of
x-v
-
not
A boolean variable that is a view of!b.- Parameters:
b- a boolean variable- Returns:
- a boolean variable that is a view of
!b
-
abs
Computes a variable that is the absolute value of the given variable. This relation is enforced by theAbsoluteconstraint posted by calling this method.- Parameters:
x- a variable- Returns:
- a variable that represents the absolute value of x
-
maximum
Computes a variable that is the maximum of a set of variables. This relation is enforced by theMaximumconstraint posted by calling this method.- Parameters:
x- the variables on which to compute the maximum- Returns:
- a variable that represents the maximum on x
- See Also:
-
minimum
Computes a variable that is the minimum of a set of variables. This relation is enforced by theMaximumconstraint posted by calling this method.- Parameters:
x- the variables on which to compute the minimum- Returns:
- a variable that represents the minimum on x
- See Also:
-
equal
Returns a constraint imposing that the variable is equal to some given value.- Parameters:
x- the variable to be assigned to vv- the value that must be assigned to x- Returns:
- a constraint so that
x = v
-
lessOrEqual
Returns a constraint imposing that the variable less or equal to some given value.- Parameters:
x- the variable that is constrained bo be less or equal to vv- the value that must be the upper bound on x- Returns:
- a constraint so that
x <= v
-
largerOrEqual
Returns a constraint imposing that the variable larger or equal to some given value.- Parameters:
x- the variable that is constrained bo be larger or equal to vv- the value that must be the lower bound on x- Returns:
- a constraint so that
x >= v
-
notEqual
Returns a constraint imposing that the variable is different from some given value.- Parameters:
x- the variable that is constrained bo be different from vv- the value that must be different from x- Returns:
- a constraint so that
x != y
-
notEqual
Returns a constraint imposing that the two different variables must take different values.- Parameters:
x- a variabley- a variable- Returns:
- a constraint so that
x != y
-
equal
Returns a constraint imposing that the two different variables must take the value.- Parameters:
x- a variabley- a variable- Returns:
- a constraint so that
x = y
-
notEqual
Returns a constraint imposing that the the first variable differs from the second one minus a constant value.- Parameters:
x- a variabley- a variablec- a constant- Returns:
- a constraint so that
x != y+c
-
isEqual
Returns a boolean variable representing whether one variable is equal to the given constant. This relation is enforced by theIsEqualconstraint posted by calling this method.- Parameters:
x- the variablec- the constant- Returns:
- a boolean variable that is true if and only if x takes the value c
- See Also:
-
isLessOrEqual
Returns a boolean variable representing whether one variable is less or equal to the given constant. This relation is enforced by theIsLessOrEqualconstraint posted by calling this method.- Parameters:
x- the variablec- the constant- Returns:
- a boolean variable that is true if and only if x takes a value less or equal to c
-
isLess
Returns a boolean variable representing whether one variable is less than the given constant. This relation is enforced by theIsLessOrEqualconstraint posted by calling this method.- Parameters:
x- the variablec- the constant- Returns:
- a boolean variable that is true if and only if x takes a value less than c
-
isLargerOrEqual
Returns a boolean variable representing whether one variable is larger or equal to the given constant. This relation is enforced by theIsLessOrEqualconstraint posted by calling this method.- Parameters:
x- the variablec- the constant- Returns:
- a boolean variable that is true if and only if x takes a value larger or equal to c
-
isLarger
Returns a boolean variable representing whether one variable is larger than the given constant. This relation is enforced by theIsLessOrEqualconstraint posted by calling this method.- Parameters:
x- the variablec- the constant- Returns:
- a boolean variable that is true if and only if x takes a value larger than c
-
lessOrEqual
Returns a constraint imposing that the a first variable is less or equal to a second one.- Parameters:
x- a variabley- a variable- Returns:
- a constraint so that
x <= y
-
largerOrEqual
Returns a constraint imposing that the a first variable is larger or equal to a second one.- Parameters:
x- a variabley- a variable- Returns:
- a constraint so that
x >= y
-
element
Returns a variable representing the value in an array at the position specified by the given index variable This relation is enforced by theElement1Dconstraint posted by calling this method.- Parameters:
array- the array of valuesy- the variable- Returns:
- a variable equal to
array[y]
-
element
Returns a variable representing the value in a matrix at the position specified by the two given row and column index variables This relation is enforced by theElement2Dconstraint posted by calling this method.- Parameters:
matrix- the n x m 2D array of valuesx- the row variable with domain included in 0..n-1y- the column variable with domain included in 0..m-1- Returns:
- a variable equal to
matrix[x][y]
-
sum
Returns a variable representing the sum of a given set of variables. This relation is enforced by theSumconstraint posted by calling this method.- Parameters:
x- the n variables to sum- Returns:
- a variable equal to
x[0]+x[1]+...+x[n-1]
-
sum
Returns a sum constraint.- Parameters:
x- an array of variablesy- a variable- Returns:
- a constraint so that
y = x[0]+x[1]+...+x[n-1]
-
sum
Returns a sum constraint.- Parameters:
x- an array of variablesy- a constant- Returns:
- a constraint so that
y = x[0]+x[1]+...+x[n-1]
-
sum
Returns a sum constraint.Uses a _parameter pack_ to automatically bundle a list of IntVar as an array
- Parameters:
y- the target value for the sum (a constant)x- a parameter pack of IntVar representing an array of variables- Returns:
- a constraint so that
y = x[0] + ... + x[n-1]
-
allDifferent
Returns a binary decomposition of the allDifferent constraint.- Parameters:
x- an array of variables- Returns:
- a constraint so that
x[i] != x[j] for all i < j
-
allDifferentDC
Returns an allDifferent constraint that enforces domain consistency.- Parameters:
x- an array of variables- Returns:
- a constraint so that
x[i] != x[j] for all i < j
-