-
Notifications
You must be signed in to change notification settings - Fork 2
/
Alg_LinearSU.cc
469 lines (395 loc) · 15.1 KB
/
Alg_LinearSU.cc
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*!
* \author Ruben Martins - ruben@sat.inesc-id.pt
*
* @section LICENSE
*
* Open-WBO, Copyright (c) 2013-2017, Ruben Martins, Vasco Manquinho, Ines Lynce
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include "Alg_LinearSU.h"
#define MAX_CLAUSES 3000000
using namespace openwbo;
/************************************************************************************************
//
// Linear Search Algorithm with Boolean Multilevel Optimization (BMO)
//
************************************************************************************************/
/*_________________________________________________________________________________________________
|
| bmoSearch : [void] -> [void]
|
| Description:
|
| Linear search algorithm with lexicographical optimization.
|
| For further details see:
| * Joao Marques-Silva, Josep Argelich, Ana Graça, Ines Lynce: Boolean
| lexicographic optimization: algorithms & applications. Ann. Math.
| Artif. Intell. 62(3-4): 317-343 (2011)
|
| Post-conditions:
| * 'lbCost' is updated.
| * 'ubCost' is updated.
| * 'nbSatisfiable' is updated.
| * 'nbCores' is updated.
|
|________________________________________________________________________________________________@*/
void LinearSU::bmoSearch() {
assert(orderWeights.size() > 0);
lbool res = l_True;
initRelaxation();
uint64_t currentWeight = orderWeights[0];
uint64_t minWeight = orderWeights[orderWeights.size() - 1];
int posWeight = 0;
vec<vec<Lit>> functions;
vec<int> weights;
solver = rebuildBMO(functions, weights, currentWeight);
uint64_t localCost = 0;
ubCost = 0;
for (;;) {
vec<Lit> dummy;
// Do not use preprocessing for linear search algorithm.
// NOTE: When preprocessing is enabled the SAT solver simplifies the
// relaxation variables which leads to incorrect results.
res = searchSATSolver(solver, dummy);
if (res == l_True) {
nbSatisfiable++;
uint64_t newCost = computeCostModel(solver->model, currentWeight);
if (currentWeight == minWeight) {
// If current weight is the same as the minimum weight, then we are in
// the last lexicographical function.
saveModel(solver->model);
printf("o %" PRId64 "\n", newCost + lbCost + off_set);
ubCost = newCost + lbCost;
} else {
if (verbosity > 0)
printf("c BMO-UB : %-12" PRIu64 "\t (Function %d/%d)\n", newCost,
posWeight + 1, (int)orderWeights.size());
}
if (newCost == 0 && currentWeight == minWeight) {
// Optimum value has been found.
printAnswer(_OPTIMUM_);
exit(_OPTIMUM_);
} else {
if (newCost == 0) {
functions.push();
new (&functions[functions.size() - 1]) vec<Lit>();
objFunction.copyTo(functions[functions.size() - 1]);
localCost = newCost;
weights.push(localCost / currentWeight);
posWeight++;
currentWeight = orderWeights[posWeight];
localCost = 0;
delete solver;
solver = rebuildBMO(functions, weights, currentWeight);
if (verbosity > 0)
printf("c LB : %-12" PRIu64 "\n", lbCost);
} else {
// Optimization of the current lexicographical function.
if (localCost == 0)
encoder.encodeCardinality(solver, objFunction,
newCost / currentWeight - 1);
else
encoder.updateCardinality(solver, newCost / currentWeight - 1);
localCost = newCost;
}
}
} else {
nbCores++;
if (currentWeight == minWeight) {
// There are no more functions to be optimized.
if (model.size() == 0) {
assert(nbSatisfiable == 0);
// If no model was found then the MaxSAT formula is unsatisfiable
printAnswer(_UNSATISFIABLE_);
exit(_UNSATISFIABLE_);
} else {
printAnswer(_OPTIMUM_);
exit(_OPTIMUM_);
}
} else {
// The current lexicographical function has been optimize. Go to the
// next lexicographical function.
functions.push();
new (&functions[functions.size() - 1]) vec<Lit>();
objFunction.copyTo(functions[functions.size() - 1]);
weights.push(localCost / currentWeight);
lbCost += localCost;
posWeight++;
currentWeight = orderWeights[posWeight];
localCost = 0;
delete solver;
solver = rebuildBMO(functions, weights, currentWeight);
if (verbosity > 0)
printf("c LB : %-12" PRIu64 "\n", lbCost);
}
}
}
}
/*_________________________________________________________________________________________________
|
| normalSearch : [void] -> [void]
|
| Description:
|
| Linear search algorithm.
|
| For further details see:
| * Daniel Le Berre, Anne Parrain: The Sat4j library, release 2.2. JSAT
| 7(2-3): 59-6 (2010)
| * Miyuki Koshimura, Tong Zhang, Hiroshi Fujita, Ryuzo Hasegawa: QMaxSAT:
| A Partial Max-SAT Solver. JSAT 8(1/2): 95-100 (2012)
|
| Post-conditions:
| * 'ubCost' is updated.
| * 'nbSatisfiable' is updated.
| * 'nbCores' is updated.
|
|________________________________________________________________________________________________@*/
void LinearSU::normalSearch() {
lbool res = l_True;
initRelaxation();
solver = rebuildSolver();
while (res == l_True) {
vec<Lit> dummy;
// Do not use preprocessing for linear search algorithm.
// NOTE: When preprocessing is enabled the SAT solver simplifies the
// relaxation variables which leads to incorrect results.
res = searchSATSolver(solver, dummy);
if (res == l_True) {
nbSatisfiable++;
uint64_t newCost = computeCostModel(solver->model);
saveModel(solver->model);
if (maxsat_formula->getFormat() == _FORMAT_PB_) {
// optimization problem
if (maxsat_formula->getObjFunction() != NULL) {
printf("o %" PRId64 "\n", newCost + off_set);
}
} else
printf("o %" PRId64 "\n", newCost + off_set);
if (newCost == 0) {
// If there is a model with value 0 then it is an optimal model
ubCost = newCost;
if (maxsat_formula->getFormat() == _FORMAT_PB_ &&
maxsat_formula->getObjFunction() == NULL) {
printAnswer(_SATISFIABLE_);
exit(_SATISFIABLE_);
} else {
printAnswer(_OPTIMUM_);
exit(_OPTIMUM_);
}
} else {
if (maxsat_formula->getProblemType() == _WEIGHTED_) {
if (!encoder.hasPBEncoding()){
// check if we can encode with GTE
encoder.setPBEncoding(_PB_GTE_);
int expected_clauses = encoder.predictPB(solver, objFunction, coeffs, newCost-1);
printf("c GTE auxiliary #clauses = %d\n",expected_clauses);
if (expected_clauses >= MAX_CLAUSES) {
printf("c Warn: changing to Adder encoding.\n");
encoder.setPBEncoding(_PB_ADDER_);
}
encoder.encodePB(solver, objFunction, coeffs, newCost - 1);
}
else
encoder.updatePB(solver, newCost - 1);
} else {
// Unweighted.
if (!encoder.hasCardEncoding())
encoder.encodeCardinality(solver, objFunction, newCost - 1);
else
encoder.updateCardinality(solver, newCost - 1);
}
ubCost = newCost;
}
} else {
nbCores++;
if (model.size() == 0) {
assert(nbSatisfiable == 0);
// If no model was found then the MaxSAT formula is unsatisfiable
printAnswer(_UNSATISFIABLE_);
exit(_UNSATISFIABLE_);
} else {
printAnswer(_OPTIMUM_);
exit(_OPTIMUM_);
}
}
}
}
// Public search method
void LinearSU::search() {
if (maxsat_formula->getProblemType() == _WEIGHTED_)
is_bmo = isBMO();
printConfiguration(is_bmo, maxsat_formula->getProblemType());
if (maxsat_formula->getProblemType() == _WEIGHTED_) {
if (bmoMode && is_bmo)
bmoSearch();
else
normalSearch();
} else
normalSearch();
}
/************************************************************************************************
//
// Rebuild MaxSAT solver
//
************************************************************************************************/
/*_________________________________________________________________________________________________
|
| rebuildSolver : (minWeight : int) -> [Solver *]
|
| Description:
|
| Rebuilds a SAT solver with the current MaxSAT formula.
| If a weight is specified, then it only considers soft clauses with weight
| smaller than the specified weight.
| NOTE: a weight is specified in the 'bmo' approach.
|
|________________________________________________________________________________________________@*/
Solver *LinearSU::rebuildSolver(uint64_t min_weight) {
vec<bool> seen;
seen.growTo(maxsat_formula->nVars(), false);
Solver *S = newSATSolver();
for (int i = 0; i < maxsat_formula->nVars(); i++)
newSATVariable(S);
for (int i = 0; i < maxsat_formula->nHard(); i++)
S->addClause(maxsat_formula->getHardClause(i).clause);
for (int i = 0; i < maxsat_formula->nPB(); i++) {
Encoder *enc = new Encoder(_INCREMENTAL_NONE_, _CARD_MTOTALIZER_,
_AMO_LADDER_, _PB_GTE_);
// Make sure the PB is on the form <=
// if (maxsat_formula->getPBConstraint(i)->_sign)
// maxsat_formula->getPBConstraint(i)->changeSign();
assert(maxsat_formula->getPBConstraint(i)->_sign);
enc->encodePB(S, maxsat_formula->getPBConstraint(i)->_lits,
maxsat_formula->getPBConstraint(i)->_coeffs,
maxsat_formula->getPBConstraint(i)->_rhs);
delete enc;
}
for (int i = 0; i < maxsat_formula->nCard(); i++) {
Encoder *enc = new Encoder(_INCREMENTAL_NONE_, _CARD_MTOTALIZER_,
_AMO_LADDER_, _PB_GTE_);
if (maxsat_formula->getCardinalityConstraint(i)->_rhs == 1) {
enc->encodeAMO(S, maxsat_formula->getCardinalityConstraint(i)->_lits);
} else {
enc->encodeCardinality(S,
maxsat_formula->getCardinalityConstraint(i)->_lits,
maxsat_formula->getCardinalityConstraint(i)->_rhs);
}
delete enc;
}
vec<Lit> clause;
for (int i = 0; i < maxsat_formula->nSoft(); i++) {
if (maxsat_formula->getSoftClause(i).weight < min_weight)
continue;
clause.clear();
maxsat_formula->getSoftClause(i).clause.copyTo(clause);
for (int j = 0; j < maxsat_formula->getSoftClause(i).relaxation_vars.size();
j++) {
clause.push(maxsat_formula->getSoftClause(i).relaxation_vars[j]);
}
S->addClause(clause);
}
return S;
}
/*_________________________________________________________________________________________________
|
| rebuildBMO : (functions : int) -> [Solver *]
|
| Description:
|
| Rebuilds a SAT solver with the current MaxSAT formula.
| Only considers soft clauses with the weight of the current
| lexicographical optimization weight ('currentWeight')
| For each function already computed in the BMO algorithm it encodes the
| respective cardinality constraint.
|
|________________________________________________________________________________________________@*/
Solver *LinearSU::rebuildBMO(vec<vec<Lit>> &functions, vec<int> &rhs,
uint64_t currentWeight) {
assert(functions.size() == rhs.size());
Solver *S = rebuildSolver(currentWeight);
objFunction.clear();
coeffs.clear();
for (int i = 0; i < maxsat_formula->nSoft(); i++) {
if (maxsat_formula->getSoftClause(i).weight == currentWeight) {
objFunction.push(maxsat_formula->getSoftClause(i).relaxation_vars[0]);
coeffs.push(maxsat_formula->getSoftClause(i).weight);
}
}
for (int i = 0; i < functions.size(); i++)
encoder.encodeCardinality(S, functions[i], rhs[i]);
return S;
}
/************************************************************************************************
//
// Other protected methods
//
************************************************************************************************/
/*_________________________________________________________________________________________________
|
| initRelaxation : (objective : vec<Lit>&) (weights : vec<int>&) -> [void]
|
| Description:
|
| Initializes the relaxation variables by adding a fresh variable to the
| 'relaxationVars' of each soft clause.
|
| Post-conditions:
| * 'objFunction' contains all relaxation variables that were added to soft
| clauses.
| * 'coeffs' contains the weights of all soft clauses.
|
|________________________________________________________________________________________________@*/
void LinearSU::initRelaxation() {
for (int i = 0; i < maxsat_formula->nSoft(); i++) {
Lit l = maxsat_formula->newLiteral();
maxsat_formula->getSoftClause(i).relaxation_vars.push(l);
objFunction.push(l);
coeffs.push(maxsat_formula->getSoftClause(i).weight);
}
}
// Print LinearSU configuration.
void LinearSU::print_LinearSU_configuration() {
printf("c | Algorithm: %23s "
" |\n",
"LinearSU");
if (maxsat_formula->getProblemType() == _WEIGHTED_) {
if (bmoMode)
printf("c | BMO strategy: %20s "
" |\n",
"On");
else
printf("c | BMO strategy: %20s "
" |\n",
"Off");
if (bmoMode) {
if (is_bmo)
printf("c | BMO search: %22s "
" |\n",
"Yes");
else
printf("c | BMO search: %22s "
" |\n",
"No");
}
}
}