summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/ai/fdsolver/constraint/OffsetConstraint.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/constraint/OffsetConstraint.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/constraint/OffsetConstraint.java')
-rw-r--r--src/jrummikub/ai/fdsolver/constraint/OffsetConstraint.java77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/jrummikub/ai/fdsolver/constraint/OffsetConstraint.java b/src/jrummikub/ai/fdsolver/constraint/OffsetConstraint.java
new file mode 100644
index 0000000..f91dcda
--- /dev/null
+++ b/src/jrummikub/ai/fdsolver/constraint/OffsetConstraint.java
@@ -0,0 +1,77 @@
+package jrummikub.ai.fdsolver.constraint;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import jrummikub.ai.fdsolver.Constraint;
+import jrummikub.ai.fdsolver.Propagator;
+import jrummikub.ai.fdsolver.Satisfiability;
+import jrummikub.ai.fdsolver.Var;
+
+public class OffsetConstraint implements Constraint {
+ private Var<Integer> x, y;
+ int offset;
+ Propagator propX, propY;
+
+ public OffsetConstraint(int offset, Var<Integer> x, Var<Integer> y) {
+ this.offset = offset;
+ this.x = x;
+ this.y = y;
+ propX = new OffsetProp(offset, x, y);
+ propY = new OffsetProp(-offset, y, x);
+ }
+
+ @Override
+ public Collection<Var<?>> getWatchedVars() {
+ return Arrays.<Var<?>> asList(x, y);
+ }
+
+ private class OffsetProp implements Propagator {
+ private Var<Integer> x, y;
+ private int offset;
+ public OffsetProp(int offset, Var<Integer> x, Var<Integer> y) {
+ this.offset = offset;
+ this.x = x;
+ this.y = y;
+ }
+
+ @Override
+ public Collection<Var<?>> getWatchedVars() {
+ return Arrays.<Var<?>>asList(y);
+ }
+
+ @Override
+ public void propagate() {
+ for(Iterator<Integer> i = x.iterator(); i.hasNext();) {
+ if(!y.getRange().contains(i.next() + offset)) {
+ i.remove();
+ }
+ }
+ }
+ }
+
+
+ @Override
+ public Collection<Propagator> getPropagators(boolean negate) {
+ return Arrays.asList(propX, propY);
+ }
+
+ @Override
+ public Satisfiability getSatisfiability() {
+ HashSet<Integer> shiftedRange = new HashSet<Integer>();
+ for (int val : x.getRange()) {
+ shiftedRange.add(val + offset);
+ }
+ shiftedRange.retainAll(y.getRange());
+ if (shiftedRange.isEmpty()) {
+ return Satisfiability.UNSAT;
+ } else if (x.getRange().size() == 1 && y.getRange().size() == 1) {
+ return Satisfiability.TAUT;
+ } else {
+ return Satisfiability.SAT;
+ }
+ }
+
+}