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