public ChocoSolver(SolvableModel model, int seed) { initModel(model); solver.addGoal( new DomOverWDegBranchingNew( solver, solver.getVar(model.getVariables()), new IncreasingDomain(), seed)); }
@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()); }
@Test public void testStrategy1() { loadQueenModel(); s = new CPSolver(); s.read(pe.model); s.attachGoal(new AssignVar(new DomOverWDeg(s), new MidVal())); checker(); }
@Test public void testStrategy2() { loadGolombRulerModel(); s = new CPSolver(); s.read(pe.model); s.attachGoal(new AssignVar(new MaxValueDomain(s), new DecreasingDomain())); checker(); }
/** * 此方法对若干条件判断表达式进行约束求解,在求解过程中将过滤掉无法处理的条件表达式 * * @param constraints * @return */ public boolean solve( ExpressionNode leftnNode, ExpressionNode rightNode, InfixExpression.Operator operator) { boolean solveable = true; CPSolver solver = new CPSolver(); solver.read(model); // solveable = solver.solve(); return solveable; }
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(); }
private void checker() { s.solveAll(); // checkSolution enbaled by assertion // s.solve(); // if(Boolean.TRUE.equals(s.isFeasible())){ // do{ // Assert.assertTrue(s.checkSolution()); // }while(s.nextSolution()); // } Assert.assertEquals("feasibility incoherence", pe.solver.isFeasible(), s.isFeasible()); Assert.assertEquals("nb sol incoherence", pe.solver.getNbSolutions(), s.getNbSolutions()); }
public boolean solve() { CPSolver solver = new CPSolver(); for (Iterator iterator = model.getConstraintIterator(); iterator.hasNext(); ) { Constraint constraint = (Constraint) iterator.next(); System.out.println(constraint); } System.out.println("****"); solver.read(model); boolean solverable = solver.solve(); return solverable; }
/* * One potential issue is determining the best way to build constraints. * Currently the model is reset after solving, and the solver * is reset right before solving. Is this the best way to do this? * We could alternatively pop constraints when backtracking, * though this would require some changes to how ProblemGeneral * is interfaced. * */ public Boolean solve() { solver.read(model); System.out.println("Model:" + model.constraintsToString()); solver.setTimeLimit(timeBound); Boolean solved = solver.solve(); boolean feasible = solver.isFeasible(); System.out.println("Solved: " + solved); System.out.println("Feasible: " + feasible); return solved; }
@Test public void test3LTSolve() { for (int seed = 0; seed < 10; seed++) { m = new CPModel(); s = new CPSolver(); int k = 2; IntegerVariable v0 = makeIntVar("v0", 0, 10); IntegerVariable v1 = makeIntVar("v1", 0, 10); m.addConstraint(distanceLT(v0, v1, k)); s.read(m); s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32)); s.setValIntSelector(new RandomIntValSelector(seed)); try { s.propagate(); } catch (ContradictionException e) { LOGGER.info(e.getMessage()); // To change body of catch statement use File | Settings | File // Templates. } s.solveAll(); int nbNode = s.getNodeCount(); LOGGER.info("solutions : " + s.getNbSolutions() + " nbNode : " + nbNode); assertEquals(s.getNbSolutions(), 31); assertEquals(nbNodeFromRegulatModel(seed), nbNode); } }
@Test public void test3BoundsSolve() { for (int seed = 0; seed < 10; seed++) { m = new CPModel(); s = new CPSolver(); int k = 9, k1 = 7, k2 = 6; IntegerVariable v0 = makeIntVar("v0", 0, 10); IntegerVariable v1 = makeIntVar("v1", 0, 10); IntegerVariable v2 = makeIntVar("v2", 0, 10); IntegerVariable v3 = makeIntVar("v3", 0, 10); m.addVariables(Options.V_BOUND, v0, v1, v2, v3); m.addConstraint(distanceEQ(v0, v1, k)); m.addConstraint(distanceEQ(v1, v2, k1)); m.addConstraint(distanceEQ(v2, v3, k2)); s.read(m); s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32)); s.setValIntSelector(new RandomIntValSelector(seed)); try { s.propagate(); } catch (ContradictionException e) { LOGGER.info(e.getMessage()); // To change body of catch statement use File | Settings | File // Templates. } s.solveAll(); int nbNode = s.getNodeCount(); LOGGER.info("solutions : " + s.getNbSolutions() + " nbNode : " + nbNode); assertEquals(s.getNbSolutions(), 4); } }
@Test public void test1NeqSolve() { for (int seed = 0; seed < 10; seed++) { m = new CPModel(); s = new CPSolver(); int k = 9, k1 = 7, k2 = 6; IntegerVariable v0 = makeIntVar("v0", 0, 10); IntegerVariable v1 = makeIntVar("v1", 0, 10); IntegerVariable v2 = makeIntVar("v2", 0, 10); IntegerVariable v3 = makeIntVar("v3", 0, 10); m.addConstraint(distanceNEQ(v0, v1, k)); m.addConstraint(distanceNEQ(v1, v2, k1)); m.addConstraint(distanceNEQ(v2, v3, k2)); s.read(m); s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32)); s.setValIntSelector(new RandomIntValSelector(seed)); try { s.propagate(); } catch (ContradictionException e) { LOGGER.info(e.getMessage()); } s.solveAll(); int nbNode = s.getNodeCount(); LOGGER.info("solutions : " + s.getNbSolutions() + " nbNode : " + nbNode); assertEquals(s.getNbSolutions(), 12147); } }
@Test public void test2() { CPSolver s = new CPSolver(); RealVar alpha = s.createRealVal("alpha", -5.5 * Math.PI, -1.5 * Math.PI); RealExp exp = new RealCos(s, alpha); s.post(s.makeEquation(exp, s.cst(1.0))); boolean first = false; s.setFirstSolution(first); s.generateSearchStrategy(); s.addGoal(new AssignInterval(new CyclicRealVarSelector(s), new RealIncreasingDomain())); s.launch(); assertTrue(s.getNbSolutions() >= 2); }
@Override protected Boolean solve(Instance instance) { CPModel chocoModel = new CPModel(); Map<Variable, IntegerVariable> variableMap = new HashMap<Variable, IntegerVariable>(); try { new ChocoTranslator(chocoModel, variableMap).translate(instance.getExpression()); CPSolver chocoSolver = new CPSolver(); chocoSolver.read(chocoModel); return chocoSolver.solve(); } catch (TranslatorUnsupportedOperation x) { log.log(Level.WARNING, x.getMessage(), x); } catch (VisitorException x) { log.log(Level.SEVERE, "encountered an exception -- this should not be happening!", x); } return null; }
public int getIntValue(Object dpVar) { try { return solver.getVar((IntegerVariable) dpVar).getVal(); } catch (Throwable t) { return ((IntegerVariable) dpVar).getLowB(); } }
@Test public void testStrategyQ() { loadQueenModel(); for (int i = 0; i < 100; i++) { LOGGER.info("seed:" + i); random = new Random(i); s = new CPSolver(); s.read(pe.model); IntBranchingFactory bf = new IntBranchingFactory(); IntDomainVar[] vars = s.getIntDecisionVars(); s.attachGoal(bf.make(random, s, vars)); checker(); } }
public ChocoSolver( SolvableModel model, int seed, SharedHeuristicWeights sharedVariablesWeight, SharedHeuristicWeights sharedConstraintsWeight) { initModel(model); solver.addGoal( new DomOverWDegBranchingSharedWeights( solver, solver.getVar(model.getVariables()), new IncreasingDomain(), seed, sharedVariablesWeight, sharedConstraintsWeight)); }
@Test public void testDXYZProp1() { IntegerVariable v0 = makeIntVar("v0", 1, 4); IntegerVariable v1 = makeIntVar("v1", 5, 7); IntegerVariable v2 = makeIntVar("v2", -100, 100); m.addVariables(Options.V_BOUND, v0, v1, v2); m.addConstraint(distanceEQ(v0, v1, v2, 0)); s.read(m); try { s.propagate(); } catch (ContradictionException e) { assertTrue(false); } assertEquals(1, s.getVar(v2).getInf()); assertEquals(6, s.getVar(v2).getSup()); }
@Test public void testDXYZProp4() { IntegerVariable v0 = makeIntVar("v0", -1, 5); IntegerVariable v1 = makeIntVar("v1", -5, 6); IntegerVariable v2 = makeIntVar("v2", -2, 2); m.addConstraint(distanceEQ(v0, v1, v2, 0)); s.read(m); try { s.propagate(); } catch (ContradictionException e) { assertTrue(false); } assertEquals(-3, s.getVar(v1).getInf()); assertEquals(0, s.getVar(v2).getInf()); }
private void initModel(SolvableModel model) { solver = new CPSolver(); this.model = model; solver.read(model.getModel()); setVerbosity(Verbosity.DEFAULT); }
@Test public void testDXYZSolve1() { for (int seed = 0; seed < 10; seed++) { m = new CPModel(); s = new CPSolver(); IntegerVariable v0 = makeIntVar("v0", 0, 5); IntegerVariable v1 = makeIntVar("v1", 0, 5); IntegerVariable v2 = makeIntVar("v2", 0, 5); m.addConstraint(distanceEQ(v0, v1, v2, 0)); s.read(m); s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32)); s.setValIntSelector(new RandomIntValSelector(seed)); s.solveAll(); assertEquals(36, s.getNbSolutions()); } }
@Test public void testDXYZProp2bis() { IntegerVariable v0 = makeIntVar("v0", 1, 5); IntegerVariable v1 = makeIntVar("v1", 5, 10); IntegerVariable v2 = makeIntVar("v2", 1, 2); m.addVariables(Options.V_BOUND, v0, v1, v2); m.addConstraint(distanceEQ(v0, v1, v2, -1)); s.read(m); try { s.propagate(); } catch (ContradictionException e) { assertTrue(false); } assertEquals(4, s.getVar(v0).getInf()); assertEquals(6, s.getVar(v1).getSup()); s.solve(); LOGGER.info("" + s.pretty()); }
@Test public void testDXYZProp5() { IntegerVariable v0 = makeIntVar("v0", -1, 1); IntegerVariable v1 = makeIntVar("v1", -5, 6); IntegerVariable v2 = makeIntVar("v2", 3, 10); m.addConstraint(distanceEQ(v0, v1, v2, 0)); s.read(m); try { s.propagate(); } catch (ContradictionException e) { assertTrue(false); } LOGGER.info("" + v1.pretty()); assertTrue(!s.getVar(v1).canBeInstantiatedTo(0)); assertTrue(!s.getVar(v1).canBeInstantiatedTo(1)); assertTrue(!s.getVar(v1).canBeInstantiatedTo(-1)); assertEquals(7, s.getVar(v2).getSup()); }
public void setHeuristic(ChocoHeuristic heuristic) { switch (heuristic) { case MIN_DOMAIN: { solver.setVarIntSelector(new MinDomain(solver, solver.getVar(model.getVariables()))); break; } case DOM_OVER_WDEG: { solver.addGoal( new DomOverWDegBranchingNew( solver, solver.getVar(model.getVariables()), new IncreasingDomain(), 1)); break; } case DEFAULT: { // default heuristic break; } } }
private int nbNodeFromRegulatModel(int seed) { m = new CPModel(); s = new CPSolver(); int k = 2; IntegerVariable v0 = makeIntVar("v0", 0, 10); IntegerVariable v1 = makeIntVar("v1", 0, 10); List<int[]> ltuple = new LinkedList<int[]>(); for (int i = 0; i <= 10; i++) { for (int j = 0; j <= 10; j++) { if (Math.abs(i - j) < k) ltuple.add(new int[] {i, j}); } } m.addConstraint(regular(new IntegerVariable[] {v0, v1}, ltuple)); s.read(m); s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32)); s.setValIntSelector(new RandomIntValSelector(seed)); try { s.propagate(); } catch (ContradictionException e) { LOGGER.info(e.getMessage()); // To change body of catch statement use File | Settings | File // Templates. } s.solveAll(); // LOGGER.info("solutions regular : " + s.getNbSolutions()); return s.getNodeCount(); }
@Test public void test1bis() { CPModel m = new CPModel(); RealVariable alpha = makeRealVar("alpha", -Math.PI, Math.PI); Constraint exp = eq(cos(alpha), sin(alpha)); m.addConstraint(exp); CPSolver s = new CPSolver(); s.read(m); LOGGER.info("eq = " + s.getCstr(exp).pretty()); boolean first = false; s.setFirstSolution(first); s.generateSearchStrategy(); s.addGoal(new AssignInterval(new CyclicRealVarSelector(s), new RealIncreasingDomain())); s.launch(); assertTrue(s.getNbSolutions() >= 2); assertTrue( Math.abs(Math.cos(s.getVar(alpha).getInf()) - Math.sin(s.getVar(alpha).getInf())) < 1e-8); }
public final void setFlags(final BitMask flags) { final SConstraint cstr = solver.getCstr(this.rsc); BitMask dest = null; if (cstr instanceof AbstractResourceSConstraint) { dest = ((AbstractResourceSConstraint) cstr).getFlags(); } else if (cstr instanceof MetaSConstraint) { dest = ((AbstractResourceSConstraint) ((MetaSConstraint) cstr).getSubConstraints(0)).getFlags(); } if (dest != null) { dest.clear(); dest.set(flags); } else ChocoLogging.getTestLogger().info("can not apply resource filtering rules"); }
@Test public void test2bis() { CPModel m = new CPModel(); RealVariable alpha = makeRealVar("alpha", -5.5 * Math.PI, -1.5 * Math.PI); m.addVariable(alpha); m.addConstraint(eq(cos(alpha), 1)); CPSolver s = new CPSolver(); s.read(m); boolean first = false; s.setFirstSolution(first); s.generateSearchStrategy(); s.addGoal(new AssignInterval(new CyclicRealVarSelector(s), new RealIncreasingDomain())); s.launch(); assertTrue(s.getNbSolutions() >= 2); }
@Test public void testDXYZSolve4() { for (int seed = 0; seed < 10; seed++) { m = new CPModel(); s = new CPSolver(); IntegerVariable v0 = makeIntVar("v0", 3, 6); IntegerVariable v1 = makeIntVar("v1", -3, 4); IntegerVariable v2 = makeIntVar("v2", 0, 5); IntegerVariable v3 = makeIntVar("v3", 2, 5); m.addConstraint(distanceGT(v0, v1, v2, 0)); m.addConstraint(distanceGT(v0, v2, v3, 0)); s.read(m); s.setVarIntSelector(new RandomIntVarSelector(s, seed + 32)); s.setValIntSelector(new RandomIntValSelector(seed)); s.solveAll(); LOGGER.info("nbsol " + s.getNbSolutions()); assertEquals(getNbSolByDecomp(2), s.getNbSolutions()); } }
@Override public Solver buildSolver() { CPSolver solver = new CPSolver(this.defaultConf); BasicSettings.updateTimeLimit(solver.getConfiguration(), -getPreProcTime()); return solver; }