public final class Factory extends Object
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();
Modifier and Type | Method and Description |
---|---|
static IntVar |
abs(IntVar x)
Computes a variable that is the absolute value of the given variable.
|
static Constraint |
allDifferent(IntVar[] x)
Returns a binary decomposition of the allDifferent constraint.
|
static Constraint |
allDifferentAC(IntVar[] x)
Returns an allDifferent constraint that enforces
global arc consistency.
|
static IntVar |
element(int[][] matrix,
IntVar x,
IntVar y)
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 the
Element2D constraint
posted by calling this method. |
static IntVar |
element(int[] array,
IntVar y)
Returns a variable representing
the value in an array at the position
specified by the given index variable
This relation is enforced by the
Element1D constraint
posted by calling this method. |
static void |
equal(IntVar x,
int v)
Forces the variable to be equal to some given value and
computes the fix point.
|
static BoolVar |
isEqual(IntVar x,
int c)
Returns a boolean variable representing
whether one variable is equal to the given constant.
|
static BoolVar |
isLarger(IntVar x,
int c)
Returns a boolean variable representing
whether one variable is larger than the given constant.
|
static BoolVar |
isLargerOrEqual(IntVar x,
int c)
Returns a boolean variable representing
whether one variable is larger or equal to the given constant.
|
static BoolVar |
isLess(IntVar x,
int c)
Returns a boolean variable representing
whether one variable is less than the given constant.
|
static BoolVar |
isLessOrEqual(IntVar x,
int c)
Returns a boolean variable representing
whether one variable is less or equal to the given constant.
|
static Constraint |
largerOrEqual(IntVar x,
IntVar y)
Returns a constraint imposing that the
a first variable is larger or equal to a second one.
|
static void |
lessOrEqual(IntVar x,
int v)
Forces the variable to be less or equal to some given value and
computes the fix point.
|
static Constraint |
lessOrEqual(IntVar x,
IntVar y)
Returns a constraint imposing that the
a first variable is less or equal to a second one.
|
static BoolVar |
makeBoolVar(Solver cp)
Creates a boolean variable.
|
static DFSearch |
makeDfs(Solver cp,
Supplier<Procedure[]> branching)
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!
|
static IntVar |
makeIntVar(Solver cp,
int sz)
Creates a variable with a domain of specified arity.
|
static IntVar |
makeIntVar(Solver cp,
int min,
int max)
Creates a variable with a domain equal to the specified range.
|
static IntVar |
makeIntVar(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 function
|
static 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 Solver |
makeSolver()
Creates a constraint programming solver
|
static Solver |
makeSolver(boolean byCopy)
Creates a constraint programming solver
|
static IntVar |
maximum(IntVar... x)
Computes a variable that is the maximum of a set of variables.
|
static IntVar |
minimum(IntVar... x)
Computes a variable that is the minimum of a set of variables.
|
static IntVar |
minus(IntVar x)
A variable that is a view of
-x . |
static IntVar |
minus(IntVar x,
int v)
A variable that is a view of
x-v . |
static IntVar |
mul(IntVar x,
int a)
A variable that is a view of
x*a . |
static void |
notEqual(IntVar x,
int v)
Forces the variable to be different to some given value and
computes the fix point.
|
static Constraint |
notEqual(IntVar x,
IntVar y)
Returns a constraint imposing that the two different variables
must take different values.
|
static Constraint |
notEqual(IntVar x,
IntVar y,
int c)
Returns a constraint imposing that the
the first variable differs from the second
one minus a constant value.
|
static IntVar |
plus(IntVar x,
int v)
A variable that is a view of
x+v . |
static IntVar |
sum(IntVar... x)
Returns a variable representing
the sum of a given set of variables.
|
static Constraint |
sum(IntVar[] x,
int y)
Returns a sum constraint.
|
static Constraint |
sum(IntVar[] x,
IntVar y)
Returns a sum constraint.
|
public static Solver makeSolver()
public static Solver makeSolver(boolean byCopy)
byCopy
- a value that should be true to specify
copy-based state management
or falso for a trail-based memory managementpublic static IntVar makeIntVar(Solver cp, int sz)
cp
- the solver in which the variable is createdsz
- a positive value that is the size of the domainpublic static IntVar makeIntVar(Solver cp, int min, int max)
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
public static IntVar makeIntVar(Solver cp, Set<Integer> values)
cp
- the solver in which the variable is createdvalues
- a set of valuespublic static BoolVar makeBoolVar(Solver cp)
cp
- the solver in which the variable is createdpublic static IntVar[] makeIntVarArray(Solver cp, int n, int sz)
cp
- the solver in which the variables are createdn
- the number of variables to createsz
- a positive value that is the size of the domainpublic static IntVar[] makeIntVarArray(Solver cp, int n, int min, int max)
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
public static IntVar[] makeIntVarArray(int n, Function<Integer,IntVar> body)
n
- the number of variables to createbody
- the function that given the index i in the array creates/map the corresponding IntVar
body.get(i)
public static DFSearch makeDfs(Solver cp, Supplier<Procedure[]> branching)
// 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 = () -> equal(qi, v); // left branch
Procedure right = () -> notEqual(qi, v); // right branch
return branch(left, right);
}
});
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 of Procedure
objects
that will be used to commit to child nodes.
It should return BranchingScheme.EMPTY
whenever the current state
is a solution.DFSearch.solve()
or
DFSearch.optimize(Objective)
using the given branching schemeBranchingScheme.firstFail(IntVar...)
,
BranchingScheme.branch(Procedure...)
public static IntVar mul(IntVar x, int a)
x*a
.x
- a variablea
- a constant to multiply x withx*a
public static IntVar minus(IntVar x)
-x
.x
- a variable-x
public static IntVar plus(IntVar x, int v)
x+v
.x
- a variablev
- a valuex+v
public static IntVar minus(IntVar x, int v)
x-v
.x
- a variablev
- a valuex-v
public static IntVar abs(IntVar x)
Absolute
constraint
posted by calling this method.x
- a variablepublic static IntVar maximum(IntVar... x)
Maximum
constraint
posted by calling this method.x
- the variables on which to compute the maximumminimum(IntVar...)
public static IntVar minimum(IntVar... x)
Maximum
constraint
posted by calling this method.x
- the variables on which to compute the minimum(IntVar...)
public static void equal(IntVar x, int v)
x
- the variable to be assigned to vv
- the value that must be assigned to xpublic static void lessOrEqual(IntVar x, int v)
x
- the variable that is constrained bo be less or equal to vv
- the value that must be the upper bound on xpublic static void notEqual(IntVar x, int v)
x
- the variable that is constrained bo be different from vv
- the value that must be different from xpublic static Constraint notEqual(IntVar x, IntVar y)
x
- a variabley
- a variablex != y
public static Constraint notEqual(IntVar x, IntVar y, int c)
x
- a variabley
- a variablec
- a constantx != y+c
public static BoolVar isEqual(IntVar x, int c)
IsEqual
constraint
posted by calling this method.x
- the variablec
- the constantIsEqual
public static BoolVar isLessOrEqual(IntVar x, int c)
IsLessOrEqual
constraint
posted by calling this method.x
- the variablec
- the constantpublic static BoolVar isLess(IntVar x, int c)
IsLessOrEqual
constraint
posted by calling this method.x
- the variablec
- the constantpublic static BoolVar isLargerOrEqual(IntVar x, int c)
IsLessOrEqual
constraint
posted by calling this method.x
- the variablec
- the constantpublic static BoolVar isLarger(IntVar x, int c)
IsLessOrEqual
constraint
posted by calling this method.x
- the variablec
- the constantpublic static Constraint lessOrEqual(IntVar x, IntVar y)
x
- a variabley
- a variablex <= y
public static Constraint largerOrEqual(IntVar x, IntVar y)
x
- a variabley
- a variablex >= y
public static IntVar element(int[] array, IntVar y)
Element1D
constraint
posted by calling this method.array
- the array of valuesy
- the variablearray[y]
public static IntVar element(int[][] matrix, IntVar x, IntVar y)
Element2D
constraint
posted by calling this method.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-1matrix[x][y]
public static IntVar sum(IntVar... x)
Sum
constraint
posted by calling this method.x
- the n variables to sumx[0]+x[1]+...+x[n-1]
public static Constraint sum(IntVar[] x, IntVar y)
x
- an array of variablesy
- a variabley = x[0]+x[1]+...+x[n-1]
public static Constraint sum(IntVar[] x, int y)
x
- an array of variablesy
- a constanty = x[0]+x[1]+...+x[n-1]
public static Constraint allDifferent(IntVar[] x)
x
- an array of variablesx[i] != x[j] for all i < j
public static Constraint allDifferentAC(IntVar[] x)
x
- an array of variablesx[i] != x[j] for all i < j
Copyright © 2018 Laurent Michel, Pierre Schaus, Pascal Van Hentenryck. All rights reserved.