1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.sat4j.csp.encodings;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import org.sat4j.core.VecInt;
27 import org.sat4j.csp.Encoding;
28 import org.sat4j.csp.Evaluable;
29 import org.sat4j.csp.Var;
30 import org.sat4j.specs.ContradictionException;
31 import org.sat4j.specs.ISolver;
32 import org.sat4j.specs.IVec;
33 import org.sat4j.specs.IVecInt;
34 import org.sat4j.specs.IteratorInt;
35
36 public class GeneralizedSupportEncoding implements Encoding {
37
38 private final Map<Set<Integer>, IVecInt> supports = new HashMap<Set<Integer>, IVecInt>();
39
40 private static final Encoding instance = new GeneralizedSupportEncoding();
41
42 private GeneralizedSupportEncoding() {
43
44 }
45
46 public static Encoding instance() {
47 return instance;
48 }
49
50 public void onFinish(ISolver solver, IVec<Var> scope)
51 throws ContradictionException {
52
53
54 }
55
56 public void onInit(ISolver solver, IVec<Var> scope) {
57 supports.clear();
58 int[] acc = new int[scope.size()];
59 fill(0, scope, acc, supports);
60 }
61
62 public void onNogood(ISolver solver, IVec<Var> scope,
63 Map<Evaluable, Integer> tuple) throws ContradictionException {
64
65 }
66
67 public void onSupport(ISolver solver, IVec<Var> scope,
68 Map<Evaluable, Integer> tuple) throws ContradictionException {
69 for (int i = 0; i < scope.size(); i++) {
70 Set<Integer> set = new TreeSet<Integer>();
71 Var vari = scope.get(i);
72 for (int j = 0; j < scope.size(); j++) {
73 if (i != j) {
74 set.add(scope.get(j).translate(tuple.get(vari)));
75 }
76 }
77 IVecInt support = supports.get(set);
78 assert support != null;
79 support.push(vari.translate(tuple.get(vari)));
80 }
81
82 }
83
84 private void fill(int n, IVec<Var> scope, int[] acc,
85 Map<Set<Integer>, IVecInt> theSupports) {
86 if (n == scope.size()) {
87 for (int j = 0; j < acc.length; j++) {
88 Set<Integer> set = new TreeSet<Integer>();
89 for (int i = 0; i < acc.length; i++)
90 if (i != j)
91 set.add(scope.get(i).translate(acc[i]));
92 theSupports.put(set, new VecInt());
93 }
94 } else
95 for (IteratorInt iterator = scope.get(n).domain().iterator() ; iterator.hasNext();) {
96 acc[n] = iterator.next();
97 fill(n + 1, scope, acc, theSupports);
98 }
99
100 }
101 }