@Override public boolean inject(ReconfigurationProblem rp) { Solver solver = rp.getSolver(); Collection<Node> nodes = constraint.getInvolvedNodes(); BoolVar[] spareNode = new BoolVar[nodes.size()]; int i = 0; for (Node node : constraint.getInvolvedNodes()) { BoolVar state = rp.getNodeAction(node).getState(); IntVar NbVms = rp.getNbRunningVMs()[rp.getNode(node)]; spareNode[i] = VF.bool(StringUtils.randomName(), solver); // if the server is off (state = false), then it is not free solver.post(new FastImpliesEq(VF.not(state), spareNode[i], 0)); // if the server hosts VMs, then it is not free solver.post(new FastImpliesEq(ICF.arithm(NbVms, "!=", 0).reif(), spareNode[i], 0)); i++; } IntVar spareNodes = VF.bounded("freeNumber", 0, nodes.size(), solver); solver.post(ICF.sum(spareNode, spareNodes)); solver.post(ICF.arithm(spareNodes, ">=", constraint.getMinSpareNodes())); return true; }
@Test(groups = "1s") public void testJL() { Solver solver = new Solver(); final SetVar s0 = VF.set("s0", 0, 1, solver); final BoolVar b0 = VF.bool("b0", solver); final BoolVar b1 = VF.bool("b1", solver); final IntVar i0 = VF.bool("i0", solver); solver.set(ISF.lexico_LB(i0)); solver.post(SCF.bool_channel(new BoolVar[] {b0, b1}, s0, 0)); solver.post(SCF.cardinality(s0, VF.fixed(0, solver))); solver.findSolution(); solver.getSearchLoop().reset(); solver.findSolution(); }
protected Solver intlincomb(int[][] domains, int[] coeffs, int b, int op) { Solver solver = new Solver(); IntVar[] bins = new IntVar[domains.length]; for (int i = 0; i < domains.length; i++) { bins[i] = VariableFactory.bounded( "v_" + i, domains[i][0], domains[i][domains[i].length - 1], solver); } String opname = "="; if (op != 0) { if (op > 0) { opname = ">="; } else { opname = "<="; } } IntVar sum = VariableFactory.bounded("scal", -99999999, 99999999, solver); Constraint[] cstrs = new Constraint[] { IntConstraintFactory.scalar(bins, coeffs, sum), IntConstraintFactory.arithm(sum, opname, b) }; solver.post(cstrs); solver.set(IntStrategyFactory.lexico_LB(bins)); return solver; }
@Test(groups = "1s") public void testSimpleAuto() { Solver solver = new Solver(); int n = 10; IntVar[] vars = new IntVar[n]; for (int i = 0; i < n; i++) { vars[i] = VariableFactory.enumerated("x_" + i, 0, 2, solver); } FiniteAutomaton auto = new FiniteAutomaton(); int start = auto.addState(); int end = auto.addState(); auto.setInitialState(start); auto.setFinal(start); auto.setFinal(end); auto.addTransition(start, start, 0, 1); auto.addTransition(start, end, 2); auto.addTransition(end, start, 2); auto.addTransition(end, start, 0, 1); solver.post(IntConstraintFactory.regular(vars, auto)); solver.set(IntStrategyFactory.lexico_LB(vars)); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 59049); }
private static Pair parseKnapsack(BufferedReader in, Solver solver) throws IOException { int objectives = Integer.parseUnsignedInt(in.readLine()); int objects = Integer.parseUnsignedInt(in.readLine()); int capacity = Integer.parseUnsignedInt(in.readLine()); int[][] energies = parse2dArray(in); if (energies.length != objectives) { throw new IOException(); } for (int[] energy : energies) { if (energy.length != objects) { throw new IOException(); } } int[] weights = parse1dArray(in); if (weights.length != objects) { throw new IOException(); } IntVar[] occurences = VF.enumeratedArray("occurences", objects, 0, 50, solver); IntVar totalWeight = VF.enumerated("totalWeight", 0, capacity, solver); IntVar[] totalEnergies = new IntVar[energies.length]; for (int i = 0; i < energies.length; i++) { totalEnergies[i] = VF.enumerated("totalEnergy[" + i + "]", 0, sum(energies[i]), solver); solver.post(ICF.knapsack(occurences, totalWeight, totalEnergies[i], weights, energies[i])); } return new Pair(totalEnergies, objects); }
@Test(groups = "1s") public void testregExp4() { Solver solver = new Solver(); IntVar[] CS = VF.enumeratedArray("CS", 2, 0, 3, solver); solver.post(ICF.regular(CS, new FiniteAutomaton(".*", 0, 3))); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 16); }
@Test(groups = "1s") public void testIss237_1() { Solver solver = new Solver(); BoolVar[] bs = VF.boolArray("b", 3, solver); solver.post(ICF.scalar(bs, new int[] {1, 2, 3}, "=", VF.fixed(2, solver))); Chatterbox.showSolutions(solver); solver.findAllSolutions(); }
@Test(groups = "1s") public void test3() { Solver s = new Solver(); IntVar three = VF.fixed(3, s); IntVar two = VF.fixed(2, s); s.post(ICF.arithm(three, "=", two, "+", 1)); Assert.assertTrue(s.findSolution()); Assert.assertEquals(ESat.TRUE, s.isSatisfied()); }
@Test(groups = "1s") public void test1() { Solver s = new Solver(); IntVar two1 = VF.fixed(2, s); IntVar two2 = VF.fixed(2, s); s.post(ICF.arithm(two1, "=", two2)); Assert.assertTrue(s.findSolution()); Assert.assertEquals(ESat.TRUE, s.isSatisfied()); }
@Test(groups = "1s") public void testregExp8() { Solver solver = new Solver(); IntVar[] CS = VF.enumeratedArray("CS", 3, new int[] {43, 59, 117}, solver); solver.post(ICF.regular(CS, new FiniteAutomaton("<43><59><117>"))); solver.set(ISF.lexico_LB(CS)); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 1); }
@Test(groups = "1s") public void test1() { Solver solver = new Solver("t1"); IntVar x = VariableFactory.bounded("X", 1, 3, solver); IntVar y = VariableFactory.bounded("Y", 1, 3, solver); solver.post(IntConstraintFactory.arithm(x, ">=", y)); solver.post(IntConstraintFactory.arithm(x, "<=", 2)); solver.findSolution(); }
@Test(groups = "1s") public void testregExp6() { Solver solver = new Solver(); IntVar[] CS = VF.enumeratedArray("CS", 4, 0, 3, solver); solver.post(ICF.regular(CS, new FiniteAutomaton("0{2,3}1*"))); Chatterbox.showSolutions(solver); Chatterbox.showDecisions(solver); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 2); }
public void solveProblem() { Solver solver = new Solver(); // Decision selection BoolVar Root = (BoolVar) VariableFactory.fixed(1, solver); BoolVar A = (BoolVar) VariableFactory.fixed(1, solver); BoolVar B = (BoolVar) VariableFactory.fixed(1, solver); BoolVar C = (BoolVar) VariableFactory.fixed(1, solver); BoolVar D = VariableFactory.bool("D", solver); BoolVar E = VariableFactory.bool("E", solver); BoolVar A1 = VariableFactory.bool("A1", solver); BoolVar A2 = VariableFactory.bool("A2", solver); BoolVar C1 = VariableFactory.bool("C1", solver); BoolVar C2 = VariableFactory.bool("C2", solver); BoolVar C3 = VariableFactory.bool("C3", solver); BoolVar E1 = VariableFactory.bool("E1", solver); BoolVar E2 = VariableFactory.bool("E2", solver); BoolVar e1 = VariableFactory.bool("e1", solver); BoolVar e2 = VariableFactory.bool("e2", solver); BoolVar e3 = VariableFactory.bool("e3", solver); // Decision cost IntVar A1Cost = VariableFactory.bounded("A1Cost", 0, 500, solver); IntVar A2Cost = VariableFactory.bounded("A2Cost", 0, 200, solver); IntVar C1Cost = VariableFactory.bounded("C1Cost", 0, 600, solver); IntVar C2Cost = VariableFactory.bounded("C2Cost", 0, 400, solver); IntVar C3Cost = VariableFactory.bounded("C3Cost", 0, 300, solver); IntVar E1Cost = VariableFactory.bounded("E1Cost", 0, 450, solver); IntVar e1Cost = VariableFactory.bounded("e1Cost", 0, 350, solver); IntVar e2Cost = VariableFactory.bounded("e2Cost", 0, 350, solver); IntVar e3Cost = VariableFactory.bounded("e3Cost", 0, 500, solver); IntVar[] Vars = new IntVar[9]; Vars[0] = A1Cost; Vars[1] = A2Cost; Vars[2] = C1Cost; Vars[3] = C2Cost; Vars[4] = C3Cost; Vars[5] = E1Cost; Vars[6] = e1Cost; Vars[7] = e2Cost; Vars[8] = e3Cost; int maxValue = 3650; IntVar TotalCost = VariableFactory.bounded("TotalCost", 0, maxValue, solver); solver.post(IntConstraintFactory.sum(Vars, TotalCost)); Chatterbox.showSolutions(solver); solver.findOptimalSolution(ResolutionPolicy.MAXIMIZE, TotalCost); Chatterbox.printStatistics(solver); }
@Test(groups = "1s") public void testJL2() { for (int k = 0; k < 50; k++) { Solver s = new Solver(); final IntVar i = VF.enumerated("i", -2, 2, s); final IntVar j = VF.enumerated("j", -2, 2, s); // Chatterbox.showDecisions(s); // Chatterbox.showSolutions(s); s.set(ISF.random_value(new IntVar[] {i, j})); s.post(new Constraint("Constraint", new PropTestDM1(i, j), new PropTestDM2(i, j))); s.findAllSolutions(); } }
@Test(groups = "1s") public void test2() { Solver solver = new Solver(); IntVar[] VARS = VF.enumeratedArray("X", 2, 0, 2, solver); Constraint CSTR = ICF.arithm(VARS[0], "+", VARS[1], "=", 2); solver.post(CSTR, CSTR); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 3); solver.getSearchLoop().reset(); solver.unpost(CSTR); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 3); }
@Test(groups = "1s") public void testEq() throws ContradictionException { Solver solver = new Solver(); IntVar x = VariableFactory.enumerated("X", 1, 6, solver); IntVar y = VariableFactory.enumerated("Y", 1, 6, solver); solver.post(IntConstraintFactory.arithm(x, "=", y)); solver.propagate(); x.removeValue(4, Cause.Null); solver.propagate(); Assert.assertFalse(y.contains(4)); }
@Test(groups = "10s") public void compareVersionSpeedNew() { int n = 14; FiniteAutomaton auto = new FiniteAutomaton("(0|1|2)*(0|1)(0|1)(0|1)(0|1|2)*"); Solver solver = new Solver(); IntVar[] vars = new IntVar[n]; for (int i = 0; i < n; i++) { vars[i] = VariableFactory.enumerated("x_" + i, 0, 2, solver); } solver.post(IntConstraintFactory.regular(vars, auto)); solver.set(IntStrategyFactory.lexico_LB(vars)); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 4371696); }
@Test(groups = "1s") public void compareVersionSpeedNew2() { int n = 5; FiniteAutomaton auto = new FiniteAutomaton("(0|<10>|<20>)*(0|<10>)"); Solver solver = new Solver(); IntVar[] vars = new IntVar[n]; for (int i = 0; i < n; i++) { vars[i] = VariableFactory.enumerated("x_" + i, new int[] {0, 10, 20}, solver); } solver.post(IntConstraintFactory.regular(vars, auto)); solver.set(IntStrategyFactory.lexico_LB(vars)); Chatterbox.showSolutions(solver); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 162); }
@Test(groups = "1s", expectedExceptions = SolverException.class) public void testNeg() { Solver solver = new Solver(); IntVar[] CS = VF.enumeratedArray("CS", 4, -10, 10, solver); solver.post(ICF.regular(CS, new FiniteAutomaton("<-9>1*"))); Chatterbox.showSolutions(solver); solver.findAllSolutions(); final List<Solution> solutions = solver.getSolutionRecorder().getSolutions(); System.out.println(solutions); Assert.assertEquals(1, solutions.size()); Assert.assertEquals(-9, (int) solutions.get(0).getIntVal(CS[0])); Assert.assertEquals(1, (int) solutions.get(0).getIntVal(CS[1])); Assert.assertEquals(1, (int) solutions.get(0).getIntVal(CS[2])); Assert.assertEquals(-5, (int) solutions.get(0).getIntVal(CS[3])); }
@Test(groups = "1s") public void testregExp7() { Solver solver = new Solver(); IntVar[] CS = VF.enumeratedArray("CS", 10, 0, 2, solver); solver.post(ICF.regular(CS, new FiniteAutomaton("0*(1{2,4}0{0,2}0)*0*"))); Chatterbox.showSolutions( solver, () -> { for (int i = 0; i < 10; i++) { System.out.printf("%d", CS[i].getValue()); } // System.out.printf("\n"); return ""; }); solver.set(ISF.lexico_LB(CS)); // Chatterbox.showDecisions(solver); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 84); }
@Test(groups = "1s") public void ccostregular2() { Solver solver = new Solver(); int n = 12; IntVar[] vars = new IntVar[n]; for (int i = 0; i < n; i++) { vars[i] = VariableFactory.enumerated("x_" + i, 0, 2, solver); } // different rules are formulated as patterns that must NOT be matched by x List<String> forbiddenRegExps = new ArrayList<>(); // do not end with '00' if start with '11' forbiddenRegExps.add("11(0|1|2)*00"); // at most three consecutive 0 forbiddenRegExps.add("(0|1|2)*0000(0|1|2)*"); // no pattern '112' at position 5 forbiddenRegExps.add("(0|1|2){4}112(0|1|2)*"); // pattern '12' after a 0 or a sequence of 0 forbiddenRegExps.add("(0|1|2)*02(0|1|2)*"); forbiddenRegExps.add("(0|1|2)*01(0|1)(0|1|2)*"); // at most three 2 on consecutive even positions forbiddenRegExps.add("(0|1|2)((0|1|2)(0|1|2))*2(0|1|2)2(0|1|2)2(0|1|2)*"); // a unique automaton is built as the complement language composed of all the forbidden patterns FiniteAutomaton auto = new FiniteAutomaton(); for (String reg : forbiddenRegExps) { FiniteAutomaton a = new FiniteAutomaton(reg); auto = auto.union(a); auto.minimize(); } auto = auto.complement(); auto.minimize(); Assert.assertEquals(auto.getNbStates(), 54); solver.post(IntConstraintFactory.regular(vars, auto)); solver.set(IntStrategyFactory.lexico_LB(vars)); solver.findAllSolutions(); Assert.assertEquals(solver.getMeasures().getSolutionCount(), 25980); }
public static void testOp(int n, int min, int max, int cMax, int seed, Operator operator) { Random random = new Random(seed); Solver s = new Solver(); IntVar[] vars = new IntVar[n]; int[] coeffs = new int[n]; for (int i = 0; i < vars.length; i++) { vars[i] = VariableFactory.enumerated("v_" + i, min, max, s); coeffs[i] = random.nextInt(cMax); } int constant = -random.nextInt(cMax); IntVar sum = VariableFactory.bounded("scal", -99999999, 99999999, s); Constraint[] cstrs = new Constraint[] { IntConstraintFactory.scalar(vars, coeffs, sum), IntConstraintFactory.arithm(sum, operatorToString(operator), constant) }; s.post(cstrs); s.set(IntStrategyFactory.lexico_LB(vars)); s.findAllSolutions(); }
@Override public boolean inject(ReconfigurationProblem rp) { Collection<Node> nodes = constraint.getInvolvedNodes(); Solver solver = rp.getSolver(); PowerView pvServers = (PowerView) rp.getSourceModel().getView(F4GConfigurationAdapter.VIEW_POWER_IDLES); PowerView pvVMs = (PowerView) rp.getSourceModel().getView(F4GConfigurationAdapter.VIEW_POWER_PER_VM); for (Node node : nodes) { IntVar powerIdle = VariableFactory.fixed(pvServers.getPower(node), solver); IntVar powerperVM = VariableFactory.fixed(pvVMs.getPower(node), solver); IntVar powerVMS = VariableFactory.bounded("powerVMS", 0, Integer.MAX_VALUE / 100, solver); IntConstraintFactory.times(rp.getNbRunningVMs()[rp.getNode(node)], powerperVM, powerVMS); solver.post( IntConstraintFactory.arithm( powerVMS, "+", powerIdle, "<=", constraint.getMaxServerPower())); } return true; }
public void max(Solver solver, IntVar x, IntVar y, IntVar z) { solver.post(IntConstraintFactory.maximum(z, x, y)); }
public static IntVar minimiserTemps(IntVar[][][] X, Probleme aResoudre, Solver solver) { IntVar OBJ = VF.enumerated("OBJ", 0, aResoudre.getHFermeture() - aResoudre.getHOuverture(), solver); // On calcule le Min et Max pour chaque patient IntVar[] MAX_Patient = VariableFactory.enumeratedArray( "MAX_Patient", aResoudre.getnPatients(), aResoudre.getHOuverture(), aResoudre.getHFermeture(), solver); IntVar[] MIN_Patient = VariableFactory.enumeratedArray( "MIN_Patient", aResoudre.getnPatients(), aResoudre.getHOuverture(), aResoudre.getHFermeture(), solver); for (int i = 0; i < aResoudre.getnPatients(); i++) { // On calcule le Min et Max pour chaque groupe de soins du patient i IntVar[] MAX_PatientI_Groupe = VariableFactory.enumeratedArray( "MAX_PatientI_Groupe", aResoudre.getnPatients(), aResoudre.getHOuverture(), aResoudre.getHFermeture(), solver); IntVar[] MIN_PatientI_Groupe = VariableFactory.enumeratedArray( "MIN_PatientI_Groupe", aResoudre.getnPatients(), aResoudre.getHOuverture(), aResoudre.getHFermeture(), solver); for (int j = 0; j < aResoudre.getnG_i()[i]; j++) { solver.post(ICF.maximum(MAX_PatientI_Groupe[j], X[i][j])); solver.post(ICF.minimum(MIN_PatientI_Groupe[j], X[i][j])); } solver.post(ICF.maximum(MAX_Patient[i], MAX_PatientI_Groupe)); solver.post(ICF.minimum(MIN_Patient[i], MIN_PatientI_Groupe)); } // TODO : vérifier que c'est juste --> possible source de problème for (int i = 0; i < MIN_Patient.length; i++) { MIN_Patient[i] = VariableFactory.minus(MIN_Patient[i]); } // On calcule la somme des max et des min IntVar SUM[] = VF.enumeratedArray( "SUM", 2, aResoudre.getHOuverture() * aResoudre.getnPatients(), aResoudre.getHFermeture() * aResoudre.getnPatients(), solver); solver.post(ICF.sum(MAX_Patient, SUM[0])); solver.post(ICF.sum(MIN_Patient, SUM[1])); solver.post(ICF.sum(SUM, OBJ)); return null; }