Example #1
0
 protected Solver intlincomb(int[][] domains, int[] coeffs, int b, int op) {
   Solver solver = new Solver();
   IntVar[] bins = new IntVar[domains.length];
   for (int i = 0; i < domains.length; i++) {
     bins[i] =
         VariableFactory.bounded(
             "v_" + i, domains[i][0], domains[i][domains[i].length - 1], solver);
   }
   String opname = "=";
   if (op != 0) {
     if (op > 0) {
       opname = ">=";
     } else {
       opname = "<=";
     }
   }
   IntVar sum = VariableFactory.bounded("scal", -99999999, 99999999, solver);
   Constraint[] cstrs =
       new Constraint[] {
         IntConstraintFactory.scalar(bins, coeffs, sum),
         IntConstraintFactory.arithm(sum, opname, b)
       };
   solver.post(cstrs);
   solver.set(IntStrategyFactory.lexico_LB(bins));
   return solver;
 }
Example #2
0
  @Test(groups = "1s")
  public void testSimpleAuto() {
    Solver solver = new Solver();

    int n = 10;
    IntVar[] vars = new IntVar[n];
    for (int i = 0; i < n; i++) {
      vars[i] = VariableFactory.enumerated("x_" + i, 0, 2, solver);
    }

    FiniteAutomaton auto = new FiniteAutomaton();
    int start = auto.addState();
    int end = auto.addState();
    auto.setInitialState(start);
    auto.setFinal(start);
    auto.setFinal(end);

    auto.addTransition(start, start, 0, 1);
    auto.addTransition(start, end, 2);

    auto.addTransition(end, start, 2);
    auto.addTransition(end, start, 0, 1);

    solver.post(IntConstraintFactory.regular(vars, auto));
    solver.set(IntStrategyFactory.lexico_LB(vars));

    solver.findAllSolutions();
    Assert.assertEquals(solver.getMeasures().getSolutionCount(), 59049);
  }
Example #3
0
 @Test(groups = "10s")
 public void testMax2() {
   Random random = new Random();
   for (int seed = 169; seed < 9999; seed++) {
     random.setSeed(seed);
     int[][] domains =
         DomainBuilder.buildFullDomains(
             3, 1, 15, random, random.nextDouble(), random.nextBoolean());
     Solver ref = new Solver();
     {
       IntVar[] xs = new IntVar[3];
       xs[0] = VariableFactory.enumerated("x", domains[0], ref);
       xs[1] = VariableFactory.enumerated("y", domains[1], ref);
       xs[2] = VariableFactory.enumerated("z", domains[2], ref);
       maxref(ref, xs[0], xs[1], xs[2]);
       //                SearchMonitorFactory.log(ref, true, true);
       ref.set(IntStrategyFactory.random_value(xs, seed));
     }
     Solver solver = new Solver();
     {
       IntVar[] xs = new IntVar[3];
       xs[0] = VariableFactory.enumerated("x", domains[0], solver);
       xs[1] = VariableFactory.enumerated("y", domains[1], solver);
       xs[2] = VariableFactory.enumerated("z", domains[2], solver);
       max(solver, xs[0], xs[1], xs[2]);
       //                SearchMonitorFactory.log(solver, true, true);
       solver.set(IntStrategyFactory.random_value(xs, seed));
     }
     ref.findAllSolutions();
     solver.findAllSolutions();
     Assert.assertEquals(
         solver.getMeasures().getSolutionCount(),
         ref.getMeasures().getSolutionCount(),
         "SOLUTIONS (" + seed + ")");
     // BEWARE: MAX does not ensure AC, unlike reformulation; so nb of nodes can be different...
     //            Assert.assertTrue(solver.getMeasures().getNodeCount() <=
     // ref.getMeasures().getNodeCount(), "NODES (" + seed + "): "
     //                    + solver.getMeasures().getNodeCount() + " vs. " +
     // ref.getMeasures().getNodeCount());
   }
 }
Example #4
0
 @Test(groups = "10s")
 public void testMax1() {
   Random random = new Random();
   for (int seed = 1; seed < 9999; seed++) {
     random.setSeed(seed);
     int[][] domains = DomainBuilder.buildFullDomains(3, 1, 15);
     Solver ref = new Solver();
     {
       IntVar[] xs = new IntVar[3];
       xs[0] = VariableFactory.bounded("x", domains[0][0], domains[0][1], ref);
       xs[1] = VariableFactory.bounded("y", domains[1][0], domains[1][1], ref);
       xs[2] = VariableFactory.bounded("z", domains[2][0], domains[2][1], ref);
       maxref(ref, xs[0], xs[1], xs[2]);
       //                SearchMonitorFactory.log(ref, true, true);
       ref.set(IntStrategyFactory.random_bound(xs, seed));
     }
     Solver solver = new Solver();
     {
       IntVar[] xs = new IntVar[3];
       xs[0] = VariableFactory.bounded("x", domains[0][0], domains[0][1], solver);
       xs[1] = VariableFactory.bounded("y", domains[1][0], domains[1][1], solver);
       xs[2] = VariableFactory.bounded("z", domains[1][0], domains[2][1], solver);
       max(solver, xs[0], xs[1], xs[2]);
       //                SearchMonitorFactory.log(solver, true, true);
       solver.set(IntStrategyFactory.random_bound(xs, seed));
     }
     ref.findAllSolutions();
     solver.findAllSolutions();
     Assert.assertEquals(
         solver.getMeasures().getSolutionCount(),
         ref.getMeasures().getSolutionCount(),
         "SOLUTIONS (" + seed + ")");
     Assert.assertTrue(
         solver.getMeasures().getNodeCount() <= ref.getMeasures().getNodeCount(),
         "NODES (" + seed + ")");
   }
 }
