/** * Get the value of variable v in this solution * * @param v IntVar (or BoolVar) * @return the value of variable v in this solution, or null if the variable is not instantiated * in the solution */ public Integer getIntVal(IntVar v) { if (empty) { throw new UnsupportedOperationException("Empty solution. No solution found"); } if (intmap.containsKey(v.getId())) { return intmap.get(v.getId()); } else { if ((v.getTypeAndKind() & Variable.TYPE) == Variable.CSTE) { return v.getValue(); } else { return null; } } }
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(); }
/** * 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; } } } }
/** * 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."); } }