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