Example #5
0
  @Test(groups = "10s")
  public void compareVersionSpeedNew() {
    int n = 14;
    FiniteAutomaton auto = new FiniteAutomaton("(0|1|2)*(0|1)(0|1)(0|1)(0|1|2)*");

    Solver solver = new Solver();
    IntVar[] vars = new IntVar[n];
    for (int i = 0; i < n; i++) {
      vars[i] = VariableFactory.enumerated("x_" + i, 0, 2, solver);
    }
    solver.post(IntConstraintFactory.regular(vars, auto));
    solver.set(IntStrategyFactory.lexico_LB(vars));

    solver.findAllSolutions();
    Assert.assertEquals(solver.getMeasures().getSolutionCount(), 4371696);
  }
Example #6
0
  @Test(groups = "1s")
  public void compareVersionSpeedNew2() {
    int n = 5;
    FiniteAutomaton auto = new FiniteAutomaton("(0|<10>|<20>)*(0|<10>)");

    Solver solver = new Solver();
    IntVar[] vars = new IntVar[n];
    for (int i = 0; i < n; i++) {
      vars[i] = VariableFactory.enumerated("x_" + i, new int[] {0, 10, 20}, solver);
    }
    solver.post(IntConstraintFactory.regular(vars, auto));
    solver.set(IntStrategyFactory.lexico_LB(vars));

    Chatterbox.showSolutions(solver);
    solver.findAllSolutions();
    Assert.assertEquals(solver.getMeasures().getSolutionCount(), 162);
  }
Example #7
0
  @Test(groups = "1s")
  public void ccostregular2() {
    Solver solver = new Solver();

    int n = 12;
    IntVar[] vars = new IntVar[n];
    for (int i = 0; i < n; i++) {
      vars[i] = VariableFactory.enumerated("x_" + i, 0, 2, solver);
    }

    // different rules are formulated as patterns that must NOT be matched by x
    List<String> forbiddenRegExps = new ArrayList<>();
    // do not end with '00' if start with '11'
    forbiddenRegExps.add("11(0|1|2)*00");
    // at most three consecutive 0
    forbiddenRegExps.add("(0|1|2)*0000(0|1|2)*");
    // no pattern '112' at position 5
    forbiddenRegExps.add("(0|1|2){4}112(0|1|2)*");
    // pattern '12' after a 0 or a sequence of 0
    forbiddenRegExps.add("(0|1|2)*02(0|1|2)*");
    forbiddenRegExps.add("(0|1|2)*01(0|1)(0|1|2)*");
    // at most three 2 on consecutive even positions
    forbiddenRegExps.add("(0|1|2)((0|1|2)(0|1|2))*2(0|1|2)2(0|1|2)2(0|1|2)*");

    // a unique automaton is built as the complement language composed of all the forbidden patterns
    FiniteAutomaton auto = new FiniteAutomaton();
    for (String reg : forbiddenRegExps) {
      FiniteAutomaton a = new FiniteAutomaton(reg);
      auto = auto.union(a);
      auto.minimize();
    }
    auto = auto.complement();
    auto.minimize();
    Assert.assertEquals(auto.getNbStates(), 54);

    solver.post(IntConstraintFactory.regular(vars, auto));
    solver.set(IntStrategyFactory.lexico_LB(vars));

    solver.findAllSolutions();
    Assert.assertEquals(solver.getMeasures().getSolutionCount(), 25980);
  }
Example #8
0
  public static void testOp(int n, int min, int max, int cMax, int seed, Operator operator) {
    Random random = new Random(seed);
    Solver s = new Solver();
    IntVar[] vars = new IntVar[n];
    int[] coeffs = new int[n];
    for (int i = 0; i < vars.length; i++) {
      vars[i] = VariableFactory.enumerated("v_" + i, min, max, s);
      coeffs[i] = random.nextInt(cMax);
    }
    int constant = -random.nextInt(cMax);

    IntVar sum = VariableFactory.bounded("scal", -99999999, 99999999, s);

    Constraint[] cstrs =
        new Constraint[] {
          IntConstraintFactory.scalar(vars, coeffs, sum),
          IntConstraintFactory.arithm(sum, operatorToString(operator), constant)
        };

    s.post(cstrs);
    s.set(IntStrategyFactory.lexico_LB(vars));

    s.findAllSolutions();
  }
Example #9
0
 @Override
 public void configureSearch() {
   solver.set(IntStrategyFactory.minDom_MidValue(true, vars));
 }
Example #10
0
 @Override
 public void configureSearch() {
   solver.set(IntStrategyFactory.minDom_LB(vars));
 }
Example #11
0
 @Override
 public void configureSearch() {
   solver.set(IntStrategyFactory.minDom_LB(ArrayUtils.append(rows)));
 }
Example #12
0
 @Override
 public void configureSearch() {
   solver.set(IntStrategyFactory.domOverWDeg(v, 0));
 }