Module org.maxicp
Package org.maxicp.cp

Class CPFactory

java.lang.Object
org.maxicp.cp.CPFactory

public final class CPFactory extends Object
Factory to create CPSolver, CPIntVar, CPConstraint, CPSeqVar and some modeling utility methods.

CPFactory may need to post new constraints to the solver when creating some variables. If it needs to do so, it *nevers* run the fixpoint.

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.neq(q[i], q[j]));
      cp.post(Factory.neqs(q[i], q[j], j - i));
      cp.post(Factory.neq(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 Details

    • makeSolver

      public static CPSolver makeSolver()
      Creates a constraint programming solver
      Returns:
      a constraint programming solver with trail-based memory management
    • makeSolver

      public static CPSolver makeSolver(boolean byCopy)
      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
    • makeSetVar

      public static CPSetVar makeSetVar(CPSolver cp, int n)
      Creates a set variable with possible elements {0,...,n-1}
      Parameters:
      cp - the solver in which the variable is created
      n - the number of possible values with n > 0
      Returns:
      a set variable without required elements, and possible elements {0,...,n-1}
    • makeIntVar

      public static CPIntVar makeIntVar(CPSolver cp, int sz)
      Creates a variable with a domain of specified arity.
      Parameters:
      cp - the solver in which the variable is created
      sz - a positive value that is the size of the domain
      Returns:
      a variable with domain equal to the set {0,...,sz-1}
    • makeIntVar

      public static CPIntVar makeIntVar(CPSolver cp, int min, int max)
      Creates a variable with a domain equal to the specified range.
      Parameters:
      cp - the solver in which the variable is created
      min - 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

      public static CPIntVar makeIntVar(CPSolver cp, Set<Integer> values)
      Creates a variable with a domain equal to the specified set of values.
      Parameters:
      cp - the solver in which the variable is created
      values - a set of values
      Returns:
      a variable with domain equal to the set of values
    • makeIntVarArray

      public static CPIntVar[] makeIntVarArray(int n, Function<Integer,CPIntVar> body)
      Creates an array of variables with specified lambda function
      Parameters:
      n - the number of variables to create
      body - the function that given the index i in the array creates/map the corresponding CPIntVar
      Returns:
      an array of n variables with variable at index i generated as body.get(i)
    • makeIntVarArray

      public static CPIntVar[] makeIntVarArray(CPSolver cp, int n, int sz)
      Creates an array of variables with specified domain size.
      Parameters:
      cp - the solver in which the variables are created
      n - the number of variables to create
      sz - 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

      public static CPIntVar[] makeIntVarArray(CPSolver cp, int n, int min, int max)
      Creates an array of variables with specified domain bounds.
      Parameters:
      cp - the solver in which the variables are created
      n - the number of variables to create
      min - 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}
    • makeBoolVar

      public static CPBoolVar makeBoolVar(CPSolver cp)
      Creates a boolean variable.
      Parameters:
      cp - the solver in which the variable is created
      Returns:
      an uninstantiated boolean variable
    • makeBoolVar

      public static CPBoolVar makeBoolVar(CPSolver cp, boolean containsTrue, boolean containsFalse)
      Creates a boolean variable.
      Parameters:
      cp - the solver in which the variable is created
      containsTrue - whether the value true is contained within the domain
      containsFalse - whether the value false is contained within the domain
      Returns:
      an uninstantiated boolean variable
    • makeBoolVarArray

      public static CPBoolVar[] makeBoolVarArray(int n, Function<Integer,CPBoolVar> body)
      Creates an array of variables with specified lambda function
      Parameters:
      n - the number of variables to create
      body - the function that given the index i in the array creates/map the corresponding CPBoolVar
      Returns:
      an array of n variables with variable at index i generated as body.get(i)
    • makeBoolVarArray

      public static CPBoolVar[] makeBoolVarArray(CPSolver cp, int n)
      Creates an array of boolean variables with specified domain size.
      Parameters:
      cp - the solver in which the variables are created
      n - the number of variables to create
      Returns:
      an array of n boolean variables
    • makeSeqVar

      public static CPSeqVar makeSeqVar(CPSolver cp, int nNodes, int start, int end)
      Creates a sequence variable.
      Parameters:
      cp - the solver in which the variable is created
      Returns:
      an uninstantiated boolean variable
    • makeIntervalVar

      public static CPIntervalVar makeIntervalVar(CPSolver cp)
      Creates a new optional interval variable with a startMin of 0, an unbounded end and unfixed length
      Parameters:
      cp - the solver
      Returns:
      a new interval variable
    • makeIntervalVar

      public static CPIntervalVar makeIntervalVar(CPSolver cp, int length)
      Creates a new optional interval variable with a startMin of 0, an unbounded end and fixed length
      Parameters:
      cp - the solver
      length - the length of the interval variable
      Returns:
      a new interval variable
    • makeIntervalVar

      public static CPIntervalVar makeIntervalVar(CPSolver cp, boolean optional, int length)
      Creates a new interval optional or mandatory variable with a startMin of 0, an unbounded end and fixed length
      Parameters:
      cp - the solver
      optional - whether the interval variable is optional (true), or mandatory (false)
      length - the length of the interval variable
      Returns:
      a new interval variable
    • makeIntervalVar

      public static CPIntervalVar makeIntervalVar(CPSolver cp, int lengthMin, int lengthMax)
      Creates a new optional interval variable with a startMin of 0, a length between lengthMin and lengthMax
      Parameters:
      cp - the solver
      lengthMin - the length of the interval variable
      lengthMax - the length of the interval variable
      Returns:
      a new interval variable
    • makeIntervalVar

      public static CPIntervalVar makeIntervalVar(CPSolver cp, boolean optional, int lengthMin, int lengthMax)
      Creates a new interval variable with a startMin of 0, a length between lengthMin and lengthMax
      Parameters:
      cp - the solver
      optional - whether the interval variable is optional
      lengthMin - the length of the interval variable
      lengthMax - the length of the interval variable
      Returns:
      a new interval variable
    • makeIntervalVarArray

      public static CPIntervalVar[] makeIntervalVarArray(CPSolver cp, int n)
      Creates an array of new interval variable with a startMin of 0, an unbounded end and unfixed length
      Parameters:
      cp - the solver
      n - the number of interval variables
      Returns:
      an array of new interval variables each with a startMin of 0 and an unbounded end
    • makeDfs

      public static DFSearch makeDfs(CPSolver cp, Supplier<Runnable[]> 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!=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 = () -> eq(qi, v); // left branch
              Procedure right = () -> neq(qi, v); // right branch
              return branch(left, right);
           }
       });
       
       
      Parameters:
      cp - the solver that will be used for the search
      branching - a generator that is called at each node of the depth first search tree to generate an array of Runnable objects that will be used to commit to child nodes. It should return Searches.EMPTY whenever the current state is a solution.
      Returns:
      the depth first search object ready to execute with AbstractSearchMethod.solve() or AbstractSearchMethod.optimize(Objective) using the given branching scheme
      See Also:
    • plus

      public static CPIntVar plus(CPIntVar x, int v)
      A variable that is a view of x+v.
      Parameters:
      x - a variable
      v - a value
      Returns:
      a variable that is a view of x+v
    • minus

      public static CPIntVar minus(CPIntVar x)
      A variable that is a view of -x.
      Parameters:
      x - a variable
      Returns:
      a variable that is a view of -x
    • minus

      public static CPIntVar minus(CPIntVar x, int v)
      A variable that is a view of x-v.
      Parameters:
      x - a variable
      v - a value
      Returns:
      a variable that is a view of x-v
    • mul

      public static CPIntVar mul(CPIntVar x, int a)
      A variable that is a view of x*a.
      Parameters:
      x - a variable
      a - a constant to multiply x with
      Returns:
      a variable that is a view of x*a
    • flip

      public static CPSeqVar flip(CPSeqVar seqVar)
      A variable that is a view of seqVar in reverse order
      Parameters:
      seqVar - a variable
      Returns:
      a variable that is a view of seqVar in reverse order
    • mul

      public static CPIntVar mul(CPIntVar x, CPBoolVar b)
      A variable that is x*b.
      Parameters:
      x - a variable
      b - a boolvar to multiply x with
      Returns:
      a variable that is equal to x*b
    • abs

      public static CPIntVar abs(CPIntVar x)
      Computes a variable that is the absolute value of the given variable. This relation is enforced by the Absolute constraint posted by calling this method.
      Parameters:
      x - a variable
      Returns:
      a variable that represents the absolute value of x
    • sum

      public static CPIntVar sum(CPIntVar... x)
      Returns a variable representing the sum of a given set of variables. This relation is enforced by the Sum constraint 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

      public static CPConstraint sum(CPIntVar[] x, CPIntVar y)
      Returns a sum constraint.
      Parameters:
      x - an array of variables
      y - a variable
      Returns:
      a constraint so that y = x[0]+x[1]+...+x[n-1]
    • sum

      public static CPConstraint sum(CPIntVar[] x, int y)
      Returns a sum constraint.
      Parameters:
      x - an array of variables
      y - a constant
      Returns:
      a constraint so that y = x[0]+x[1]+...+x[n-1]
    • sum

      public static CPConstraint sum(int y, CPIntVar... x)
      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 - array of variables
      Returns:
      a constraint so that y = x[0] + ... + x[n-1]
    • mul

      public static CPIntVar mul(CPIntVar... x)
      Returns a variable representing the multiplication of a given set of variables. This relation is enforced by the Mul binary constraint posted by calling this method.
      Parameters:
      x - the n variables to multiply
      Returns:
      a variable equal to x[0]*x[1]*...*x[n-1]
    • mul

      public static CPIntVar mul(CPIntVar x, CPIntVar y)
      Parameters:
      x - a variable in the same store as y
      y - a variable in the same store as x
      Returns:
      a variable in the same store representing: x * y
    • max

      public static CPIntVar max(CPIntVar... x)
      Computes a variable that is the maximum of a set of variables. This relation is enforced by the Maximum constraint 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

      public static CPIntVar minimum(CPIntVar... x)
      Computes a variable that is the minimum of a set of variables. This relation is enforced by the Maximum constraint 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:
    • eq

      public static CPConstraint eq(CPIntVar x, int v)
      Returns a constraint imposing that the variable is equal to some given value.
      Parameters:
      x - the variable to be assigned to v
      v - the value that must be assigned to x
      Returns:
      a constraint so that x = v
    • include

      public static CPConstraint include(CPSetVar x, int v)
      Returns a constraint imposing that the value is included in the set variable.
      Parameters:
      x - the set variable that is constrained to include v
      v - the value that must be included in x
      Returns:
      a constraint so that v in x
    • exclude

      public static CPConstraint exclude(CPSetVar x, int v)
      Returns a constraint imposing that the value is excluded from the set variable.
      Parameters:
      x - the set variable that is constrained to exclude v
      v - the value that must be excluded from x
      Returns:
      a constraint so that v not in x
    • eq

      public static CPConstraint eq(CPIntVar x, CPIntVar y)
      Returns a constraint imposing that the two different variables must take the value.
      Parameters:
      x - a variable
      y - a variable
      Returns:
      a constraint so that x = y
    • neq

      public static CPConstraint neq(CPIntVar x, int v)
      Returns a constraint imposing that the variable is different from some given value.
      Parameters:
      x - the variable that is constrained bo be different from v
      v - the value that must be different from x
      Returns:
      a constraint so that x != y
    • neq

      public static CPConstraint neq(CPIntVar x, CPIntVar y)
      Returns a constraint imposing that the two different variables must take different values.
      Parameters:
      x - a variable
      y - a variable
      Returns:
      a constraint so that x != y
    • neq

      public static CPConstraint neq(CPIntVar x, CPIntVar y, int c)
      Returns a constraint imposing that the first variable differs from the second one minus a constant value.
      Parameters:
      x - a variable
      y - a variable
      c - a constant
      Returns:
      a constraint so that x != y+c
    • le

      public static CPConstraint le(CPIntVar x, int v)
      Returns a constraint imposing that the variable is less or equal to some given value.
      Parameters:
      x - the variable that is constrained bo be less or equal to v
      v - the value that must be the upper bound on x
      Returns:
      a constraint so that x <= v
    • le

      public static CPConstraint le(CPIntVar x, CPIntVar y)
      Returns a constraint imposing that the first variable is less or equal to a second one.
      Parameters:
      x - a variable
      y - a variable
      Returns:
      a constraint so that x <= y
    • lt

      public static CPConstraint lt(CPIntVar x, int v)
      Returns a constraint imposing that the variable is less to some given value.
      Parameters:
      x - the variable that is constrained bo be less to v
      v - the value that must be the upper bound on x
      Returns:
      a constraint so that x < v
    • lt

      public static CPConstraint lt(CPIntVar x, CPIntVar y)
      Returns a constraint imposing that the first variable is less to a second one.
      Parameters:
      x - a variable
      y - a variable
      Returns:
      a constraint so that x < y
    • ge

      public static CPConstraint ge(CPIntVar x, int v)
      Returns a constraint imposing that the variable is larger or equal to some given value.
      Parameters:
      x - the variable that is constrained bo be larger or equal to v
      v - the value that must be the lower bound on x
      Returns:
      a constraint so that x >= v
    • ge

      public static CPConstraint ge(CPIntVar x, CPIntVar y)
      Returns a constraint imposing that the a first variable is larger or equal to a second one.
      Parameters:
      x - a variable
      y - a variable
      Returns:
      a constraint so that x >= y
    • gt

      public static CPConstraint gt(CPIntVar x, int v)
      Returns a constraint imposing that the variable is larger to some given value.
      Parameters:
      x - the variable that is constrained bo be larger to v
      v - the value that must be the lower bound on x
      Returns:
      a constraint so that x > v
    • gt

      public static CPConstraint gt(CPIntVar x, CPIntVar y)
      Returns a constraint imposing that the a first variable is larger to a second one.
      Parameters:
      x - a variable
      y - a variable
      Returns:
      a constraint so that x > y
    • isEq

      public static CPBoolVar isEq(CPIntVar x, int c)
      Returns a boolean variable representing whether one variable is equal to the given constant. This relation is enforced by the IsEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      c - the constant
      Returns:
      a boolean variable that is true if and only if x takes the value c
      See Also:
    • isEq

      public static CPBoolVar isEq(CPIntVar x, CPIntVar y)
      Returns a boolean variable representing whether two variables are equal This relation is enforced by the IsEqualVar constraint posted by calling this method.
      Parameters:
      x - first variable
      y - second variable
      Returns:
      boolean variable that is set to true if and only if x == y
      See Also:
    • isNeq

      public static CPBoolVar isNeq(CPIntVar x, int c)
      Returns a boolean variable representing whether one variable is not equal to the given constant.
      Parameters:
      x - the variable
      c - the constant
      Returns:
      a boolean variable that is true if and only if x don't take the value c
    • isNeq

      public static CPBoolVar isNeq(CPIntVar x, CPIntVar y)
      Returns a boolean variable representing whether two variables are not equal
      Parameters:
      x - first variable
      y - second variable
      Returns:
      boolean variable that is set to true if and only if x != y
    • isLe

      public static CPBoolVar isLe(CPIntVar x, int c)
      Returns a boolean variable representing whether one variable is less or equal to the given constant. This relation is enforced by the IsLessOrEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      c - the constant
      Returns:
      a boolean variable that is true if and only if x takes a value less or equal to c
    • isLe

      public static CPBoolVar isLe(CPIntVar x, CPIntVar y)
      Returns a boolean variable representing whether one variable is less or equal to another. This relation is enforced by the IsLessOrEqualVar constraint posted by calling this method.
      Parameters:
      x - the variable
      y - the variable
      Returns:
      a boolean variable that is true if and only if x takes a value less or equal to y
    • isLe

      public static CPConstraint isLe(CPBoolVar b, CPIntVar x, CPIntVar y)
      Returns the reified version of the constraint x <= y, i.e. the boolean variable that is set to true if and only if x <= y.
      Parameters:
      b - the boolean variable that will be set to true if and only if x <= y
      x - left hand side of less or equal operator
      y - right hand side of less or equal operator
      Returns:
      the reified constraint b <-> x <= y x <= y, false otherwise
    • isLt

      public static CPConstraint isLt(CPBoolVar b, CPIntVar x, CPIntVar y)
      Returns the reified version of the constraint x < y, i.e. the boolean variable that is set to true if and only if x < y.
      Parameters:
      b - the boolean variable that will be set to true if and only if x < y
      x - left hand side of less or equal operator
      y - right hand side of less or equal operator
      Returns:
      the reified constraint b <-> x < y x < y, false otherwise
    • strictOrder

      public static CPBoolVar strictOrder(CPIntVar x, CPIntVar y)
      Creates a Boolean variable b encoding the strict ordering between x and y. The constraint enforces x ≠ y and links b to the ordering as follows: - b = true if and only if x invalid input: '<' y - b = false if and only if x > y
      Parameters:
      x - the first integer variable
      y - the second integer variable
      Returns:
      a Boolean variable representing the strict order between x and y
    • isLt

      public static CPBoolVar isLt(CPIntVar x, int c)
      Returns a boolean variable representing whether one variable is less than the given constant. This relation is enforced by the IsLessOrEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      c - the constant
      Returns:
      a boolean variable that is true if and only if x takes a value less than c
    • isLt

      public static CPBoolVar isLt(CPIntVar x, CPIntVar y)
      Returns a boolean variable representing whether one variable is less than the given constant. This relation is enforced by the IsLessOrEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      y - second variable
      Returns:
      a boolean variable that is true if and only if x takes a value less than y
    • isGe

      public static CPBoolVar isGe(CPIntVar x, int c)
      Returns a boolean variable representing whether one variable is larger or equal to the given constant. This relation is enforced by the IsLessOrEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      c - the constant
      Returns:
      a boolean variable that is true if and only if x takes a value larger or equal to c
    • isGe

      public static CPBoolVar isGe(CPIntVar x, CPIntVar y)
      Returns a boolean variable representing whether one variable is larger or equal to another. This relation is enforced by the IsLessOrEqualVar constraint posted by calling this method.
      Parameters:
      x - left hand side of less or equal operator
      y - right hand side of less or equal operator
      Returns:
      boolean variable value that will be set to true if x >= y, false otherwise
    • isGt

      public static CPBoolVar isGt(CPIntVar x, int c)
      Returns a boolean variable representing whether one variable is larger than the given constant. This relation is enforced by the IsLessOrEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      c - the constant
      Returns:
      a boolean variable that is true if and only if x takes a value larger than c
    • isGt

      public static CPBoolVar isGt(CPIntVar x, CPIntVar y)
      Returns a boolean variable representing whether one variable is larger than the given constant. This relation is enforced by the IsLessOrEqual constraint posted by calling this method.
      Parameters:
      x - the variable
      y - second variable
      Returns:
      a boolean variable that is true if and only if x takes a value larger than y
    • isIncluded

      public static CPBoolVar isIncluded(CPSetVar x, int v)
      Returns a boolean variable representing if a set variable contains a given value.
      Parameters:
      x - the set variable
      v - the value
      Returns:
      a boolean variable that is true if and only if x contains v
    • not

      public static CPBoolVar not(CPBoolVar b)
      A boolean variable that is a view of !b.
      Parameters:
      b - a boolean variable
      Returns:
      a boolean variable that is a view of !b
    • or

      public static CPConstraint or(CPBoolVar... x)
      Returns an or constraint.

      Uses a _parameter pack_ to automatically bundle a list of IntVar as an array

      Parameters:
      x - array of variables
      Returns:
      a constraint so that x[0] or ... or x[n-1]
    • implies

      public static CPConstraint implies(CPBoolVar b1, CPBoolVar b2)
      Model the logical implication constraint
      Parameters:
      b1 - left-hand side of the implication
      b2 - right-hand side of the implication
      Returns:
      a constraint enforcing "b1 implies b2".
    • isOr

      public static CPBoolVar isOr(CPBoolVar... x)
      Returns a variable that is true if at least one variable in x is true, false otherwise.
      Parameters:
      x - an array of variables
    • element

      public static CPIntVar element(int[] array, CPIntVar 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.
      Parameters:
      array - the array of values
      y - the variable
      Returns:
      a variable equal to array[y]
    • elementDC

      public static CPIntVar elementDC(int[] array, CPIntVar 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. This constraint is Domain consistent.
      Parameters:
      array - the array of values
      y - the variable
      Returns:
      a variable equal to array[y]
    • element

      public static CPIntVar element(CPIntVar[] array, CPIntVar 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.
      Parameters:
      array - the array of values
      y - the variable
      Returns:
      a variable equal to array[y]
    • element

      public static CPIntVar element(int[][] matrix, CPIntVar x, CPIntVar 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.
      Parameters:
      matrix - the n x m 2D array of values
      x - the row variable with domain included in 0..n-1
      y - the column variable with domain included in 0..m-1
      Returns:
      a variable equal to matrix[x][y]
    • allDifferent

      public static CPConstraint allDifferent(CPIntVar[] x)
      Returns an allDifferent constraint using forward checking algo
      Parameters:
      x - an array of variables
      Returns:
      a constraint so that x[i] != x[j] for all i < j
    • allDifferentDC

      public static CPConstraint allDifferentDC(CPIntVar[] x)
      Returns an allDifferent constraint that enforces global arc consistency.
      Parameters:
      x - an array of variables
      Returns:
      a constraint so that x[i] != x[j] for all i < j
    • atLeastNValue

      public static CPConstraint atLeastNValue(CPIntVar[] x, CPIntVar nValue)
      Returns an atLeastNValue constraint using a forward checking algo
      Parameters:
      x - an array of variables
      nValue - the number of values
      Returns:
      a constraint so that #{x[i] | i in 0..x.length-1} >= nValue
    • among

      public static CPConstraint among(CPIntVar[] x, Set<Integer> vals, CPIntVar N)
      Return a constraint that enforce that N is the number of indices i such that x[i] is in vals
      Parameters:
      x - an array of variables
      vals - a set of values
      N - a variable
      Returns:
      a constraint so that N = #{i | x[i] in vals}
    • circuit

      public static CPConstraint circuit(CPIntVar[] x)
      Returns a circuit constraint (using the successor model)
      Parameters:
      x - an array of variables (successor array)
      Returns:
      a constraint so that the path described by the successor array forms an hamiltonian circuit
    • table

      public static CPConstraint table(CPIntVar[] x, int[][] table)
      Returns a table constraint
      Parameters:
      x - an array of variables
      table - an array of tuples
      Returns:
      a constraint the value taken by the variables in x are from a tuple from the table
    • shortTable

      public static CPConstraint shortTable(CPIntVar[] x, int[][] table, int star)
      Returns a short table constraint
      Parameters:
      x - an array of variables
      table - an array of tuples
      star - the value symbolizing the * (i.e. universal value) in the table
      Returns:
      a constraint the value taken by the variables in x are from a tuple from the table
    • negTable

      public static CPConstraint negTable(CPIntVar[] x, int[][] table)
      Returns a short table constraint
      Parameters:
      x - an array of variables
      table - an array of tuples
      Returns:
      a constraint the value taken by the variables in x are not from a tuple from the table
    • insert

      public static CPConstraint insert(CPSeqVar seqVar, int prev, int node)
      Returns a constraint inserting a node within a sequence
      Parameters:
      seqVar - sequence is which the node must be inserted
      prev - predecessor of the node to insert
      node - node that must be inserted
      Returns:
      a constraint so that seqVar.memberAfter(prev) == node
    • require

      public static CPConstraint require(CPSeqVar seqVar, int node)
      Returns a constraint requiring a node within a sequence
      Parameters:
      seqVar - sequence is which the node must be required
      node - node to require
      Returns:
      a constraint so that seqVar.isNode(node, REQUIRED) holds
    • exclude

      public static CPConstraint exclude(CPSeqVar seqVar, int node)
      Returns a constraint excluding a node from a sequence
      Parameters:
      seqVar - sequence is which the node must be excluded
      node - node to exclude
      Returns:
      a constraint so that seqVar.isNode(node, EXCLUDED) holds
    • notBetween

      public static CPConstraint notBetween(CPSeqVar seqVar, int prev, int node, int succ)
      Forbids a subsequence of length 3 to appear in a CPSeqVar, removing from the domain all sequences containing the sub-sequence given as input.

      For technical reasons, the two endpoints of the subsequence must belong to the current partial sequence.

      Parameters:
      seqVar - sequence in which the subsequence must be removed
      prev - origin of the subsequence, a member node
      node - node in the middle of the subsequence
      succ - end of the subsequence, a member node
      Returns:
      a constraint applying a seqVar.notBetween(prev, node, succ)
    • status

      public static CPIntVar status(CPIntervalVar var)
      Returns a CPBoolVar that is equal to the status of the interval.
      Parameters:
      var - the interval variable
      Returns:
      a CPBoolVar that is equal to the status of the interval
    • present

      public static CPConstraint present(CPIntervalVar var1)
      Create a constraint that enforces an interval to be present
      Parameters:
      var1 - the interval variable
      Returns:
      a constraint that enforces the interval to be present
    • absent

      public static CPConstraint absent(CPIntervalVar var1)
      Create a constraint that enforces an interval to be absent
      Parameters:
      var1 - the interval variable
      Returns:
      a constraint that enforces the interval to be absent
    • delay

      public static CPIntervalVar delay(CPIntervalVar intervalVar, int v)
      A variable that is a view of intervalVar+v, adding an offset to it
      Parameters:
      intervalVar - a variable
      v - a value
      Returns:
      a variable that is a view of intervalVar+v
    • start

      public static CPIntVar start(CPIntervalVar var)
      Returns a CPIntVar that is equal to the start of interval.
      Parameters:
      var - the interval variable, it must be present at the time of the posting
      Returns:
      a CPIntVar that is equal to the start of interval
    • startOr

      public static CPIntVar startOr(CPIntervalVar interval, int val)
      Returns a CPIntVar that is equal to the start of interval if it is present, or equal to val if interval is absent
      Parameters:
      interval - the interval variable
      val - the value taken by the returned variable if interval is absent
      Returns:
      a CPIntVar that is equal to the start of interval if it is present, or equal to val if interval is absent
    • startAt

      public static CPConstraint startAt(CPIntervalVar var1, int start)
      Create a constraint that enforces the start of var1 to be equal to start
      Parameters:
      var1 - the interval variable
      start - the start value
      Returns:
      a constraint that enforces the start of var1 to be equal to start
    • startAfter

      public static CPConstraint startAfter(CPIntervalVar var1, int start)
      Create a constraint that enforces the start of var1 to be at or after start
      Parameters:
      var1 - the interval variable
      start - the start value
      Returns:
      a constraint that enforces the start of var1 to be at or after start
    • startBefore

      public static CPConstraint startBefore(CPIntervalVar var1, int start)
      Create a constraint that enforces the start of var1 to be before or at start
      Parameters:
      var1 - the interval variable
      start - the start value
      Returns:
      a constraint that enforces the start of var1 to be before or at start
    • startAtStart

      public static CPConstraint startAtStart(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the start of var1 is equal to the start of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces the start of var1 to be equal to the start of var2
    • startAtStart

      public static CPConstraint startAtStart(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the start of var1 is equal to the start of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces the start of var1 to be equal to the start of var2 + delay
    • startAtEnd

      public static CPConstraint startAtEnd(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the start of var1 is equal to the end of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces the start of var1 to be equal to the end of var2
    • startAtEnd

      public static CPConstraint startAtEnd(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the start of var1 is equal to the end of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces the start of var1 to be equal to the end of var2 + delay
    • startBeforeStart

      public static CPConstraint startBeforeStart(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the start of var1 is lower or equal to the start of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces var1.start <= var2.start
    • startBeforeStart

      public static CPConstraint startBeforeStart(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the start of var1 is lower or equal to the start of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces var1.start <= var2.start
    • startBeforeEnd

      public static CPConstraint startBeforeEnd(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the start of var1 is lower or equal to the end of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces var1.start <= var2.end
    • startBeforeEnd

      public static CPConstraint startBeforeEnd(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the start of var1 is lower or equal to the end of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces var1.start <= var2.end + delay
    • isStartBeforeStart

      public static CPBoolVar isStartBeforeStart(CPIntervalVar var1, CPIntervalVar var2)
      Returns a boolean variable representing whether the interval variable var1 starts at or before the start of the interval variable var2. This relation is enforced by the IsStartBeforeStart constraint posted by calling this method.
      Parameters:
      var1 - An interval variable
      var2 - An interval variable
      Returns:
      a boolean variable which is a reification of the constraint var1.start <= var2.start
    • isStartBeforeEnd

      public static CPBoolVar isStartBeforeEnd(CPIntervalVar var1, CPIntervalVar var2)
      Returns a boolean variable representing whether the interval variable var1 starts at or before the end of the interval variable var2. This relation is enforced by the IsStartBeforeEnd constraint posted by calling this method.
      Parameters:
      var1 - An interval variable
      var2 - An interval variable
      Returns:
      a boolean variable which is a reification of the constraint var1.start <= var2.end
    • end

      public static CPIntVar end(CPIntervalVar var)
      Returns a CPIntVar that is equal to the start of interval.
      Parameters:
      var - the interval variable, it must be present at the time of the posting
      Returns:
      a CPIntVar that is equal to the end of interval
    • endOr

      public static CPIntVar endOr(CPIntervalVar interval, int val)
      Returns a CPIntVar that is equal to the end of interval if it is present, or equal to val if interval is absent
      Parameters:
      interval - the interval variable
      val - the value taken by the returned variable if interval is absent
      Returns:
      a CPIntVar that is equal to the end of interval if it is present, or equal to val if interval is absent
    • endAt

      public static CPConstraint endAt(CPIntervalVar var1, int end)
      Create a constraint that enforces that var1 ends at end
      Parameters:
      var1 - the interval variable
      end - the end value
      Returns:
      a constraint that enforces that var1 ends at end
    • endAtStart

      public static CPConstraint endAtStart(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the end of var1 is equal to the start of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces the end of var1 to be equal to the start of var2
    • endAtStart

      public static CPConstraint endAtStart(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the end of var1 is equal to the start of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces the end of var1 to be equal to the start of var2 + delay
    • endAtEnd

      public static CPConstraint endAtEnd(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the end of var1 is equal to the end of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces the end of var1 to be equal to the end of var2
    • endAtEnd

      public static CPConstraint endAtEnd(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the end of var1 is equal to the end of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces the end of var1 to be equal to the end of var2 + delay
    • endBeforeStart

      public static CPConstraint endBeforeStart(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the end of var1 is lower or equal to the start of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces var1.end <= var2.start
    • endBeforeStart

      public static CPConstraint endBeforeStart(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the end of var1 is lower or equal to the start of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces var1.end <= var2.start + delay
    • endBeforeEnd

      public static CPConstraint endBeforeEnd(CPIntervalVar var1, CPIntervalVar var2)
      Creates a constraint that enforces that the end of var1 is lower or equal to the end of var2
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      Returns:
      a constraint that enforces var1.end <= var2.end
    • endBeforeEnd

      public static CPConstraint endBeforeEnd(CPIntervalVar var1, CPIntervalVar var2, int delay)
      Creates a constraint that enforces that the end of var1 is lower or equal to the end of var2 + delay
      Parameters:
      var1 - an interval variable
      var2 - an interval variable
      delay - a value
      Returns:
      a constraint that enforces var1.end <= var2.end
    • isEndBeforeStart

      public static CPBoolVar isEndBeforeStart(CPIntervalVar var1, CPIntervalVar var2)
      Returns a boolean variable representing whether the interval variable var1 ends at or before the start of the interval variable var2. This relation is enforced by the IsEndBeforeStart constraint posted by calling this method.
      Parameters:
      var1 - An interval variable
      var2 - An interval variable
      Returns:
      a boolean variable which is a reification of the constraint var1.end <= var2.start
    • isEndBeforeEnd

      public static CPBoolVar isEndBeforeEnd(CPIntervalVar var1, CPIntervalVar var2)
      Returns a boolean variable representing whether the interval variable var1 ends at or before the end of the interval variable var2. This relation is enforced by the IsEndBeforeEnd constraint posted by calling this method.
      Parameters:
      var1 - An interval variable
      var2 - An interval variable
      Returns:
      a boolean variable which is a reification of the constraint var1.end <= var2.end
    • length

      public static CPIntVar length(CPIntervalVar var)
      Returns a CPIntVar that is equal to the length of interval.
      Parameters:
      var - the interval variable, it must be present at the time of the posting
      Returns:
      a CPIntVar that is equal to the length of interval
    • lengthOr

      public static CPIntVar lengthOr(CPIntervalVar interval, int val)
      Returns a CPIntVar that is equal to the length of interval if it is present, or equal to val if interval is absent
      Parameters:
      interval - the interval variable
      val - the value taken by the returned variable if interval is absent
      Returns:
      a CPIntVar that is equal to the length of interval if it is present, or equal to val if interval is absent
    • makespan

      public static CPIntVar makespan(CPIntervalVar... vars)
      Returns a CPIntVar that is equal to the makespan (the latest end) of the intervals.
      Parameters:
      vars - the interval vars
      Returns:
      a CPIntVar that is equal to the makespan of the intervals
    • noOverlap

      public static NoOverlap noOverlap(CPIntervalVar... vars)
      Creates a constraint that enforces that there is no overlap between the intervals in vars.
      Parameters:
      vars - one or more interval variables
      Returns:
      a noOverlap constraint on the elements of vars.
    • nonOverlapSequence

      public static CPSeqVar nonOverlapSequence(CPIntervalVar[] intervals)
      Creates and post a non-overlap constraint on the intervals in vars and create a sequence variable linked with these intervals representing the order of the intervals in time. The id of an interval is its index in the array vars. The intervals cannot overlap.
      Parameters:
      intervals - intervals that must be linked with a sequence variable
      Returns:
      sequence variable linked with the intervals
    • alternative

      public static CPConstraint alternative(CPIntervalVar interval, CPIntervalVar[] alternatives, CPIntVar cardinality)
      Returns an Alternative constraint: Enforces that if the interval variable interval is present, then cardinality intervals from the array alternatives must be present and synchronized with a. If a is not present, then all the intervals of alternatives must be absent.
      Parameters:
      interval - an interval variable
      alternatives - an array of interval variables
      cardinality - the cardinality
      Returns:
      an Alternative constraint
    • alternative

      public static CPConstraint alternative(CPIntervalVar interval, CPIntervalVar[] alternatives, int cardinality)
      Returns an Alternative constraint: Enforces that if the interval variable interval is present, then cardinality intervals from the array alternatives must be present and synchronized with interval. If a is not present, then all the intervals of alternatives must be absent.
      Parameters:
      interval - an interval variable
      alternatives - an array of interval variables
      cardinality - the cardinality
      Returns:
      an Alternative constraint
    • alternative

      public static CPConstraint alternative(CPIntervalVar interval, CPIntervalVar[] alternatives)
      Returns an Alternative constraint: Enforces that if the interval variable interval is present, then one of the intervals from the array alternatives must be present and synchronized with a. If a is not present, then all the intervals of alternatives must be absent.
      Parameters:
      interval - an interval variable
      alternatives - an array of interval variables
      Returns:
      an Alternative constraint
    • flat

      public static CPCumulFunction flat()
      Creates an elementary flat Cumulative Function.
      Returns:
      a cumulative function that is flat
    • pulse

      public static CPCumulFunction pulse(CPIntervalVar var, int h)
      Creates an elementary Cumulative Function that is a pulse of height h that happen when the Interval variable var is present.
      Parameters:
      var - an interval variable
      h - an int value
      Returns:
      a cumulative function that is a pulse of height h when var is present
    • pulse

      public static CPCumulFunction pulse(CPIntervalVar var, int hMin, int hMax)
      Creates an elementary Cumulative Function that is a pulse that happen when the Interval variable var is present. Its height is set in the interval [hMin, hMax].
      Parameters:
      var - an interval variable
      hMin - an int value
      hMax - an int value
      Returns:
      a cumulative function that is a pulse of height [hMin, hMax] when var is present
    • stepAtStart

      public static CPCumulFunction stepAtStart(CPIntervalVar var, int h)
      Creates an elementary Cumulative Function that is a step of height h that happen at the start of the Interval variable var if it is present.
      Parameters:
      var - an interval variable
      h - an int value
      Returns:
      a cumulative function that is a step of height h at the start of var if it is present
    • stepAtStart

      public static CPCumulFunction stepAtStart(CPIntervalVar var, int hMin, int hMax)
      Creates an elementary Cumulative Function that is a step that happen at the start of the Interval variable var if it is present. Its height is set in the interval [hMin, hMax].
      Parameters:
      var - an interval variable
      hMin - an int value
      hMax - an int value
      Returns:
      a cumulative function that is a step of height [hMin, hMax] at the start of var if it is present
    • stepAtEnd

      public static CPCumulFunction stepAtEnd(CPIntervalVar var, int h)
      Creates an elementary Cumulative Function that is a step of height h that happen at the end of the Interval variable var if it is present.
      Parameters:
      var - an interval variable
      h - an int value
      Returns:
      a cumulative function that is a step of height h at the end of var if it is present
    • stepAtEnd

      public static CPCumulFunction stepAtEnd(CPIntervalVar var, int hMin, int hMax)
      Creates an elementary Cumulative Function that is a step that happen at the end of the Interval variable var if it is present. Its height is set in the interval [hMin, hMax].
      Parameters:
      var - an interval variable
      hMin - an int value
      hMax - an int value
      Returns:
      a cumulative function that is a step of height [hMin, hMax] at the end of var if it is present
    • step

      public static CPCumulFunction step(CPSolver cp, int from, int h)
      Creates an elementary Cumulative Function that is a step of height h that happen at time from.
      Parameters:
      from - an int value
      h - an int value
      Returns:
      a cumulative function that is a step of height h at time from
    • pulse

      public static CPCumulFunction pulse(CPSolver cp, int from, int to, int h)
      Creates an elementary Cumulative Function that is a pulse of height h that happen at time from.
      Parameters:
      from - an int value
      h - an int value
      Returns:
      a cumulative function that is a step of height h at time from
    • plus

      public static CPCumulFunction plus(CPCumulFunction fun1, CPCumulFunction fun2)
      Creates a cumulative Function that is the sum of two cumulative Functions.
      Parameters:
      fun1 - a cumulative Function
      fun2 - a cumulative Function
      Returns:
      a cumulative function that is the sum of fun1 and fun2
    • minus

      public static CPCumulFunction minus(CPCumulFunction fun1, CPCumulFunction fun2)
      Creates a cumulative Function that is the difference of two cumulative Functions.
      Parameters:
      fun1 - a cumulative Function
      fun2 - a cumulative Function
      Returns:
      a cumulative function that is the difference of fun1 and fun2
    • sum

      public static CPCumulFunction sum(CPCumulFunction... fun)
      Creates a cumulative Function that is the sum of zero or more cumulative Functions.
      Parameters:
      fun - a cumulative Function
      Returns:
      a cumulative function that is the sum of the cumulative Functions in fun
    • alwaysIn

      public static CPConstraint alwaysIn(CPCumulFunction fun, int minValue, int maxValue, Constants.CumulativeAlgo algo)
      Requires a cumulative function to always be within the range [minValue..maxValue] on the execution range of the cumulative function.
      Parameters:
      fun - a cumulative function
      minValue - an int value
      maxValue - an int value
      algo - a cumulative algorithm
      Returns:
      a constraint which enforces the cumulative function fun to stay within the range [minValue..maxValue]
    • alwaysIn

      public static CPConstraint alwaysIn(CPCumulFunction fun, int minValue, int maxValue)
      Requires a cumulative function to always be within the range [minValue..maxValue] on the execution range of the cumulative function.
      Parameters:
      fun - a cumulative function
      minValue - an int value
      maxValue - an int value
      Returns:
      a constraint which enforces the cumulative function fun to stay within the range [minValue..maxValue]
    • alwaysIn

      public static CPConstraint alwaysIn(CPCumulFunction fun, int minValue, int maxValue, int from, int to, Constants.CumulativeAlgo algo)
      Requires a cumulative function to always be within the range [minValue..maxValue] on the execution range [from..to).
      Parameters:
      fun - a cumulative function
      minValue - an int value
      maxValue - an int value
      from - an int value
      to - an int value
      Returns:
      a constraint which enforces the cumulative function fun to stay within the range [minValue..maxValue]
    • alwaysIn

      public static CPConstraint alwaysIn(CPCumulFunction fun, int minValue, int maxValue, int from, int to)
      Requires a cumulative function to always be within the range [minValue..maxValue] on the execution range [from..to).
      Parameters:
      fun - a cumulative function
      minValue - an int value
      maxValue - an int value
      from - an int value
      to - an int value
      Returns:
      a constraint which enforces the cumulative function fun to stay within the range [minValue..maxValue]
    • le

      public static CPConstraint le(CPCumulFunction fun, int maxValue, Constants.CumulativeAlgo algo)
      Requires a cumulative function to always be equal or lesser than a value on the execution range [0..Constants.HORIZON).
      Parameters:
      fun - a cumulative function
      maxValue - an int value
      algo - a cumulative algorithm
      Returns:
      a constraint which ensures that fun is always lesser or equal to maxVal
    • le

      public static CPConstraint le(CPCumulFunction fun, int maxValue)
      Requires a cumulative function to always be equal or lesser than a value on the execution range [0..Constants.HORIZON).
      Parameters:
      fun - a cumulative function
      maxValue - an int value
      Returns:
      a constraint which ensures that fun is always lesser or equal to maxVal