Beispiel #1
0
 @Test(groups = "1s")
 public void test2() {
   Solver solver = new Solver();
   IntVar[] VARS = VF.enumeratedArray("X", 2, 0, 2, solver);
   Constraint CSTR = ICF.arithm(VARS[0], "+", VARS[1], "=", 2);
   solver.post(CSTR, CSTR);
   solver.findAllSolutions();
   Assert.assertEquals(solver.getMeasures().getSolutionCount(), 3);
   solver.getSearchLoop().reset();
   solver.unpost(CSTR);
   solver.findAllSolutions();
   Assert.assertEquals(solver.getMeasures().getSolutionCount(), 3);
 }
Beispiel #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);
  }
Beispiel #3
0
 @Test(groups = "1s")
 public void testregExp4() {
   Solver solver = new Solver();
   IntVar[] CS = VF.enumeratedArray("CS", 2, 0, 3, solver);
   solver.post(ICF.regular(CS, new FiniteAutomaton(".*", 0, 3)));
   solver.findAllSolutions();
   Assert.assertEquals(solver.getMeasures().getSolutionCount(), 16);
 }
Beispiel #4
0
 @Test(groups = "1s")
 public void testregExp8() {
   Solver solver = new Solver();
   IntVar[] CS = VF.enumeratedArray("CS", 3, new int[] {43, 59, 117}, solver);
   solver.post(ICF.regular(CS, new FiniteAutomaton("<43><59><117>")));
   solver.set(ISF.lexico_LB(CS));
   solver.findAllSolutions();
   Assert.assertEquals(solver.getMeasures().getSolutionCount(), 1);
 }
Beispiel #5
0
 @Test(groups = "1s")
 public void testregExp6() {
   Solver solver = new Solver();
   IntVar[] CS = VF.enumeratedArray("CS", 4, 0, 3, solver);
   solver.post(ICF.regular(CS, new FiniteAutomaton("0{2,3}1*")));
   Chatterbox.showSolutions(solver);
   Chatterbox.showDecisions(solver);
   solver.findAllSolutions();
   Assert.assertEquals(solver.getMeasures().getSolutionCount(), 2);
 }
Beispiel #6
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());
   }
 }
Beispiel #7
0
  @Test(groups = "1m")
  public void testSumvsIntLinCombTest() {
    Random rand = new Random();
    for (int seed = 0; seed < 400; seed++) {
      rand.setSeed(seed);
      int n = 1 + rand.nextInt(6);
      int min = -10 + rand.nextInt(20);
      int max = min + rand.nextInt(20);
      int[][] domains = DomainBuilder.buildFullDomains(n, min, max, rand, 1.0, false);
      int[] coeffs = new int[n];
      for (int i = 0; i < n; i++) {
        coeffs[i] = -25 + rand.nextInt(50);
      }
      int lb = -50 + rand.nextInt(100);
      int op = -1 + rand.nextInt(3);

      Solver sum = sum(domains, coeffs, lb, op);
      Solver intlincomb = intlincomb(domains, coeffs, lb, op);

      sum.findAllSolutions();
      intlincomb.findAllSolutions();
      Assert.assertEquals(
          sum.getMeasures().getSolutionCount(), intlincomb.getMeasures().getSolutionCount());
      Assert.assertEquals(
          sum.getMeasures().getNodeCount(), intlincomb.getMeasures().getNodeCount());
      LoggerFactory.getLogger("test")
          .info(
              "({}) {}ms vs {}ms",
              op,
              sum.getMeasures().getTimeCount(),
              intlincomb.getMeasures().getTimeCount());
    }
  }
  @Test
  public void testKnapsack() throws IOException, URISyntaxException {
    for (Path path : filesInDirectory("/kp")) {
      for (int k = 0; k < 3; k++) {
        Solver solver = SolverFactory.makeSolver();

        Pair pair = parseKnapsack(Files.newBufferedReader(path), solver);
        IntVar[] objectives = pair.totalEnergies;

        long start = System.currentTimeMillis();
        solver.plugMonitor(
            new IMonitorOpenNode() {

              @Override
              public void beforeOpenNode() {
                if (System.currentTimeMillis() - start > 30000) {
                  solver.getSearchLoop().interrupt("Times up", true);
                }
              }

              @Override
              public void afterOpenNode() {}
            });

        String out;
        if (k == 0) {
          out = "gia " + objectives.length + " " + pair.objects + " " + Moo.gia(solver, objectives);
        } else if (k == 1) {
          out =
              "classic "
                  + objectives.length
                  + " "
                  + pair.objects
                  + " "
                  + Moo.classic(solver, objectives).size();
        } else {
          out =
              "oia "
                  + objectives.length
                  + " "
                  + pair.objects
                  + " "
                  + Moo.oia(solver, objectives).size()
                  + " "
                  + solver.getMeasures().getSolutionCount();
        }
        boolean limit = solver.hasReachedLimit();
        long time = System.currentTimeMillis() - start;
        System.out.println(out + " " + time + " " + limit);
      }
    }
  }
Beispiel #9
0
  @Test(groups = "1m")
  public void test16to32() {
    int[] size = {32, 36, 40, 44, 48, 52, 56, 60, 64};
    int[] sols = {1, 1, 1, 1, 1, 1, 1, 1, 1};
    //        int[] nodes = {633, 760, 2250, 6331, 19832, 19592, 60477, 139296, 180302};

    for (int i = 0; i < size.length; i++) {
      Solver sol = modeler(size[i]);
      sol.findSolution();
      Assert.assertEquals(sol.getMeasures().getSolutionCount(), sols[i]);
      //            Assert.assertEquals(sol.getMeasures().getNodeCount(), nodes[i]);
    }
  }
Beispiel #10
0
  @Test(groups = "1m")
  public void test4to14() {
    int[] size = {8, 12, 16, 20, 24, 28};
    int[] sols = {1, 1, 7, 24, 296, 1443};
    //        int[] nodes = {3, 22, 189, 1739, 17889, 189944};

    for (int i = 0; i < size.length; i++) {
      Solver sol = modeler(size[i]);
      sol.findAllSolutions();
      Assert.assertEquals(sol.getMeasures().getSolutionCount(), sols[i]);
      //            Assert.assertEquals(sol.getMeasures().getNodeCount(), nodes[i]);
    }
  }
Beispiel #11
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 + ")");
   }
 }
Beispiel #12
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);
  }
Beispiel #13
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);
  }
Beispiel #14
0
 @Test(groups = "1s")
 public void testregExp7() {
   Solver solver = new Solver();
   IntVar[] CS = VF.enumeratedArray("CS", 10, 0, 2, solver);
   solver.post(ICF.regular(CS, new FiniteAutomaton("0*(1{2,4}0{0,2}0)*0*")));
   Chatterbox.showSolutions(
       solver,
       () -> {
         for (int i = 0; i < 10; i++) {
           System.out.printf("%d", CS[i].getValue());
         }
         //            System.out.printf("\n");
         return "";
       });
   solver.set(ISF.lexico_LB(CS));
   //        Chatterbox.showDecisions(solver);
   solver.findAllSolutions();
   Assert.assertEquals(solver.getMeasures().getSolutionCount(), 84);
 }
Beispiel #15
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);
  }