// // Given an instance, is there a partion +/- 1? // import org.chocosolver.solver.Model; import org.chocosolver.solver.Solver; import org.chocosolver.solver.variables.IntVar; import org.chocosolver.solver.search.strategy.Search; public class Partition { int n; Model model; Solver solver; IntVar[] D; Partition(int[] w){ n = w.length; model = new Model("Partition"); solver = model.getSolver(); int total = 0; for (int x : w) total = total + x; D = model.intVarArray("D",n,0,1); // decision ... left or right? model.scalar(D,w,"=",total/2).post(); } boolean solve(){ solver.setSearch(Search.minDomUBSearch(D)); // take 1 then 0 return solver.solve(); } long stats(){ return solver.getMeasures().getNodeCount(); } }