|
1 |
| package org.sat4j.reader.csp; |
|
2 |
| |
|
3 |
| import org.sat4j.core.VecInt; |
|
4 |
| import org.sat4j.specs.ContradictionException; |
|
5 |
| import org.sat4j.specs.ISolver; |
|
6 |
| import org.sat4j.specs.IVec; |
|
7 |
| import org.sat4j.specs.IVecInt; |
|
8 |
| |
|
9 |
| |
|
10 |
| public class SupportsDirectEncoding implements Relation { |
|
11 |
| |
|
12 |
| protected final int arity; |
|
13 |
| |
|
14 |
| protected int[][] tuples; |
|
15 |
| |
|
16 |
| private IVecInt clause; |
|
17 |
| private int lastmatch; |
|
18 |
| |
|
19 |
0
| public SupportsDirectEncoding(int arity, int nbtuples) {
|
|
20 |
0
| this.arity = arity;
|
|
21 |
0
| tuples = new int[nbtuples][];
|
|
22 |
| } |
|
23 |
| |
|
24 |
0
| public void addTuple(int index, int[] tuple) {
|
|
25 |
0
| tuples[index] = tuple;
|
|
26 |
| } |
|
27 |
| |
|
28 |
0
| public void toClause(ISolver solver, IVec<Var> scope, IVec<Evaluable> vars)
|
|
29 |
| throws ContradictionException { |
|
30 |
| |
|
31 |
0
| clause = new VecInt();
|
|
32 |
0
| int[] tuple = new int[vars.size()];
|
|
33 |
0
| lastmatch=-1;
|
|
34 |
0
| find(tuple, 0, scope,vars, solver);
|
|
35 |
| } |
|
36 |
| |
|
37 |
0
| private void find(int[] tuple, int n, IVec<Var> scope, IVec<Evaluable> vars, ISolver solver)
|
|
38 |
| throws ContradictionException { |
|
39 |
0
| if (n == vars.size()) {
|
|
40 |
0
| if (notPresent(tuple)) {
|
|
41 |
0
| clause.clear();
|
|
42 |
0
| for (int j = 0; j < vars.size(); j++) {
|
|
43 |
0
| clause.push(-vars.get(j).translate(tuple[j]));
|
|
44 |
| } |
|
45 |
| |
|
46 |
0
| solver.addClause(clause);
|
|
47 |
| } |
|
48 |
| } else { |
|
49 |
0
| int[] domain = vars.get(n).domain();
|
|
50 |
0
| for (int i = 0; i < domain.length; i++) {
|
|
51 |
0
| tuple[n] = domain[i];
|
|
52 |
0
| find(tuple, n + 1, scope,vars, solver);
|
|
53 |
| } |
|
54 |
| |
|
55 |
| } |
|
56 |
| |
|
57 |
| } |
|
58 |
| |
|
59 |
0
| private boolean notPresent(int[] tuple) {
|
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
0
| int i = lastmatch+1;
|
|
64 |
0
| int j = 0;
|
|
65 |
0
| while (i<tuples.length&&j < tuple.length) {
|
|
66 |
0
| if (tuples[i][j] < tuple[j]) {
|
|
67 |
0
| i++;
|
|
68 |
0
| j = 0;
|
|
69 |
0
| continue;
|
|
70 |
| } |
|
71 |
0
| if (tuples[i][j] > tuple[j])
|
|
72 |
0
| return true;
|
|
73 |
0
| j++;
|
|
74 |
| } |
|
75 |
0
| if (j==tuple.length) {
|
|
76 |
0
| lastmatch=i;
|
|
77 |
0
| return false;
|
|
78 |
| } |
|
79 |
0
| return true;
|
|
80 |
| } |
|
81 |
| |
|
82 |
0
| public int arity() {
|
|
83 |
0
| return arity;
|
|
84 |
| } |
|
85 |
| } |