Ejemplo n.º 1
0
 private int nbNodeFromRegulatModel(int seed) {
   m = new CPModel();
   s = new CPSolver();
   int k = 2;
   IntegerVariable v0 = makeIntVar("v0", 0, 10);
   IntegerVariable v1 = makeIntVar("v1", 0, 10);
   List<int[]> ltuple = new LinkedList<int[]>();
   for (int i = 0; i <= 10; i++) {
     for (int j = 0; j <= 10; j++) {
       if (Math.abs(i - j) < k) ltuple.add(new int[] {i, j});
     }
   }
   m.addConstraint(regular(new IntegerVariable[] {v0, v1}, ltuple));
   s.read(m);
   s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32));
   s.setValIntSelector(new RandomIntValSelector(seed));
   try {
     s.propagate();
   } catch (ContradictionException e) {
     LOGGER.info(e.getMessage()); // To change body of catch statement use File | Settings | File
     // Templates.
   }
   s.solveAll();
   // LOGGER.info("solutions regular : " + s.getNbSolutions());
   return s.getNodeCount();
 }
Ejemplo n.º 2
0
  @Test
  public void test1NeqSolve() {
    for (int seed = 0; seed < 10; seed++) {
      m = new CPModel();
      s = new CPSolver();
      int k = 9, k1 = 7, k2 = 6;
      IntegerVariable v0 = makeIntVar("v0", 0, 10);
      IntegerVariable v1 = makeIntVar("v1", 0, 10);
      IntegerVariable v2 = makeIntVar("v2", 0, 10);
      IntegerVariable v3 = makeIntVar("v3", 0, 10);

      m.addConstraint(distanceNEQ(v0, v1, k));
      m.addConstraint(distanceNEQ(v1, v2, k1));
      m.addConstraint(distanceNEQ(v2, v3, k2));
      s.read(m);
      s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32));
      s.setValIntSelector(new RandomIntValSelector(seed));
      try {
        s.propagate();
      } catch (ContradictionException e) {
        LOGGER.info(e.getMessage());
      }
      s.solveAll();
      int nbNode = s.getNodeCount();
      LOGGER.info("solutions : " + s.getNbSolutions() + " nbNode : " + nbNode);
      assertEquals(s.getNbSolutions(), 12147);
    }
  }
Ejemplo n.º 3
0
 @Test
 public void test3LTSolve() {
   for (int seed = 0; seed < 10; seed++) {
     m = new CPModel();
     s = new CPSolver();
     int k = 2;
     IntegerVariable v0 = makeIntVar("v0", 0, 10);
     IntegerVariable v1 = makeIntVar("v1", 0, 10);
     m.addConstraint(distanceLT(v0, v1, k));
     s.read(m);
     s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32));
     s.setValIntSelector(new RandomIntValSelector(seed));
     try {
       s.propagate();
     } catch (ContradictionException e) {
       LOGGER.info(e.getMessage()); // To change body of catch statement use File | Settings | File
       // Templates.
     }
     s.solveAll();
     int nbNode = s.getNodeCount();
     LOGGER.info("solutions : " + s.getNbSolutions() + " nbNode : " + nbNode);
     assertEquals(s.getNbSolutions(), 31);
     assertEquals(nbNodeFromRegulatModel(seed), nbNode);
   }
 }
