diff options
Diffstat (limited to 'src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java')
-rw-r--r-- | src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java | 56 |
1 files changed, 31 insertions, 25 deletions
diff --git a/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java b/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java index 9d80a37..6698282 100644 --- a/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java +++ b/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java @@ -8,6 +8,8 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import org.w3c.dom.ranges.Range; + import jrummikub.ai.fdsolver.Constraint; import jrummikub.ai.fdsolver.Propagator; import jrummikub.ai.fdsolver.Satisfiability; @@ -47,16 +49,16 @@ public class IndexConstraint<T> extends Constraint { @Override public void propagate() { - HashSet<T> union = new HashSet<T>(); + HashSet<T> invUnion = new HashSet<T>(target.getRange()); for (int i : index.getRange()) { - union.addAll(list.get(i).getRange()); - } - for (Iterator<T> i = target.iterator(); i.hasNext();) { - T val = i.next(); - if (!union.contains(val)) { - i.remove(); + invUnion.removeAll(list.get(i).getRange()); + if (invUnion.isEmpty()) { + return; } } + for (T val : invUnion) { + target.invalidate(val); + } }; } @@ -70,9 +72,8 @@ public class IndexConstraint<T> extends Constraint { public void propagate() { for (Iterator<Integer> i = index.iterator(); i.hasNext();) { int id = i.next(); - HashSet<T> range = new HashSet<T>(target.getRange()); - range.retainAll(list.get(id).getRange()); - if (range.isEmpty()) { + Var<T> item = list.get(id); + if (Collections.disjoint(item.getRange(), target.getRange())) { i.remove(); } } @@ -107,28 +108,33 @@ public class IndexConstraint<T> extends Constraint { } return Arrays.<Propagator> asList(new UnionProp(), new IndexProp(), new VarProp()); } - + @Override - public Satisfiability getSatisfiability() { - HashSet<T> union = new HashSet<T>(); + public boolean isSatisfiable() { for (int i : index.getRange()) { - union.addAll(list.get(i).getRange()); - } - boolean isSat = false; - for (T val : target.getRange()) { - if (union.contains(val)) { - isSat = true; - break; + if(!Collections.disjoint(list.get(i).getRange(), target.getRange())) { + return true; } } - if (!isSat) { + return false; + } + + @Override + public Satisfiability getSatisfiability() { + boolean sat = isSatisfiable(); + if (!sat) { return Satisfiability.UNSAT; - } else { - if (union.size() == 1 && target.getRange().size() == 1) { - return Satisfiability.TAUT; - } else { + } + if (target.getRange().size() > 1) + return Satisfiability.SAT; + for (int i : index.getRange()) { + Var<T> var = list.get(i); + if (var.getRange().size() > 1) + return Satisfiability.SAT; + if(var.getValue() != target.getValue()) { return Satisfiability.SAT; } } + return Satisfiability.TAUT; } } |