Ejemplo n.º 1
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();
  }
Ejemplo n.º 2
0
  public void addMinCoverConstraint() {
    IntegerVariable[] worked = ArrayUtils.transpose(cvs)[4];
    IntegerVariable[] night = ArrayUtils.transpose(cvs)[5];
    IntegerVariable[] rest = ArrayUtils.transpose(cvs)[6];
    // 3 days and 1 night per day -> 4*28 working days
    this.addConstraint(eq(sum(worked), 4 * 28));
    this.addConstraint(eq(sum(night), 28));
    this.addConstraint(eq(sum(rest), 28 * 4));

    IntegerVariable[] week0 = ArrayUtils.transpose(cvs)[0];
    this.addConstraint(eq(sum(week0), 4 * 7));

    IntegerVariable[] week1 = ArrayUtils.transpose(cvs)[1];
    this.addConstraint(eq(sum(week1), 4 * 7));

    IntegerVariable[] week2 = ArrayUtils.transpose(cvs)[2];
    this.addConstraint(eq(sum(week2), 4 * 7));

    IntegerVariable[] week3 = ArrayUtils.transpose(cvs)[3];
    this.addConstraint(eq(sum(week3), 4 * 7));
  }
Ejemplo n.º 3
0
  void fillModel() {

    vs = makeIntVarArray("x", 8, 28, 0, 2);

    int[][][] csts = new int[vs[0].length][3][7];

    for (int i = 0; i < csts.length; i++) {
      for (int j = 0; j < csts[i].length; j++) {
        if (j == 0 || j == 1) csts[i][j][4] = 1;
        if (j == 1) csts[i][j][5] = 1;
        if (j == 2) csts[i][j][6] = 1;

        if (j == 0 || j == 1) {
          csts[i][j][i / 7] = 1;
        }
      }
    }

    cvs = new IntegerVariable[8][7];
    for (int i = 0; i < 4; i++) {
      IntegerVariable[] tmp = cvs[i];
      tmp[4] = makeIntVar("z_{" + i + ",0}", 0, 18, Options.V_BOUND);
      tmp[5] = makeIntVar("z_{" + i + ",1}", 0, 4, Options.V_BOUND);
      tmp[6] = makeIntVar("z_{" + i + ",2}", 10, 28, Options.V_BOUND);
      for (int j = 0; j < 4; j++)
        tmp[j] = makeIntVar("z_{" + i + "," + j + "}", 4, 5, Options.V_BOUND);
      this.addVariables(tmp);
      //    this.addConstraint(eq(minus(tmp[4],28),minus(0,tmp[6])));

      this.addVariables(vs[i]);
    }

    for (int i = 4; i < 8; i++) {
      IntegerVariable[] tmp = cvs[i];
      tmp[4] = makeIntVar("z_{" + i + ",0}", 0, 10, Options.V_BOUND);
      tmp[5] = makeIntVar("z_{" + i + ",1}", 0, 4, Options.V_BOUND);
      tmp[6] = makeIntVar("z_{" + i + ",1}", 18, 28, Options.V_BOUND);

      for (int j = 0; j < 4; j++)
        tmp[j] = makeIntVar("z_{" + i + "," + j + "}", 2, 3, Options.V_BOUND);
      this.addVariables(tmp);
      //  this.addConstraint(eq(plus(tmp[4],tmp[6]),28));
      this.addVariables(vs[i]);
    }

    FiniteAutomaton auto = new FiniteAutomaton();
    auto.fill(full, alpha);

    for (int i = 0; i < 8; i++) {
      Constraint mr = multiCostRegular(cvs[i], vs[i], auto, csts);

      this.addConstraint(mr);
    }

    int[] low = {3, 1, 4};
    int[] up = {3, 1, 4};

    IntegerVariable[][] trans = ArrayUtils.transpose(vs);
    for (int i = 0; i < 28; i++) {
      this.addConstraint(Options.C_GCC_BC, globalCardinality(trans[i], low, up, 0));
    }
  }