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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "internal.hpp"
namespace CaDiCaL {
/*------------------------------------------------------------------------*/
// We experimented with resetting and reinitializing the saved phase with
// many solvers. Actually RSAT had already such a scheme. Our newest
// version seems to be quite beneficial for satisfiable instances. In an
// arithmetic increasing interval in the number of conflicts we either use
// the original phase (set by the option 'phase'), its inverted value, flip
// the current phase, pick random phases, then pick the best since the last
// time a best phase was picked and finally also use local search to find
// phases which minimize the number of falsified clauses. During
// stabilization (see 'stabilizing' in 'restart.cpp' when 'stable' is true)
// we execute a different rephasing schedule. The same applies if local
// search is disabled.
/*------------------------------------------------------------------------*/
bool Internal::rephasing () {
if (!opts.rephase)
return false;
if (opts.forcephase)
return false;
return stats.conflicts > lim.rephase;
}
/*------------------------------------------------------------------------*/
// Pick the original default phase.
char Internal::rephase_original () {
stats.rephased.original++;
signed char val = opts.phase ? 1 : -1; // original = initial
PHASE ("rephase", stats.rephased.total, "switching to original phase %d",
val);
for (auto idx : vars)
phases.saved[idx] = val;
return 'O';
}
// Pick the inverted original default phase.
char Internal::rephase_inverted () {
stats.rephased.inverted++;
signed char val = opts.phase ? -1 : 1; // original = -initial
PHASE ("rephase", stats.rephased.total,
"switching to inverted original phase %d", val);
for (auto idx : vars)
phases.saved[idx] = val;
return 'I';
}
// Flip the current phase.
char Internal::rephase_flipping () {
stats.rephased.flipped++;
PHASE ("rephase", stats.rephased.total,
"flipping all phases individually");
for (auto idx : vars)
phases.saved[idx] *= -1;
return 'F';
}
// Complete random picking of phases.
char Internal::rephase_random () {
stats.rephased.random++;
PHASE ("rephase", stats.rephased.total, "resetting all phases randomly");
Random random (opts.seed); // global seed
random += stats.rephased.random; // different every time
for (auto idx : vars)
phases.saved[idx] = random.generate_bool () ? -1 : 1;
return '#';
}
// Best phases are those saved at the largest trail height without conflict.
// See code and comments in 'update_target_and_best' in 'backtrack.cpp'
char Internal::rephase_best () {
stats.rephased.best++;
PHASE ("rephase", stats.rephased.total,
"overwriting saved phases by best phases");
signed char val;
for (auto idx : vars)
if ((val = phases.best[idx]))
phases.saved[idx] = val;
return 'B';
}
// Trigger local search 'walk' in 'walk.cpp'.
char Internal::rephase_walk () {
stats.rephased.walk++;
PHASE ("rephase", stats.rephased.total,
"starting local search to improve current phase");
walk ();
return 'W';
}
/*------------------------------------------------------------------------*/
void Internal::rephase () {
stats.rephased.total++;
PHASE ("rephase", stats.rephased.total,
"reached rephase limit %" PRId64 " after %" PRId64 " conflicts",
lim.rephase, stats.conflicts);
// Report current 'target' and 'best' and then set 'rephased' below, which
// will trigger reporting the new 'target' and 'best' after updating it in
// the next 'update_target_and_best' called from the next 'backtrack'.
//
report ('~', 1);
backtrack ();
clear_phases (phases.target);
target_assigned = 0;
size_t count = lim.rephased[stable]++;
bool single;
char type;
if (opts.stabilize && opts.stabilizeonly)
single = true;
else
single = !opts.stabilize;
if (single && !opts.walk) {
// (inverted,best,flipping,best,random,best,original,best)^\omega
switch (count % 8) {
case 0:
type = rephase_inverted ();
break;
case 1:
type = rephase_best ();
break;
case 2:
type = rephase_flipping ();
break;
case 3:
type = rephase_best ();
break;
case 4:
type = rephase_random ();
break;
case 5:
type = rephase_best ();
break;
case 6:
type = rephase_original ();
break;
case 7:
type = rephase_best ();
break;
default:
type = 0;
break;
}
} else if (single && opts.walk) {
// (inverted,best,walk,
// flipping,best,walk,
// random,best,walk,
// original,best,walk)^\omega
switch (count % 12) {
case 0:
type = rephase_inverted ();
break;
case 1:
type = rephase_best ();
break;
case 2:
type = rephase_walk ();
break;
case 3:
type = rephase_flipping ();
break;
case 4:
type = rephase_best ();
break;
case 5:
type = rephase_walk ();
break;
case 6:
type = rephase_random ();
break;
case 7:
type = rephase_best ();
break;
case 8:
type = rephase_walk ();
break;
case 9:
type = rephase_original ();
break;
case 10:
type = rephase_best ();
break;
case 11:
type = rephase_walk ();
break;
default:
type = 0;
break;
}
} else if (stable && !opts.walk) {
// original,inverted,(best,original,best,inverted)^\omega
if (!count)
type = rephase_original ();
else if (count == 1)
type = rephase_inverted ();
else
switch ((count - 2) % 4) {
case 0:
type = rephase_best ();
break;
case 1:
type = rephase_original ();
break;
case 2:
type = rephase_best ();
break;
case 3:
type = rephase_inverted ();
break;
default:
type = 0;
break;
}
} else if (stable && opts.walk) {
// original,inverted,(best,walk,original,best,walk,inverted)^\omega
if (!count)
type = rephase_original ();
else if (count == 1)
type = rephase_inverted ();
else
switch ((count - 2) % 6) {
case 0:
type = rephase_best ();
break;
case 1:
type = rephase_walk ();
break;
case 2:
type = rephase_original ();
break;
case 3:
type = rephase_best ();
break;
case 4:
type = rephase_walk ();
break;
case 5:
type = rephase_inverted ();
break;
default:
type = 0;
break;
}
} else if (!stable && (!opts.walk || !opts.walknonstable)) {
// flipping,(random,best,flipping,best)^\omega
if (!count)
type = rephase_flipping ();
else
switch ((count - 1) % 4) {
case 0:
type = rephase_random ();
break;
case 1:
type = rephase_best ();
break;
case 2:
type = rephase_flipping ();
break;
case 3:
type = rephase_best ();
break;
default:
type = 0;
break;
}
} else {
assert (!stable && opts.walk && opts.walknonstable);
// flipping,(random,best,walk,flipping,best,walk)^\omega
if (!count)
type = rephase_flipping ();
else
switch ((count - 1) % 6) {
case 0:
type = rephase_random ();
break;
case 1:
type = rephase_best ();
break;
case 2:
type = rephase_walk ();
break;
case 3:
type = rephase_flipping ();
break;
case 4:
type = rephase_best ();
break;
case 5:
type = rephase_walk ();
break;
default:
type = 0;
break;
}
}
assert (type);
int64_t delta = opts.rephaseint * (stats.rephased.total + 1);
lim.rephase = stats.conflicts + delta;
PHASE ("rephase", stats.rephased.total,
"new rephase limit %" PRId64 " after %" PRId64 " conflicts",
lim.rephase, delta);
// This will trigger to report the effect of this new set of phases at the
// 'backtrack' (actually 'update_target_and_best') after the next
// conflict, as well as resetting 'best_assigned' then to allow to compute
// a new "best" assignment at that point.
//
last.rephase.conflicts = stats.conflicts;
rephased = type;
if (stable)
shuffle_scores ();
else
shuffle_queue ();
}
} // namespace CaDiCaL