-
Notifications
You must be signed in to change notification settings - Fork 0
/
prooflogging.patch
6086 lines (6039 loc) · 225 KB
/
prooflogging.patch
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
diff -ruN src_tmp/core/Main.C src/core/Main.C
--- src_tmp/core/Main.C 2022-02-19 16:47:37.927568213 +0100
+++ src/core/Main.C 2022-02-19 12:06:50.626500104 +0100
@@ -26,7 +26,13 @@
#include <signal.h>
#include <zlib.h>
+// Added for chrono
+#include <iostream>
+#include <chrono>
+#include <string>
+
#include "Solver.h"
+#include "Prooflogger.h"
/*************************************************************************************/
#ifdef _MSC_VER
@@ -131,18 +137,18 @@
return neg ? -val : val; }
template<class B>
-static void readClause(B& in, Solver& S, vec<Lit>& lits,
+static void readClause(B& in, Solver& S, Prooflogger& PL, vec<Lit>& lits,
int nbvar, int top, int& nbsoft) { // koshi 10.01.04
- int parsed_lit, var;
+ int parsed_lit, var, weight;
lits.clear();
- parsed_lit = parseInt(in); // koshi 10.01.04
- if (parsed_lit == 1) { // soft clause
+ weight = parseInt(in); // koshi 10.01.04
+ if (weight == 1) { // soft clause
nbsoft++;
lits.push(Lit(S.newVar()));
- } else if (parsed_lit != top) { // weight of hard clause must be top
+ } else if (weight != top) { // weight of hard clause must be top
reportf("Unexpected weight %c\n", *in), exit(3);
- }
+ } else weight = top;
for (;;){
parsed_lit = parseInt(in);
@@ -163,42 +169,55 @@
template<class B>
-static void parse_DIMACS_main(B& in, Solver& S,
+static void parse_DIMACS_main(B& in, Solver& S, Prooflogger &PL,
int& out_nbvar, int& out_top, int& out_nbsoft) {
vec<Lit> lits;
+ int vars, clauses;
+
+ // Read header
+ skipWhitespace(in);
+ while(*in == 'c') skipLine(in);
+ if (*in == 'p'){
+ if (match(in, "p wcnf")){ // koshi 10.01.04
+ vars = parseInt(in);
+ clauses = parseInt(in);
+ int top = parseInt(in);
+ out_nbvar = vars;
+ out_top = top;
+ reportf("| Number of variables: %-12d |\n", vars);
+ reportf("| Number of clauses: %-12d |\n", clauses);
+ reportf("| Weight of hard clauses: %-12d |\n", top);
+ while (vars > S.nVars()) S.newVar();
+ } else {
+ reportf("PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
+ }
+ } else reportf("PARSE ERROR! No header given!");
+
+ // Read clauses
for (;;){
skipWhitespace(in);
if (*in == EOF)
break;
- else if (*in == 'p'){
- if (match(in, "p wcnf")){ // koshi 10.01.04
- int vars = parseInt(in);
- int clauses = parseInt(in);
- int top = parseInt(in);
- out_nbvar = vars;
- out_top = top;
- reportf("| Number of variables: %-12d |\n", vars);
- reportf("| Number of clauses: %-12d |\n", clauses);
- reportf("| Weight of hard clauses: %-12d |\n", top);
- while (vars > S.nVars()) S.newVar();
- }else{
- reportf("PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
- }
- } else if (*in == 'c' || *in == 'p')
- skipLine(in);
- else
- readClause(in, S, lits, out_nbvar,out_top,out_nbsoft),
+ else if (*in == 'c' || *in == 'p') skipLine(in);
+ else {
+ readClause(in, S, PL, lits, out_nbvar,out_top,out_nbsoft),
S.addClause(lits);
+ }
}
reportf("| Number of soft clauses: %-12d |\n", out_nbsoft);
+ PL.n_variables = vars+out_nbsoft;
+ PL.variable_counter = PL.n_variables;
+ PL.formula_length = clauses;
+ PL.constraint_counter = clauses;
+ PL.write_proof_header(clauses);
}
// Inserts problem into solver.
//
-static void parse_DIMACS(gzFile input_stream, Solver& S,
+static void parse_DIMACS(gzFile input_stream, Solver& S, Prooflogger &PL,
int& out_nbvar, int& out_top, int& out_nbsoft) { // koshi 10.01.04
StreamBuffer in(input_stream);
- parse_DIMACS_main(in, S, out_nbvar, out_top, out_nbsoft); }
+ parse_DIMACS_main(in, S, PL, out_nbvar, out_top, out_nbsoft); }
//=================================================================================================
@@ -232,10 +251,12 @@
{
reportf("USAGE: %s [options] <input-file> <result-output-file>\n\n where input may be either in plain or gzipped DIMACS.\n\n", argv[0]);
reportf("OPTIONS:\n\n");
- reportf(" -polarity-mode = {true,false,rnd}\n");
- reportf(" -decay = <num> [ 0 - 1 ]\n");
- reportf(" -rnd-freq = <num> [ 0 - 1 ]\n");
- reportf(" -verbosity = {0,1,2}\n");
+ reportf(" -polarity-mode = {true,false,rnd}\n");
+ reportf(" -decay = <num> [ 0 - 1 ]\n");
+ reportf(" -rnd-freq = <num> [ 0 - 1 ]\n");
+ reportf(" -verbosity = {0,1,2}\n");
+ reportf(" -proof-file = /path/to/proof_file.pbp (default: maxsat_proof.pbp)\n");
+ reportf(" -meaningful_names = whether or not to assign meaningful names to the auxiliairy variables\n");
reportf("\n");
}
@@ -251,7 +272,7 @@
// koshi 10.01.08
void genCardinals(int from, int to,
- Solver& S, vec<Lit>& lits, vec<Lit>& linkingVar) {
+ Solver& S, Prooflogger& PL, vec<Lit>& lits, vec<Lit>& linkingVar) {
int inputSize = to - from + 1;
linkingVar.clear();
@@ -261,52 +282,83 @@
Var varZero = S.newVar();
Var varLast = S.newVar();
+ // First
lits.clear(); lits.push(Lit(varZero)); S.addClause(lits);
+ PL.write_learnt_clause(lits);
+
+ // Last
lits.clear(); lits.push(~Lit(varLast)); S.addClause(lits);
+ PL.write_learnt_clause(lits);
+
if (inputSize > 2) {
int middle = inputSize/2;
- genCardinals(from, from+middle, S,lits,linkingAlpha);
- genCardinals(from+middle+1, to, S,lits,linkingBeta);
+ genCardinals(from, from+middle, S,PL,lits,linkingAlpha);
+ genCardinals(from+middle+1, to, S,PL,lits,linkingBeta);
} else if (inputSize == 2) {
- genCardinals(from, from, S,lits,linkingAlpha);
- genCardinals(to, to, S,lits,linkingBeta);
+ genCardinals(from, from, S,PL,lits,linkingAlpha);
+ genCardinals(to, to, S,PL,lits,linkingBeta);
}
if (inputSize == 1) {
linkingVar.push(Lit(varZero));
linkingVar.push(Lit(from));
linkingVar.push(Lit(varLast));
} else { // inputSize >= 2
+
+ PL.write_comment("- Node clauses:");
linkingVar.push(Lit(varZero));
for (int i = 0; i < inputSize; i++) linkingVar.push(Lit(S.newVar()));
linkingVar.push(Lit(varLast));
+
for (int sigma = 0; sigma <= inputSize; sigma++) {
- for (int alpha = 0; alpha < linkingAlpha.size()-1; alpha++) {
- int beta = sigma - alpha;
- if (0 <= beta && beta < linkingBeta.size()-1) { // create constraints
- lits.clear();
- lits.push(~linkingAlpha[alpha]);
- lits.push(~linkingBeta[beta]);
- lits.push(linkingVar[sigma]);
- S.addClause(lits);
- lits.clear();
- lits.push(linkingAlpha[alpha+1]);
- lits.push(linkingBeta[beta+1]);
- lits.push(~linkingVar[sigma+1]);
- S.addClause(lits);
- }
- }
+ for (int alpha = 0; alpha < linkingAlpha.size()-1; alpha++) {
+ int beta = sigma - alpha;
+ if (0 <= beta && beta < linkingBeta.size()-1) { // create constraints
+ lits.clear();
+ lits.push(~linkingAlpha[alpha]);
+ lits.push(~linkingBeta[beta]);
+ lits.push(linkingVar[sigma]);
+ PL.write_C1(lits, sigma, from, to);
+ S.addClause(lits);
+ lits.clear();
+ lits.push(linkingAlpha[alpha+1]);
+ lits.push(linkingBeta[beta+1]);
+ lits.push(~linkingVar[sigma+1]);
+ PL.write_C2(lits, sigma+1, from, to);
+ S.addClause(lits);
+ }
+ }
}
+ PL.write_comment("-------------------------------------------");
}
linkingAlpha.clear();
linkingBeta.clear();
}
+class MyChrono{
+ public:
+ static std::chrono::time_point<std::chrono::system_clock> startClock(){
+ return std::chrono::system_clock::now();
+ }
+
+ static std::string duration_since(std::chrono::time_point<std::chrono::system_clock>& start){
+ auto end = std::chrono::system_clock::now();
+ std::chrono::duration<double> diff = end - start;
+
+ return std::to_string(diff.count());
+ }
+
+};
+
int main(int argc, char** argv)
{
- Solver S;
+ Prooflogger PL;
+ Solver S(&PL);
S.verbosity = 1;
+ // Duration file
+ bool log_duration_totalizer = false;
+ const char *duration_file_name = "totalizer_duration.txt";
int i, j;
const char* value;
@@ -347,6 +399,16 @@
printUsage(argv);
exit(0);
+ } else if ((value = hasPrefix(argv[i], "-proof-file="))) {
+ PL.set_proof_name(value);
+
+ } else if ((value = hasPrefix(argv[i], "-log_duration_totalizer="))) {
+ log_duration_totalizer = true;
+ duration_file_name = value;
+
+ }else if (strcmp(argv[i], "-mn") == 0 || strcmp(argv[i], "-meaningful_names") == 0 || strcmp(argv[i], "--meaningful_names") == 0){
+ PL.meaningful_names = true;
+
}else if (strncmp(argv[i], "-", 1) == 0){
reportf("ERROR! unknown flag %s\n", argv[i]);
exit(0);
@@ -357,7 +419,7 @@
argc = j;
- reportf("This is QMaxSAT 0.1 based on MiniSat 2.0 beta\n");
+ reportf("This is QMaxSAT 0.1 based on MiniSat 2.0 beta, extended for prooflogging\n");
#if defined(__linux__)
fpu_control_t oldcw, newcw;
_FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
@@ -376,6 +438,12 @@
if (in == NULL)
reportf("ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]), exit(1);
+ // Open proof file
+ PL.open_proof_file();
+ if (log_duration_totalizer)
+
+
+
reportf("============================[ Problem Statistics ]=============================\n");
reportf("| |\n");
@@ -383,8 +451,15 @@
int nbvar = 0; // number of original variables
int top = 0; // weight of hard clause
int nbsoft = 0; // number of soft clauses
- parse_DIMACS(in, S, nbvar, top, nbsoft);
+ parse_DIMACS(in, S, PL, nbvar, top, nbsoft);
+
+ // Initialise PL constraint counter
+ //PL.constraint_counter = S.nClauses();
+
+ // Close input file
gzclose(in);
+
+ // Open output file
FILE* res = (argc >= 3) ? fopen(argv[2], "wb") : NULL;
double parse_time = cpuTime() - cpu_time;
@@ -393,6 +468,8 @@
if (!S.simplify()){
reportf("Solved by unit propagation\n");
if (res != NULL) fprintf(res, "UNSAT\n"), fclose(res);
+ PL.write_empty_clause();
+ PL.close_proof_file();
printf("UNSATISFIABLE\n");
exit(20);
}
@@ -406,23 +483,64 @@
if (ret) { // koshi 09.12.25
lcnt++;
int answerNew = 0;
- for (int i = nbvar; i < nbvar+nbsoft; i++) // count the number of
- if (S.model[i] == l_True) answerNew++; // unsatisfied soft clauses
- if (lcnt == 1) { // first model: generate cardinal constraints
- genCardinals(nbvar,nbvar+nbsoft-1, S,lits,linkingVar);
- for (int i = answerNew; i < linkingVar.size()-1; i++) {
- lits.clear();
- lits.push(~linkingVar[i]);
- S.addClause(lits);
- }
- answer = answerNew;
+
+ std::chrono::time_point<std::chrono::system_clock> start;
+
+ for (int i = nbvar; i < nbvar+nbsoft; i++) if (S.model[i] == l_True) answerNew++; // count the number ofunsatisfied soft clauses
+ if (lcnt == 1) { // first model: generate cardinality constraints
+ PL.write_comment("==============================================================");
+ PL.write_comment("First model found:");
+ PL.write_bound_update(S.model);
+ PL.write_comment("==============================================================");
+ PL.write_comment("Cardinality definitions:");
+
+ start = MyChrono::startClock();
+
+ PL.genCardinalDefinitions(nbvar, nbvar+nbsoft-1, lits, linkingVar);
+
+ auto duration_genCardinalDefinitions = "genCardinalDefinitions: " + MyChrono::duration_since(start) + "s";
+
+ PL.write_comment("==============================================================");
+ PL.write_comment("Tree derivation:");
+
+ start = MyChrono::startClock();
+
+ genCardinals(nbvar,nbvar+nbsoft-1, S, PL, lits, linkingVar);
+
+ auto duration_genCardinals = "genCardinals: " + MyChrono::duration_since(start) + "s";
+
+ if(log_duration_totalizer){
+ std::ofstream log_duration_totalizer_stream;
+ log_duration_totalizer_stream.open(duration_file_name);
+ log_duration_totalizer_stream << duration_genCardinalDefinitions << "\n" << duration_genCardinals << "\n";
+ log_duration_totalizer_stream.close();
+ }
+
+ PL.write_comment("==============================================================");
+ PL.write_comment("Constraining through linking variables:");
+
+ for (int i = answerNew; i < linkingVar.size()-1; i++) {
+ lits.clear();
+ lits.push(~linkingVar[i]);
+ PL.write_linkingVar_clause(lits);
+ S.addClause(lits);
+ }
+ PL.write_comment("==============================================================");
+ answer = answerNew;
} else { // lcnt > 1
- for (int i = answerNew; i < answer; i++) {
- lits.clear();
- lits.push(~linkingVar[i]);
- S.addClause(lits);
- }
- answer = answerNew;
+ PL.write_comment("==============================================================");
+ PL.write_comment("New model found:");
+ PL.write_bound_update(S.model);
+ PL.write_comment("==============================================================");
+ PL.write_comment("Constraining through linking variables:");
+ for (int i = answerNew; i < answer; i++) {
+ lits.clear();
+ lits.push(~linkingVar[i]);
+ PL.write_linkingVar_clause(lits);
+ S.addClause(lits);
+ }
+ PL.write_comment("==============================================================");
+ answer = answerNew;
}
reportf("Current answer = %d\n",answer);
goto solve;
@@ -439,10 +557,11 @@
if (S.model[i] != l_Undef)
fprintf(res, "%s%s%d", (i==0)?"":" ", (S.model[i]==l_True)?"":"-", i+1);
fprintf(res, " 0\n");
- }else
+ } else
fprintf(res, "UNSAT\n");
fclose(res);
}
+ PL.close_proof_file();
#ifdef NDEBUG
exit(ret ? 10 : 20); // (faster than "return", which will invoke the destructor for 'Solver')
diff -ruN src_tmp/core/Prooflogger.C src/core/Prooflogger.C
--- src_tmp/core/Prooflogger.C 1970-01-01 01:00:00.000000000 +0100
+++ src/core/Prooflogger.C 2022-02-19 16:47:21.126981223 +0100
@@ -0,0 +1,286 @@
+#include "Prooflogger.h"
+
+//=================================================================================================
+// Proof file
+
+void Prooflogger::write_proof_header(int nbclause) {
+ proof << "pseudo-Boolean proof version 1.0\n";
+ proof << "f " << nbclause << "\n" ;
+}
+
+void Prooflogger::write_comment(const char* comment) {
+ proof<< "* " << comment << "\n" ;
+}
+
+void Prooflogger::write_contradiction() {
+ proof << "c " << constraint_counter << "\n" ;
+}
+
+void Prooflogger::write_empty_clause() {
+ proof<< "u >= 1;\n" ;
+ constraint_counter++;
+ write_contradiction();
+}
+
+bool Prooflogger::is_aux_var(int var) {
+ return var + 1 > n_variables;
+}
+
+std::string Prooflogger::var_name(int var) {
+ std::string name;
+ if(meaningful_names && meaningful_name_UB.find(var) != meaningful_name_UB.end()) {
+ int lb = meaningful_name_LB[var];
+ int ub = meaningful_name_UB[var];
+ int n = meaningful_name_n[var];
+ name = "v" + std::to_string(n) + "_x" + std::to_string(lb) + "_x" + std::to_string(ub);
+ } else if(is_aux_var(var)) {
+ name = "y";
+ name += std::to_string(var+1);
+ } else {
+ name = "x";
+ name += std::to_string(var+1);
+ }
+ return name;
+}
+
+void Prooflogger::write_literal(Lit literal) {
+
+ // Weight and sign
+ std::string weight_and_sign = "1 ";
+ weight_and_sign += (sign(literal) == 1 ? "~" : "");
+
+ // Variable symbol
+ std::string name = var_name(var(literal));
+
+ // Write
+ proof << weight_and_sign << name << " " ;
+}
+
+void Prooflogger::write_literal_assignment(lbool assignment, int var) {
+
+ // Sign
+ std::string sign = assignment == l_True? "" : "~";
+
+ // Variable symbol
+ std::string symbol = var_name(var);
+
+ // Write
+ proof << sign << symbol << " " ;
+}
+
+void Prooflogger::write_witness(Lit literal) {
+ std::string name = var_name(var(literal));
+ proof<< name << " -> " << std::to_string(sign(literal) == 0) ;
+}
+
+void Prooflogger::write_clause(vec<Lit>& clause) {
+ for (int i = 0; i < clause.size(); i++) write_literal(clause[i]);
+}
+
+void Prooflogger::write_learnt_clause(vec<Lit>& clause) {
+ proof << "u ";
+ write_clause(clause);
+ proof << " >= 1;\n" ;
+ constraint_counter++;
+}
+
+void Prooflogger::write_linkingVar_clause(vec<Lit>& clause) {
+ int variable = var(clause[0]);
+ int constraint_id = C2_store[variable];
+ if(constraint_id != 0) {
+ proof << "p " << constraint_id << " " << last_bound_constraint_id << " + s\n" ;
+ constraint_counter++;
+ }
+ write_learnt_clause(clause);
+}
+
+void Prooflogger::write_bound_update(vec<lbool>& model) {
+ proof<< "o ";
+ for(int i = 0; i < model.size(); i++) write_literal_assignment(model[i], i);
+ proof << "\n" ;
+
+ // Veripb automatically adds an improvement constraint so counter needs to be incremented
+ last_bound_constraint_id = ++constraint_counter;
+}
+
+void Prooflogger::write_unit_sub_red(vec<Lit>& definition, int sigma, int from, int to) {
+
+ if(meaningful_names) {
+
+ // If variable does not already have a meaningful name
+ if(meaningful_name_LB.find(var(definition[0])) == meaningful_name_LB.end()) {
+ meaningful_name_LB[var(definition[0])] = from+1;
+ meaningful_name_UB[var(definition[0])] = to+1;
+ meaningful_name_n[var(definition[0])] = sigma;
+ }
+ }
+
+ proof << "red ";
+ write_clause(definition);
+ proof << ">= 1; ";
+ write_witness(definition[0]);
+ proof << "\n" ;
+ constraint_counter++;
+}
+
+void Prooflogger::write_P1_sub_red_cardinality(int var, int sigma, int from, int to) {
+
+ if(meaningful_names) {
+
+ // If variable does not already have a meaningful name
+ if(meaningful_name_LB.find(var) == meaningful_name_LB.end()) {
+ meaningful_name_LB[var] = from+1;
+ meaningful_name_UB[var] = to+1;
+ meaningful_name_n[var] = sigma;
+ }
+ }
+
+ int weight = (to-from+1)-(sigma - 1);
+ proof << "red ";
+ for(int i = from; i < to+1; i++) {
+ write_literal(~Lit(i));
+ }
+ proof << weight << " " << var_name(var) << " >= " << weight << "; ";
+ write_witness(Lit(var));
+ proof << "\n" ;
+ constraint_counter++;
+ C1_store[var] = constraint_counter;
+}
+
+void Prooflogger::write_P2_sub_red_cardinality(int var, int sigma, int from, int to) {
+
+ if(meaningful_names) {
+
+ // If variable does not already have a meaningful name
+ if(meaningful_name_LB.find(var) == meaningful_name_LB.end()) {
+ meaningful_name_LB[var] = from+1;
+ meaningful_name_UB[var] = to+1;
+ meaningful_name_n[var] = sigma;
+ }
+ }
+
+ int weight = sigma;
+ proof << "red ";
+ for(int i = from; i < to+1; i++) {
+ write_literal(Lit(i));
+ }
+ proof << weight << " ~" << var_name(var) << " >= " << weight << "; ";
+ write_witness(~Lit(var));
+ proof << "\n" ;
+ constraint_counter++;
+ C2_store[var] = constraint_counter;
+}
+
+void Prooflogger::write_C1(vec<Lit>& definition, int sigma, int from, int to) {
+ int first = var(definition[0]);
+ int second = var(definition[1]);
+ int third = var(definition[2]);
+
+ // Write derivation of parts
+ bool resolved_one = false;
+ if(C2_store.find(first) != C2_store.end()) {
+ resolved_one = true;
+ proof << "p " << C1_store[third] << " " << C2_store[first] << " +\n" ;
+ constraint_counter++;
+ }
+ if(C2_store.find(second) != C2_store.end()) {
+ int to_add_to = resolved_one? constraint_counter : C1_store[third];
+ proof << "p " << to_add_to << " " << C2_store[second] << " +\n" ;
+ constraint_counter++;
+ resolved_one = true;
+ }
+ if(resolved_one) {
+ proof << "p " << constraint_counter << " s\n" ;
+ constraint_counter++;
+ }
+
+ // Derivation is done so clause can be written as RUP
+ write_learnt_clause(definition);
+}
+
+void Prooflogger::write_C2(vec<Lit>& definition, int sigma, int from, int to) {
+ int first = var(definition[0]);
+ int second = var(definition[1]);
+ int third = var(definition[2]);
+
+ // Write derivation of parts
+ bool resolved_one = false;
+ if(C1_store.find(first) != C1_store.end()) {
+ resolved_one = true;
+ proof << "p " << C2_store[third] << " " << C1_store[first] << " +\n" ;
+ constraint_counter++;
+ }
+ if(C1_store.find(second) != C1_store.end()) {
+ int to_add_to = resolved_one? constraint_counter : C2_store[third];
+ proof << "p " << to_add_to << " " << C1_store[second] << " +\n" ;
+ constraint_counter++;
+ resolved_one = true;
+ }
+ if(resolved_one) {
+ proof << "p " << constraint_counter << " 2 d s\n" ;
+ constraint_counter++;
+ }
+
+ // Derivation is done so clause can be written as RUP
+ write_learnt_clause(definition);
+}
+
+void Prooflogger::genCardinalDefinitions(int from, int to, vec<Lit>& lits, vec<Lit>& linkingVar) {
+ int inputSize = to - from + 1;
+ linkingVar.clear();
+
+ vec<Lit> linkingAlpha;
+ vec<Lit> linkingBeta;
+
+ Var varZero = variable_counter++;
+ Var varLast = variable_counter++;
+
+ // First
+ lits.clear(); lits.push(Lit(varZero));
+ write_unit_sub_red(lits, 0, from, to);
+
+ // Last
+ lits.clear(); lits.push(~Lit(varLast));
+ write_unit_sub_red(lits, inputSize+1, from, to);
+
+
+ if (inputSize > 2) {
+ int middle = inputSize/2;
+ genCardinalDefinitions(from, from+middle, lits, linkingAlpha);
+ genCardinalDefinitions(from+middle+1, to, lits, linkingBeta);
+ } else if (inputSize == 2) {
+ genCardinalDefinitions(from, from, lits, linkingAlpha);
+ genCardinalDefinitions(to, to, lits, linkingBeta);
+ }
+ if (inputSize == 1) {
+ linkingVar.push(Lit(varZero));
+ linkingVar.push(Lit(from));
+ linkingVar.push(Lit(varLast));
+ } else { // inputSize >= 2
+
+ write_comment("- Node clauses:");
+ linkingVar.push(Lit(varZero));
+ for (int i = 0; i < inputSize; i++) linkingVar.push(Lit(variable_counter++));
+ linkingVar.push(Lit(varLast));
+
+ for (int sigma = 0; sigma <= inputSize; sigma++) {
+
+ // Verify if PB cardinality definition exists
+ if(C1_store.find(var(linkingVar[sigma])) == C1_store.end()) {
+
+ // Write a substitution redundancy PB cardinality definition
+ write_P1_sub_red_cardinality(var(linkingVar[sigma]), sigma, from, to);
+ }
+
+ // Verify if PB cardinality definition exists
+ if(C2_store.find(var(~linkingVar[sigma+1])) == C2_store.end()) {
+
+ // Write a substitution redundancy PB cardinality definition
+ write_P2_sub_red_cardinality(var(~linkingVar[sigma+1]), sigma+1, from, to);
+ }
+ }
+ write_comment("-------------------------------------------");
+ }
+ linkingAlpha.clear();
+ linkingBeta.clear();
+}
\ No newline at end of file
diff -ruN src_tmp/core/Prooflogger.h src/core/Prooflogger.h
--- src_tmp/core/Prooflogger.h 1970-01-01 01:00:00.000000000 +0100
+++ src/core/Prooflogger.h 2022-02-19 12:06:50.626500104 +0100
@@ -0,0 +1,79 @@
+#ifndef Prooflogger_h
+#define Prooflogger_h
+
+#include <cstdio>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <map>
+#include <string>
+#include <regex>
+
+#include "Vec.h"
+#include "Heap.h"
+#include "Alg.h"
+
+#include "SolverTypes.h"
+
+//=================================================================================================
+// Prooflogger
+
+
+class Prooflogger {
+public:
+
+ // Formula information
+ //
+ int formula_length = 0;
+ int n_variables = 0;
+
+ // Constraint counter
+ //
+ int constraint_counter = 0;
+ int last_bound_constraint_id;
+
+ // Tree derivation
+ //
+ int variable_counter = 0;
+
+ // Meaningful variable names
+ bool meaningful_names = false;
+ std::map<int, int> meaningful_name_LB;
+ std::map<int, int> meaningful_name_UB;
+ std::map<int, int> meaningful_name_n;
+
+ // Constraint stores
+ //
+ std::map<int, int> C1_store;
+ std::map<int, int> C2_store;
+
+ // Proof file
+ std::ofstream proof;
+ const char *proof_file_name = "maxsat_proof.pbp";
+ void set_proof_name (const char* name) {proof_file_name = name;};
+ void open_proof_file (){proof.open(proof_file_name);};
+ void close_proof_file (){proof.close();};
+ void write_tree_derivation ();
+ void write_proof_header (int nbclause);
+ void write_comment (const char* comment);
+ void write_contradiction ();
+ void write_empty_clause ();
+ bool is_aux_var (int var);
+ std::string var_name (int var);
+ void write_literal (Lit literal);
+ void write_literal_assignment (lbool assignment, int var);
+ void write_witness (Lit literal);
+ void write_clause (vec<Lit>& clause);
+ void write_learnt_clause (vec<Lit>& clause);
+ void write_linkingVar_clause (vec<Lit>& clause);
+ void write_bound_update (vec<lbool>& model);
+ void write_unit_sub_red (vec<Lit>& definition, int sigma, int from, int to);
+ void write_C1 (vec<Lit>& definition, int sigma, int from, int to);
+ void write_P1_sub_red_cardinality (int var, int sigma, int from, int to);
+ void write_C2 (vec<Lit>& definition, int sigma, int from, int to);
+ void write_P2_sub_red_cardinality (int var, int sigma, int from, int to);
+ void genCardinalDefinitions (int from, int to, vec<Lit>& lits, vec<Lit>& linkingVar);
+};
+
+//=================================================================================================
+#endif
\ No newline at end of file
diff -ruN src_tmp/core/Solver.C src/core/Solver.C
--- src_tmp/core/Solver.C 2022-02-19 16:47:37.927568213 +0100
+++ src/core/Solver.C 2022-02-19 12:06:50.626500104 +0100
@@ -26,7 +26,7 @@
// Constructor/Destructor:
-Solver::Solver() :
+Solver::Solver(Prooflogger *PL) :
// Parameters: (formerly in 'SearchParams')
var_decay(1 / 0.95), clause_decay(1 / 0.999), random_var_freq(0.02)
@@ -53,6 +53,10 @@
, random_seed (91648253)
, progress_estimate(0)
, remove_satisfied (true)
+
+ // Prooflogger
+ //
+ , PL(PL)
{}
@@ -544,7 +548,7 @@
| all variables are decision variables, this means that the clause set is satisfiable. 'l_False'
| if the clause set is unsatisfiable. 'l_Undef' if the bound on number of conflicts is reached.
|________________________________________________________________________________________________@*/
-lbool Solver::search(int nof_conflicts, int nof_learnts)
+lbool Solver::search(int nof_conflicts, int nof_learnts)
{
assert(ok);
int backtrack_level;
@@ -560,12 +564,19 @@
if (confl != NULL){
// CONFLICT
conflicts++; conflictC++;
- if (decisionLevel() == 0) return l_False;
+ if (decisionLevel() == 0) {
+ PL->write_empty_clause();
+ return l_False;
+ }
first = false;
learnt_clause.clear();
analyze(confl, learnt_clause, backtrack_level);
+
+ // Write the learnt clause to the proof file
+ PL->write_learnt_clause(learnt_clause);
+
cancelUntil(backtrack_level);
assert(value(learnt_clause[0]) == l_Undef);
@@ -592,8 +603,10 @@
return l_Undef; }
// Simplify the set of problem clauses:
- if (decisionLevel() == 0 && !simplify())
+ if (decisionLevel() == 0 && !simplify()) {
+ PL->write_empty_clause();
return l_False;
+ }
if (nof_learnts >= 0 && learnts.size()-nAssigns() >= nof_learnts)
// Reduce the set of learnt clauses:
@@ -654,7 +667,10 @@
model.clear();
conflict.clear();
- if (!ok) return false;
+ if (!ok) {
+ PL->write_empty_clause();
+ return false;
+ }
assumps.copyTo(assumptions);
diff -ruN src_tmp/core/Solver.h src/core/Solver.h
--- src_tmp/core/Solver.h 2022-02-19 16:47:37.927568213 +0100
+++ src/core/Solver.h 2022-02-19 12:06:50.626500104 +0100
@@ -21,12 +21,15 @@
#define Solver_h
#include <cstdio>
+#include <fstream>
+#include <iostream>
#include "Vec.h"
#include "Heap.h"
#include "Alg.h"
#include "SolverTypes.h"
+#include "Prooflogger.h"
//=================================================================================================
@@ -38,9 +41,13 @@
// Constructor/Destructor:
//
- Solver();
+ Solver(Prooflogger *PL);
~Solver();
+ // Proof logging:
+ //
+ Prooflogger* PL;
+
// Problem specification:
//
Var newVar (bool polarity = true, bool dvar = true); // Add a new variable with parameters specifying variable mode.
@@ -146,19 +153,19 @@
// Main internal methods:
//
- void insertVarOrder (Var x); // Insert a variable in the decision order priority queue.
- Lit pickBranchLit (int polarity_mode, double random_var_freq); // Return the next decision variable.
- void newDecisionLevel (); // Begins a new decision level.
- void uncheckedEnqueue (Lit p, Clause* from = NULL); // Enqueue a literal. Assumes value of literal is undefined.
- bool enqueue (Lit p, Clause* from = NULL); // Test if fact 'p' contradicts current state, enqueue otherwise.
- Clause* propagate (); // Perform unit propagation. Returns possibly conflicting clause.
- void cancelUntil (int level); // Backtrack until a certain level.
- void analyze (Clause* confl, vec<Lit>& out_learnt, int& out_btlevel); // (bt = backtrack)
- void analyzeFinal (Lit p, vec<Lit>& out_conflict); // COULD THIS BE IMPLEMENTED BY THE ORDINARIY "analyze" BY SOME REASONABLE GENERALIZATION?
- bool litRedundant (Lit p, uint32_t abstract_levels); // (helper method for 'analyze()')
- lbool search (int nof_conflicts, int nof_learnts); // Search for a given number of conflicts.
- void reduceDB (); // Reduce the set of learnt clauses.
- void removeSatisfied (vec<Clause*>& cs); // Shrink 'cs' to contain only non-satisfied clauses.
+ void insertVarOrder (Var x); // Insert a variable in the decision order priority queue.
+ Lit pickBranchLit (int polarity_mode, double random_var_freq); // Return the next decision variable.
+ void newDecisionLevel (); // Begins a new decision level.
+ void uncheckedEnqueue (Lit p, Clause* from = NULL); // Enqueue a literal. Assumes value of literal is undefined.
+ bool enqueue (Lit p, Clause* from = NULL); // Test if fact 'p' contradicts current state, enqueue otherwise.
+ Clause* propagate (); // Perform unit propagation. Returns possibly conflicting clause.
+ void cancelUntil (int level); // Backtrack until a certain level.
+ void analyze (Clause* confl, vec<Lit>& out_learnt, int& out_btlevel); // (bt = backtrack)
+ void analyzeFinal (Lit p, vec<Lit>& out_conflict); // COULD THIS BE IMPLEMENTED BY THE ORDINARIY "analyze" BY SOME REASONABLE GENERALIZATION?
+ bool litRedundant (Lit p, uint32_t abstract_levels); // (helper method for 'analyze()')
+ lbool search (int nof_conflicts, int nof_learnts); // Search for a given number of conflicts.
+ void reduceDB (); // Reduce the set of learnt clauses.
+ void removeSatisfied (vec<Clause*>& cs); // Shrink 'cs' to contain only non-satisfied clauses.
// Maintaining Variable/Clause activity:
//
diff -ruN src_tmp/minisat/core/depend.mk src/minisat/core/depend.mk
--- src_tmp/minisat/core/depend.mk 1970-01-01 01:00:00.000000000 +0100
+++ src/minisat/core/depend.mk 2022-02-19 12:06:50.626500104 +0100
@@ -0,0 +1,16 @@
+Main.o: Main.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h
+Solver.o: Solver.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h ../mtl/Sort.h
+Main.op: Main.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h
+Solver.op: Solver.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h ../mtl/Sort.h
+Main.od: Main.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h
+Solver.od: Solver.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h ../mtl/Sort.h
+Main.or: Main.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h
+Solver.or: Solver.C Solver.h ../mtl/Vec.h ../mtl/Heap.h ../mtl/Vec.h \
+ ../mtl/Alg.h SolverTypes.h ../mtl/Sort.h
diff -ruN src_tmp/minisat/core/Dimacs.h src/minisat/core/Dimacs.h
--- src_tmp/minisat/core/Dimacs.h 1970-01-01 01:00:00.000000000 +0100
+++ src/minisat/core/Dimacs.h 2022-02-19 12:06:50.626500104 +0100
@@ -0,0 +1,89 @@
+/****************************************************************************************[Dimacs.h]
+Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
+Copyright (c) 2007-2010, Niklas Sorensson
+
+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.
+**************************************************************************************************/
+
+#ifndef Minisat_Dimacs_h
+#define Minisat_Dimacs_h
+
+#include <stdio.h>
+
+#include "utils/ParseUtils.h"
+#include "core/SolverTypes.h"
+
+namespace Minisat {
+
+//=================================================================================================
+// DIMACS Parser:
+
+template<class B, class Solver>
+static void readClause(B& in, Solver& S, vec<Lit>& lits) {
+ int parsed_lit, var;
+ lits.clear();
+ for (;;){
+ parsed_lit = parseInt(in);
+ if (parsed_lit == 0) break;
+ var = abs(parsed_lit)-1;
+ while (var >= S.nVars()) S.newVar();
+ lits.push( (parsed_lit > 0) ? mkLit(var) : ~mkLit(var) );
+ }
+}
+
+template<class B, class Solver>
+static void parse_DIMACS_main(B& in, Solver& S) {
+ vec<Lit> lits;