Exemplo n.º 1
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;
 }
Exemplo n.º 2
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})};
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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));
    }
  }