//Author: Heather H-B (1003914) import static choco.Choco.*; import choco.cp.model.CPModel; import choco.cp.solver.CPSolver; import choco.kernel.model.Model; import choco.kernel.solver.Solver; import choco.kernel.model.variables.integer.IntegerVariable; public class HZebra { public static void main(String[] args) { //Create model Model m = new CPModel(); //Create variables IntegerVariable v[] = new IntegerVariable[25]; //Houses v[0] = makeIntVar("Red", 1, 5); v[1] = makeIntVar("Blue", 1, 5); v[2] = makeIntVar("Yellow", 1, 5); v[3] = makeIntVar("Green", 1, 5); v[4] = makeIntVar("Ivory", 1, 5); m.addConstraint(allDifferent(v[0], v[1], v[2], v[3], v[4])); //Cigarettes v[5] = makeIntVar("Old-Gold", 1, 5); v[6] = makeIntVar("Parliament", 1, 5); v[7] = makeIntVar("Kools", 1, 5); v[8] = makeIntVar("Lucky", 1, 5); v[9] = makeIntVar("Chesterfield", 1, 5); m.addConstraint(allDifferent(v[5], v[6], v[7], v[8], v[9])); //Nationalities v[10] = makeIntVar("Norwegian", 1, 5); v[11] = makeIntVar("Ukranian", 1, 5); v[12] = makeIntVar("Englishman", 1, 5); v[13] = makeIntVar("Spaniard", 1, 5); v[14] = makeIntVar("Japanese", 1, 5); m.addConstraint(allDifferent(v[10], v[11], v[12], v[13], v[14])); //Pets v[15] = makeIntVar("Zebra", 1, 5); v[16] = makeIntVar("Dog", 1, 5); v[17] = makeIntVar("Horse", 1, 5); v[18] = makeIntVar("Fox", 1, 5); v[19] = makeIntVar("Snails", 1, 5); m.addConstraint(allDifferent(v[15], v[16], v[17], v[18], v[19])); //Drinks v[20] = makeIntVar("Coffee", 1, 5); v[21] = makeIntVar("Tea", 1, 5); v[22] = makeIntVar("Water", 1, 5); v[23] = makeIntVar("Milk", 1, 5); v[24] = makeIntVar("Orange Juice", 1, 5); m.addConstraint(allDifferent(v[20], v[21], v[22], v[23], v[24])); //Create constraints //The Englishman lives in the Red house. m.addConstraint(eq(v[12], v[0])); //The Spaniard owns a Dog. m.addConstraint(eq(v[13], v[16])); //Coffee is drunk in the Green house. m.addConstraint(eq(v[20],v[3])); //The Ukranian drinks Tea. m.addConstraint(eq(v[11], v[21])); //The Green house is to the right of the Ivory house. m.addConstraint(gt(v[3], v[4])); //The Old-Gold smoker owns Snails. m.addConstraint(eq(v[5], v[19])); //Kools are smoked in the Yellow house. m.addConstraint(eq(v[7], v[2])); //Milk is drunk in the middle house. m.addConstraint(eq(v[23], 3)); //The Norwegian lives in the first house on the left. m.addConstraint(eq(v[10], 1)); //The Chesterfield smoker lives next to the Fox owner. m.addConstraint(eq(abs(minus(v[9], v[18])), 1)); //Kools are smoked in the house next to the house where the Horse is kept. m.addConstraint(eq(abs(minus(v[7], v[17])), 1)); //The Lucky smoker drinks Orange Juice. m.addConstraint(eq(v[8], v[24])); //The Japanese smokes Parliament. m.addConstraint(eq(v[14], v[6])); //The Norwegian lives next to the Blue house. m.addConstraint(eq(abs(minus(v[10], v[1])), 1)); //Create solver Solver s = new CPSolver(); s.read(m); //First solution: //s.solve(); //System.out.println(s.pretty()); //Num solutions: //s.solve(true); //System.out.println("nbSol: " + s.getNbSolutions()); //All solutions: s.solve(); do { int w = s.getVar(v[22]).getVal(); for (int i = 10; i < 15; i++) { if (w == s.getVar(v[i]).getVal()) System.out.print("Water: " + v[i].getName()); } int z = s.getVar(v[15]).getVal(); for (int i = 10; i < 15; i++) { if (z == s.getVar(v[i]).getVal()) System.out.println(", Zebra: " + v[i].getName()); } } while(s.nextSolution()); s.printRuntimeSatistics(); } }