// // Toolkit constraint encoding // import java.io.*; import java.util.*; 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; import choco.kernel.solver.variables.integer.IntDomainVar; import choco.kernel.solver.ContradictionException; import choco.cp.solver.search.integer.varselector.StaticVarOrder; public class SR { int n; int[][] rank; // rank[i][j] = k <-> agent_i ranks agent_j as k^th choice int[][] pref; // pref[i][k] = j <-> agent_i has agent_j as k^th choice int[] length; // length of agent's preference list Model model; Solver solver; IntegerVariable[] agent; // domain of ranks, last is unmatched long totalTime, modelTime, solveTime, readTime, modelSize; boolean search; int solutions, matchingSize; SR(String fname) throws IOException { search = true; totalTime = System.currentTimeMillis(); readTime = System.currentTimeMillis(); read(fname); readTime = System.currentTimeMillis() - readTime; } SR(SMSRInstance inst) { search = true; totalTime = System.currentTimeMillis(); readTime = System.currentTimeMillis(); read(inst); readTime = System.currentTimeMillis() - readTime; } void read(String fname) throws IOException { BufferedReader fin = new BufferedReader(new FileReader(fname)); n = Integer.parseInt(fin.readLine()); pref = new int[n][n]; rank = new int[n][n]; length = new int[n]; for (int i=0;i)inst.pref[i]){ rank[i][j] = k; pref[i][k] = j; length[i] = length[i] + 1; k = k + 1; } rank[i][i] = k; pref[i][k] = i; } } void build(){ modelTime = System.currentTimeMillis(); model = new CPModel(); agent = new IntegerVariable[n]; for (int i=0;i 0) getMatchingSize(); } else if (command.equals("all")){ // enumerate all solutions if (solver.solve().booleanValue()){ getMatchingSize(); displayMatching(); solutions = 1; while (solver.nextSolution().booleanValue()){solutions++; displayMatching();} } } else if (command.equals("propagate")){ search = false; try{solver.propagate(); displayPhase1Table();} catch (ContradictionException e){displayPhase1Table();} } else if (solver.solve().booleanValue()){ solutions = 1; getMatchingSize(); displayMatching(); } solveTime = System.currentTimeMillis() - solveTime; totalTime = System.currentTimeMillis() - totalTime; } int getMatchingSize(){ matchingSize = 0; for (int i=0;i 1) sr.solve(args[1]); else sr.solve("first"); sr.stats(); } }