From 7568f3782466531fe04fc14a40dc4d3a393c1fb9 Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Tue, 14 Jun 2011 17:48:22 +0200 Subject: Added TurnLogic with rule variables and constraints git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@436 72836036-5685-4462-b002-a69064685172 --- .../ai/fdsolver/constraint/IndexConstraint.java | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java (limited to 'src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java') diff --git a/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java b/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java new file mode 100644 index 0000000..999924f --- /dev/null +++ b/src/jrummikub/ai/fdsolver/constraint/IndexConstraint.java @@ -0,0 +1,134 @@ +package jrummikub.ai.fdsolver.constraint; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; + +import jrummikub.ai.fdsolver.Constraint; +import jrummikub.ai.fdsolver.Propagator; +import jrummikub.ai.fdsolver.Satisfiability; +import jrummikub.ai.fdsolver.Var; + +public class IndexConstraint implements Constraint { + Var target; + Var index; + List> list; + Collection> vars = new ArrayList>(); + Collection> varsNoTarget = new ArrayList>(); + Collection> varsNoIndex = new ArrayList>(); + + public IndexConstraint(Var target, Var index, List> list) { + this.target = target; + this.index = index; + this.list = list; + vars.addAll(list); + vars.add(index); + vars.add(target); + varsNoTarget.addAll(list); + varsNoTarget.add(index); + varsNoIndex.addAll(list); + varsNoIndex.add(target); + } + + @Override + public Collection> getWatchedVars() { + return vars; + } + + private class UnionProp implements Propagator { + @Override + public Collection> getWatchedVars() { + return varsNoTarget; + } + + @Override + public void propagate() { + HashSet union = new HashSet(); + for (int i : index.getRange()) { + union.addAll(list.get(i).getRange()); + } + for (Iterator i = target.iterator(); i.hasNext();) { + T val = i.next(); + if (!union.contains(val)) { + i.remove(); + } + } + }; + } + + private class IndexProp implements Propagator { + @Override + public Collection> getWatchedVars() { + return varsNoIndex; + } + + @Override + public void propagate() { + for (Iterator i = index.iterator(); i.hasNext();) { + int id = i.next(); + HashSet range = new HashSet(target.getRange()); + range.retainAll(list.get(id).getRange()); + if (range.isEmpty()) { + i.remove(); + } + } + } + } + + private class VarProp implements Propagator { + @Override + public Collection> getWatchedVars() { + return Arrays.asList(target, index); + } + + @Override + public void propagate() { + if (index.getRange().size() != 1) + return; + int id = index.getValue(); + Var var = list.get(id); + for(Iterator i = var.iterator(); i.hasNext();) { + if (!target.getRange().contains(i.next())) { + i.remove(); + } + } + } + + } + + @Override + public Collection getPropagators(boolean negate) { + if (negate) { + return Collections.emptyList(); + } + return Arrays. asList(new UnionProp(), new IndexProp(), new VarProp()); + } + + @Override + public Satisfiability getSatisfiability() { + HashSet union = new HashSet(); + 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 (!isSat) { + return Satisfiability.UNSAT; + } else { + if (union.size() == 1 && target.getRange().size() == 1) { + return Satisfiability.TAUT; + } else { + return Satisfiability.SAT; + } + } + } +} -- cgit v1.2.3