Exemple #1
0
 @Test(groups = "1s")
 public void testSolution() {
   Solver s = modelit();
   long sl = 50;
   SearchMonitorFactory.limitSolution(s, sl);
   s.findAllSolutions();
   long sc = s.getMeasures().getSolutionCount();
   Assert.assertEquals(sc, sl);
 }
Exemple #2
0
 @Test(groups = "1s")
 public void testNode() {
   Solver s = modelit();
   long nl = 50;
   SearchMonitorFactory.limitNode(s, nl);
   s.findAllSolutions();
   long nc = s.getMeasures().getNodeCount();
   Assert.assertEquals(nc, nl);
 }
Exemple #3
0
 @Test(groups = "1s")
 public void testBacktrack() {
   Solver s = modelit();
   long bl = 50;
   SearchMonitorFactory.limitBacktrack(s, bl);
   s.findAllSolutions();
   long bc = s.getMeasures().getBackTrackCount();
   Assert.assertEquals(bc, bl);
 }
Exemple #4
0
 @Test(groups = "1s")
 public void testFail() {
   Solver s = modelit();
   long fl = 50;
   SearchMonitorFactory.limitFail(s, fl);
   s.findAllSolutions();
   long fc = s.getMeasures().getFailCount();
   Assert.assertEquals(fc, fl);
 }
Exemple #5
0
 @Test(groups = "1s")
 public void testThreadTime() {
   Solver s = modelit();
   long tl = 500;
   SearchMonitorFactory.limitThreadTime(s, tl);
   s.findAllSolutions();
   float tc = s.getMeasures().getTimeCount();
   Assert.assertTrue(tl - (tl * 10 / 100) <= tc && tc <= tl + (tl * 10 / 100), tl + " vs. " + tc);
 }
  /**
   * Builds an ExplanationEngine
   *
   * @param slv associated solver's environment
   */
  public LazyExplanationEngine(Solver slv) {
    super(slv);
    IEnvironment env = slv.getEnvironment();
    curChunk = env.makeInt(0);
    nextTop = env.makeInt(0);

    varChunks = new IntVar[1][];
    varChunks[0] = new IntVar[CHUNK_SIZE];

    cauChunks = new ICause[1][];
    cauChunks[0] = new ICause[CHUNK_SIZE];

    masChunks = new EventType[1][];
    masChunks[0] = new EventType[CHUNK_SIZE];

    val1Chunks = new int[1][];
    val1Chunks[0] = new int[CHUNK_SIZE];

    val2Chunks = new int[1][];
    val2Chunks[0] = new int[CHUNK_SIZE];

    val3Chunks = new int[1][];
    val3Chunks[0] = new int[CHUNK_SIZE];

    up2date = false;
  }
Exemple #7
0
  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;
  }
Exemple #8
0
 public static IntVar bounded(String name, int min, int max, Solver solver) {
   checkIntVar(name, min, max);
   if (min == max) {
     return Views.fixed(name, min, solver);
   } else {
     IntVar var = new IntervalIntVarImpl(name, min, max, solver);
     var.setHeuristicVal(HeuristicValFactory.presetI(var));
     solver.associates(var);
     return var;
   }
 }
Exemple #9
0
 public static IntVar enumerated(String name, int[] values, Solver solver) {
   checkIntVar(name, values[0], values[values.length - 1]);
   if (values.length == 1) {
     return Views.fixed(name, values[0], solver);
   } else {
     BitsetIntVarImpl var = new BitsetIntVarImpl(name, values, solver);
     var.setHeuristicVal(HeuristicValFactory.presetI(var));
     solver.associates(var);
     return var;
   }
 }
Exemple #10
0
  protected DomOverWDeg(Solver solver, long seed) {
    this.solver = solver;
    rand = new Random(seed);
    counter = new FailPerPropagator(solver.getCstrs(), solver);
    vid2dsize = new TIntIntHashMap();
    vid2degree = new TIntIntHashMap();
    vid2weig = new TIntIntHashMap();
    pid2ari = new TIntObjectHashMap<IStateInt>();
    pid2arity = new TIntIntHashMap(10, 0.5F, -1, -1);

    Variable[] vars = solver.getVars();
    for (int i = 0; i < vars.length; i++) {
      vars[i].addMonitor(this);
    }
    Constraint[] cstrs = solver.getCstrs();
    for (int i = 0; i < cstrs.length; i++) {
      Propagator[] props = cstrs[i].getPropagators();
      for (int j = 0; j < props.length; j++) {
        pid2ari.put(props[j].getId(), solver.getEnvironment().makeInt(props[j].arity()));
      }
    }
  }
Exemple #11
0
 public BitsetIntVarImpl(String name, int min, int max, Solver solver) {
   super(name, solver);
   IEnvironment env = solver.getEnvironment();
   this.OFFSET = min;
   int capacity = max - min + 1;
   this.VALUES = env.makeBitSet(capacity);
   for (int i = 0; i <= max - min; i++) {
     this.VALUES.set(i, true);
   }
   this.LB = env.makeInt(0);
   this.UB = env.makeInt(max - min);
   this.SIZE = env.makeInt(capacity);
 }
 @Override
 public void run() {
   while (running) {
     GemGrid grid = new GemGrid();
     Solver solver = new Solver(grid);
     try {
       solver.generateGrid();
     } catch (AWTException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
   while (!running) {
     try {
       Thread.sleep(2000);
     } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
   run();
 }
Exemple #13
0
 public BitsetIntVarImpl(String name, int[] sortedValues, Solver solver) {
   super(name, solver);
   IEnvironment env = solver.getEnvironment();
   OFFSET = sortedValues[0];
   int capacity = sortedValues[sortedValues.length - 1] - OFFSET + 1;
   this.VALUES = env.makeBitSet(capacity);
   for (int i = 0; i < sortedValues.length; i++) {
     this.VALUES.set(sortedValues[i] - OFFSET, true);
   }
   this.LB = env.makeInt(0);
   this.UB = env.makeInt(capacity - 1);
   this.SIZE = env.makeInt(sortedValues.length);
 }
Exemple #14
0
 /**
  * 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;
 }
Exemple #15
0
 /**
  * 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;
 }
Exemple #16
0
  public BitsetXYSumView(IntVar a, IntVar b, Solver solver) {
    super(a, b, solver);
    int lbA = A.getLB();
    int ubA = A.getUB();
    int lbB = B.getLB();
    int ubB = B.getUB();
    OFFSET = lbA + lbB;
    VALUES = solver.getEnvironment().makeBitSet((ubA + ubB) - (lbA + lbB) + 1);

    DisposableRangeIterator itA = A.getRangeIterator(true);
    DisposableRangeIterator itB = B.getRangeIterator(true);
    while (itA.hasNext()) {
      itB.bottomUpInit();
      while (itB.hasNext()) {
        VALUES.set(itA.min() + itB.min() - OFFSET, itA.max() + itB.max() - OFFSET + 1);
        itB.next();
      }
      itB.dispose();
      itA.next();
    }
    itA.dispose();
    SIZE.set(VALUES.cardinality());
  }
Exemple #17
0
 /**
  * Build a boolean variable, ie domain is [0,1]
  *
  * @param name name of variable
  * @param solver solver involving the variable
  * @return a BoolVar
  */
 public static BoolVar bool(String name, Solver solver) {
   BooleanBoolVarImpl var = new BooleanBoolVarImpl(name, solver);
   var.setHeuristicVal(HeuristicValFactory.presetI(var));
   solver.associates(var);
   return var;
 }