Ejemplo n.º 4
0
 @Test
 public void test3BoundsSolve() {
   for (int seed = 0; seed < 10; seed++) {
     m = new CPModel();
     s = new CPSolver();
     int k = 9, k1 = 7, k2 = 6;
     IntegerVariable v0 = makeIntVar("v0", 0, 10);
     IntegerVariable v1 = makeIntVar("v1", 0, 10);
     IntegerVariable v2 = makeIntVar("v2", 0, 10);
     IntegerVariable v3 = makeIntVar("v3", 0, 10);
     m.addVariables(Options.V_BOUND, v0, v1, v2, v3);
     m.addConstraint(distanceEQ(v0, v1, k));
     m.addConstraint(distanceEQ(v1, v2, k1));
     m.addConstraint(distanceEQ(v2, v3, k2));
     s.read(m);
     s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32));
     s.setValIntSelector(new RandomIntValSelector(seed));
     try {
       s.propagate();
     } catch (ContradictionException e) {
       LOGGER.info(e.getMessage()); // To change body of catch statement use File | Settings | File
       // Templates.
     }
     s.solveAll();
     int nbNode = s.getNodeCount();
     LOGGER.info("solutions : " + s.getNbSolutions() + " nbNode : " + nbNode);
     assertEquals(s.getNbSolutions(), 4);
   }
 }
  public PerformanceResult answer(Reasoner r) {
    ChocoResult result = new ChocoResult();
    coreFeats = new LinkedList<GenericFeature>();
    Solver s = new CPSolver();
    ChocoReasoner choco = (ChocoReasoner) r;
    Model model = choco.getProblem();
    s.read(model);

    // utilizando propagacion
    try {
      s.propagate();
    } catch (ContradictionException e) {
      e.printStackTrace();
    }

    Collection<GenericAttributedFeature> allFeats = choco.getAllFeatures();
    Map<String, IntegerVariable> vars = choco.getVariables();
    for (GenericAttributedFeature feat : allFeats) {
      IntegerVariable v = vars.get(feat.getName());
      IntDomainVar vs = s.getVar(v);
      int upper = vs.getSup();
      int lower = vs.getInf();
      if ((upper == lower) && (upper > 0)) {
        coreFeats.add(feat);
      }
    }

    //		result.fillFields(s);
    return result;
  }
Ejemplo n.º 6
0
  @Test
  public void testDXYZProp1bis() {

    IntegerVariable v0 = makeIntVar("v0", 1, 4);
    IntegerVariable v1 = makeIntVar("v1", 5, 7);
    IntegerVariable v2 = makeIntVar("v2", -100, 100);
    m.addVariables(Options.V_BOUND, v0, v1, v2);
    m.addConstraint(distanceEQ(v0, v1, v2, 2));
    s.read(m);
    try {
      s.propagate();
    } catch (ContradictionException e) {
      LOGGER.info(e.getMessage());
      assertTrue(false);
    }
    assertEquals(-1, s.getVar(v2).getInf());
    assertEquals(4, s.getVar(v2).getSup());
  }
