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; }
@Override public Constraint[] build( Solver solver, String name, List<Expression> exps, List<EAnnotation> annotations, THashMap<String, Object> map) { IntVar a = exps.get(0).intVarValue(solver); IntVar b = exps.get(1).intVarValue(solver); IntVar c = exps.get(2).intVarValue(solver); return new Constraint[] {IntConstraintFactory.mod(a, b, c)}; }
@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})}; }
/** * 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; }
/** * 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; }
@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)); } }