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());
}
}
}
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;
}
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();
}
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;
}
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);
}
Aggregations