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