@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); }
@Test(groups = "1m") public void testSumvsIntLinCombTest() { Random rand = new Random(); for (int seed = 0; seed < 400; seed++) { rand.setSeed(seed); int n = 1 + rand.nextInt(6); int min = -10 + rand.nextInt(20); int max = min + rand.nextInt(20); int[][] domains = DomainBuilder.buildFullDomains(n, min, max, rand, 1.0, false); int[] coeffs = new int[n]; for (int i = 0; i < n; i++) { coeffs[i] = -25 + rand.nextInt(50); } int lb = -50 + rand.nextInt(100); int op = -1 + rand.nextInt(3); Solver sum = sum(domains, coeffs, lb, op); Solver intlincomb = intlincomb(domains, coeffs, lb, op); sum.findAllSolutions(); intlincomb.findAllSolutions(); Assert.assertEquals( sum.getMeasures().getSolutionCount(), intlincomb.getMeasures().getSolutionCount()); Assert.assertEquals( sum.getMeasures().getNodeCount(), intlincomb.getMeasures().getNodeCount()); LoggerFactory.getLogger("test") .info( "({}) {}ms vs {}ms", op, sum.getMeasures().getTimeCount(), intlincomb.getMeasures().getTimeCount()); } }
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; }
@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 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 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 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 testAdd() { Solver sol = new Solver(); EnumDelta d = new EnumDelta(sol.getEnvironment()); for (int i = 1; i < 40; i++) { d.add(i, Cause.Null); Assert.assertEquals(d.size(), i); } }
@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); }
@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(); }
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 public void testKnapsack() throws IOException, URISyntaxException { for (Path path : filesInDirectory("/kp")) { for (int k = 0; k < 3; k++) { Solver solver = SolverFactory.makeSolver(); Pair pair = parseKnapsack(Files.newBufferedReader(path), solver); IntVar[] objectives = pair.totalEnergies; long start = System.currentTimeMillis(); solver.plugMonitor( new IMonitorOpenNode() { @Override public void beforeOpenNode() { if (System.currentTimeMillis() - start > 30000) { solver.getSearchLoop().interrupt("Times up", true); } } @Override public void afterOpenNode() {} }); String out; if (k == 0) { out = "gia " + objectives.length + " " + pair.objects + " " + Moo.gia(solver, objectives); } else if (k == 1) { out = "classic " + objectives.length + " " + pair.objects + " " + Moo.classic(solver, objectives).size(); } else { out = "oia " + objectives.length + " " + pair.objects + " " + Moo.oia(solver, objectives).size() + " " + solver.getMeasures().getSolutionCount(); } boolean limit = solver.hasReachedLimit(); long time = System.currentTimeMillis() - start; System.out.println(out + " " + time + " " + limit); } } }
@Test(groups = "1m") public void test16to32() { int[] size = {32, 36, 40, 44, 48, 52, 56, 60, 64}; int[] sols = {1, 1, 1, 1, 1, 1, 1, 1, 1}; // int[] nodes = {633, 760, 2250, 6331, 19832, 19592, 60477, 139296, 180302}; for (int i = 0; i < size.length; i++) { Solver sol = modeler(size[i]); sol.findSolution(); Assert.assertEquals(sol.getMeasures().getSolutionCount(), sols[i]); // Assert.assertEquals(sol.getMeasures().getNodeCount(), nodes[i]); } }
@Test(groups = "1m") public void test4to14() { int[] size = {8, 12, 16, 20, 24, 28}; int[] sols = {1, 1, 7, 24, 296, 1443}; // int[] nodes = {3, 22, 189, 1739, 17889, 189944}; for (int i = 0; i < size.length; i++) { Solver sol = modeler(size[i]); sol.findAllSolutions(); Assert.assertEquals(sol.getMeasures().getSolutionCount(), sols[i]); // Assert.assertEquals(sol.getMeasures().getNodeCount(), nodes[i]); } }
@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 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)); }
/** * Set all variables to their respective value in the solution Throws an exception is this empties * a domain (i.e. this domain does not contain the solution value) * * <p>BEWARE: A restart might be required so that domains contain the solution values */ public void restore(Solver solver) throws ContradictionException { if (empty) { throw new UnsupportedOperationException("Empty solution. No solution found"); } Variable[] vars = solver.getVars(); for (int i = 0; i < vars.length; i++) { if ((vars[i].getTypeAndKind() & Variable.TYPE) != Variable.CSTE) { int kind = vars[i].getTypeAndKind() & Variable.KIND; switch (kind) { case Variable.INT: case Variable.BOOL: IntVar v = (IntVar) vars[i]; int value = intmap.get(v.getId()); if (value != NO_ENTRY) { v.instantiateTo(value, this); } // otherwise, this is not a decision variable break; case Variable.REAL: RealVar r = (RealVar) vars[i]; double[] bounds = realmap.get(r.getId()); if (bounds != null) { r.updateBounds(bounds[0], bounds[1], this); } // otherwise, this is not a decision variable break; case Variable.SET: SetVar s = (SetVar) vars[i]; int[] values = setmap.get(s.getId()); if (values != null) { s.instantiateTo(values, Cause.Null); } // otherwise, this is not a decision variable break; } } } }
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); }
public String toString(Solver solver) { Variable[] vars = solver.getVars(); StringBuilder st = new StringBuilder("Solution: "); for (int i = 0; i < vars.length; i++) { if ((vars[i].getTypeAndKind() & Variable.TYPE) != Variable.CSTE) { int kind = vars[i].getTypeAndKind() & Variable.KIND; switch (kind) { case Variable.INT: case Variable.BOOL: IntVar v = (IntVar) vars[i]; st.append(v.getName()).append("=").append(intmap.get(v.getId())).append(", "); break; case Variable.REAL: RealVar r = (RealVar) vars[i]; double[] bounds = realmap.get(r.getId()); st.append(r.getName()) .append("=[") .append(bounds[0]) .append(",") .append(bounds[1]) .append("], "); break; case Variable.SET: SetVar s = (SetVar) vars[i]; st.append(s.getName()) .append("=") .append(Arrays.toString(setmap.get(s.getId()))) .append(", "); break; } } } return st.toString(); }
@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); }
/** * Records the current solution of the solver clears all previous recordings * * @param solver a solver */ public void record(Solver solver) { if (empty) { Variable[] _dvars = solver.getStrategy().getVariables(); for (int i = 0; i < _dvars.length; i++) { dvars.add(_dvars[i].getId()); } empty = false; } boolean warn = false; intmap.clear(); realmap.clear(); setmap.clear(); Variable[] vars = solver.getVars(); for (int i = 0; i < vars.length; i++) { if ((vars[i].getTypeAndKind() & Variable.TYPE) != Variable.CSTE) { int kind = vars[i].getTypeAndKind() & Variable.KIND; if (!vars[i].isInstantiated()) { if (dvars.contains(vars[i].getId())) { throw new SolverException(vars[i] + " is not instantiated when recording a solution."); } else { warn = true; } } else { switch (kind) { case Variable.INT: case Variable.BOOL: IntVar v = (IntVar) vars[i]; intmap.put(v.getId(), v.getValue()); break; case Variable.REAL: RealVar r = (RealVar) vars[i]; realmap.put(r.getId(), new double[] {r.getLB(), r.getUB()}); break; case Variable.SET: SetVar s = (SetVar) vars[i]; setmap.put(s.getId(), s.getValues()); break; } } } } if (warn && solver.getSettings().warnUser()) { Chatterbox.err.printf( "Some non decision variables are not instantiated in the current solution."); } }
@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])); }
public ExplainingCut(Solver aSolver, int level, long seed) { super(aSolver); if (!(aSolver.getExplainer() instanceof LazyExplanationEngineFromRestart)) { aSolver.set(new LazyExplanationEngineFromRestart(aSolver)); } this.mExplanationEngine = aSolver.getExplainer(); this.level = level; this.random = new Random(seed); path = new ArrayList<>(16); related2cut = new BitSet(16); notFrozen = new BitSet(16); unrelated = new BitSet(16); refuted = new BitSet(16); // TEMPORARY DATA STRUCTURES tmpDeductions = new ArrayList<>(16); tmpValueDeductions = new HashSet<>(16); mSolver.getSearchLoop().plugSearchMonitor(this); }
/** * Default propagation test: When an opposite var is declared, the lower (resp. upper) bound * modification should be transposed in upper (resp. lower) bound event... */ @Test(groups = "1s") public void testUSum2() throws ContradictionException { Solver sum = sum( new int[][] {{-2, 7}, {-1, 6}, {2}, {-2, 5}, {-2, 4}, {-2, 6}}, new int[] {-7, 13, -3, -18, -24, 1}, 30, 0); PropagationEngineFactory.DEFAULT.make(sum); Variable[] vars = sum.getVars(); int offSet = 2; // ZERO and ONE constants ((IntVar) vars[offSet]).instantiateTo(-2, Cause.Null); ((IntVar) vars[1 + offSet]).instantiateTo(-1, Cause.Null); sum.propagate(); // sum.getSearchLoop().timeStamp++; ((IntVar) vars[2 + offSet]).removeValue(-2, Cause.Null); sum.propagate(); Assert.assertTrue(vars[2 + offSet].isInstantiated()); }
@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); }
@Override public boolean init() { boolean init = super.init(); ObjectiveManager<IntVar, Integer> om = mSolver.getObjectiveManager(); this.objectiveManager = om; if (objectiveManager.getPolicy() == ResolutionPolicy.SATISFACTION) { throw new UnsupportedOperationException("HBFS is not adapted to satisfaction problems."); } isMinimization = objectiveManager.getPolicy() == ResolutionPolicy.MINIMIZE; return init; }
@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); }