-
Notifications
You must be signed in to change notification settings - Fork 2
/
preencoder.cpp
430 lines (368 loc) · 10.4 KB
/
preencoder.cpp
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
#include "preencoder.h"
#include <vector>
using namespace PBLib;
using namespace std;
template <class PBCon>
void PreEncoder::init_and_normalize(PBCon const & pbconstraint, ClauseDatabase& formula)
{
literals = pbconstraint.getWeightedLiterals();
comparator = pbconstraint.getComparator();
max_sum = 0;
max_weight = 0;
bound_offset = 0;
type = PB;
n = literals.size();
isAMK = false;
isAMKEqual = false;
leq = pbconstraint.getLeq();
geq = pbconstraint.getGeq();
if (comparator == GEQ)
{ // normalize to LEQ by negation
comparator = LEQ;
leq = -geq;
for (int i = 0; i < (int)literals.size(); ++i)
literals[i].weight = -literals[i].weight;
}
join_duplicat_literals();
normalize_variables();
leq += bound_offset;
geq += bound_offset;
remove_lits_with_w_greater_leq_and_check_isamk(formula);
}
void PreEncoder::remove_lits_with_w_greater_leq_and_check_isamk(ClauseDatabase& formula)
{
isAMK = true;
isAMKEqual = true;
check_amk_equal = 0;
if (literals.size() > 0)
{
check_amk_equal = literals[0].weight;
}
else
{
isAMKEqual = false;
isAMK = false;
}
for (int i = 0; i < (int)literals.size(); ++i)
{
tmpWeight = literals[i].weight;
if(tmpWeight > leq)
{
formula.addClause(-literals[i].lit);
literals[i] = literals[literals.size() - 1];
literals.pop_back();
i--;
n--;
continue;
}
max_sum += tmpWeight;
if (isAMK && tmpWeight != 1)
{
isAMK = false;
}
if (isAMKEqual && tmpWeight != check_amk_equal)
{
isAMKEqual = false;
}
if (tmpWeight > max_weight)
max_weight = tmpWeight;
}
if (check_amk_equal == 0)
isAMKEqual = false;
}
void PreEncoder::normalize_variables()
{
for (int i = 0; i < (int)literals.size(); ++i)
{
tmpWeight = literals[i].weight;
if ( tmpWeight == 0) // ignore literals with weight 0
{
literals[i] = literals[literals.size() - 1];
literals.pop_back();
i--;
n--;
continue;
}
if (tmpWeight < 0) // if the weight is negative, normalize literal
{
tmpWeight = -tmpWeight; // results in a positive weight
bound_offset += tmpWeight; // adjust bound offset (for leq / geq lateron)
literals[i].weight = tmpWeight; // save new (positive) weight
literals[i].lit = -literals[i].lit; // save the complement of the literal
}
}
}
void PreEncoder::join_duplicat_literals()
{
sort(literals.begin(), literals.end(),WeightedLit::compVariable_des_var);
vector<int32_t> del_element;
for (int i = 1; i < literals.size(); ++i)
{
if (literals[i].lit == literals[i-1].lit)
{
literals[i].weight += literals[i-1].weight;
del_element.push_back(i-1);
}
else if (literals[i].lit == -literals[i-1].lit)
{
bound_offset -= literals[i-1].weight;
literals[i].weight += -literals[i-1].weight;
del_element.push_back(i-1);
}
}
for (int i = del_element.size() - 1; i >= 0; --i)
{
// no delete duplicates
literals[del_element[i]] = literals.back();
literals.pop_back();
n--;
}
}
void PreEncoder::check_for_trivial_constraints(ClauseDatabase & formula)
{
assert(literals.size() == n);
if (leq < 0 || (comparator == BOTH && leq < geq) || (comparator == BOTH && max_sum < geq))
{
type = DONTCARE;
formula.addUnsat();
stats->num_trivial++;
}
else if (n == 0)
{
if (comparator == BOTH && geq > 0)
formula.addUnsat();
type = DONTCARE;
stats->num_trivial++;
max_weight = 0;
max_sum = 0;
}
else if (leq == 0)
{
// in the incremental case we will detected leq < 0 in the encodeNewLeq methode of IncSimplePBConstraint
// set all literals to false to satisfied == 0
type = DONTCARE;
stats->num_trivial++;
for (int i = 0; i < (int)literals.size(); ++i)
{
formula.addClause(-literals[i].lit);
}
literals.clear();
n = 0;
max_weight = 0;
max_sum = 0;
}
}
void PreEncoder::sort_literals()
{
sort(literals.begin(), literals.end(),WeightedLit::compVariable_des);
// sort(literals.begin(), literals.end(),WeightedLit::compVariable_asc);
}
// WARNING DONT TOUCH THIS, THIS HAVE TO BE REWRITTEN (TO MANY DEPENDENCIES) TODO
shared_ptr< IncSimplePBConstraint > PreEncoder::preEncodeIncPBConstraint(IncPBConstraint & pbconstraint, ClauseDatabase& formula)
{
formula.addConditionals(pbconstraint.getConditionals());
init_and_normalize(pbconstraint, formula);
check_for_trivial_constraints(formula);
if (type != DONTCARE)
{
if (isAMK)
{
if (leq == 1)
{
type = AMO;
stats->num_amo++;
}
else
{
type = AMK;
stats->num_amk++;
}
}
else
{
type = PB;
stats->num_pb++;
}
sort_literals();
}
for (int i = 0; i < pbconstraint.getConditionals().size(); ++i)
formula.getConditionals().pop_back();
if (leq > max_sum)
leq = max_sum;
if (comparator == LEQ)
{
shared_ptr<IncSimplePBConstraint> incSimplePbConstraint = make_shared<IncSimplePBConstraint>(IncSimplePBConstraint(max_sum, max_weight, bound_offset, type, literals, LEQ, leq));
pbconstraint.setIncSimplePBConstraint(incSimplePbConstraint);
incSimplePbConstraint->addConditionals(pbconstraint.getConditionals());
return incSimplePbConstraint;
}
else
{
if (geq < 0)
geq = 0;
assert (comparator == BOTH);
shared_ptr<IncSimplePBConstraint> incSimplePbConstraint = make_shared<IncSimplePBConstraint>(IncSimplePBConstraint(max_sum, max_weight, bound_offset, type, literals, comparator, leq, geq));
pbconstraint.setIncSimplePBConstraint(incSimplePbConstraint);
incSimplePbConstraint->addConditionals(pbconstraint.getConditionals());
return incSimplePbConstraint;
}
}
// WARNING DONT TOUCH THIS, THIS HAVE TO BE REWRITTEN (TO MANY DEPENDENCIES) TODO
SimplePBConstraint PreEncoder::preEncodePBConstraint(const PBConstraint& pbconstraint, ClauseDatabase& formula)
{
formula.addConditionals(pbconstraint.getConditionals());
init_and_normalize(pbconstraint, formula);
check_for_trivial_constraints(formula);
if (type == DONTCARE)
; // we already encode this constraint and continue below
else if (comparator == BOTH && geq == max_sum) // note that the following trivial constraints cannot used for incremental constraints
{
// set all literals to true to satisfied == 0
type = DONTCARE;
stats->num_trivial++;
for (int i = 0; i < (int)literals.size(); ++i)
{
formula.addClause(literals[i].lit);
}
}
else if (max_sum <= leq)
{
if (comparator == LEQ)
{
stats->num_trivial++;
type = DONTCARE;
}
else
{
assert(comparator == BOTH);
PBConstraint c(literals, GEQ, geq);
c.addConditionals(pbconstraint.getConditionals());
for (int i = 0; i < pbconstraint.getConditionals().size(); ++i)
formula.getConditionals().pop_back();
return preEncodePBConstraint(c, formula);
}
}
else if (n == 2 && comparator == LEQ && max_sum > leq)
{
// since maxsum > leq and n = 2 we know that this is an AMO and with n= 2 that this is a clause
type = DONTCARE;
clause.clear();
for (int i = 0; i < (int) literals.size(); ++i)
{
clause.push_back(-literals[i].lit);
}
formula.addClause(clause);
stats->num_clause++;
}
else
{
// no trivial constraint found ... trying to simplify
if (comparator == BOTH && ((max_sum - leq) < geq) && ((max_sum - geq) < leq) )
{
// both bounds getting smaller if we negate the formula
for (int i = 0; i < (int)literals.size(); ++i)
{
literals[i].lit = -literals[i].lit;
}
// we can simply use max_sum as bound offset, since we know that all weights are > 0
// but we have to change leq in geq and vice versa, because of the negation
int64_t oldGeq = geq;
geq = max_sum - leq;
leq = max_sum - oldGeq;
// we have changed k so we have to readjust the variables
PBConstraint c(literals, BOTH, leq, geq);
c.addConditionals(pbconstraint.getConditionals());
for (int i = 0; i < pbconstraint.getConditionals().size(); ++i)
formula.getConditionals().pop_back();
return preEncodePBConstraint(c, formula);
}
if (comparator == BOTH && geq <= 0)
comparator = LEQ; // since we know that all weights > 0
if (comparator == BOTH && geq == 1)
{
// >= 1 is a clause since all weights are > 0 ... we simply add this clause and remove GEQ constraint
comparator = LEQ;
clause.clear();
for (int i = 0; i < (int) literals.size(); ++i)
{
clause.push_back(literals[i].lit);
}
formula.addClause(clause);
stats->num_clause++;
comparator = LEQ;
// we have change the comparator so we can try to find new simplifications
PBConstraint c(literals, LEQ, leq);
c.addConditionals(pbconstraint.getConditionals());
for (int i = 0; i < pbconstraint.getConditionals().size(); ++i)
formula.getConditionals().pop_back();
return preEncodePBConstraint(c, formula);
}
if (isAMK)
{
// it should not happen that comparator is BOTH and n == leq + 1, since is propably handled already be the two previouse simplification
if (comparator == LEQ && n == leq + 1)
{
// this is a clause
type = DONTCARE;
clause.clear();
for (int i = 0; i < (int) literals.size(); ++i)
{
clause.push_back(-literals[i].lit);
}
formula.addClause(clause);
stats->num_clause++;
}
else
{
if (leq == 1)
{
stats->num_amo++;
type = AMO;
}
else
{
stats->num_amk++;
type = AMK;
}
}
}
else
{
type = PB;
stats->num_pb++;
}
// NOTE that the XOR (a + b = 1) case is already handled by the previouse leq and geq clause detactions
sort_literals();
}
for (int i = 0; i < pbconstraint.getConditionals().size(); ++i)
formula.getConditionals().pop_back();
if (comparator == LEQ)
{
SimplePBConstraint sc(max_sum, max_weight, type, literals, comparator, leq);
sc.addConditionals(pbconstraint.getConditionals());
return sc;
}
else
{
SimplePBConstraint sc(max_sum, max_weight,type, literals, comparator, leq, geq);
sc.addConditionals(pbconstraint.getConditionals());
return sc;
}
}
PreEncoder::PreEncoder(PBConfig config, statistic* _stats) : config(config), stats(_stats)
{
if (stats == 0)
{
stats = new statistic;
private_stats = true;
}
else
{
private_stats = false;
}
}
PreEncoder::~PreEncoder()
{
if (private_stats)
delete stats;
}