예제 #1
0
  @Test
  public void testNonOverlap() {
    CPModel model = new CPModel();

    List<GeostObject> geosts = new ArrayList<GeostObject>();
    Map<Integer, ShiftedBox> boxesById = new HashMap<Integer, ShiftedBox>();

    // Blocks to be placed
    int currentShapeId = 0;
    ShiftedBox block = new ShiftedBox(currentShapeId, new int[] {0, 0}, new int[] {20, 1});
    boxesById.put(currentShapeId, block);

    IntegerVariable[] fixedCoordinates = new IntegerVariable[] {constant(0), constant(0)};
    IntegerVariable[] variableCoordinates =
        new IntegerVariable[] {makeIntVar("variable", 0, 0), constant(0)};

    geosts.add(
        new GeostObject(
            2,
            0,
            constant(currentShapeId),
            fixedCoordinates,
            constant(0),
            constant(1),
            constant(1)));

    geosts.add(
        new GeostObject(
            2,
            1,
            constant(currentShapeId),
            fixedCoordinates,
            constant(0),
            constant(1),
            constant(1)));

    List<IExternalConstraint> ectr = new ArrayList<IExternalConstraint>();

    int[] ectrDim = new int[] {0, 1};
    int[] objOfEctr = new int[geosts.size()];

    NonOverlappingModel n = new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim, objOfEctr);
    ectr.add(n);

    List<int[]> ctrlVs = new ArrayList<int[]>();
    int[] v0 = {1, -3, -2};
    // int[] v1 = { 1, -2, 2 };
    // ctrlVs.add(v1);
    ctrlVs.add(v0);

    // Definition of the GEOST constraint
    Constraint geost =
        geost(2, geosts, new ArrayList<ShiftedBox>(boxesById.values()), ectr, ctrlVs);
    model.addConstraint(geost);

    CPSolver solver = new CPSolver();
    solver.read(model);

    Assert.assertFalse(solver.solve());
  }
예제 #2
0
  public static void main(String[] args) {
    RuleModel m = new RuleModel();

    m.buildConsecutiveWERule();
    m.buildNoNightBeforeFreeWE();
    m.buildNoMoreThanDayRule();
    m.buildRestAfterNight();
    m.buildCompleteWE();

    m.fillModel();

    m.addLexConstraint();
    m.addMandatoryShift();
    m.addMinCoverConstraint();

    CPSolver s = new CPSolver();
    s.read(m);

    ArrayList<IntDomainVar> mars = new ArrayList<IntDomainVar>();
    for (int i = 0; i < 8; i++) mars.add(s.getVar(m.cvs[i][4]));

    int[][] lowb = new int[28][3];
    {
      for (int i = 0; i < lowb.length; i++) {
        lowb[i][0] = 3;
        lowb[i][1] = 1;
        lowb[i][2] = 4;
      }
    }

    CoverVarValSelector sel = new CoverVarValSelector(s, m.vs, lowb);
    //   s.attachGoal(new AssignVar(sel,sel));

    // s.attachGoal(new AssignVar(new StaticVarOrder(mars.toArray(new IntDomainVar[8])),new
    // DecreasingDomain()));
    s.attachGoal(
        new AssignVar(
            new StaticVarOrder(s, s.getVar(ArrayUtils.flatten(ArrayUtils.transpose(m.vs)))),
            new IncreasingDomain()));
    // s.addGoal(new AssignVar(new
    // RandomIntVarSelector(s,s.getVar(ArrayUtils.flatten(ArrayUtils.transpose(m.vs))),0),new
    // RandomIntValSelector()));

    if (s.solve()) {

      int i = 0;
      for (IntegerVariable[] va : m.vs) {

        for (IntDomainVar v : s.getVar(va)) {
          System.out.print(toChar(v.getVal()) + " ");
        }
        System.out.print("     |   ");
        for (IntDomainVar v : s.getVar(m.cvs[i++])) {
          System.out.print(v.getVal() + " ");
        }
        System.out.println("");
      }
    }
    s.printRuntimeStatistics();
  }
예제 #3
0
 public boolean solve() {
   CPSolver solver = new CPSolver();
   for (Iterator iterator = model.getConstraintIterator(); iterator.hasNext(); ) {
     Constraint constraint = (Constraint) iterator.next();
     System.out.println(constraint);
   }
   System.out.println("****");
   solver.read(model);
   boolean solverable = solver.solve();
   return solverable;
 }
예제 #4
0
  /*
   * One potential issue is determining the best way to build constraints.
   * Currently the model is reset after solving, and the solver
   * is reset right before solving. Is this the best way to do this?
   * We could alternatively pop constraints when backtracking,
   * though this would require some changes to how ProblemGeneral
   * is interfaced.
   *
   */
  public Boolean solve() {
    solver.read(model);

    System.out.println("Model:" + model.constraintsToString());

    solver.setTimeLimit(timeBound);
    Boolean solved = solver.solve();
    boolean feasible = solver.isFeasible();

    System.out.println("Solved: " + solved);
    System.out.println("Feasible: " + feasible);

    return solved;
  }
예제 #5
0
 @Override
 protected Boolean solve(Instance instance) {
   CPModel chocoModel = new CPModel();
   Map<Variable, IntegerVariable> variableMap = new HashMap<Variable, IntegerVariable>();
   try {
     new ChocoTranslator(chocoModel, variableMap).translate(instance.getExpression());
     CPSolver chocoSolver = new CPSolver();
     chocoSolver.read(chocoModel);
     return chocoSolver.solve();
   } catch (TranslatorUnsupportedOperation x) {
     log.log(Level.WARNING, x.getMessage(), x);
   } catch (VisitorException x) {
     log.log(Level.SEVERE, "encountered an exception -- this should not be happening!", x);
   }
   return null;
 }
예제 #6
0
  @Test
  public void testDXYZProp2bis() {

    IntegerVariable v0 = makeIntVar("v0", 1, 5);
    IntegerVariable v1 = makeIntVar("v1", 5, 10);
    IntegerVariable v2 = makeIntVar("v2", 1, 2);
    m.addVariables(Options.V_BOUND, v0, v1, v2);
    m.addConstraint(distanceEQ(v0, v1, v2, -1));
    s.read(m);
    try {
      s.propagate();
    } catch (ContradictionException e) {
      assertTrue(false);
    }
    assertEquals(4, s.getVar(v0).getInf());
    assertEquals(6, s.getVar(v1).getSup());
    s.solve();
    LOGGER.info("" + s.pretty());
  }
 private void solve() {
   if (!(solver.solve())) {
     System.err.println("No solutions");
   }
 }