Esempio n. 1
0
  @Override
  public void buildModel() {
    double precision = 1.0e-3;
    // finite domain
    intx = VariableFactory.enumerated("x", new int[] {-10, -9, 0, 2, 42}, solver);
    // continuous view
    x = VariableFactory.real(intx, precision);
    y = VariableFactory.real("y", -1.0e8, 1.0e8, precision, solver);
    z = VariableFactory.real("z", -1.0e8, 1.0e8, precision, solver);

    vars = new RealVar[] {x, y, z};
    RealConstraint rcons = new RealConstraint(solver);
    rcons.addFunction(
        "{1}^2 * (1 + {2}^2) + {2} * ({2} - 24 * {1}) = -13;"
            + "{0}^2 * (1 + {1}^2) + {1} * ({1} - 24 * {0}) = -13;"
            + "{2}^2 * (1 + {0}^2) + {0} * ({0} - 24 * {2}) = -13",
        Ibex.HC4_NEWTON, vars);
    solver.post(rcons);
  }
Esempio n. 2
0
 /**
  * Build an unbounded {@link solver.variables.Variable} named {@code name}, defined by {@code
  * type}.
  *
  * @param name name of the variable
  * @param expression
  * @param map
  * @param solver @return {@link solver.variables.Variable}
  */
 private static IntVar buildWithInt(
     String name, Expression expression, THashMap<String, Object> map, Solver solver) {
   final IntVar iv;
   if (expression != null) {
     iv = buildOnExpression(DEBUG ? name : NO_NAME, expression, map, solver);
   } else {
     iv = VariableFactory.bounded(DEBUG ? name : NO_NAME, -999999, 999999, solver);
   }
   map.put(name, iv);
   return iv;
 }
Esempio n. 3
0
 /**
  * Build a {@link solver.variables.Variable} named {@code name}.
  *
  * @param name name of the boolean variable
  * @param map
  * @param solver
  * @return {@link solver.variables.Variable}
  */
 private static BoolVar buildWithBool(
     String name, Expression expression, THashMap<String, Object> map, Solver solver) {
   final BoolVar bi;
   if (expression != null) {
     bi = (BoolVar) buildOnExpression(DEBUG ? name : NO_NAME, expression, map, solver);
   } else {
     bi = VariableFactory.bool(DEBUG ? name : NO_NAME, solver);
   }
   map.put(name, bi);
   return bi;
 }
Esempio n. 4
0
 /**
  * Build a {@link solver.variables.Variable} named {@code name}, defined by {@code type}.
  *
  * @param name name of the variable
  * @param type {@link parser.flatzinc.ast.declaration.DInt2} object
  * @param map
  * @param solver
  * @return {@link solver.variables.Variable}
  */
 private static IntVar buildWithInt2(
     String name, DInt2 type, Expression expression, THashMap<String, Object> map, Solver solver) {
   final IntVar iv;
   if (expression != null) {
     iv = buildOnExpression(DEBUG ? name : NO_NAME, expression, map, solver);
     int lb = type.getLow();
     int ub = type.getUpp();
     solver.post(IntConstraintFactory.member(iv, lb, ub));
   } else {
     int size = type.getUpp() - type.getLow() + 1;
     if (size < 256) {
       iv =
           VariableFactory.enumerated(
               DEBUG ? name : NO_NAME, type.getLow(), type.getUpp(), solver);
     } else {
       iv = VariableFactory.bounded(DEBUG ? name : NO_NAME, type.getLow(), type.getUpp(), solver);
     }
   }
   map.put(name, iv);
   return iv;
 }
Esempio n. 5
0
 private static IntVar buildOnExpression(
     String name, Expression expression, THashMap<String, Object> map, Solver solver) {
   final IntVar iv;
   switch (expression.getTypeOf()) {
     case BOO:
       iv = expression.boolVarValue(solver);
       break;
     case INT:
       iv = VariableFactory.fixed(name, expression.intValue(), solver);
       break;
     case IDE:
       iv = VariableFactory.eq((IntVar) map.get(expression.toString()));
       break;
     case IDA:
       EIdArray eida = (EIdArray) expression;
       iv = ((IntVar[]) map.get(eida.name))[eida.index - 1];
       break;
     default:
       iv = null;
       Exit.log("Unknown expression");
   }
   return iv;
 }
Esempio n. 6
0
 @Override
 public Constraint[] build(
     Solver solver,
     String name,
     List<Expression> exps,
     List<EAnnotation> annotations,
     THashMap<String, Object> map) {
   // int: n, array[int] of var int: x, int: v
   int n = exps.get(0).intValue();
   IntVar[] x = exps.get(1).toIntVarArray(solver);
   int v = exps.get(2).intValue();
   IntVar limit = VariableFactory.bounded("limit_" + n, n, x.length, solver);
   return new Constraint[] {IntConstraintFactory.among(limit, x, new int[] {v})};
 }
