Search in sources :

Example 16 with Constraint

use of choco.kernel.model.constraints.Constraint in project abstools by abstools.

the class ChocoSolver method solve.

public boolean solve() {
    // add the constraints
    if (!solved) {
        for (Constraint c : constraints) cpmodel.addConstraint(c);
    }
    // show the problem
    if (absmodel.debug) {
        absmodel.println("## The constraints:");
        // ast.println(m.pretty());
        for (Constraint c : constraints) {
            if (!c.pretty().startsWith("true"))
                absmodel.println(prettyConst(c));
        }
        absmodel.println("-----");
    }
    // Read the model
    solver.read(cpmodel);
    // Solve the model
    newsol = solver.solve();
    solved = true;
    return newsol;
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) MetaConstraint(choco.kernel.model.constraints.MetaConstraint) ComponentConstraint(choco.kernel.model.constraints.ComponentConstraint)

Example 17 with Constraint

use of choco.kernel.model.constraints.Constraint in project abstools by abstools.

the class ChocoSolver method countSolutions.

public int countSolutions() {
    if (absmodel.debug) {
        absmodel.print("## The constraints:");
        absmodel.println(cpmodel.pretty());
    }
    // add the constraints
    for (Constraint c : constraints) cpmodel.addConstraint(c);
    // Read the model
    solver.read(cpmodel);
    // Solve the model
    solver.solveAll();
    return solver.getNbSolutions();
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) MetaConstraint(choco.kernel.model.constraints.MetaConstraint) ComponentConstraint(choco.kernel.model.constraints.ComponentConstraint)

Example 18 with Constraint

use of choco.kernel.model.constraints.Constraint 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)

Example 19 with Constraint

use of choco.kernel.model.constraints.Constraint 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)

Aggregations

Constraint (choco.kernel.model.constraints.Constraint)19 ComponentConstraint (choco.kernel.model.constraints.ComponentConstraint)8 MetaConstraint (choco.kernel.model.constraints.MetaConstraint)8 IntegerVariable (choco.kernel.model.variables.integer.IntegerVariable)6 CPModel (choco.cp.model.CPModel)5 HashSet (java.util.HashSet)4 CPSolver (choco.cp.solver.CPSolver)3 ChocoSolver (abs.frontend.mtvl.ChocoSolver)2 ArrayList (java.util.ArrayList)2 ChocoSolver (org.abs_models.frontend.mtvl.ChocoSolver)2 WrongProgramArgumentException (abs.common.WrongProgramArgumentException)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 AttrAssignment (org.abs_models.frontend.ast.AttrAssignment)1 Feature (org.abs_models.frontend.ast.Feature)1 IntVal (org.abs_models.frontend.ast.IntVal)1 Model (org.abs_models.frontend.ast.Model)1 Product (org.abs_models.frontend.ast.Product)1 ProductDecl (org.abs_models.frontend.ast.ProductDecl)1