summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/ai/fdsolver/Solver.java
diff options
context:
space:
mode:
authorJannis Harder <harder@informatik.uni-luebeck.de>2011-06-14 17:48:22 +0200
committerJannis Harder <harder@informatik.uni-luebeck.de>2011-06-14 17:48:22 +0200
commit7568f3782466531fe04fc14a40dc4d3a393c1fb9 (patch)
tree7778b598da5b698c35189234fe3a9dd58ae82913 /src/jrummikub/ai/fdsolver/Solver.java
parent79767f762e39eac3a0247b9ee75b24258790e0de (diff)
downloadJRummikub-7568f3782466531fe04fc14a40dc4d3a393c1fb9.tar
JRummikub-7568f3782466531fe04fc14a40dc4d3a393c1fb9.zip
Added TurnLogic with rule variables and constraints
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@436 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/ai/fdsolver/Solver.java')
-rw-r--r--src/jrummikub/ai/fdsolver/Solver.java19
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();