Search in sources :

Example 1 with CPSolver

use of choco.cp.solver.CPSolver 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 2 with CPSolver

use of choco.cp.solver.CPSolver in project Malai by arnobl.

the class ChocoWrapper method initialization.

/*
	 * Initialize the warpper.
	 */
public static void initialization() {
    m = new CPModel();
    s = new CPSolver();
    stack = new Stack<Constraint>();
    varMap = new HashMap<String, IntegerVariable>();
    solved = false;
}
Also used : CPSolver(choco.cp.solver.CPSolver) IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) Constraint(choco.kernel.model.constraints.Constraint) CPModel(choco.cp.model.CPModel)

Example 3 with CPSolver

use of choco.cp.solver.CPSolver in project abstools by abstools.

the class ChocoSolverExperiment method main.

@SuppressWarnings("unused")
public static void main(final String[] args) throws Exception {
    CPModel m = new CPModel();
    boolean verbose = false;
    if (!verbose)
        ChocoLogging.setVerbosity(Verbosity.OFF);
    // m.addVariable(i1);
    IntegerVariable i1 = Choco.makeIntVar("i1", -10, 100);
    // m.addVariable(i2);
    IntegerVariable i2 = Choco.makeIntVar("i2", -10, 100);
    // m.addVariable(i3);
    IntegerVariable i3 = Choco.makeIntVar("i3", -10, 100);
    // m.addVariable(i4);
    IntegerVariable i4 = Choco.makeIntVar("i4", -10, 100);
    // m.addVariable(b1);
    IntegerVariable b1 = Choco.makeBooleanVar("b1");
    // m.addVariable(b2);
    IntegerVariable b2 = Choco.makeBooleanVar("b2");
    // m.addVariable(b3);
    IntegerVariable b3 = Choco.makeBooleanVar("b3");
    // m.addVariable(b4);
    IntegerVariable b4 = Choco.makeBooleanVar("b4");
    // m.addVariable(b5);
    IntegerVariable b5 = Choco.makeBooleanVar("b5");
    m.addConstraint(// Choco.and( Choco.lt(i1, i2) , Choco.lt(i2, 7) )
    Choco.or(Choco.eq(i1, -3), Choco.eq(i1, 5)));
    // Build the solver
    CPSolver s = new CPSolver();
    if (verbose)
        System.out.println("####" + s.getConfiguration().stringPropertyNames());
    // print the problem
    if (verbose)
        System.out.println(m.pretty());
    // Read the model
    s.read(m);
    // Solve the model
    // s.solve();
    // s.setObjective(s.getVar(i1))
    // s.solve(); //
    Boolean solved = s.maximize(s.getVar(i1), true);
    if (solved) {
        System.out.println("i1: " + s.getVar(i1).getVal());
    // System.out.println("i2: "+s.getVar(i2).getVal());
    } else {
        System.out.println("no sol...");
    }
    try {
    // s.getVar(b1).setVal(1);
    // //      s.getVar(b2).setVal(0);
    // //      s.getVar(b3).setVal(1);
    // System.out.println("$$$ check sol: "+s.checkSolution()+" $$$");
    // System.out.println("$$$ b1: "+s.getVar(b1).isInstantiated()+" $$$");
    // System.out.println("$$$ b2: "+s.getVar(b2).isInstantiated()+" $$$");
    } catch (Exception e1) {
        // Catch-all
        System.err.println("$$$ Failed to check solution... $$$");
    // e1.printStackTrace();
    }
    if (verbose)
        System.out.println(s.pretty());
}
Also used : CPSolver(choco.cp.solver.CPSolver) IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) CPModel(choco.cp.model.CPModel)

Example 4 with CPSolver

use of choco.cp.solver.CPSolver in project abstools by abstools.

the class ChocoSolverExperiment method othermain.

public static void othermain(final String[] args) throws Exception {
    // Constant declaration
    // Order of the magic square
    int n = 3;
    // Magic sum
    int magicSum = n * (n * n + 1) / 2;
    // Build the model
    CPModel m = new CPModel();
    // Creation of an array of variables
    IntegerVariable[][] var = new IntegerVariable[n][n];
    // For each variable, we define its name and the boundaries of its domain.
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            var[i][j] = Choco.makeIntVar("var_" + i + "_" + j, 1, n * n);
            // Associate the variable to the model.
            m.addVariable(var[i][j]);
        }
    }
    // All cells of the matrix must be different
    for (int i = 0; i < n * n; i++) {
        for (int j = i + 1; j < n * n; j++) {
            Constraint c = (Choco.neq(var[i / n][i % n], var[j / n][j % n]));
            m.addConstraint(c);
        }
    }
    // All rows must be equal to the magic sum
    for (int i = 0; i < n; i++) {
        m.addConstraint(Choco.eq(Choco.sum(var[i]), magicSum));
    }
    IntegerVariable[][] varCol = new IntegerVariable[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            // Copy of var in the column order
            varCol[i][j] = var[j][i];
        }
        // Each column?s sum is equal to the magic sum
        m.addConstraint(Choco.eq(Choco.sum(varCol[i]), magicSum));
    }
    IntegerVariable[] varDiag1 = new IntegerVariable[n];
    IntegerVariable[] varDiag2 = new IntegerVariable[n];
    for (int i = 0; i < n; i++) {
        // Copy of var in varDiag1
        varDiag1[i] = var[i][i];
        // Copy of var in varDiag2
        varDiag2[i] = var[(n - 1) - i][i];
    }
    // Every diagonal?s sum has to be equal to the magic sum
    m.addConstraint(Choco.eq(Choco.sum(varDiag1), magicSum));
    m.addConstraint(Choco.eq(Choco.sum(varDiag2), magicSum));
    // Build the solver
    CPSolver s = new CPSolver();
    // print the problem
    System.out.println(m.pretty());
    // Read the model
    s.read(m);
    // Solve the model
    s.solve();
    // Print the solution
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(MessageFormat.format("{0} ", s.getVar(var[i][j]).getVal()));
        }
        System.out.println();
    }
}
Also used : CPSolver(choco.cp.solver.CPSolver) IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) Constraint(choco.kernel.model.constraints.Constraint) CPModel(choco.cp.model.CPModel) Constraint(choco.kernel.model.constraints.Constraint)

Example 5 with CPSolver

use of choco.cp.solver.CPSolver 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)

Aggregations

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