diff options
Diffstat (limited to 'src/jrummikub/ai/fdsolver/Solver.java')
-rw-r--r-- | src/jrummikub/ai/fdsolver/Solver.java | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/jrummikub/ai/fdsolver/Solver.java b/src/jrummikub/ai/fdsolver/Solver.java index c47b1c1..c3ede1b 100644 --- a/src/jrummikub/ai/fdsolver/Solver.java +++ b/src/jrummikub/ai/fdsolver/Solver.java @@ -11,6 +11,10 @@ import java.util.Map; import java.util.Set; import static jrummikub.ai.fdsolver.Satisfiability.*; +/** + * The Solver class is the Main Constraint Propagator (MCP) that tries to find a + * labeling of all variables that satisfies all given constraints. + */ public class Solver { Set<Var<?>> vars = new HashSet<Var<?>>(); @@ -80,9 +84,10 @@ public class Solver { } public void propagateOnce() { - Var<?> dirtyVar = Collections.max(dirtyVars); + Var<?> dirtyVar = Collections.min(dirtyVars); dirtyVars.remove(dirtyVar); - outerLoop: for (Iterator<Constraint> i = dirtyVar.getConstraints().iterator(); i.hasNext();) { + outerLoop: for (Iterator<Constraint> i = dirtyVar.getConstraints() + .iterator(); i.hasNext();) { Constraint constraint = i.next(); Satisfiability sat = constraint.getSatisfiability(); if (sat == UNSAT) { @@ -130,14 +135,14 @@ public class Solver { // backtracking and logging void branch() { - branchOn(unsolvedVars.iterator().next()); + branchOn(Collections.min(unsolvedVars)); } @SuppressWarnings("unchecked") void branchOn(Var<?> var) { - + Set<?> range = var.getRange(); - int n = (int)(Math.random() * range.size()); + int n = (int) (Math.random() * range.size()); Iterator<?> it = range.iterator(); for (int i = 0; i < n; i++) { it.next(); @@ -230,13 +235,13 @@ public class Solver { public <T> Var<T> makeVar(T... range) { return makeVar(Arrays.asList(range)); } - + public void record() { for (Var<?> var : vars) { var.record(); } } - + public void restore() { for (Var<?> var : vars) { var.restore(); |