-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathsolvertypesmini.h
More file actions
602 lines (514 loc) · 15.7 KB
/
solvertypesmini.h
File metadata and controls
602 lines (514 loc) · 15.7 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/******************************************
Copyright (C) 2009-2020 Authors of CryptoMiniSat, see AUTHORS file
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.
***********************************************/
#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <memory>
#include <vector>
#include <gmpxx.h>
namespace CMSat {
constexpr uint32_t var_Undef(0xffffffffU >> 4);
class TooManyVarsError {};
class TooLongClauseError {};
class Lit
{
uint32_t x;
constexpr explicit Lit(uint32_t i) : x(i) { }
public:
constexpr Lit() : x(var_Undef<<1) {} // (lit_Undef)
constexpr explicit Lit(uint32_t var, bool is_inverted) :
x(var + var + is_inverted)
{}
constexpr const uint32_t& toInt() const { // Guarantees small, positive integers suitable for array indexing.
return x;
}
constexpr Lit operator~() const {
return Lit(x ^ 1);
}
constexpr Lit operator^(const bool b) const {
return Lit(x ^ static_cast<uint32_t>(b));
}
constexpr Lit& operator^=(const bool b) {
x ^= static_cast<uint32_t>(b);
return *this;
}
constexpr bool sign() const {
return x & 1;
}
constexpr uint32_t var() const {
return x >> 1;
}
constexpr Lit unsign() const {
return Lit(x & ~1U);
}
constexpr bool operator==(const Lit& p) const {
return x == p.x;
}
constexpr bool operator!= (const Lit& p) const {
return x != p.x;
}
/**
@brief ONLY to be used for ordering such as: a, b, ~b, etc.
*/
constexpr bool operator < (const Lit& p) const {
return x < p.x; // '<' guarantees that p, ~p are adjacent in the ordering.
}
constexpr bool operator > (const Lit& p) const {
return x > p.x;
}
constexpr bool operator >= (const Lit& p) const {
return x >= p.x;
}
constexpr static Lit toLit(uint32_t data)
{
return Lit(data);
}
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/) {
ar & x;
}
};
constexpr Lit lit_Undef(var_Undef, false); // Useful special constants.
constexpr Lit lit_Error(var_Undef, true ); //
inline std::ostream& operator<<(std::ostream& os, const Lit lit)
{
if (lit == lit_Undef) {
os << "lit_Undef";
} else {
os << (lit.sign() ? "-" : "") << (lit.var() + 1);
}
return os;
}
inline std::ostream& operator<<(std::ostream& co, const std::vector<Lit>& lits)
{
for (size_t i = 0; i < lits.size(); i++) {
if (i > 0) co << " ";
co << lits[i];
}
return co;
}
class lbool {
uint8_t value;
public:
constexpr explicit lbool(uint8_t v) : value(v) { }
constexpr lbool() : value(0) { }
constexpr explicit lbool(bool x) : value(!x) { }
constexpr bool operator == (lbool b) const {
return ((b.value & 2) & (value & 2)) | (!(b.value & 2) & (value == b.value));
}
constexpr bool operator != (lbool b) const {
return !(*this == b);
}
constexpr lbool operator ^ (bool b) const {
return lbool(static_cast<uint8_t>(value ^ static_cast<uint8_t>(b)));
}
constexpr lbool operator && (lbool b) const {
uint8_t sel = (value << 1) | (b.value << 3);
uint8_t v = (0xF7F755F4 >> sel) & 3;
return lbool(v);
}
constexpr lbool operator || (lbool b) const {
uint8_t sel = (value << 1) | (b.value << 3);
uint8_t v = (0xFCFCF400 >> sel) & 3;
return lbool(v);
}
template<class Archive>
void serialize(Archive& ar, const unsigned int /*version*/) {
ar & value;
}
constexpr uint8_t getValue() const { return value; }
friend lbool toLbool(uint8_t v);
constexpr friend uint32_t toInt (lbool l);
};
constexpr lbool l_True = lbool((uint8_t)0);
constexpr lbool l_False = lbool((uint8_t)1);
constexpr lbool l_Undef = lbool((uint8_t)2);
inline lbool toLbool(uint8_t v)
{
lbool l;
l.value = v;
return l;
}
constexpr inline uint32_t toInt (lbool l)
{
return l.value;
}
constexpr lbool boolToLBool(const bool b)
{
return b ? l_True : l_False;
}
inline std::ostream& operator<<(std::ostream& cout, const lbool val)
{
if (val == l_True) cout << "l_True";
if (val == l_False) cout << "l_False";
if (val == l_Undef) cout << "l_Undef";
return cout;
}
class OrGate {
public:
template<typename T>
OrGate(const Lit& _rhs, const T& _lits, int32_t _ID) :
lits(_lits), rhs(_rhs), id(_ID)
{
assert(std::is_sorted(lits.begin(), lits.end()));
}
bool operator==(const OrGate& other) const
{
return rhs == other.rhs && lits == other.lits;
}
const std::vector<Lit>& get_lhs() const
{
return lits;
}
//LHS
std::vector<Lit> lits;
//RHS
Lit rhs;
//ID of long clause
int32_t id;
};
class ITEGate {
public:
ITEGate() {}
ITEGate(const Lit& _rhs, Lit _lit1, Lit _lit2, Lit _lit3) :
rhs(_rhs)
{
lhs[0] = _lit1;
lhs[1] = _lit2;
lhs[2] = _lit3;
std::sort(lhs.begin(), lhs.end());
}
std::array<Lit, 4> get_all() const
{
return std::array<Lit, 4>{{lhs[0], lhs[1], lhs[2], rhs}};
}
//LHS
std::array<Lit, 3> lhs;
//RHS
Lit rhs;
};
enum class PolarityMode {
polarmode_pos
, polarmode_neg
, polarmode_rnd
, polarmode_automatic
, polarmode_stable
, polarmode_best_inv
, polarmode_best
, polarmode_saved
, polarmode_weighted
};
enum class rst_dat_type {norm, var, cl};
struct FastBackwData {
std::vector<Lit>* _assumptions = nullptr;
std::vector<uint32_t>* indic_to_var = nullptr;
uint32_t orig_num_vars = 0;
std::vector<uint32_t>* non_indep_vars = nullptr;
std::vector<uint32_t>* indep_vars = nullptr;
bool fast_backw_on = false;
uint32_t* test_var = nullptr;
uint32_t* test_indic = nullptr;
uint32_t max_confl = 500;
uint32_t cur_max_confl = 0;
uint32_t indep_because_ran_out_of_confl = 0;
uint64_t start_sumConflicts;
};
class BNN
{
public:
typedef Lit* iterator;
typedef const Lit* const_iterator;
BNN()
{}
explicit BNN(
const std::vector<Lit>& _in,
const int32_t _cutoff,
const Lit _out):
cutoff(_cutoff),
out (_out)
{
if (out == lit_Undef) {
set = true;
}
assert(_in.size() > 0);
undefs = _in.size();
ts = 0;
sz = _in.size();
std::copy(_in.begin(), _in.end(), getData());
}
Lit* getData()
{
return reinterpret_cast<Lit*>(reinterpret_cast<char*>(this) + sizeof(BNN));
}
const Lit* getData() const
{
return reinterpret_cast<const Lit*>(reinterpret_cast<const char*>(this) + sizeof(BNN));
}
const Lit& operator[](const uint32_t at) const
{
return getData()[at];
}
Lit& operator[](const uint32_t at)
{
return getData()[at];
}
const Lit& get_out() const
{
return out;
}
const Lit* begin() const
{
return getData();
}
Lit* begin()
{
return getData();
}
const Lit* end() const
{
return getData()+size();
}
Lit* end()
{
return getData()+size();
}
bool empty() const
{
return sz == 0;
}
void resize(uint32_t _sz) {
sz = _sz;
}
uint32_t size() const {
return sz;
}
int32_t cutoff;
Lit out = lit_Undef;
bool set = false;
bool isRemoved = false;
int32_t ts = 0;
int32_t undefs = 0;
uint32_t sz;
};
inline std::ostream& operator<<(std::ostream& os, const BNN& bnn)
{
for (uint32_t i = 0; i < bnn.size(); i++) {
os << "lit[" << bnn[i] << "]";
if (i+1 < bnn.size())
os << " + ";
}
os << " >= " << bnn.cutoff;
if (!bnn.set)
os << " <-> " << bnn.out;
os << " [size: " << bnn.size() << "]";
return os;
}
class Field {
public:
virtual ~Field() = default;
// Virtual methods for field operations
virtual Field& operator=(const Field& other) = 0;
virtual Field& operator+=(const Field& other) = 0;
virtual std::unique_ptr<Field> add(const Field& other) = 0;
virtual Field& operator-=(const Field& other) = 0;
virtual Field& operator*=(const Field& other) = 0;
virtual Field& operator/=(const Field& other) = 0;
virtual bool operator==(const Field&) const = 0;
virtual bool is_zero() const = 0;
virtual bool is_one() const = 0;
virtual void set_zero() = 0;
virtual void set_one() = 0;
virtual uint64_t bytes_used() const = 0;
virtual std::unique_ptr<Field> dup() const = 0;
virtual bool parse(const std::string& str, const uint32_t line_no) = 0;
// A method to display the value (for demonstration purposes)
virtual std::ostream& display(std::ostream& os) const = 0;
static void skip_whitespace(const std::string& str, uint32_t& at) {
char c = str[at];
while (c == '\t' || c == '\r' || c == ' ') {
at++;
c = str[at];
}
}
static mpz_class parse_sign(const std::string& str, uint32_t& at) {
mpz_class sign = 1;
if (str[at] == '-') {sign = -1; at++;}
else if (str[at] == '+') {sign = 1; at++;}
return sign;
}
static bool parse_int(mpz_class& ret, const std::string& str, uint32_t& at,
const size_t line_num, int* len = nullptr) {
mpz_class val = 0;
skip_whitespace(str, at);
char c = str[at];
if (c < '0' || c > '9') {
std::cerr
<< "PARSE ERROR! Unexpected char (dec: '" << c << ")"
<< " At line " << line_num
<< " we expected a number"
<< std::endl;
return false;
}
while (c >= '0' && c <= '9') {
if (len) (*len)++;
mpz_class val2 = val*10 + (c - '0');
if (val2 < val) {
std::cerr << "PARSE ERROR! At line " << line_num
<< " the variable number is to high"
<< std::endl;
return false;
}
val = val2;
c = str[++at];
}
ret = val;
return true;
}
static bool check_end_of_weight(const std::string& str, uint32_t& at, const uint32_t line_no) {
skip_whitespace(str, at);
if (str[at] != '0') {
std::cerr << "PARSE ERROR! expected 0 at the end of the weight."
<< "At line " << line_no
<< " -- did you forget to set the mode?" << std::endl;
return false;
}
at++;
skip_whitespace(str, at);
if (at < str.size() && str[at] != '\n') {
std::cerr << "PARSE ERROR! expected end of line at the end of the weight."
<< "At line " << line_no
<< " -- did you forget to set the mode?" << std::endl;
return false;
}
return true;
}
};
inline std::ostream& operator<<(std::ostream& os, const Field& f) {
return f.display(os);
}
class FieldGen {
public:
virtual ~FieldGen() = default;
virtual std::unique_ptr<Field> zero() const = 0;
virtual std::unique_ptr<Field> one() const = 0;
virtual std::unique_ptr<FieldGen> dup() const = 0;
virtual bool larger_than(const Field&, const Field&) const = 0;
virtual bool weighted() const = 0;
virtual bool exact() const = 0;
};
class FDouble final : public Field {
public:
double val;
FDouble(const double _val) : val(_val) {}
FDouble(const FDouble& other) : val(other.val) {}
Field& operator=(const Field& other) final {
const auto& od = static_cast<const FDouble&>(other);
val = od.val;
return *this;
}
Field& operator+=(const Field& other) final {
const auto& od = static_cast<const FDouble&>(other);
val += od.val;
return *this;
}
std::unique_ptr<Field> add(const Field& other) final {
const auto& od = static_cast<const FDouble&>(other);
return std::make_unique<FDouble>(val + od.val);
}
Field& operator-=(const Field& other) final {
const auto& od = static_cast<const FDouble&>(other);
val -= od.val;
return *this;
}
Field& operator*=(const Field& other) final {
const auto& od = static_cast<const FDouble&>(other);
val *= od.val;
return *this;
}
Field& operator/=(const Field& other) final {
const auto& od = static_cast<const FDouble&>(other);
if (od.val == 0) throw std::runtime_error("Division by zero");
val /= od.val;
return *this;
}
bool operator==(const Field& other) const final {
const auto& od = static_cast<const FDouble&>(other);
return od.val == val;
}
std::ostream& display(std::ostream& os) const final {
os << val;
return os;
}
std::unique_ptr<Field> dup() const final {
return std::make_unique<FDouble>(val);
}
bool is_zero() const final { return val == 0; }
bool is_one() const final { return val == 1; }
void set_zero() final { val = 0; }
void set_one() final { val = 1; }
uint64_t bytes_used() const final { return sizeof(FDouble); }
bool parse(const std::string& str, const uint32_t line_no) final {
mpz_class head;
mpz_class mult;
uint32_t at = 0;
auto sign = parse_sign(str, at);
parse_int(head, str, at, line_no);
mpq_class vald;
if (str[at] == '.') {
at++;
mpz_class tail;
int len = 0;
if (!parse_int(tail, str, at, line_no, &len)) return false;
mpz_class ten(10);
mpz_ui_pow_ui(ten.get_mpz_t(), 10, len);
mpq_class tenq(ten);
mpq_class tailq(tail);
vald = head + tailq/tenq;
} else {
vald = head;
}
vald *= sign;
val = vald.get_d();
return check_end_of_weight(str, at, line_no);
}
};
class FGenDouble final : public FieldGen {
public:
~FGenDouble() final = default;
std::unique_ptr<Field> zero() const final {
return std::make_unique<FDouble>(0);
}
std::unique_ptr<Field> one() const final {
return std::make_unique<FDouble>(1.0);
}
std::unique_ptr<FieldGen> dup() const final {
return std::make_unique<FGenDouble>();
}
bool larger_than(const Field& a, const Field& b) const final {
const auto& ad = static_cast<const FDouble&>(a);
const auto& bd = static_cast<const FDouble&>(b);
return ad.val > bd.val;
}
bool weighted() const final { return true; }
bool exact() const final { return false; }
};
}