1 |
| package org.sat4j.reader.csp; |
2 |
| |
3 |
| import java.util.HashMap; |
4 |
| import java.util.Map; |
5 |
| |
6 |
| import org.sat4j.core.VecInt; |
7 |
| import org.sat4j.specs.ContradictionException; |
8 |
| import org.sat4j.specs.ISolver; |
9 |
| import org.sat4j.specs.IVecInt; |
10 |
| |
11 |
| |
12 |
| public class Var { |
13 |
| |
14 |
| Map<Integer, Integer> mapping = new HashMap<Integer, Integer>(); |
15 |
| |
16 |
| private final int[] domain; |
17 |
| |
18 |
0
| public Var(int[] domain, int startid) {
|
19 |
0
| this.domain = domain;
|
20 |
0
| for (int i = 0; i < domain.length; i++)
|
21 |
0
| mapping.put(domain[i], ++startid);
|
22 |
| } |
23 |
| |
24 |
0
| public int[] domain() {
|
25 |
0
| return domain;
|
26 |
| } |
27 |
| |
28 |
0
| public int translate(int key) {
|
29 |
0
| return mapping.get(key);
|
30 |
| } |
31 |
| |
32 |
0
| public void toClause(ISolver solver) throws ContradictionException {
|
33 |
0
| IVecInt clause = new VecInt();
|
34 |
0
| for (int v : mapping.values())
|
35 |
0
| clause.push(v);
|
36 |
0
| solver.addClause(clause);
|
37 |
0
| solver.addAtMost(clause, 1);
|
38 |
| } |
39 |
| |
40 |
0
| public int findValue(int[] model) {
|
41 |
0
| for (Map.Entry<Integer, Integer> entry : mapping.entrySet()) {
|
42 |
0
| if ( model[entry.getValue()-1]==entry.getValue())
|
43 |
0
| return entry.getKey();
|
44 |
| } |
45 |
0
| throw new RuntimeException("BIG PROBLEM: no value for a var!");
|
46 |
| } |
47 |
| } |