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(); }
public PropTaskIntervals( IntVar[] st, IntVar[] end, IntVar[] dur, int[][] dist, Constraint constraint, Solver solver) { super(ArrayUtils.append(st, end, dur), solver, constraint, PropagatorPriority.BINARY); starts = st; durations = dur; ends = end; this.n = st.length; this.dist = dist; }
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)); }
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); }
MemberC(Constraint cons0, Constraint... cons) { this(ArrayUtils.append(new Constraint[] {cons0}, cons)); }
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)); } }