Example #1
0
  @Test
  public void testNEQ() {
    Model m1 = new CPModel();
    IntegerVariable x = makeIntVar("x", 2, 4);
    IntegerVariable y = makeIntVar("y", 2, 4);

    m1.addConstraint(distanceNEQ(x, y, -2));

    Model m2 = new CPModel();
    m2.addConstraint(neq(abs(minus(x, y)), -2));

    Solver s1 = new CPSolver();
    s1.read(m1);

    Solver s2 = new CPSolver();
    s2.read(m2);

    //        s1.solveAll();
    //        s2.solveAll();
    s1.solve();
    do {
      LOGGER.info("x:" + s1.getVar(x).getVal() + " y:" + s1.getVar(y).getVal());
    } while (s1.nextSolution());
    LOGGER.info("=======");

    s2.solve();
    do {
      LOGGER.info("x:" + s2.getVar(x).getVal() + " y:" + s2.getVar(y).getVal());
    } while (s2.nextSolution());

    Assert.assertEquals("nb sol", s1.getNbSolutions(), s2.getNbSolutions());
  }
Example #2
0
  @Test
  public void testSerge() {
    // On this very first experiment, we'll consider only a single shape
    //  for each object
    // Labels to be placed are:
    // - a label of size (2,2) near (6,3) : at (7,4) or at (4,1)
    // - a label of size (2,4) near (3,2) : at (4,3) or at (0,3)
    // Graphically, this gives (a and b are alternatives for label 1, c and d for 2):
    //
    //        5                               a   a
    //        4   d   d   d   d   c   c   c   ac  a
    //        3   d   d   d   d   c   c   1c  c
    //        2               2   b   b
    //        1                   b   b
    //        0
    //            0   1   2   3   4   5   6   7   8
    //
    // Expected solutions are: "1=a,2=d", "1=b,2=c" and "1=b,2=d"

    // Both labels 1 and 2 will have a single shape each

    IntegerVariable X1 = makeIntVar("X1", 4, 7);
    IntegerVariable Y1 = makeIntVar("Y1", 1, 4);
    IntegerVariable[] p1 = new IntegerVariable[] {X1, Y1};
    GeostObject lab1 =
        new GeostObject(2, 1, constant(1), p1, constant(0), constant(1), constant(1));
    ShiftedBox sb1 = new ShiftedBox(1, new int[] {0, 0}, new int[] {2, 2});

    IntegerVariable X2 = makeIntVar("X2", 0, 4);
    IntegerVariable Y2 = makeIntVar("Y2", 3, 3);
    IntegerVariable[] p2 = new IntegerVariable[] {X2, Y2};
    GeostObject lab2 =
        new GeostObject(2, 2, constant(2), p2, constant(0), constant(1), constant(1));
    ShiftedBox sb2 = new ShiftedBox(2, new int[] {0, 0}, new int[] {4, 2});

    List<GeostObject> gos = new ArrayList<GeostObject>();
    gos.add(lab1);
    gos.add(lab2);
    List<ShiftedBox> SBs = new ArrayList<ShiftedBox>();
    SBs.add(sb1);
    SBs.add(sb2);

    int[] ectrDim = new int[] {0, 1};
    int[] objOfEctr = new int[] {1, 2}; // IDs of labels
    List<IExternalConstraint> ectr = new ArrayList<IExternalConstraint>();
    ectr.add(new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim, objOfEctr));

    Model m = new CPModel();
    m.addConstraint(geost(2, gos, SBs, ectr));
    Solver s = new CPSolver();
    System.out.println("Avant 'Solver.read'");
    s.read(m);
    System.out.println("Avant 'Solver.solve'");
    s.solve();
    System.out.println("Apr�s 'Solver.solve'");
  }
