1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.sat4j.pb.tools;
31
32 import java.math.BigInteger;
33 import java.util.Collection;
34
35 import org.sat4j.core.Vec;
36 import org.sat4j.core.VecInt;
37 import org.sat4j.pb.OptToPBSATAdapter;
38 import org.sat4j.specs.IVec;
39 import org.sat4j.specs.IVecInt;
40 import org.sat4j.specs.TimeoutException;
41 import org.sat4j.tools.SolutionFoundListener;
42
43 public abstract class AbstractLexicoHelper<T, C> extends DependencyHelper<T, C>
44 implements SolutionFoundListener {
45
46 private final LexicoDecoratorPB lexico;
47
48 public AbstractLexicoHelper(LexicoDecoratorPB lexico) {
49 super(new OptToPBSATAdapter(lexico));
50 this.lexico = lexico;
51 ((OptToPBSATAdapter) getSolver()).setSolutionFoundListener(this);
52 }
53
54 public AbstractLexicoHelper(LexicoDecoratorPB lexico,
55 boolean explanationEnabled) {
56 super(new OptToPBSATAdapter(lexico), explanationEnabled);
57 this.lexico = lexico;
58 ((OptToPBSATAdapter) getSolver()).setSolutionFoundListener(this);
59 }
60
61 public AbstractLexicoHelper(LexicoDecoratorPB lexico,
62 boolean explanationEnabled, boolean canonicalOptFunctionEnabled) {
63 super(new OptToPBSATAdapter(lexico), explanationEnabled,
64 canonicalOptFunctionEnabled);
65 this.lexico = lexico;
66 ((OptToPBSATAdapter) getSolver()).setSolutionFoundListener(this);
67 }
68
69 private boolean hasASolution;
70
71 public void addCriterion(Collection<T> things) {
72 IVecInt literals = new VecInt(things.size());
73 for (T thing : things) {
74 literals.push(getIntValue(thing));
75 }
76 this.lexico.addCriterion(literals);
77 }
78
79 public void addWeightedCriterion(Collection<WeightedObject<T>> things) {
80 IVecInt literals = new VecInt(things.size());
81 IVec<BigInteger> coefs = new Vec<BigInteger>(things.size());
82 for (WeightedObject<T> wo : things) {
83 literals.push(getIntValue(wo.thing));
84 coefs.push(wo.getWeight());
85 }
86 this.lexico.addCriterion(literals, coefs);
87 }
88
89
90
91
92
93
94
95 @Override
96 public boolean hasASolution() throws TimeoutException {
97 try {
98 return super.hasASolution();
99 } catch (TimeoutException e) {
100 if (this.hasASolution) {
101 return true;
102 } else {
103 throw e;
104 }
105 }
106 }
107
108
109
110
111
112
113
114 @Override
115 public boolean hasASolution(IVec<T> assumps) throws TimeoutException {
116 try {
117 return super.hasASolution(assumps);
118 } catch (TimeoutException e) {
119 if (this.hasASolution) {
120 return true;
121 } else {
122 throw e;
123 }
124 }
125 }
126
127
128
129
130
131
132
133 @Override
134 public boolean hasASolution(Collection<T> assumps) throws TimeoutException {
135 try {
136 return super.hasASolution(assumps);
137 } catch (TimeoutException e) {
138 if (this.hasASolution) {
139 return true;
140 } else {
141 throw e;
142 }
143 }
144 }
145
146 public boolean isOptimal() {
147 return ((OptToPBSATAdapter) getSolver()).isOptimal();
148 }
149
150 public void onSolutionFound(int[] solution) {
151 this.hasASolution = true;
152 }
153
154 public void onSolutionFound(IVecInt solution) {
155 this.hasASolution = true;
156
157 }
158
159 public void onUnsatTermination() {
160
161 }
162
163 }