Esempio n. 7
0
 /**
  * Build a {@link solver.variables.Variable} named {@code name}, defined by {@code type}. {@code
  * type} is expected to be a {@link parser.flatzinc.ast.declaration.DManyInt} object.
  *
  * @param name name of the variable
  * @param type {@link parser.flatzinc.ast.declaration.DManyInt} object.
  * @param map
  * @param solver
  * @return {@link solver.variables.Variable}
  */
 private static IntVar buildWithManyInt(
     String name,
     DManyInt type,
     Expression expression,
     THashMap<String, Object> map,
     Solver solver) {
   final IntVar iv;
   if (expression != null) {
     iv = buildOnExpression(DEBUG ? name : NO_NAME, expression, map, solver);
     int[] values = type.getValues();
     solver.post(IntConstraintFactory.member(iv, values));
   } else {
     iv = VariableFactory.enumerated(DEBUG ? name : NO_NAME, type.getValues(), solver);
   }
   map.put(name, iv);
   return iv;
 }
Esempio n. 8
0
  protected static Solver modelit() {
    Solver solver = new Solver();
    int n = 12;
    IntVar[] vars = new IntVar[n];
    for (int i = 0; i < vars.length; i++) {
      vars[i] = VariableFactory.enumerated("Q_" + i, 1, n, solver);
    }

    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        int k = j - i;
        Constraint neq = IntConstraintFactory.arithm(vars[i], "!=", vars[j]);
        solver.post(neq);
        solver.post(IntConstraintFactory.arithm(vars[i], "!=", vars[j], "+", -k));
        solver.post(IntConstraintFactory.arithm(vars[i], "!=", vars[j], "+", k));
      }
    }
    return solver;
  }
Esempio n. 9
0
  @Override
  public void buildModel() {

    /**
     * 1 2 3 4 5 +---+---+---+---+---+ Given the list of words: 1 | 1 | | 2 | | 3 | AFT LASER
     * +---+---+---+---+---+ ALE LEE 2 | # | # | | # | | EEL LINE +---+---+---+---+---+ HEEL SAILS 3
     * | # | 4 | | 5 | | HIKE SHEET +---+---+---+---+---+ HOSES STEER 4 | 6 | # | 7 | | | KEEL TIE
     * +---+---+---+---+---+ KNOT 5 | 8 | | | | | +---+---+---+---+---+ 6 | | # | # | | # | The
     * numbers 1,2,3,4,5,6,7,8 in the crossword +---+---+---+---+---+ puzzle correspond to the words
     */

    //
    // variables
    //
    A = new IntVar[num_words][word_len];
    // for labeling on A and E
    all = new IntVar[(num_words * word_len) + N];
    IntVar[] A_flat = new IntVar[num_words * word_len];

    for (int I = 0; I < num_words; I++) {
      for (int J = 0; J < word_len; J++) {
        A[I][J] = VariableFactory.enumerated("A[" + I + "," + J + "]", 0, 26, solver);
        A_flat[I * word_len + J] = A[I][J];
        all[I * word_len + J] = A[I][J];
      }
    }

    E = VariableFactory.boundedArray("E", N, 0, num_words, solver);
    for (int I = 0; I < N; I++) {
      all[num_words * word_len + I] = E[I];
    }

    //
    // constraints
    //
    solver.post(IntConstraintFactory.alldifferent(E, "BC"));

    for (int I = 0; I < num_words; I++) {
      for (int J = 0; J < word_len; J++) {
        solver.post(
            IntConstraintFactory.arithm(A[I][J], "=", VariableFactory.fixed(AA[I][J], solver)));
      }
    }

    for (int I = 0; I < num_overlapping; I++) {
      /*
        // MiniZinc
        forall(i in 1..num_overlapping) (
             A[E[overlapping[i,1]], overlapping[i,2]] =  A[E[overlapping[i,3]], overlapping[i,4]]
         )
      */

      IntVar vv = VariableFactory.bounded("v_" + I, 0, num_words * word_len, solver);

      IntVar w1 = VariableFactory.bounded("w1_" + I, 0, num_words * word_len, solver);
      solver.post(
          IntConstraintFactory.times(
              E[overlapping[I][0]], VariableFactory.fixed(word_len, solver), w1));
      IntVar s1 = VariableFactory.bounded("s1_" + I, 0, num_words * word_len * 2, solver);
      solver.post(
          IntConstraintFactory.sum(
              new IntVar[] {w1, VariableFactory.fixed(overlapping[I][1], solver)}, s1));
      solver.post(IntConstraintFactory.element(vv, A_flat, s1, 0));

      IntVar w2 = VariableFactory.bounded("w2_" + I, 0, num_words * word_len, solver);
      solver.post(
          IntConstraintFactory.times(
              E[overlapping[I][2]], VariableFactory.fixed(word_len, solver), w2));
      IntVar s2 = VariableFactory.bounded("s2_" + I, 0, num_words * word_len * 2, solver);
      solver.post(
          IntConstraintFactory.sum(
              new IntVar[] {w2, VariableFactory.fixed(overlapping[I][3], solver)}, s2));
      solver.post(IntConstraintFactory.element(vv, A_flat, s2, 0));
    }
  }