Search in sources :

Example 1 with Constraint

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

the class Main method analyzeMTVL.

/**
 * TODO: Should probably be introduced in Model through JastAdd by MTVL package.
 * However, the command-line argument handling will have to stay in Main. Pity.
 */
private void analyzeMTVL(Model m) {
    ProductDecl productDecl = null;
    try {
        productDecl = product == null ? null : m.findProduct(product);
    } catch (WrongProgramArgumentException e) {
    // ignore in case we're just solving.
    }
    if (m.hasMTVL()) {
        if (solve) {
            if (verbose)
                System.out.println("Searching for solutions for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.getSolutionsAsString());
        }
        if (minimise) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for minimum solutions of " + product + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.minimiseToString(product));
        }
        if (maximise) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for maximum solutions of " + product + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            // System.out.print(s.maximiseToInt(product));
            s.addConstraint(ChocoSolver.eqeq(s.vars.get(product), s.maximiseToInt(product)));
            ChocoSolver s1 = m.instantiateCSModel();
            int i = 1;
            while (s1.solveAgain()) {
                System.out.println("------ " + (i++) + "------");
                System.out.print(s1.getSolutionsAsString());
            }
        }
        if (solveall) {
            if (verbose)
                System.out.println("Searching for all solutions for the feature model...");
            ChocoSolver solver = m.instantiateCSModel();
            System.out.print(solver.getSolutionsAsString());
        }
        if (solveWith) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for solution that includes " + product + "...");
            if (productDecl != null) {
                ChocoSolver s = m.instantiateCSModel();
                HashSet<Constraint> newcs = new HashSet<>();
                productDecl.getProduct().getProdConstraints(s.vars, newcs);
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution:\n" + s.getSolutionsAsString());
            } else {
                System.out.println("Product '" + product + "' not found.");
            }
        }
        if (minWith) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for solution that includes " + product + "...");
            ChocoSolver s = m.instantiateCSModel();
            HashSet<Constraint> newcs = new HashSet<>();
            s.addIntVar("difference", 0, 50);
            if (productDecl != null) {
                m.getDiffConstraints(productDecl.getProduct(), s.vars, newcs, "difference");
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution: " + s.minimiseToString("difference"));
            } else {
                System.out.println("Product '" + product + "' not found.");
            }
        }
        if (maxProduct) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for solution that includes " + product + "...");
            ChocoSolver s = m.instantiateCSModel();
            HashSet<Constraint> newcs = new HashSet<>();
            s.addIntVar("noOfFeatures", 0, 50);
            if (m.getMaxConstraints(s.vars, newcs, "noOfFeatures")) {
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution: " + s.maximiseToString("noOfFeatures"));
            } else {
                System.out.println("---No solution-------------");
            }
        }
        if (check) {
            assert product != null;
            ChocoSolver s = m.instantiateCSModel();
            if (productDecl == null) {
                System.out.println("Product '" + product + "' not found.");
            } else {
                Map<String, Integer> guess = productDecl.getProduct().getSolution();
                System.out.println("checking solution: " + s.checkSolution(guess, m));
            }
        }
        if (numbersol && !ignoreattr) {
            ChocoSolver s = m.instantiateCSModel();
            System.out.println("Number of solutions found: " + s.countSolutions());
        } else if (numbersol && ignoreattr) {
            ChocoSolver s = m.instantiateCSModel();
            System.out.println("Number of solutions found (without attributes): " + s.countSolutions());
        }
    }
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) WrongProgramArgumentException(abs.common.WrongProgramArgumentException) Constraint(choco.kernel.model.constraints.Constraint) ChocoSolver(abs.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Example 2 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 3 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 4 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 5 with Constraint

use of choco.kernel.model.constraints.Constraint in project Malai by arnobl.

the class ChocoWrapper method pushNOT.

public static void pushNOT() {
    Constraint elem = stack.pop();
    Constraint res = Choco.not(elem);
    stack.push(res);
}
Also used : 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