Search in sources :

Example 1 with IntegerVariable

use of choco.kernel.model.variables.integer.IntegerVariable in project abstools by abstools.

the class ChocoSolver method getSolution.

public Map<String, Integer> getSolution() {
    if (!solved)
        solve();
    HashMap<String, Integer> result = new HashMap<>();
    Iterator<IntegerVariable> it = cpmodel.getIntVarIterator();
    while (it.hasNext()) {
        IntegerVariable var = it.next();
        result.put(var.getName(), solver.getVar(var).getVal());
    }
    return result;
}
Also used : IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) HashMap(java.util.HashMap)

Example 2 with IntegerVariable

use of choco.kernel.model.variables.integer.IntegerVariable in project abstools by abstools.

the class ChocoSolver method checkSolution.

public boolean checkSolution(Map<String, Integer> solution, Model model, CPModel m) {
    // Read the model
    CPSolver s = new CPSolver();
    s.read(m);
    if (absmodel.debug)
        absmodel.println("solution to check:\n" + solution);
    // HashMap<String,Integer> selection = new HashMap<String,Integer>();
    Iterator<IntegerVariable> it = m.getIntVarIterator();
    try {
        // aux variables
        int val;
        Set<String> newFeatures = new HashSet<>();
        Set<String> newParents = new HashSet<>();
        if (absmodel.debug)
            absmodel.println("Adding new values:");
        while (it.hasNext()) {
            // for all variables in the constraints
            // (model): round 1
            IntegerVariable var = it.next();
            // IF used variable is present in the solution, update it!
            if (solution.containsKey(var.getName())) {
                val = solution.get(var.getName());
                if (absmodel.debug)
                    absmodel.println("  " + var + " -> " + val);
                s.getVar(var).setVal(val);
                // Possible feature name -- include later the parents.
                if (val == 1)
                    newFeatures.add(var.getName());
            }
        }
        // constraints (model)
        for (Map.Entry<String, Integer> entry : solution.entrySet()) {
            if (entry.getValue() == 1)
                if (!entry.getKey().contains("."))
                    newFeatures.add(entry.getKey());
        }
        // collect parents of 'newFeatures'
        if (model != null)
            model.collectParents(newFeatures, newParents);
        // add newParents and default values to the solution
        it = m.getIntVarIterator();
        while (it.hasNext()) {
            // for all variables in the constraints
            // (model): round 2
            IntegerVariable var = it.next();
            // If it is a parent to include, set
            if (newParents.contains(var.getName())) {
                if (absmodel.debug)
                    absmodel.println("  " + var + " (parent) -> 1");
                s.getVar(var).setVal(1);
            } else // ELSE use default value
            if (!solution.containsKey(var.getName())) {
                // By default, the optional wrapper "$..." is ALWAYS true
                if (var.getName().startsWith("$")) {
                    if (absmodel.debug)
                        absmodel.println("  " + var + " (default) -> 1");
                    s.getVar(var).setVal(1);
                // By default, unrefered features & attributes are false
                } else if (defaultvals.containsKey(var.getName())) {
                    int defval = defaultvals.get(var.getName());
                    if (absmodel.debug)
                        absmodel.println("  " + var.getName() + " (default) -> " + defval);
                    s.getVar(var).setVal(defval);
                } else {
                    if (absmodel.debug)
                        absmodel.println("  " + var.getName() + " (default) -> 0");
                    s.getVar(var).setVal(0);
                }
            }
        }
    } catch (ContradictionException e1) {
        if (absmodel.debug)
            System.err.println("$$$ Contradiction found... $$$");
    }// Catch-all
     catch (Exception e1) {
        // Catch-all
        if (absmodel.debug) {
            System.err.println("$$$ Failed to check solution... $$$");
            e1.printStackTrace();
        }
    }
    return s.checkSolution();
}
Also used : CPSolver(choco.cp.solver.CPSolver) IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) ContradictionException(choco.kernel.solver.ContradictionException) HashMap(java.util.HashMap) Map(java.util.Map) Constraint(choco.kernel.model.constraints.Constraint) MetaConstraint(choco.kernel.model.constraints.MetaConstraint) ComponentConstraint(choco.kernel.model.constraints.ComponentConstraint) ContradictionException(choco.kernel.solver.ContradictionException) HashSet(java.util.HashSet)

Example 3 with IntegerVariable

use of choco.kernel.model.variables.integer.IntegerVariable in project abstools by abstools.

the class ChocoSolver method forceTrue.

/**
 * set a bool variable to true *
 */
public void forceTrue(String name) {
    IntegerVariable v = Choco.makeIntVar(name, 1, 1);
    vars.put(name, v);
    defaultvals.put(name, 1);
    cpmodel.addVariable(v);
}
Also used : IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable)

Example 4 with IntegerVariable

use of choco.kernel.model.variables.integer.IntegerVariable in project abstools by abstools.

the class ChocoSolver method addBoolVar.

public void addBoolVar(String name) {
    IntegerVariable v = Choco.makeBooleanVar(name);
    vars.put(name, v);
    defaultvals.put(name, 0);
    if (absmodel.debug)
        absmodel.println("  adding Bool var '" + name + "' (default -> False)");
// m.addVariable(v); // not needed - if variable is not constrained in
// any way, it should not be considered when solving.
}
Also used : IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable)

Example 5 with IntegerVariable

use of choco.kernel.model.variables.integer.IntegerVariable in project abstools by abstools.

the class ChocoSolver method checkSolutionWithErrors.

// public boolean checkSolution(Map<String,Integer> solution, Model model) {
// boolean res = true;
// for (Constraint c: constraints) {
// CPModel m = new CPModel();
// m.addConstraint(c);
// boolean csol = checkSolution(solution,model,m);
// res = res & csol;
// //if (debug)
// if (!csol) ast.println("Constraint failed: "+prettyConst(c));
// }
// 
// return res;
// for (Constraint c: constraints)
// m.addConstraint(c);
// return checkSolution(solution,model,m);
// }
public List<String> checkSolutionWithErrors(Map<String, Integer> solution, Model model) {
    List<String> res = new ArrayList<>();
    // check first for limits of variables
    for (IntegerVariable v : vars.values()) {
        CPModel m = new CPModel();
        m.addVariable(v);
        if (!checkSolution(solution, model, m))
            res.add(v.toString());
    }
    // now check all explicit constraints
    for (Constraint c : constraints) {
        CPModel m = new CPModel();
        m.addConstraint(c);
        if (!checkSolution(solution, model, m))
            res.add(prettyConst(c));
    }
    return res;
}
Also used : IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) Constraint(choco.kernel.model.constraints.Constraint) MetaConstraint(choco.kernel.model.constraints.MetaConstraint) ComponentConstraint(choco.kernel.model.constraints.ComponentConstraint) ArrayList(java.util.ArrayList) CPModel(choco.cp.model.CPModel)

Aggregations

IntegerVariable (choco.kernel.model.variables.integer.IntegerVariable)26 Constraint (choco.kernel.model.constraints.Constraint)8 CPModel (choco.cp.model.CPModel)7 CPSolver (choco.cp.solver.CPSolver)7 HashMap (java.util.HashMap)6 ComponentConstraint (choco.kernel.model.constraints.ComponentConstraint)4 MetaConstraint (choco.kernel.model.constraints.MetaConstraint)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 ContradictionException (choco.kernel.solver.ContradictionException)2 ArrayList (java.util.ArrayList)2