-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patharjun.h
More file actions
1466 lines (1282 loc) · 50.3 KB
/
arjun.h
File metadata and controls
1466 lines (1282 loc) · 50.3 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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************
Copyright (C) 2020 Mate Soos
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 <cstdint>
#include <cstdlib>
#include <functional>
#include <memory>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <fstream>
#include <gmpxx.h>
#include <cryptominisat5/solvertypesmini.h>
#include <cryptominisat5/cryptominisat.h>
#include <mpfr.h>
namespace ArjunNS {
class AIG;
class AIGManager;
class SimplifiedCNF;
using aig_ptr = std::shared_ptr<AIG>;
enum class AIGT {t_and, t_lit, t_const};
inline std::ostream& operator<<(std::ostream& os, const AIGT& value) {
switch (value) {
case AIGT::t_and: return os << "t_and";
case AIGT::t_lit: return os << "t_lit";
case AIGT::t_const: return os << "t_const";
default: assert(false && "Unknown AIGT"); exit(EXIT_FAILURE);
}
}
class AIG {
public:
AIG() = default;
~AIG() = default;
AIG(const AIG&) = delete;
AIG& operator=(const AIG&) = delete;
bool invariants() const {
if (type == AIGT::t_lit) {
if (l != nullptr || r != nullptr) std::cout << "ERROR: AIG literal has children!" << std::endl;
if (var == none_var) std::cout << "ERROR: AIG var node doesn't have a var!" << std::endl;
return l == nullptr && r == nullptr && var != none_var;
}
if (type == AIGT::t_const) {
if (l != nullptr || r != nullptr) std::cout << "ERROR: AIG const has children!" << std::endl;
if (var != none_var) std::cout << "ERROR: AIG const node has var!" << std::endl;
return l == nullptr && r == nullptr && var == none_var;
}
if (type == AIGT::t_and) {
if (var != none_var) std::cout << "ERROR: AIG AND node has var!" << std::endl;
if (l == nullptr || r == nullptr) std::cout << "ERROR: AIG AND node missing children!" << std::endl;
return l != nullptr && r != nullptr && var == none_var;
}
assert(false && "Unknown AIG type");
std::exit(EXIT_FAILURE);
}
// vals = input variable assignments
// aig = AIG to evaluate
// defs = known definitions of variables
static CMSat::lbool evaluate(const std::vector<CMSat::lbool>& vals, const aig_ptr& a, const std::vector<aig_ptr>& defs, std::map<aig_ptr, CMSat::lbool>& cache) {
std::function<CMSat::lbool(const aig_ptr&)> sub_eval = [&](const aig_ptr& aig) -> CMSat::lbool {
if (cache.count(aig)) return cache.at(aig);
assert(aig->invariants());
if (aig->type == AIGT::t_lit) {
if (defs[aig->var] != nullptr) {
auto ret = sub_eval(defs.at(aig->var));
if (ret == CMSat::l_Undef) {
cache[aig] = CMSat::l_Undef;
return CMSat::l_Undef;
}
ret = ret ^ aig->neg;
cache[aig] = ret;
return ret;
} else {
assert(aig->var < vals.size());
auto ret = vals[aig->var];
if (ret == CMSat::l_Undef) {
cache[aig] = CMSat::l_Undef;
return CMSat::l_Undef;
}
ret = ret ^ aig->neg;
cache[aig] = ret;
return ret;
}
}
if (aig->type == AIGT::t_const) return CMSat::boolToLBool(!aig->neg);
if (aig->type == AIGT::t_and) {
const auto l = sub_eval(aig->l);
const auto r = sub_eval(aig->r);
CMSat::lbool ret;
if (l ==CMSat::l_Undef && r == CMSat::l_Undef) ret = CMSat::l_Undef;
else if (l == CMSat::l_False) ret = CMSat::l_False ^ aig->neg;
else if (r == CMSat::l_False) ret = CMSat::l_False ^ aig->neg;
else ret = (l && r) ^ aig->neg;
cache[aig] = ret;
return ret;
}
assert(false && "Unknown AIG type");
exit(EXIT_FAILURE);
};
return sub_eval(a);
}
static aig_ptr new_lit(CMSat::Lit l) {
return new_lit(l.var(), l.sign());
}
static aig_ptr new_lit(uint32_t var, bool neg = false) {
auto ret = std::make_shared<AIG>();
ret->type = AIGT::t_lit;
ret->var = var;
ret->neg = neg;
return ret;
}
static aig_ptr new_ite(const aig_ptr& l, const aig_ptr& r, CMSat::Lit b) {
assert(l != nullptr);
assert(r != nullptr);
auto branch = new_lit(b.var(), b.sign());
return new_or(new_and(branch, l), new_and(new_not(branch), r));
}
static aig_ptr new_not(const aig_ptr& a) {
assert(a != nullptr);
auto ret = std::make_shared<AIG>();
ret->type = AIGT::t_and;
ret->l = a;
ret->r = a;
ret->neg = true;
return ret;
}
static aig_ptr new_and(const aig_ptr& l, const aig_ptr& r, bool neg = false) {
assert(l != nullptr && r != nullptr);
auto ret = std::make_shared<AIG>();
ret->type = AIGT::t_and;
ret->l = l;
ret->r = r;
ret->neg = neg;
return ret;
}
static aig_ptr new_or(const aig_ptr& l, const aig_ptr& r) {
assert(l != nullptr && r != nullptr);
auto ret = std::make_shared<AIG>();
ret->type = AIGT::t_and;
ret->neg = true;
ret->l = new_not(l);
ret->r = new_not(r);
return ret;
}
// Key for CSE: (type, var, neg, left_ptr, right_ptr)
using AIGKey = std::tuple<AIGT, uint32_t, bool, aig_ptr, aig_ptr>;
static aig_ptr new_ite(const aig_ptr& l, const aig_ptr& r, const aig_ptr& b) {
assert(l != nullptr);
assert(r != nullptr);
assert(b != nullptr);
return AIG::new_or(AIG::new_and(b, l), AIG::new_and(AIG::new_not(b), r));
}
static void get_dependent_vars(const aig_ptr& aig_orig, std::set<uint32_t>& dep, uint32_t v) {
std::set<aig_ptr> visited;
std::function<void(const aig_ptr&)> helper =
[&](const aig_ptr& aig) {
if (visited.count(aig)) return;
if (aig->type == AIGT::t_lit) {
assert(aig->var != v && "Variable cannot depend on itself");
dep.insert(aig->var);
}
if (aig->type == AIGT::t_and) {
helper(aig->l);
helper(aig->r);
}
visited.insert(aig);
};
helper(aig_orig);
}
static std::vector<aig_ptr> deep_clone_vec(const std::vector<aig_ptr>& aigs) {
std::vector<aig_ptr> ret;
std::map<aig_ptr, aig_ptr> cache;
ret.reserve(aigs.size());
for (auto& aig : aigs) {
if (aig == nullptr) {
ret.push_back(nullptr);
continue;
}
ret.push_back(deep_clone(aig, cache));
}
return ret;
}
template<typename T>
static T deep_clone_map(const T& aigs) {
T ret;
std::map<aig_ptr, aig_ptr> cache;
for (auto& [x, aig] : aigs) ret[x] = deep_clone(aig, cache);
return ret;
}
static aig_ptr deep_clone(const aig_ptr& aig, std::map<aig_ptr, aig_ptr>& cache) {
if (!aig) return nullptr;
std::function<aig_ptr(const aig_ptr&)> clone_helper =
[&](const aig_ptr& node) -> aig_ptr {
if (!node) return nullptr;
// Check cache to avoid cloning the same node multiple times
auto it = cache.find(node);
if (it != cache.end()) return it->second;
// Create new AIG node
auto cloned = std::make_shared<AIG>();
cloned->type = node->type;
cloned->var = node->var;
cloned->neg = node->neg;
// Add to cache before recursing to handle cycles
cache[node] = cloned;
// Recursively clone children for AND nodes
if (node->type == AIGT::t_and) {
cloned->l = clone_helper(node->l);
cloned->r = clone_helper(node->r);
}
return cloned;
};
return clone_helper(aig);
}
// Generic recursive traversal function that applies a function to each AIG node
// The function receives the current node as an aig_ptr
// Use cache to avoid visiting the same node multiple times
template<typename Func>
static void traverse(const aig_ptr& aig, Func&& func) {
if (!aig) return;
std::set<aig_ptr> visited;
traverse_helper(aig, std::forward<Func>(func), visited);
}
template<typename Func>
static void traverse_helper(const aig_ptr& node, Func&& func, std::set<aig_ptr>& visited) {
if (!node) return;
// Check if already visited to avoid infinite loops
if (visited.count(node)) return;
visited.insert(node);
// Apply the function to the current node
func(node);
// Recursively traverse children for AND nodes
if (node->type == AIGT::t_and) {
traverse_helper(node->l, std::forward<Func>(func), visited);
traverse_helper(node->r, std::forward<Func>(func), visited);
}
}
// Transform function that performs post-order traversal and builds up a result
// The visitor receives: (type, var, neg, left_result*, right_result*)
// - type: the node type (t_const, t_lit, or t_and)
// - var: variable number (only meaningful for t_lit)
// - neg: negation flag
// - left_result, right_result: pointers to children results (nullptr for non-AND nodes)
template<typename ResultType, typename Visitor>
static ResultType transform(
const aig_ptr& aig,
Visitor&& visitor,
std::map<aig_ptr, ResultType>& cache
) {
assert(aig);
// Check cache first
auto it = cache.find(aig);
if (it != cache.end()) return it->second;
ResultType result;
if (aig->type == AIGT::t_and) {
// Post-order: process children first
ResultType left_result = transform<ResultType>(aig->l, std::forward<Visitor>(visitor), cache);
ResultType right_result = transform<ResultType>(aig->r, std::forward<Visitor>(visitor), cache);
result = visitor(aig->type, aig->var, aig->neg, &left_result, &right_result);
} else {
// Leaf nodes (t_const or t_lit)
result = visitor(aig->type, aig->var, aig->neg, nullptr, nullptr);
}
cache[aig] = result;
return result;
}
static size_t count_aig_nodes(const aig_ptr& aig);
static void simplify_aigs(uint32_t verb, std::vector<aig_ptr>& defs);
static aig_ptr simplify_aig(aig_ptr aig);
friend std::ostream& operator<<(std::ostream& out, const aig_ptr& aig);
friend class AIGManager;
friend class SimplifiedCNF;
private:
static aig_ptr simplify(aig_ptr aig);
static aig_ptr simplify(aig_ptr aig, std::map<aig_ptr, aig_ptr>& cache);
static aig_ptr simplify_cse(aig_ptr aig, std::map<AIGKey, aig_ptr>& cse_map, std::map<aig_ptr, aig_ptr>& cache);
static void count_aig_nodes(const aig_ptr& aig, std::set<aig_ptr>& counted);
AIGT type = AIGT::t_const;
static constexpr uint32_t none_var = std::numeric_limits<uint32_t>::max();
uint32_t var = none_var;
bool neg = false;
aig_ptr l = nullptr;
aig_ptr r = nullptr;
};
inline std::ostream& operator<<(std::ostream& out, const aig_ptr& aig) {
if (!aig) {
out << "NULL_AIG";
return out;
}
assert(aig->invariants());
if (aig->type == AIGT::t_lit) {
out << (aig->neg ? "~" : "") << "x" << aig->var+1;
return out;
}
if (aig->type == AIGT::t_const) {
out << (aig->neg ? "FALSE" : "TRUE");
return out;
}
assert(aig->type == AIGT::t_and);
out << (aig->neg ? "~" : "") << "AND(";
out << (aig->l) << ", " << (aig->r) << ")";
return out;
}
class AIGManager {
public:
~AIGManager() = default;
AIGManager() {
const_true = std::make_shared<AIG>();
const_true->type = AIGT::t_const;
const_true->neg = false;
const_false = std::make_shared<AIG>();
const_false->type = AIGT::t_const;
const_false->neg = true;
}
AIGManager& operator=(const AIGManager& other) {
if (this != &other) {
clear();
// With shared_ptr, just copy the pointers - no deep copy needed!
const_true = other.const_true;
const_false = other.const_false;
}
return *this;
}
AIGManager(const AIGManager& other) {
const_true = other.const_true;
const_false = other.const_false;
}
aig_ptr new_const(bool val) const {
return val ? const_true : const_false;
}
private:
void clear() {
const_true = nullptr;
const_false = nullptr;
}
// There could be other const true, this is just a good example so we don't always copy
// Due to copying we don'g guarantee uniqueness
aig_ptr const_true = nullptr;
// There could be other const false, this is just a good example so we don't always copy
// Due to copying we don'g guarantee uniqueness
aig_ptr const_false = nullptr;
};
class FMpz final : public CMSat::Field {
public:
mpz_class val;
FMpz(const int _val) : val(_val) {}
FMpz(const mpz_class& _val) : val(_val) {}
FMpz(const FMpz& other) : val(other.val) {}
Field& operator=(const Field& other) final {
const auto& od = static_cast<const FMpz&>(other);
val = od.val;
return *this;
}
Field& operator+=(const Field& other) final {
const auto& od = static_cast<const FMpz&>(other);
val += od.val;
return *this;
}
std::unique_ptr<Field> add(const Field& other) final {
const auto& od = static_cast<const FMpz&>(other);
return std::make_unique<FMpz>(val + od.val);
}
Field& operator-=(const Field& other) final {
const auto& od = static_cast<const FMpz&>(other);
val -= od.val;
return *this;
}
Field& operator*=(const Field& other) final {
const auto& od = static_cast<const FMpz&>(other);
val *= od.val;
return *this;
}
Field& operator/=(const Field& other) final {
const auto& od = static_cast<const FMpz&>(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 FMpz&>(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<FMpz>(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; }
bool parse(const std::string& str, const uint32_t line_no) final {
uint32_t at = 0;
skip_whitespace(str, at);
auto sign = parse_sign(str, at);
if (!parse_int(val, str, at, line_no)) return false;
val*=sign;
return check_end_of_weight(str, at, line_no);
}
uint64_t helper(const mpz_class& v) const {
return v.get_mpz_t()->_mp_alloc * sizeof(mp_limb_t);
}
uint64_t bytes_used() const final {
return sizeof(FMpz) + helper(val);
}
};
class FGenMpz final : public CMSat::FieldGen {
public:
~FGenMpz() final = default;
std::unique_ptr<CMSat::Field> zero() const final {
return std::make_unique<FMpz>(0);
}
std::unique_ptr<CMSat::Field> one() const final {
return std::make_unique<FMpz>(1);
}
std::unique_ptr<FieldGen> dup() const final {
return std::make_unique<FGenMpz>();
}
bool larger_than(const CMSat::Field& a, const CMSat::Field& b) const final {
const auto& ad = static_cast<const FMpz&>(a);
const auto& bd = static_cast<const FMpz&>(b);
return ad.val > bd.val;
}
bool weighted() const final { return false; }
};
class FMpq final : public CMSat::Field {
public:
mpq_class val;
FMpq() : val(0) {}
FMpq(const int _val) : val(_val) {}
FMpq(const mpz_class& _val) : val(_val) {}
FMpq(const mpq_class& _val) : val(_val) {}
FMpq(const FMpq& other) : val(other.val) {}
const mpq_class& get_val() const { return val; }
Field& operator=(const Field& other) final {
const auto& od = static_cast<const FMpq&>(other);
val = od.val;
return *this;
}
Field& operator+=(const Field& other) final {
const auto& od = static_cast<const FMpq&>(other);
val += od.val;
return *this;
}
std::unique_ptr<Field> add(const Field& other) final {
const auto& od = static_cast<const FMpq&>(other);
return std::make_unique<FMpq>(val + od.val);
}
Field& operator-=(const Field& other) final {
const auto& od = static_cast<const FMpq&>(other);
val -= od.val;
return *this;
}
Field& operator*=(const Field& other) final {
const auto& od = static_cast<const FMpq&>(other);
val *= od.val;
return *this;
}
Field& operator/=(const Field& other) final {
const auto& od = static_cast<const FMpq&>(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 FMpq&>(other);
return val == od.val;
}
std::ostream& display(std::ostream& os) const final {
os << val;
return os;
}
std::unique_ptr<Field> dup() const final {
return std::make_unique<FMpq>(val);
}
bool is_zero() const final {
return val == 0;
}
bool is_one() const final {
return val == 1;
}
bool parse(const std::string& str, const uint32_t line_no) final {
uint32_t at = 0;
if (!parse_mpq(str, at, line_no)) return false;
return check_end_of_weight(str, at, line_no);
}
void set_zero() final { val = 0; }
void set_one() final { val = 1; }
inline uint64_t helper(const mpz_class& v) const {
return v.get_mpz_t()->_mp_alloc * sizeof(mp_limb_t);
}
uint64_t bytes_used() const final {
return sizeof(FMpq) +
helper(val.get_num()) + helper(val.get_den());
}
bool parse_mpq(const std::string& str, uint32_t& at, const uint32_t line_no) {
skip_whitespace(str, at);
mpz_class head;
auto sign = parse_sign(str, at);
if (!parse_int(head, str, at, line_no)) return false;
bool rational = false;
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);
val = mpq_class(head) + tailq/tenq;
} else if (str[at] == '/') {
at++;
rational = true;
mpz_class tail;
if (!parse_int(tail, str, at, line_no)) return false;
val = mpq_class(head)/mpq_class(tail);
} else {
val = head;
}
if (str[at] == 'e' || str[at] == 'E') {
if (rational) {
std::cerr << "PARSE ERROR! You can't have BOTH rational AND exponent"
<< " At line " << line_no << " Probably looks like 1/2e-4"
<< std::endl;
return false;
}
at++;
mpz_class exp;
auto sign2 = parse_sign(str, at);
if (!parse_int(exp, str, at, line_no)) return false;
exp*=sign2;
if (!exp.fits_sint_p()) {
std::cerr << "PARSE ERROR! Exponent too large for int64_t"
<< " At line " << line_no << " Probably looks like 1e100" << std::endl;
return false;
}
int64_t ex = exp.get_si();
mpz_class x(1);
if (ex < 0) {
ex *=-1;
mpz_pow_ui(x.get_mpz_t(), mpz_class(10).get_mpz_t(), ex);
val /= x;
} else {
mpz_pow_ui(x.get_mpz_t(), mpz_class(10).get_mpz_t(), ex);
val *=x;
}
}
val *= sign;
return true;
}
};
class FGenMpq final : public CMSat::FieldGen {
public:
~FGenMpq() final = default;
std::unique_ptr<CMSat::Field> zero() const final {
return std::make_unique<FMpq>(0);
}
std::unique_ptr<CMSat::Field> one() const final {
return std::make_unique<FMpq>(1);
}
std::unique_ptr<FieldGen> dup() const final {
return std::make_unique<FGenMpq>();
}
bool larger_than(const CMSat::Field& a, const CMSat::Field& b) const final {
const auto& ad = static_cast<const FMpq&>(a);
const auto& bd = static_cast<const FMpq&>(b);
return ad.val > bd.val;
}
bool weighted() const final { return true; }
};
inline unsigned int mpfr_memory_usage(const mpfr_t& x) {
// Get the precision of the mpfr_t variable (in bits)
mpfr_prec_t precision = mpfr_get_prec(x);
// Determine the size of a limb (in bits)
size_t limb_size = mp_bits_per_limb;
// Calculate the number of limbs required for the significand
size_t num_limbs = (precision + limb_size - 1) / limb_size;
// Base size of mpfr_t structure
unsigned int base_size = sizeof(__mpfr_struct);
// Size of the mantissa (significand) data
unsigned int mantissa_size = num_limbs * sizeof(mp_limb_t);
// Total memory usage
return base_size + mantissa_size;
}
class FMpfr final : public CMSat::Field {
public:
mpfr_t val;
~FMpfr() final { mpfr_clear(val); }
FMpfr() = delete;
explicit FMpfr(mpfr_prec_t prec) {
mpfr_init2(val, prec);
mpfr_set_si(val, 0, MPFR_RNDN);
}
explicit FMpfr(const long _val, mpfr_prec_t prec) {
mpfr_init2(val, prec);
mpfr_set_si(val, _val, MPFR_RNDN);
}
explicit FMpfr(const double _val, mpfr_prec_t prec) {
mpfr_init2(val, prec);
mpfr_set_d(val, _val, MPFR_RNDN);
}
explicit FMpfr(const mpfr_t& _val) {
const auto prec = mpfr_get_prec(_val);
mpfr_init2(val, prec);
mpfr_set(val, _val, MPFR_RNDN);
}
explicit FMpfr(const FMpfr& other) {
const auto prec = mpfr_get_prec(other.val);
mpfr_init2(val, prec);
mpfr_set(val, other.val, MPFR_RNDN);
}
const mpfr_t& get_val() const { return val; }
Field& operator=(const Field& other) final {
const auto& od = static_cast<const FMpfr&>(other);
mpfr_set(val, od.val, MPFR_RNDN);
return *this;
}
Field& operator+=(const Field& other) final {
const auto& od = static_cast<const FMpfr&>(other);
mpfr_add(val, val, od.val, MPFR_RNDN);
return *this;
}
std::unique_ptr<Field> add(const Field& other) final {
const auto& od = static_cast<const FMpfr&>(other);
mpfr_t res;
const auto prec = mpfr_get_prec(val);
mpfr_init2(res, prec);
mpfr_add(res, val, od.val, MPFR_RNDN);
std::unique_ptr<FMpfr> ret = std::make_unique<FMpfr>(res);
mpfr_clear(res);
return ret;
}
Field& operator-=(const Field& other) final {
const auto& od = static_cast<const FMpfr&>(other);
mpfr_sub(val, val, od.val, MPFR_RNDN);
return *this;
}
Field& operator*=(const Field& other) final {
const auto& od = static_cast<const FMpfr&>(other);
mpfr_mul(val, val, od.val, MPFR_RNDN);
return *this;
}
Field& operator/=(const Field& other) final {
const auto& od = static_cast<const FMpfr&>(other);
if (mpfr_cmp_si(od.val, 0) == 0)
throw std::runtime_error("Division by zero");
mpfr_div(val, val, od.val, MPFR_RNDN);
return *this;
}
bool operator==(const Field& other) const final {
const auto& od = static_cast<const FMpfr&>(other);
return mpfr_cmp(val, od.val) == 0;
}
std::ostream& display(std::ostream& os) const final {
char* tmp = nullptr;
mpfr_asprintf(&tmp, "%.8Re", val);
os << tmp;
mpfr_free_str(tmp);
return os;
}
std::unique_ptr<Field> dup() const final {
return std::make_unique<FMpfr>(val);
}
bool is_zero() const final {
return mpfr_cmp_si(val, 0) == 0;
}
bool is_one() const final {
return mpfr_cmp_si(val, 1) == 0;
}
bool parse(const std::string& str, const uint32_t line_no) final {
uint32_t at = 0;
FMpq val_pre;
if (!val_pre.parse_mpq(str, at, line_no)) return false;
skip_whitespace(str, at);
mpfr_set_q(val, val_pre.get_val().get_mpq_t(), MPFR_RNDN);
return true;
}
void set_zero() final { mpfr_set_si(val, 0, MPFR_RNDN); }
void set_one() final { mpfr_set_si(val, 1, MPFR_RNDN); }
uint64_t bytes_used() const final {
return sizeof(FMpfr) + mpfr_memory_usage(val);
}
};
class FGenMpfr final : public CMSat::FieldGen {
public:
mpfr_prec_t prec;
~FGenMpfr() final = default;
FGenMpfr(const FGenMpfr& other) : prec(other.prec) {}
FGenMpfr& operator=(const FGenMpfr& other) {
if (this != &other) {
prec = other.prec;
}
return *this;
}
FGenMpfr(mpfr_prec_t _prec) : prec(_prec) {}
std::unique_ptr<CMSat::Field> zero() const final {
return std::make_unique<FMpfr>((long)0, prec);
}
std::unique_ptr<CMSat::Field> one() const final {
return std::make_unique<FMpfr>((long)1, prec);
}
std::unique_ptr<FieldGen> dup() const final {
return std::make_unique<FGenMpfr>(prec);
}
bool larger_than(const CMSat::Field& a, const CMSat::Field& b) const final {
const auto& ad = static_cast<const FMpfr&>(a);
const auto& bd = static_cast<const FMpfr&>(b);
return mpfr_cmp(ad.val, bd.val) > 0;
}
bool weighted() const final { return true; }
};
struct SimpConf {
bool oracle_extra = true;
bool oracle_vivify = true;
bool oracle_vivify_get_learnts = true;
bool oracle_sparsify = true;
int iter1 = 2;
int iter2 = 2;
int bve_grow_iter1 = 0;
int bve_grow_iter2 = 6;
bool bve_grow_nonstop = false;
bool do_bve = true;
bool appmc = false;
int bve_too_large_resolvent = 12;
int do_subs_with_resolvent_clauses = 1;
bool do_backbone_puura = true;
int weaken_limit = 8000;
};
class SimplifiedCNF {
public:
std::unique_ptr<CMSat::FieldGen> fg = nullptr;
SimplifiedCNF(const std::unique_ptr<CMSat::FieldGen>& _fg) : fg(_fg->dup()), multiplier_weight(fg->one()) {}
SimplifiedCNF(const CMSat::FieldGen* _fg) : fg(_fg->dup()), multiplier_weight(fg->one()) {}
~SimplifiedCNF() = default;
SimplifiedCNF& operator=(const SimplifiedCNF& other) {
assert(other.defs_invariant());
fg = other.fg->dup();
need_aig = other.need_aig;
clauses = other.clauses;
red_clauses = other.red_clauses;
proj = other.proj;
sampl_vars_set = other.sampl_vars_set;
opt_sampl_vars_set = other.opt_sampl_vars_set;
sampl_vars = other.sampl_vars;
opt_sampl_vars = other.opt_sampl_vars;
nvars = other.nvars;
multiplier_weight = other.multiplier_weight->dup();
weighted = other.weighted;
backbone_done = other.backbone_done;
weights = other.weights;
orig_to_new_var = other.orig_to_new_var;
preserve_existing_defs = other.preserve_existing_defs;
if (!need_aig) {
assert(defs.empty());
assert(other.defs.empty());
assert(other.orig_sampl_vars.empty());
assert(other.orig_clauses.empty());
} else {
after_backward_round_synth = other.after_backward_round_synth;
aig_mng = other.aig_mng;
defs = AIG::deep_clone_vec(other.defs);
orig_clauses = other.orig_clauses;
orig_sampl_vars = other.orig_sampl_vars;
orig_sampl_vars_set = other.orig_sampl_vars_set;
}
assert(defs_invariant());
return *this;
}
SimplifiedCNF(const SimplifiedCNF& other) {
*this = other;
}
const auto& nVars() const { return nvars; }
const auto& get_clauses() const { return clauses; }
const auto& get_red_clauses() const { return red_clauses; }
const auto& get_weights() const { return weights; }
const auto& get_sampl_vars() const { return sampl_vars; }
auto get_orig_num_vars() const {
assert(defs.size() >= nvars);
return defs.size();
}
const auto& get_orig_sampl_vars() const { assert(orig_sampl_vars_set); return orig_sampl_vars; }
const auto& get_orig_clauses() const { return orig_clauses; }
const auto& get_opt_sampl_vars() const { return opt_sampl_vars; }
const auto& get_backbone_done() const { return backbone_done; }
bool synth_done() const;
bool is_projected() const { return proj; }
template<class T>
void set_orig_sampl_vars(const T& vars) {
assert(need_aig);
assert(orig_sampl_vars.empty());
assert(!orig_sampl_vars_set);
orig_sampl_vars_set = true;
for(const auto& v: vars) orig_sampl_vars.insert(v);
}
void set_orig_clauses(const std::vector<std::vector<CMSat::Lit>>& cls) {
assert(need_aig);
assert(orig_clauses.empty());
orig_clauses = cls;
}
// ORIG variable
bool defined(const uint32_t v) const {
assert(v < defs.size());
assert(need_aig);
if (defs[v] != nullptr) return true;
return false;
}
void set_need_aig() {
// should be the first thing to set
assert(!need_aig);
assert(nvars == 0);
assert(clauses.empty());
assert(red_clauses.empty());
assert(defs.empty());
assert(opt_sampl_vars.empty());
assert(sampl_vars.empty());
assert(orig_sampl_vars_set == false);
assert(orig_sampl_vars.empty());
assert(sampl_vars_set == false);
assert(opt_sampl_vars_set == false);
need_aig = true;
}
bool get_need_aig() const { return need_aig; }
uint32_t num_defs() const { return defs.size(); }
// Returns NEW vars, i.e. < nVars()
// It is checked that it is correct and total
std::tuple<std::set<uint32_t>, std::set<uint32_t>, std::set<uint32_t>>
get_var_types([[maybe_unused]] uint32_t verb, const std::string& str = "") const;
bool check_all_opt_sampl_vars_depend_only_on_orig_sampl_vars() const;
bool check_orig_sampl_vars_undefined() const;
bool defs_invariant() const;
// Get the orig vars this AIG depends on, recursively expanding defined vars
std::set<uint32_t> get_dependent_vars_recursive(const uint32_t orig_v, std::map<uint32_t, std::set<uint32_t>>& cache) const;
bool check_aig_cycles() const;
void check_self_dependency() const;
void check_cnf_vars() const;
// all vars are either: in orig_sampl_vars, defined, or in the cnf
void check_all_vars_accounted_for() const;
// this checks that NO unsat-define has been made yet
void check_pre_post_backward_round_synth() const;
void set_all_opt_indep();
void add_opt_sampl_var(const uint32_t v);
void clean_idiotic_mccomp_weights();
void check_cnf_sampl_sanity() const;
// Gives all the orig lits that map to this variable
std::map<uint32_t, std::vector<CMSat::Lit>> get_new_to_orig_var_list() const;