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 package org.sat4j.minisat.orders;
29
30 import static org.sat4j.core.LiteralsUtils.neg;
31 import static org.sat4j.core.LiteralsUtils.var;
32
33 import java.io.PrintWriter;
34 import java.io.Serializable;
35
36 import org.sat4j.minisat.core.ILits;
37 import org.sat4j.minisat.core.IOrder;
38 import org.sat4j.minisat.core.IPhaseSelectionStrategy;
39
40
41
42
43
44
45
46
47
48 public class VarOrder<L extends ILits> implements Serializable, IOrder<L> {
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55 private static final double VAR_RESCALE_FACTOR = 1e-100;
56
57 private static final double VAR_RESCALE_BOUND = 1 / VAR_RESCALE_FACTOR;
58
59
60
61
62 protected double[] activity = new double[1];
63
64
65
66
67 protected int lastVar = 1;
68
69
70
71
72 protected int[] order = new int[1];
73
74 private double varDecay = 1.0;
75
76
77
78
79 private double varInc = 1.0;
80
81
82
83
84 protected int[] varpos = new int[1];
85
86 protected L lits;
87
88 private long nullchoice = 0;
89
90 protected IPhaseSelectionStrategy phaseStrategy;
91
92 public VarOrder() {
93 this(new PhaseInLastLearnedClauseSelectionStrategy());
94 }
95
96 public VarOrder(IPhaseSelectionStrategy strategy) {
97 this.phaseStrategy = strategy;
98 }
99
100
101
102
103
104
105 public void setPhaseSelectionStrategy(IPhaseSelectionStrategy strategy) {
106 phaseStrategy = strategy;
107 }
108
109 public IPhaseSelectionStrategy getPhaseSelectionStrategy() {
110 return phaseStrategy;
111 }
112
113
114
115
116
117
118 public void setLits(L lits) {
119 this.lits = lits;
120 }
121
122
123
124
125
126
127 public int select() {
128 assert lastVar > 0;
129 for (int i = lastVar; i < order.length; i++) {
130 assert i > 0;
131 if (lits.isUnassigned(order[i])) {
132 lastVar = i;
133 if (activity[i] < 0.0001) {
134
135
136
137
138
139
140
141
142
143 nullchoice++;
144 }
145 return order[i];
146 }
147 }
148 return ILits.UNDEFINED;
149 }
150
151
152
153
154
155
156
157 public void setVarDecay(double d) {
158 varDecay = d;
159 }
160
161
162
163
164
165
166 public void undo(int x) {
167 assert x > 0;
168 assert x < order.length;
169 int pos = varpos[x];
170 if (pos < lastVar) {
171 lastVar = pos;
172 }
173 assert lastVar > 0;
174 }
175
176
177
178
179
180
181 public void updateVar(int p) {
182 assert p > 1;
183 final int var = var(p);
184
185 updateActivity(var);
186 int i = varpos[var];
187 for (; i > 1
188 && (activity[var(order[i - 1])] < activity[var]); i--) {
189 assert i > 1;
190
191 final int orderpm1 = order[i - 1];
192 assert varpos[var(orderpm1)] == i - 1;
193 varpos[var(orderpm1)] = i;
194 order[i] = orderpm1;
195 }
196 assert i >= 1;
197 varpos[var] = i;
198 order[i] = p;
199
200 if (i < lastVar) {
201 lastVar = i;
202 }
203 }
204
205 protected void updateActivity(final int var) {
206 if ((activity[var] += varInc) > VAR_RESCALE_BOUND) {
207 varRescaleActivity();
208 }
209 }
210
211
212
213
214 public void varDecayActivity() {
215 varInc *= varDecay;
216 }
217
218
219
220
221 private void varRescaleActivity() {
222 for (int i = 1; i < activity.length; i++) {
223 activity[i] *= VAR_RESCALE_FACTOR;
224 }
225 varInc *= VAR_RESCALE_FACTOR;
226 }
227
228 public double varActivity(int p) {
229 return activity[var(p)];
230 }
231
232
233
234
235 public int numberOfInterestingVariables() {
236 int cpt = 0;
237 for (int i = 1; i < activity.length; i++) {
238 if (activity[i] > 1.0) {
239 cpt++;
240 }
241 }
242 return cpt;
243 }
244
245
246
247
248
249
250 public void init() {
251 int nlength = lits.nVars() + 1;
252 int reallength = lits.realnVars() + 1;
253 int[] nvarpos = new int[nlength];
254 double[] nactivity = new double[nlength];
255 int[] norder = new int[reallength];
256 nvarpos[0] = -1;
257 nactivity[0] = -1;
258 norder[0] = ILits.UNDEFINED;
259 for (int i = 1, j = 1; i < nlength; i++) {
260 assert i > 0;
261 assert i <= lits.nVars() : "" + lits.nVars() + "/" + i;
262 if (lits.belongsToPool(i)) {
263 norder[j] = neg(lits.getFromPool(i));
264
265
266 nvarpos[i] = j++;
267 }
268 nactivity[i] = 0.0;
269 }
270 varpos = nvarpos;
271 activity = nactivity;
272 order = norder;
273 lastVar = 1;
274 }
275
276
277
278
279
280
281
282 @Override
283 public String toString() {
284 return "VSIDS like heuristics from MiniSAT using a sorted array";
285 }
286
287 public ILits getVocabulary() {
288 return lits;
289 }
290
291
292
293
294
295
296
297 public void printStat(PrintWriter out, String prefix) {
298 out.println(prefix + "non guided choices\t" + nullchoice);
299
300 }
301
302 public void assignLiteral(int p) {
303
304 }
305
306 }