Example #3
0
  public int getNbSolByDecomp(int op) {
    Model m = new CPModel();
    Solver s = new CPSolver();
    IntegerVariable v0 = makeIntVar("v0", 3, 6);
    IntegerVariable v1 = makeIntVar("v1", -3, 4);
    IntegerVariable interV0V1 = makeIntVar("v01", -100, 100);
    IntegerVariable v2 = makeIntVar("v2", 0, 5);
    IntegerVariable interV0V2 = makeIntVar("v02", -100, 100);
    IntegerVariable v3 = makeIntVar("v3", 2, 5);

    m.addConstraint(eq(minus(v0, v1), interV0V1));
    m.addConstraint(eq(minus(v0, v2), interV0V2));
    if (op == 0) {
      m.addConstraint(abs(v2, interV0V1));
      m.addConstraint(abs(v3, interV0V2));
    } else if (op == 1) {
      IntegerVariable interV0V1bis = makeIntVar("v01b", -100, 100);
      IntegerVariable interV0V2bis = makeIntVar("v02b", -100, 100);
      m.addConstraint(abs(interV0V1bis, interV0V1));
      m.addConstraint(abs(interV0V2bis, interV0V2));
      m.addConstraint(lt(interV0V1bis, v2));
      m.addConstraint(lt(interV0V2bis, v3));
    } else {
      IntegerVariable interV0V1bis = makeIntVar("v01b", -100, 100);
      IntegerVariable interV0V2bis = makeIntVar("v02b", -100, 100);
      m.addConstraint(abs(interV0V1bis, interV0V1));
      m.addConstraint(abs(interV0V2bis, interV0V2));
      m.addConstraint(gt(interV0V1bis, v2));
      m.addConstraint(gt(interV0V2bis, v3));
    }
    s.read(m);
    s.solveAll();
    return s.getNbSolutions();
  }
Example #4
0
  @Test
  public void testLT() {
    Model m1 = new CPModel();
    IntegerVariable x = makeIntVar("x", 2, 10);
    IntegerVariable y = makeIntVar("y", 2, 10);

    m1.addConstraint(distanceLT(x, y, -2));

    Model m2 = new CPModel();
    m2.addConstraint(lt(abs(minus(x, y)), -2));

    Solver s1 = new CPSolver();
    s1.read(m1);

    Solver s2 = new CPSolver();
    s2.read(m2);

    s1.solveAll();
    s2.solveAll();

    Assert.assertEquals("nb sol", s1.getNbSolutions(), s2.getNbSolutions());
  }
Example #5
0
  @Test
  @Ignore
  public void RandomProblemGeneration() {

    for (int seed = 0; seed < /*20*/ 5; seed++) {
      // nb of objects, shapes, shifted boxes and maxLength respectively
      // The nb of Obj should be equal to nb Of shapes for NOW. as For the number of the shifted
      // Boxes it should be greater or equal to thhe nb of Objects

      RandomProblemGenerator rp = new RandomProblemGenerator(this.dim, 7, 7, 9, 25);

      rp.generateProb();

      Model m = rp.getModel();

      List<IExternalConstraint> ectr = new ArrayList<IExternalConstraint>();
      int[] ectrDim = new int[this.dim];
      for (int i = 0; i < this.dim; i++) ectrDim[i] = i;

      int[] objOfEctr = new int[rp.getObjects().size()];
      for (int i = 0; i < rp.getObjects().size(); i++) {
        objOfEctr[i] = rp.getObjects().get(i).getObjectId();
      }

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

      Constraint geost = geost(this.dim, rp.getObjects(), rp.getSBoxes(), ectr);
      m.addConstraint(geost);
      Solver s = new CPSolver();
      s.read(m);
      s.setValIntSelector(new RandomIntValSelector(seed));
      s.setVarIntSelector(new RandomIntVarSelector(s, seed));
      s.solveAll();
      Assert.assertEquals("number of solutions", 0, s.getNbSolutions());
    }
  }
Example #6
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;
  }
