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.minisat.orders;
31
32 import static org.sat4j.core.LiteralsUtils.var;
33
34 import java.io.PrintWriter;
35 import java.io.Serializable;
36
37 import org.sat4j.minisat.core.Heap;
38 import org.sat4j.minisat.core.ILits;
39 import org.sat4j.minisat.core.IOrder;
40 import org.sat4j.minisat.core.IPhaseSelectionStrategy;
41
42
43
44
45
46
47
48
49
50 public class VarOrderHeap implements IOrder, Serializable {
51
52 private static final long serialVersionUID = 1L;
53
54 private static final double VAR_RESCALE_FACTOR = 1e-100;
55
56 private static final double VAR_RESCALE_BOUND = 1 / VAR_RESCALE_FACTOR;
57
58
59
60
61 protected double[] activity = new double[1];
62
63 private double varDecay = 1.0;
64
65
66
67
68 private double varInc = 1.0;
69
70 protected ILits lits;
71
72 private long nullchoice = 0;
73
74 protected Heap heap;
75
76 protected IPhaseSelectionStrategy phaseStrategy;
77
78 public VarOrderHeap() {
79 this(new PhaseInLastLearnedClauseSelectionStrategy());
80 }
81
82 public VarOrderHeap(IPhaseSelectionStrategy strategy) {
83 this.phaseStrategy = strategy;
84 }
85
86
87
88
89
90
91 public void setPhaseSelectionStrategy(IPhaseSelectionStrategy strategy) {
92 this.phaseStrategy = strategy;
93 }
94
95 public IPhaseSelectionStrategy getPhaseSelectionStrategy() {
96 return this.phaseStrategy;
97 }
98
99 public void setLits(ILits lits) {
100 this.lits = lits;
101 }
102
103
104
105
106
107
108
109 public int select() {
110 while (!this.heap.empty()) {
111 int var = this.heap.getmin();
112 int next = this.phaseStrategy.select(var);
113 if (this.lits.isUnassigned(next)) {
114 if (this.activity[var] < 0.0001) {
115 this.nullchoice++;
116 }
117 return next;
118 }
119 }
120 return ILits.UNDEFINED;
121 }
122
123
124
125
126
127
128
129 public void setVarDecay(double d) {
130 this.varDecay = d;
131 }
132
133
134
135
136
137
138 public void undo(int x) {
139 if (!this.heap.inHeap(x)) {
140 this.heap.insert(x);
141 }
142 }
143
144
145
146
147
148
149
150 public void updateVar(int p) {
151 int var = var(p);
152 updateActivity(var);
153 this.phaseStrategy.updateVar(p);
154 if (this.heap.inHeap(var)) {
155 this.heap.increase(var);
156 }
157 }
158
159 protected void updateActivity(final int var) {
160 if ((this.activity[var] += this.varInc) > VAR_RESCALE_BOUND) {
161 varRescaleActivity();
162 }
163 }
164
165
166
167
168 public void varDecayActivity() {
169 this.varInc *= this.varDecay;
170 }
171
172
173
174
175 private void varRescaleActivity() {
176 for (int i = 1; i < this.activity.length; i++) {
177 this.activity[i] *= VAR_RESCALE_FACTOR;
178 }
179 this.varInc *= VAR_RESCALE_FACTOR;
180 }
181
182 public double varActivity(int p) {
183 return this.activity[var(p)];
184 }
185
186
187
188
189 public int numberOfInterestingVariables() {
190 int cpt = 0;
191 for (int i = 1; i < this.activity.length; i++) {
192 if (this.activity[i] > 1.0) {
193 cpt++;
194 }
195 }
196 return cpt;
197 }
198
199
200
201
202
203 public void init() {
204 int nlength = this.lits.nVars() + 1;
205 if (this.activity == null || this.activity.length < nlength) {
206 this.activity = new double[nlength];
207 }
208 this.phaseStrategy.init(nlength);
209 this.activity[0] = -1;
210 this.heap = new Heap(this.activity);
211 this.heap.setBounds(nlength);
212 for (int i = 1; i < nlength; i++) {
213 assert i > 0;
214 assert i <= this.lits.nVars() : "" + this.lits.nVars() + "/" + i;
215 this.activity[i] = 0.0;
216 if (this.lits.belongsToPool(i)) {
217 this.heap.insert(i);
218 }
219 }
220 }
221
222 @Override
223 public String toString() {
224 return "VSIDS like heuristics from MiniSAT using a heap " + this.phaseStrategy;
225 }
226
227 public ILits getVocabulary() {
228 return this.lits;
229 }
230
231 public void printStat(PrintWriter out, String prefix) {
232 out.println(prefix + "non guided choices\t" + this.nullchoice);
233 }
234
235 public void assignLiteral(int p) {
236 this.phaseStrategy.assignLiteral(p);
237 }
238
239 public void updateVarAtDecisionLevel(int q) {
240 this.phaseStrategy.updateVarAtDecisionLevel(q);
241
242 }
243
244 public double[] getVariableHeuristics() {
245 return this.activity;
246 }
247 }