Example #1
0
  /** Solves the N Queens problem. See http://www.hakank.org/google_or_tools/nqueens2.py */
  private static void solve(int n, int num, int print) {

    Solver solver = new Solver("NQueens");

    System.out.println("n: " + n);

    //
    // variables
    //
    IntVar[] q = solver.makeIntVarArray(n, 0, n - 1, "q");

    //
    // constraints
    //
    solver.addConstraint(solver.makeAllDifferent(q, true));

    IntVar b = solver.makeIntVar(1, 1, "b");
    IntVar[] q1 = new IntVar[n];
    IntVar[] q2 = new IntVar[n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        // // q[i]+i != q[j]+j
        solver.addConstraint(
            solver.makeNonEquality(solver.makeSum(q[i], i).Var(), solver.makeSum(q[j], j).Var()));

        // q[i]-i != q[j]-j
        solver.addConstraint(
            solver.makeNonEquality(solver.makeSum(q[i], -i).Var(), solver.makeSum(q[j], -j).Var()));
      }
    }

    //
    // Solve
    //
    DecisionBuilder db =
        solver.makePhase(q, solver.CHOOSE_MIN_SIZE_LOWEST_MAX, solver.ASSIGN_CENTER_VALUE);
    solver.newSearch(db);
    int c = 0;
    while (solver.nextSolution()) {
      if (print != 0) {
        for (int i = 0; i < n; i++) {
          System.out.print(q[i].value() + " ");
        }
        System.out.println();
      }
      c++;
      if (num > 0 && c >= num) {
        break;
      }
    }
    solver.endSearch();

    // Statistics
    System.out.println();
    System.out.println("Solutions: " + solver.solutions());
    System.out.println("Failures: " + solver.failures());
    System.out.println("Branches: " + solver.branches());
    System.out.println("Wall time: " + solver.wall_time() + "ms");
  }
Example #2
0
  /**
   * Implements the all interval problem. See http://www.hakank.org/google_or_tools/all_interval.py
   */
  private static void solve(int n) {

    Solver solver = new Solver("AllInterval");

    //
    // variables
    //
    IntVar[] x = solver.makeIntVarArray(n, 0, n - 1, "x");
    IntVar[] diffs = solver.makeIntVarArray(n - 1, 1, n - 1, "diffs");

    //
    // constraints
    //
    solver.addConstraint(solver.makeAllDifferent(x, true));
    solver.addConstraint(solver.makeAllDifferent(diffs, true));

    for (int k = 0; k < n - 1; k++) {
      solver.addConstraint(
          solver.makeEquality(
              diffs[k], solver.makeAbs(solver.makeDifference(x[k + 1], x[k])).Var()));
    }

    // symmetry breaking
    solver.addConstraint(solver.makeLess(x[0], x[n - 1]));
    solver.addConstraint(solver.makeLess(diffs[0], diffs[1]));

    //
    // search
    //
    DecisionBuilder db = solver.makePhase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE);

    solver.newSearch(db);

    //
    // output
    //
    while (solver.nextSolution()) {
      System.out.print("x    : ");
      for (int i = 0; i < n; i++) {
        System.out.print(x[i].value() + " ");
      }
      System.out.print("\ndiffs: ");

      for (int i = 0; i < n - 1; i++) {
        System.out.print(diffs[i].value() + " ");
      }
      System.out.println("\n");
    }
    solver.endSearch();

    // Statistics
    System.out.println();
    System.out.println("Solutions: " + solver.solutions());
    System.out.println("Failures: " + solver.failures());
    System.out.println("Branches: " + solver.branches());
    System.out.println("Wall time: " + solver.wall_time() + "ms");
  }
Example #3
0
  /** Solves the Coins Grid problm. See http://www.hakank.org/google_or_tools/coins_grid.py */
  private static void solve() {

    Solver solver = new Solver("CoinsGrid");

    //
    // data
    //
    int n = 31;
    int c = 14;

    //
    // variables
    //
    IntVar[][] x = new IntVar[n][n];
    IntVar[] x_flat = new IntVar[n * n];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        x[i][j] = solver.makeIntVar(0, 1, "x[" + i + "," + j + "]");
        x_flat[i * n + j] = x[i][j];
      }
    }

    //
    // constraints
    //

    // sum row/columns == c
    for (int i = 0; i < n; i++) {
      IntVar[] row = new IntVar[n];
      IntVar[] col = new IntVar[n];
      for (int j = 0; j < n; j++) {
        row[j] = x[i][j];
        col[j] = x[j][i];
      }
      solver.addConstraint(solver.makeSumEquality(row, c));
      solver.addConstraint(solver.makeSumEquality(col, c));
    }

    System.out.println("here1");

    // quadratic horizonal distance
    IntVar[] obj_tmp = new IntVar[n * n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        obj_tmp[i * n + j] = solver.makeProd(x[i][j], (i - j) * (i - j)).Var();
      }
    }
    IntVar obj_var = solver.makeSum(obj_tmp).Var();

    //
    // objective
    //
    OptimizeVar obj = solver.makeMinimize(obj_var, 1);

    //
    // search
    //
    DecisionBuilder db =
        solver.makePhase(x_flat, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MAX_VALUE);

    solver.newSearch(db, obj);

    //
    // output
    //
    while (solver.nextSolution()) {
      System.out.println("obj_var: " + obj_var.value());
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          System.out.print(x[i][j].value() + " ");
        }
        System.out.println();
      }
      System.out.println();
    }
    solver.endSearch();

    // Statistics
    System.out.println();
    System.out.println("Solutions: " + solver.solutions());
    System.out.println("Failures: " + solver.failures());
    System.out.println("Branches: " + solver.branches());
    System.out.println("Wall time: " + solver.wall_time() + "ms");
  }