Example #7
0
  @Test
  public void testCustomProblem() {

    int lengths[] = {5, 3, 2};
    int widths[] = {2, 2, 1};
    int heights[] = {1, 1, 1};

    int nbOfObj = 3;

    for (int seed = 0; seed < 20; seed++) {
      // create the choco problem
      Model m = new CPModel();

      // Create Objects
      List<GeostObject> obj2 = new ArrayList<GeostObject>();

      for (int i = 0; i < nbOfObj; i++) {
        IntegerVariable shapeId = makeIntVar("sid", i, i);
        IntegerVariable coords[] = new IntegerVariable[this.dim];
        for (int j = 0; j < coords.length; j++) {
          coords[j] = makeIntVar("x" + j, 0, 2);
        }
        IntegerVariable start = makeIntVar("start", 1, 1);
        IntegerVariable duration = makeIntVar("duration", 1, 1);
        IntegerVariable end = makeIntVar("end", 1, 1);
        obj2.add(new GeostObject(dim, i, shapeId, coords, start, duration, end));
      }

      // create shiftedboxes and add them to corresponding shapes
      List<ShiftedBox> sb2 = new ArrayList<ShiftedBox>();
      int h = 0;
      while (h < nbOfObj) {

        int[] l = {lengths[h], heights[h], widths[h]};
        int[] t = {0, 0, 0};

        sb2.add(new ShiftedBox(h, t, l));
        h++;
      }

      // Create the external constraints vecotr
      List<IExternalConstraint> ectr2 = new ArrayList<IExternalConstraint>();
      // create the list od dimensions for the external constraint
      int[] ectrDim2 = new int[this.dim];
      for (int d = 0; d < 3; d++) ectrDim2[d] = d;

      // create the list of object ids for the external constraint
      int[] objOfEctr2 = new int[nbOfObj];
      for (int d = 0; d < nbOfObj; d++) {
        objOfEctr2[d] = obj2.get(d).getObjectId();
      }

      // create the external constraint of type non overlapping
      NonOverlappingModel n2 =
          new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim2, objOfEctr2);
      // add the external constraint to the vector
      ectr2.add(n2);

      // create the geost constraint object
      Constraint geost = geost(this.dim, obj2, sb2, ectr2);
      m.addConstraint(geost);
      // post the geost constraint to the choco problem
      Solver s = new CPSolver();
      s.read(m);
      s.setValIntSelector(new RandomIntValSelector(seed));
      s.setVarIntSelector(new RandomIntVarSelector(s, seed));
      s.solveAll();
      Assert.assertEquals("number of solutions", 9828, s.getNbSolutions());
    }
  }
Example #8
0
  @Test
  public void exp2D2Test() {

    dim = 2;
    int nbOfObj = 3;

    // create the choco problem
    Model m = new CPModel();

    // Create Objects
    List<GeostObject> objects = new ArrayList<GeostObject>();

    for (int i = 0; i < nbOfObj; i++) {
      IntegerVariable shapeId = makeIntVar("sid_" + i, domShapes[i][0], domShapes[i][1]);
      IntegerVariable coords[] = new IntegerVariable[dim];
      coords[0] = makeIntVar("x_" + i, domOrigins[i][0], domOrigins[i][1]);
      coords[1] = makeIntVar("y_" + i, domOrigins[i][2], domOrigins[i][3]);

      // ++ Modification
      // Additional Constraint
      m.addConstraint(geq(coords[0], 1));
      // -- Modification

      IntegerVariable start = makeIntVar("start", 0, 0);
      IntegerVariable duration = makeIntVar("duration", 1, 1);
      IntegerVariable end = makeIntVar("end", 1, 1);
      objects.add(new GeostObject(dim, i, shapeId, coords, start, duration, end));
    }

    // create shiftedboxes and add them to corresponding shapes
    List<ShiftedBox> sb = new ArrayList<ShiftedBox>();

    for (int i = 0; i < shBoxes.length; i++) {
      int[] offset = {shBoxes[i][1], shBoxes[i][2]};
      int[] sizes = {shBoxes[i][3], shBoxes[i][4]};
      sb.add(new ShiftedBox(shBoxes[i][0], offset, sizes));
    }

    // Create the external constraints vecotr
    List<IExternalConstraint> ectr = new ArrayList<IExternalConstraint>();

    // create the list of dimensions for the external constraint
    int[] ectrDim = new int[dim];
    for (int d = 0; d < dim; d++) ectrDim[d] = d;

    // create the list of object ids for the external constraint
    int[] objOfEctr = new int[nbOfObj];
    for (int d = 0; d < nbOfObj; d++) {
      objOfEctr[d] = objects.get(d).getObjectId();
    }

    // create the external constraint of non overlapping type
    NonOverlappingModel n = new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim, objOfEctr);
    // add the external constraint to the ectr vector
    ectr.add(n);

    // create the geost constraint
    Constraint geost = geost(dim, objects, sb, ectr);

    // post the geost constraint to the choco problem
    m.addConstraint(geost);

    // build a solver
    Solver s = new CPSolver();
    // read the problem
    s.read(m);

    // solve the probem
    s.solve();

    Assert.assertSame("No solution expected", Boolean.FALSE, s.isFeasible());

    // print the solution
    LOGGER.info(s.pretty());
  }
