Пример #1
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);
  }
Пример #2
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;
 }
Пример #3
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);
 }
Пример #4
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());
   }
 }
Пример #5
0
 @Test(groups = "1s")
 public void testJL2() {
   for (int k = 0; k < 50; k++) {
     Solver s = new Solver();
     final IntVar i = VF.enumerated("i", -2, 2, s);
     final IntVar j = VF.enumerated("j", -2, 2, s);
     // Chatterbox.showDecisions(s);
     // Chatterbox.showSolutions(s);
     s.set(ISF.random_value(new IntVar[] {i, j}));
     s.post(new Constraint("Constraint", new PropTestDM1(i, j), new PropTestDM2(i, j)));
     s.findAllSolutions();
   }
 }
Пример #6
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 + ")");
   }
 }
Пример #7
0
  @Test(groups = "1s")
  public void testJL() {
    Solver solver = new Solver();
    final SetVar s0 = VF.set("s0", 0, 1, solver);
    final BoolVar b0 = VF.bool("b0", solver);
    final BoolVar b1 = VF.bool("b1", solver);
    final IntVar i0 = VF.bool("i0", solver);
    solver.set(ISF.lexico_LB(i0));
    solver.post(SCF.bool_channel(new BoolVar[] {b0, b1}, s0, 0));
    solver.post(SCF.cardinality(s0, VF.fixed(0, solver)));

    solver.findSolution();
    solver.getSearchLoop().reset();
    solver.findSolution();
  }
Пример #8
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);
  }
Пример #9
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);
  }
Пример #10
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);
 }
Пример #11
0
  public ExplainingCut(Solver aSolver, int level, long seed) {
    super(aSolver);
    if (!(aSolver.getExplainer() instanceof LazyExplanationEngineFromRestart)) {
      aSolver.set(new LazyExplanationEngineFromRestart(aSolver));
    }
    this.mExplanationEngine = aSolver.getExplainer();
    this.level = level;
    this.random = new Random(seed);

    path = new ArrayList<>(16);
    related2cut = new BitSet(16);
    notFrozen = new BitSet(16);
    unrelated = new BitSet(16);
    refuted = new BitSet(16);
    // TEMPORARY DATA STRUCTURES
    tmpDeductions = new ArrayList<>(16);
    tmpValueDeductions = new HashSet<>(16);
    mSolver.getSearchLoop().plugSearchMonitor(this);
  }
Пример #12
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);
  }
Пример #13
0
 public static void main(String[] args) {
   long[][] stats = new long[2][3];
   for (AbstractProblem problem : problems) {
     // System.out.printf("%s", problem.getClass().getName());
     for (int i = 0; i < 1; i++) {
       for (PropagationEngineFactory pe : engines) {
         //                    System.out.printf(".");
         problem.createSolver();
         Solver solver = problem.getSolver();
         problem.buildModel();
         problem.configureSearch();
         solver.set(pe.make(solver));
         // SMF.toCSV(solver, problem.getClass().getCanonicalName() + ";" + pe.name(),
         // "/Users/kyzrsoze/Sandbox/pren/pe.csv");
         problem.solve();
         switch (pe) {
           case PROPAGATORDRIVEN_7QD:
             stats[0][0] = problem.solver.getMeasures().getSolutionCount();
             stats[0][1] = problem.solver.getMeasures().getNodeCount();
             stats[0][2] = problem.solver.getMeasures().getFailCount();
             break;
           case TWOBUCKETPROPAGATIONENGINE:
             stats[1][0] = problem.solver.getMeasures().getSolutionCount();
             stats[1][1] = problem.solver.getMeasures().getNodeCount();
             stats[1][2] = problem.solver.getMeasures().getFailCount();
             break;
         }
       }
       Assert.assertEquals(
           stats[0],
           stats[1],
           problem.getClass().getCanonicalName()
               + ":"
               + Arrays.toString(stats[0])
               + "!="
               + Arrays.toString(stats[1]));
     }
     // System.out.printf("OK\n");
   }
 }
Пример #14
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();
  }