Ejemplo n.º 7
0
  public static void main(String[] args) {

    final CPSolver solver = new CPSolver();
    CPModel model = new CPModel();

    // VARIABLES sŽrie varA :
    // 12 variables varsA[i] avec D( varsA[i] ) = { 0, 1, 2, .., 15, i+16}
    int tmpdomain[] = new int[17];
    for (int i = 0; i < 16; i++) tmpdomain[i] = i;
    IntegerVariable[] varsA = new IntegerVariable[12];
    for (int i = 0; i < 12; i++) {
      tmpdomain[16] = 16 + i;
      varsA[i] = makeIntVar("varsA" + i, tmpdomain.clone());
    }
    model.addVariables(varsA);

    // VARIABLES sŽrie varB
    // 16 variables varsB[i] avec D( varsB[i] ) = { 1, 2, .., 11, i+12}
    tmpdomain = new int[13];
    for (int i = 0; i < 12; i++) tmpdomain[i] = i;
    IntegerVariable[] varsB = new IntegerVariable[16];
    for (int i = 0; i < 16; i++) {
      tmpdomain[12] = 16 + i;
      varsB[i] = makeIntVar("varsB" + i, tmpdomain.clone());
    }
    model.addVariables(varsB);

    // variable pour compter les varsA et varsB infŽrieureures ˆ 16
    final IntegerVariable nbvarsInf16 =
        makeIntVar("nbvarsInf16", 0, 28, Options.V_NO_DECISION, Options.V_BOUND);
    model.addVariable(nbvarsInf16);

    // ----------- dŽfinition relation binaire : rel
    int[] min = new int[] {0, 0};
    int[] max = new int[] {27, 27};
    ArrayList<int[]> couplesOK = new ArrayList<int[]>();
    addCouples(couplesOK, 0, 1);
    addCouples(couplesOK, 0, 4);
    addCouples(couplesOK, 0, 3);
    addCouples(couplesOK, 1, 2);
    addCouples(couplesOK, 2, 3);
    addCouples(couplesOK, 2, 11);
    addCouples(couplesOK, 3, 5);
    addCouples(couplesOK, 3, 10);
    addCouples(couplesOK, 4, 5);
    addCouples(couplesOK, 4, 6);
    addCouples(couplesOK, 5, 7);
    addCouples(couplesOK, 5, 8);
    addCouples(couplesOK, 6, 7);
    addCouples(couplesOK, 7, 9);
    addCouples(couplesOK, 8, 10);
    addCouples(couplesOK, 8, 14);
    addCouples(couplesOK, 8, 9);
    addCouples(couplesOK, 9, 15);
    addCouples(couplesOK, 10, 11);
    addCouples(couplesOK, 10, 13);
    addCouples(couplesOK, 11, 12);
    addCouples(couplesOK, 12, 13);
    addCouples(couplesOK, 13, 14);
    addCouples(couplesOK, 14, 15);
    for (int j = 0; j < 12; j++) {
      for (int k = 0; k < 12; k++) {
        addCouples(couplesOK, j + 16, k);
        if (j <= k) addCouples(couplesOK, j + 16, k + 16);
      }
    }
    BinRelation rel = makeBinRelation(min, max, couplesOK, true);

    // ----contraintes entre des Vars A basŽes sur rel2
    model.addConstraint(allDifferent(varsA));

    model.addConstraint(relationPairAC(varsA[10], varsA[11], rel));
    model.addConstraint(relationPairAC(varsA[8], varsA[9], rel));
    model.addConstraint(relationPairAC(varsA[6], varsA[7], rel));
    model.addConstraint(relationPairAC(varsA[5], varsA[9], rel));
    model.addConstraint(relationPairAC(varsA[5], varsA[7], rel));
    model.addConstraint(relationPairAC(varsA[4], varsA[10], rel));
    model.addConstraint(relationPairAC(varsA[4], varsA[8], rel));
    model.addConstraint(relationPairAC(varsA[4], varsA[5], rel));
    model.addConstraint(relationPairAC(varsA[3], varsA[6], rel));
    model.addConstraint(relationPairAC(varsA[3], varsA[5], rel));
    model.addConstraint(relationPairAC(varsA[2], varsA[3], rel));
    model.addConstraint(relationPairAC(varsA[1], varsA[2], rel));
    model.addConstraint(relationPairAC(varsA[0], varsA[11], rel));
    model.addConstraint(relationPairAC(varsA[0], varsA[4], rel));
    model.addConstraint(relationPairAC(varsA[0], varsA[3], rel));
    model.addConstraint(relationPairAC(varsA[0], varsA[1], rel));

    // autres contraintes
    model.addConstraint(lt(varsB[0], varsB[2]));
    model.addConstraint(lt(varsB[0], varsB[4]));
    model.addConstraint(lt(varsB[0], varsB[7]));
    model.addConstraint(lt(varsB[0], varsB[9]));
    model.addConstraint(lt(varsB[0], varsB[11]));
    model.addConstraint(lt(varsB[0], varsB[13]));
    model.addConstraint(lt(varsB[0], varsB[14]));

    // ---- channeling entre vars A et vars B
    model.addConstraint(inverseChannelingWithinRange(varsA, varsB));

    // --- among pour compter les varsA et varsB infŽrieureures ˆ 16
    tmpdomain = new int[16];
    for (int i = 0; i < 16; i++) tmpdomain[i] = i;

    IntegerVariable[] vars_A_et_B = ArrayUtils.append(varsA, varsB);

    model.addConstraint(among(nbvarsInf16, vars_A_et_B, tmpdomain));

    // --------
    // model.addConstraint(Choco.eq(nbvarsInfN, 24));

    solver.read(model);

    try {
      solver.propagate();
    } catch (ContradictionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    solver.maximize(solver.getVar(nbvarsInf16), true);
    Assert.assertEquals(solver.getVar(nbvarsInf16).getVal(), 24);
  }