Example #9
0
  @Test
  public void exp2DTest() {

    // The data
    int dim = 2;
    int[][] domOrigins = {
      {0, 5, 0, 3},
      {0, 5, 0, 5},
      {0, 6, 0, 4},
      {0, 6, 0, 5},
      {0, 5, 0, 5},
      {0, 7, 0, 4},
      {0, 6, 0, 5},
      {0, 6, 0, 5},
      {0, 5, 0, 6},
      {0, 7, 0, 5}
    };

    int[][] shBoxes = {
      {0, 0, 0, 2, 3},
      {0, 1, 2, 2, 2},
      {1, 0, 0, 3, 2},
      {2, 0, 0, 2, 3},
      {3, 0, 0, 2, 2},
      {4, 0, 0, 3, 1},
      {4, 1, 0, 1, 2},
      {5, 0, 0, 1, 3},
      {6, 0, 0, 1, 2},
      {6, 0, 1, 2, 1},
      {7, 0, 0, 2, 1},
      {7, 1, 0, 1, 2},
      {8, 0, 0, 3, 1},
      {9, 0, 0, 1, 2}
    };
    int[] v0 = {-1, -2, -3};
    int[] v1 = {-1, 2, 3};
    int[] v2 = {-1, 2, -3};
    int[] v3 = {-1, 3, -2};

    int nbOfObj = 10;

    // create the choco problem
    Model m = new CPModel();

    // Create Objects
    List<GeostObject> obj = new ArrayList<GeostObject>();

    for (int i = 0; i < nbOfObj; i++) {
      IntegerVariable shapeId = makeIntVar("sid", i, i);
      IntegerVariable coords[] = new IntegerVariable[dim];
      coords[0] = makeIntVar("x", domOrigins[i][0], domOrigins[i][1]);
      coords[1] = makeIntVar("y", domOrigins[i][2], domOrigins[i][3]);

      IntegerVariable start = makeIntVar("start", 1, 1);
      IntegerVariable duration = makeIntVar("duration", 1, 1);
      IntegerVariable end = makeIntVar("end", 1, 1);
      obj.add(new GeostObject(dim, i, shapeId, coords, start, duration, end));
    }

    // create shiftedboxes and add them to corresponding shapes
    List<ShiftedBox> sb = new ArrayList<ShiftedBox>();
    for (int i = 0; i < shBoxes.length; i++) {
      int[] offset = {shBoxes[i][1], shBoxes[i][2]};
      int[] sizes = {shBoxes[i][3], shBoxes[i][4]};
      sb.add(new ShiftedBox(shBoxes[i][0], offset, sizes));
    }

    // Create the external constraints vecotr
    List<IExternalConstraint> ectr = new ArrayList<IExternalConstraint>();
    // create the list of dimensions for the external constraint
    int[] ectrDim = new int[dim];
    for (int d = 0; d < dim; d++) ectrDim[d] = d;

    // create the list of object ids for the external constraint
    int[] objOfEctr = new int[nbOfObj];
    for (int d = 0; d < nbOfObj; d++) {
      objOfEctr[d] = obj.get(d).getObjectId();
    }

    //		create the external constraint of type non overlapping
    NonOverlappingModel n = new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim, objOfEctr);
    // add the external constraint to the vector
    ectr.add(n);

    // create the list of controlling vectors
    List<int[]> ctrlVs = new ArrayList<int[]>();
    ctrlVs.add(v0);
    // ctrlVs.add(v1);
    // ctrlVs.add(v2);
    // ctrlVs.add(v3);

    // create the geost constraint
    Constraint geost = geost(dim, obj, sb, ectr, ctrlVs);

    // NOTA: you can choose to not take into account of the greedy mode by creating the geost
    // constraint as follows:
    // Geost_Constraint geost = new Geost_Constraint(vars, dim, obj, sb, ectr);

    // post the geost constraint to the choco problem
    m.addConstraint(geost);

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

    // solve the probem
    s.solve();

    for (int i = 0; i < obj.size(); i++) {
      GeostObject o = obj.get(i);
      StringBuffer st = new StringBuffer();
      st.append(MessageFormat.format("Object {0}: ", o.getObjectId()));
      for (int j = 0; j < dim; j++)
        st.append(MessageFormat.format("{0} ", s.getVar(o.getCoordinates()[j])));
      LOGGER.info(st.toString());
    }
  }
