Example #1
0
  @Test
  public void test_ajdvries() {
    for (int nbO = 4; nbO < 257; nbO *= 4) {
      boolean inc = true;
      do {
        int width = nbO * 5;
        int height = nbO * 5;
        int maxX = width - 5;
        int maxY = height - 5;

        CPModel m = new CPModel();

        List<GeostObject> geosts = new Vector<GeostObject>();
        List<ShiftedBox> sb = new Vector<ShiftedBox>();

        List<IntegerVariable> x = new ArrayList<IntegerVariable>();
        List<IntegerVariable> y = new ArrayList<IntegerVariable>();
        for (int a = 0; a < 16; a++) {
          IntegerVariable varX = makeIntVar("img_" + a + "_x", 0, maxX);
          IntegerVariable varY = makeIntVar("img_" + a + "_y", 0, maxY);
          x.add(varX);

          y.add(varY);
          IntegerVariable coordinates[] = new IntegerVariable[] {varX, varY};

          geosts.add(
              new GeostObject(
                  2, a, constant(a), coordinates, constant(0), constant(1), constant(1)));

          sb.add(new ShiftedBox(a, new int[] {0, 0}, new int[] {5, 5}));
        }

        Vector<IExternalConstraint> ectr = new Vector();

        int[] ectrDim = new int[] {0, 1};
        int[] objOfEctr = new int[geosts.size()];
        for (int i = 0; i < geosts.size(); i++) {
          objOfEctr[i] = geosts.get(i).getObjectId();
        }

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

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

        // Definition of the GEOST constraint
        GeostOptions.increment = inc;
        Constraint geost = geost(2, geosts, sb, ectr, ctrlVs);
        m.addConstraint(geost);

        Solver solver = new CPSolver();
        solver.read(m);
        Assert.assertTrue(solver.solve());
        inc ^= true;
      } while (!inc);
    }
  }
Example #2
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());
  }
Example #3
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 #4
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);
  }