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