Example #10
0
  @Test
  public void testOfSharingShape() {

    int lengths[] = {5, 3, 6};
    int widths[] = {2, 4, 1};
    int heights[] = {1, 2, 4};

    int nbOfObj = 3;
    Model m = new CPModel();

    // Create Objects
    List<GeostObject> obj2 = new ArrayList<GeostObject>();

    for (int i = 0; i < nbOfObj; i++) {
      IntegerVariable shapeId = makeIntVar("sid", 0, 0);
      IntegerVariable coords[] = new IntegerVariable[this.dim];
      for (int j = 0; j < coords.length; j++) {
        coords[j] = makeIntVar("x" + j, 0, 2);
      }
      IntegerVariable start = makeIntVar("start", 1, 1);
      IntegerVariable duration = makeIntVar("duration", 1, 1);
      IntegerVariable end = makeIntVar("end", 1, 1);
      obj2.add(new GeostObject(dim, i, shapeId, coords, start, duration, end));
    }
    for (int i = 0; i < obj2.size(); i++) {
      for (int d = 0; d < this.dim; d++) {
        LOGGER.info(
            ""
                + obj2.get(i).getCoordinates()[d].getLowB()
                + "    "
                + obj2.get(i).getCoordinates()[d].getUppB());
      }
    }

    // create shiftedboxes and add them to corresponding shapes
    List<ShiftedBox> sb2 = new ArrayList<ShiftedBox>();

    int[] l = {lengths[0], heights[0], widths[0]};
    int[] t = {0, 0, 0};

    sb2.add(new ShiftedBox(0, t, l));

    List<IExternalConstraint> ectr2 = new ArrayList<IExternalConstraint>();
    int[] ectrDim2 = new int[this.dim];
    for (int d = 0; d < 3; d++) ectrDim2[d] = d;

    int[] objOfEctr2 = new int[nbOfObj];
    for (int d = 0; d < nbOfObj; d++) {
      objOfEctr2[d] = obj2.get(d).getObjectId();
    }

    NonOverlappingModel n2 =
        new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim2, objOfEctr2);
    ectr2.add(n2);

    Constraint geost2 = geost(this.dim, obj2, sb2, ectr2);
    m.addConstraint(geost2);
    Solver s = new CPSolver();
    s.read(m);
    // Here the solve will only do a test for the first constraint and not the second.
    // However for our purposes this is not important. If it is just change the code
    // of solve to take 2 constraints as parameters and then run the two solution testers
    s.solveAll();
    Assert.assertEquals(s.getNbSolutions(), 7290);
  }
Example #11
0
  @Test
  public void PolyMorphicTest() {
    int[][] objects =
        new int[][] {
          {0, 0, 1, 0, 3, 0, 4, 1, 1, 1, 1, 1, 1},
          {1, 0, 1, 0, 3, 0, 4, 1, 1, 1, 1, 1, 1},
          {2, 0, 1, 0, 3, 0, 4, 1, 1, 1, 1, 1, 1},
          {3, 0, 1, 0, 3, 0, 4, 1, 1, 1, 1, 1, 1},
          {4, 0, 1, 0, 3, 0, 4, 1, 1, 1, 1, 1, 1},
          {5, 2, 2, 0, 0, 6, 6, 1, 1, 1, 1, 1, 1}
        };
    int[] shapes = new int[] {2, 1, 0};
    int[][] shiftedBoxes =
        new int[][] {
          {0, 0, 0, 3, 2},
          {1, 0, 0, 2, 3},
          {2, 0, 0, 5, 1},
          {2, 5, -6, 1, 7},
        };

    this.dim = 2;

    InputParser parser;
    new InputParser();

    InputParser.GeostProblem gp = new InputParser.GeostProblem(objects, shapes, shiftedBoxes);
    for (int seed = 0; seed < 20; seed++) {
      parser = new InputParser(gp, dim);
      try {
        parser.parse();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      Model m = new CPModel();

      // create a vector to hold in it all the external constraints we want to add to geost
      List<IExternalConstraint> ectr = new ArrayList<IExternalConstraint>();

      // ////////////Create the needed external constraints//////////////

      // first of all create a array of intergers containing all the dimensions where the constraint
      // will be active
      int[] ectrDim = new int[dim];
      for (int i = 0; i < dim; i++) ectrDim[i] = i;

      // Create an array of object ids representing all the objects that the external constraint
      // will be applied to
      int[] objOfEctr = new int[parser.getObjects().size()];
      for (int i = 0; i < parser.getObjects().size(); i++) {
        objOfEctr[i] = parser.getObjects().get(i).getObjectId();
      }

      // Create the external constraint, in our case it is the NonOverlappingModel
      // constraint (it is the only one implemented for now)
      NonOverlappingModel n =
          new NonOverlappingModel(Constants.NON_OVERLAPPING, ectrDim, objOfEctr);

      // add the created external constraint to the vector we created
      ectr.add(n);

      // /////////////Create the array of variables to make choco happy//////////////
      // vars will be stored as follows: object 1 coords(so k coordinates), sid, start, duration,
      // end,
      //                                 object 2 coords(so k coordinates), sid, start, duration,
      // end and so on ........
      // To retrieve the index of a certain variable, the formula is (nb of the object in question =
      // objId assuming objIds are consecutive and
      // start from 0) * (k + 4) + number of the variable wanted the number of the variable wanted
      // is decided as follows: 0 ... k-1
      // (the coords), k (the sid), k+1 (start), k+2 (duration), k+3 (end)

      int originOfObjects = parser.getObjects().size() * dim;
      // Number of domain  variables  to  represent the origin of all  objects
      int otherVariables =
          parser.getObjects().size()
              * 4; // each object  has 4 other variables: shapeId, start,  duration; end
      IntegerVariable[] vars = new IntegerVariable[originOfObjects + otherVariables];

      for (int i = 0; i < parser.getObjects().size(); i++) {
        for (int j = 0; j < dim; j++) {
          vars[(i * (dim + 4)) + j] = parser.getObjects().get(i).getCoordinates()[j];
        }
        vars[(i * (dim + 4)) + dim] = parser.getObjects().get(i).getShapeId();
        vars[(i * (dim + 4)) + dim + 1] = parser.getObjects().get(i).getStartTime();
        vars[(i * (dim + 4)) + dim + 2] = parser.getObjects().get(i).getDurationTime();
        vars[(i * (dim + 4)) + dim + 3] = parser.getObjects().get(i).getEndTime();
      }

      Constraint geost = geost(dim, parser.getObjects(), parser.getShiftedBoxes(), ectr);

      // /////////////Add the constraint to the choco problem//////////////
      m.addConstraint(geost);

      for (int i = 0; i < parser.getObjects().size() - 2; i++) {
        m.addConstraint(
            lex(
                parser.getObjects().get(i).getCoordinates(),
                parser.getObjects().get(i + 1).getCoordinates()));
      }

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

      s.setValIntSelector(new RandomIntValSelector(seed));
      s.setVarIntSelector(new RandomIntVarSelector(s, seed));

      s.solveAll();
      Assert.assertEquals(s.getNbSolutions(), 2);
    }
  }
  public static void main(String[] args) {
    // Define input Data
    int nbNodes = 5;
    int nbEdges = 9;
    int pathLength = nbNodes - 1;
    int nbLabels = nbEdges;
    FiniteAutomaton dfa = new FiniteAutomaton();
    int a = dfa.addState();
    int b = dfa.addState();
    int c = dfa.addState();
    int d = dfa.addState();
    int e = dfa.addState();
    dfa.setInitialState(a);
    dfa.setFinal(e);
    dfa.addTransition(a, b, 0);
    dfa.addTransition(a, c, 1);
    dfa.addTransition(c, a, 2);
    dfa.addTransition(b, c, 3);
    dfa.addTransition(d, b, 4);
    dfa.addTransition(b, e, 5);
    dfa.addTransition(c, d, 6);
    dfa.addTransition(d, e, 7);
    dfa.addTransition(e, e, 8);
    int[][][] costs = new int[pathLength][nbLabels][2];
    for (int i = 0; i < pathLength; i++) {
      costs[i][0][0] = 6;
      costs[i][0][1] = 4;
      costs[i][1][0] = 4;
      costs[i][1][1] = 7;
      costs[i][2][0] = 5;
      costs[i][2][1] = 3;
      costs[i][3][0] = 3;
      costs[i][3][1] = 2;
      costs[i][4][0] = 6;
      costs[i][4][1] = 4;
      costs[i][5][0] = 4;
      costs[i][5][1] = 6;
      costs[i][6][0] = 10;
      costs[i][6][1] = 1;
      costs[i][7][0] = 2;
      costs[i][7][1] = 1;
      costs[i][8][0] = 0;
      costs[i][8][1] = 0;
    }
    int[] maxCost = new int[] {30, 10};

    // Declare variables
    IntegerVariable[] x =
        makeIntVarArray(
            "Edge n¡", pathLength, 0, nbEdges - 1); // x[i] = k if the ith edge of the path is k
    IntegerVariable[] totalCost = makeIntVarArray("Total cost", 2, 0, 100);

    // Add constraints to the model
    Model m = new CPModel();
    m.addConstraint(
        multiCostRegular(
            totalCost, x, dfa, costs)); // x is a word recognized by dfa and totalCost = cost of x
    m.addConstraint(leq(totalCost[0], maxCost[0]));
    m.addConstraint(leq(totalCost[1], maxCost[1]));

    // Solve !
    Solver s = new CPSolver();
    s.read(m);
    s.solve();
    System.out.println(s.pretty());
    for (int i = 0; i < nbNodes - 1; i++)
      System.out.println(x[i].getName() + " = " + s.getVar(x[i]).getVal());
    System.out.println(totalCost[0].getName() + " = " + s.getVar(totalCost[0]).getVal());
    System.out.println(totalCost[1].getName() + " = " + s.getVar(totalCost[1]).getVal());
  }
Example #13
0
 public void post(Object constraint) {
   model.addConstraint((Constraint) constraint);
 }