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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
#include "internal.hpp"
#include <algorithm>
namespace CaDiCaL {
/*----------------------------------------------------------------------------*/
//
// Mark a variable as an observed one. It can be a new variable. It is
// assumed to be clean (not eliminated by previous simplifications).
//
void Internal::add_observed_var (int ilit) {
int idx = vidx (ilit);
if (idx >= (int64_t) relevanttab.size ())
relevanttab.resize (1 + (size_t) idx, 0);
unsigned &ref = relevanttab[idx];
if (ref < UINT_MAX) {
ref++;
LOG ("variable %d is observed %u times", idx, ref);
} else
LOG ("variable %d remains observed forever", idx);
// TODO: instead of actually backtracking, it would be enough to notify
// backtrack and re-play again every levels' notification to the
// propagator
if (val (ilit) && level && !fixed (ilit)) {
// The variable is already assigned, but we can not send a notification
// about it because it happened on an earlier decision level.
// To not break the stack-like view of the trail, we simply backtrack to
// undo this unnotifiable assignment.
REQUIRE (!conflict,
"can not observe assigned variable during conflict analysis");
const int assignment_level = var (ilit).level;
backtrack (assignment_level - 1);
} else if (level && fixed (ilit)) {
backtrack (0);
}
}
/*----------------------------------------------------------------------------*/
//
// Removing an observed variable should happen only once it is ensured
// that there is no unexplained propagation in the implication
// graph involving this variable. To ensure that, the solver might
// need to backtrack so that the variable becomes unassigned.
//
void Internal::remove_observed_var (int ilit) {
if (!fixed (ilit) && level && val (ilit)) {
REQUIRE (
!conflict,
"can not unobserve assigned variable during conflict analysis");
const int assignment_level = var (ilit).level;
backtrack (assignment_level - 1);
}
assert (fixed (ilit) || !val (ilit));
const int idx = vidx (ilit);
assert ((size_t) idx < relevanttab.size ());
unsigned &ref = relevanttab[idx];
assert (fixed (ilit) || ref > 0);
if (fixed (ilit))
ref = 0;
else if (ref < UINT_MAX) {
if (!--ref) {
LOG ("variable %d is not observed anymore", idx);
} else
LOG ("variable %d is unobserved once but remains observed %u times",
ilit, ref);
} else
LOG ("variable %d remains observed forever", idx);
}
/*----------------------------------------------------------------------------*/
//
// Supposed to be used only by mobical.
//
bool Internal::observed (int ilit) const {
assert ((size_t) vidx (ilit) < relevanttab.size ());
return relevanttab[vidx (ilit)] > 0;
}
/*----------------------------------------------------------------------------*/
//
// Check for unexplained propagations upon disconnecting external propagator
//
void Internal::set_changed_val () {
if (!opts.ilb) {
return;
}
for (auto idx : vars) {
if (!val (idx))
continue;
if (var (idx).reason != external_reason)
continue;
if (!changed_val) {
changed_val = idx;
continue;
}
assert (val (changed_val));
if (var (idx).level < var (changed_val).level) {
changed_val = idx;
}
}
}
void Internal::renotify_trail_after_ilb () {
assert (opts.ilb);
if (!external_prop || external_prop_is_lazy || !trail.size () ||
!opts.ilb) {
return;
}
LOG ("notify external propagator about new assignments (after ilb)");
#ifndef NDEBUG
LOG ("(decision level: %d, trail size: %zd, notified %zd)", level,
trail.size (), notified);
#endif
renotify_full_trail ();
}
void Internal::renotify_trail_after_local_search () {
if (!external_prop || external_prop_is_lazy || !trail.size ()) {
return;
}
LOG ("notify external propagator about new assignments (after local "
"search)");
#ifndef NDEBUG
LOG ("(decision level: %d, trail size: %zd, notified %zd)", level,
trail.size (), notified);
#endif
renotify_full_trail ();
}
void Internal::renotify_full_trail_between_trail_pos (
int start_level, int end_level, int propagator_level,
std::vector<int> &assigned, bool start_new_level) {
assert (assigned.empty ());
int j = start_level;
#ifdef LOGGING
LOG ("starting notification of level %d from trail %d .. %d",
propagator_level, start_level, end_level);
#else
(void) propagator_level;
#endif
if (start_new_level) {
if (assigned.size ())
external->propagator->notify_assignment (assigned);
assigned.clear ();
external->propagator->notify_new_decision_level ();
}
for (; j < end_level; ++j) {
int ilit = trail[j];
// In theory, 0 ilit can happen due to pseudo-decision levels
if (!ilit)
continue;
if (!observed (ilit))
continue;
int elit = externalize (ilit); // TODO: double-check tainting
LOG ("notifying elit %d @ %d aka %s", propagator_level, elit,
LOGLIT (ilit));
assert (elit);
// Fixed variables might get mapped (during compact) to another
// non-observed but fixed variable.
// This happens on root level, so notification about their assignment
// is already done.
assert (external->observed (elit) || fixed (ilit));
if (!external->ervars[abs (elit)])
assigned.push_back (elit);
}
if (assigned.size ())
external->propagator->notify_assignment (assigned);
assigned.clear ();
}
// It repeats ALL assignments of the trail, so the already notified
// root-level assignments will be notified multiple times.
//
// As CaDiCaL is missing some '0' seperators, it is important to go
// over slices from the control stack instead of going over the trail
// directly.
void Internal::renotify_full_trail () {
const size_t end_of_trail = trail.size ();
if (level) {
notified = 0; // TODO: save the last notified root-level position
// somewhere and use it here
notify_backtrack (0);
}
std::vector<int> assigned;
int propagator_level = 0;
const int c_size = control.size ();
{ // first all root-level literals
const int start_level = 0;
const int end_level =
(control.size () > 1 ? control[1].trail : end_of_trail);
renotify_full_trail_between_trail_pos (
start_level, end_level, propagator_level, assigned, false);
}
// notify all intermediate levels
for (int i = 2; i < c_size; ++i) {
const int start_level = control[i - 1].trail;
const int end_level = control[i].trail;
propagator_level++;
LOG ("notification of %d", propagator_level);
renotify_full_trail_between_trail_pos (
start_level, end_level, propagator_level, assigned, true);
}
// and the current level if there is non-root level one
if (level) {
const int start_level = control.back ().trail;
propagator_level++;
renotify_full_trail_between_trail_pos (
start_level, end_of_trail, propagator_level, assigned, true);
}
assert (propagator_level == level);
notified = trail.size ();
return;
}
/*----------------------------------------------------------------------------*/
//
// Check if the variable is assigned by decision.
//
bool Internal::is_decision (int ilit) {
if (!level || fixed (ilit) || !val (ilit))
return false;
const int idx = vidx (ilit);
Var &v = var (idx);
#ifndef NDEBUG
LOG (v.reason,
"is_decision: i%d (current level: %d, is_fixed: %d, v.level: %d, "
"is_external_reason: %d, v.reason: )",
ilit, level, fixed (ilit), v.level, v.reason == external_reason);
#endif
if (!v.level || v.reason)
return false;
assert (!v.reason);
return true;
}
void Internal::force_backtrack (int new_level) {
REQUIRE (forced_backt_allowed,
"not allowed to force backtrack in that state of the solver.");
REQUIRE (new_level >= 0,
"the target level of a forced backtrack must be non-negative.");
REQUIRE (level > 0 && new_level < level,
"the target level of a forced backtrack must be smaller than "
"the current decision level.");
#ifndef NDEBUG
LOG ("external propagator forces backtrack to decision level"
"%d (from level %d)",
new_level, level);
#endif
backtrack (new_level);
}
/*----------------------------------------------------------------------------*/
//
// Call external propagator to check if there is a literal to be propagated.
// The reason of the propagation is not necessarily asked at that point.
//
// In case the externally propagated literal is already falsified, the
// reason is asked and conflict analysis starts. In case the externally
// propagated literal is already satisfied, nothing happens.
//
bool Internal::external_propagate () {
if (level)
require_mode (SEARCH);
assert (!unsat);
size_t before = num_assigned;
bool cb_repropagate_needed = true;
while (cb_repropagate_needed && !conflict && external_prop &&
!external_prop_is_lazy && !private_steps) {
#ifndef NDEBUG
LOG ("external propagation starts (decision level: %d, trail size: "
"%zd, notified %zd)",
level, trail.size (), notified);
#endif
cb_repropagate_needed = false;
// external->reset_extended (); //TODO for inprocessing
notify_assignments ();
int elit = external->propagator->cb_propagate ();
REQUIRE (
!elit || ((size_t) abs (elit) < external->is_observed.size () &&
external->is_observed[abs (elit)]),
"external propagations are only allowed over observed variables.");
stats.ext_prop.ext_cb++;
stats.ext_prop.eprop_call++;
while (elit) {
assert (external->is_observed[abs (elit)]);
int ilit = external->e2i[abs (elit)];
if (elit < 0)
ilit = -ilit;
int tmp = val (ilit);
#ifndef NDEBUG
assert (fixed (ilit) || observed (ilit));
LOG ("External propagation of e%d (i%d val: %d)", elit, ilit, tmp);
#endif
if (!tmp) {
// variable is not assigned, it can be propagated
if (!level) {
Clause *res = learn_external_reason_clause (ilit, elit);
#ifndef LOGGING
LOG (res, "reason clause of external propagation of %d:", elit);
#endif
(void) res;
} else
search_assign_external (ilit);
stats.ext_prop.eprop_prop++;
if (unsat || conflict)
break;
propagate ();
if (unsat || conflict)
break;
notify_assignments ();
} else if (tmp < 0) {
LOG ("External propgation of %d is falsified under current trail",
ilit);
stats.ext_prop.eprop_conf++;
int level_before = level;
size_t assigned = num_assigned;
Clause *res = learn_external_reason_clause (ilit, elit);
#ifndef LOGGING
LOG (res, "reason clause of external propagation of %d:", elit);
#endif
(void) res;
bool trail_changed =
(num_assigned != assigned || level != level_before ||
propagated < trail.size ());
if (unsat || conflict)
break;
if (trail_changed) {
propagate ();
if (unsat || conflict)
break;
notify_assignments ();
}
} // else (tmp > 0) -> the case of a satisfied literal is ignored
elit = external->propagator->cb_propagate ();
stats.ext_prop.ext_cb++;
stats.ext_prop.eprop_call++;
}
#ifndef NDEBUG
LOG ("External propagation ends (decision level: %d, trail size: %zd, "
"notified %zd)",
level, trail.size (), notified);
#endif
if (!unsat && !conflict) {
int level_before = level;
size_t assigned = num_assigned;
bool has_external_clause = ask_external_clause ();
// New observed variable might have triggered a backtrack during this
// ask_external_clause call, so we need to propagate before continuing
stats.ext_prop.ext_cb++;
stats.ext_prop.elearn_call++;
bool trail_changed =
(num_assigned != assigned || level != level_before ||
propagated < trail.size ());
if (trail_changed) {
propagate (); // unsat or conflict will be caught later
if (!unsat || !conflict)
notify_assignments ();
}
#ifndef NDEBUG
if (has_external_clause)
LOG ("New external clauses are to be added.");
else
LOG ("No external clauses to be added.");
#endif
while (has_external_clause) {
level_before = level;
assigned = num_assigned;
add_external_clause (0);
trail_changed =
(num_assigned != assigned || level != level_before ||
propagated < trail.size ());
cb_repropagate_needed = true;
if (unsat || conflict) {
cb_repropagate_needed = false;
break;
}
if (trail_changed) {
propagate ();
if (unsat || conflict) {
cb_repropagate_needed = false;
break;
}
notify_assignments ();
}
has_external_clause = ask_external_clause ();
stats.ext_prop.ext_cb++;
stats.ext_prop.elearn_call++;
}
}
#ifndef NDEBUG
LOG ("External clause addition ends on decision level %d at trail "
"size "
"%zd (notified %zd)",
level, trail.size (), notified);
#endif
}
if (before < num_assigned)
did_external_prop = true;
return !conflict;
}
/*----------------------------------------------------------------------------*/
//
// Helper function, calls 'cb_has_external_clause', while maintains the
// related redundancy type of the clause.
//
bool Internal::ask_external_clause () {
ext_clause_forgettable = false;
bool res =
external->propagator->cb_has_external_clause (ext_clause_forgettable);
return res;
}
/*----------------------------------------------------------------------------*/
//
// Literals of the externally learned clause must be reordered based on the
// assignment levels of the literals.
//
void Internal::move_literals_to_watch () {
if (clause.size () < 2)
return;
if (!level)
return;
for (int i = 0; i < 2; i++) {
int highest_position = i;
int highest_literal = clause[i];
int highest_level = var (highest_literal).level;
int highest_value = val (highest_literal);
for (size_t j = i + 1; j < clause.size (); j++) {
const int other = clause[j];
const int other_level = var (other).level;
const int other_value = val (other);
if (other_value < 0) {
if (highest_value >= 0)
continue;
if (other_level <= highest_level)
continue;
} else if (other_value > 0) {
if (highest_value > 0 && other_level >= highest_level)
continue;
} else {
if (highest_value >= 0)
continue;
}
highest_position = j;
highest_literal = other;
highest_level = other_level;
highest_value = other_value;
}
#ifndef NDEBUG
LOG ("highest position: %d highest level: %d highest value: %d",
highest_position, highest_level, highest_value);
#endif
if (highest_position == i)
continue;
if (highest_position > i) {
std::swap (clause[i], clause[highest_position]);
}
}
}
/*----------------------------------------------------------------------------*/
//
// Reads out from the external propagator the lemma/proapgation reason
// clause literal by literal. In case propagated_elit is 0, it is about an
// external clause via 'cb_add_external_clause_lit'. Otherwise, it is about
// learning the reason of 'propagated_elit' via 'cb_add_reason_clause_lit'.
// The learned clause is simplified by the current root-level assignment
// (i.e. root-level falsified literals are removed, root satisfied clauses
// are skipped). Duplicate literals are removed, tauotologies are detected
// and skipped. It always adds the original (un-simplified) external clause
// to the proof as an input clause and
// the simplified version of it (except exceptions below) as a derived
// clause.
//
// In case the external clause, after simplifications, is satisfied, no
// clause is constructed, and the function returns 0. In case the external
// clause, after simplifications, is empty, no clause is constructed, unsat
// is set true and the function returns 0. In case the external clause,
// after simplifications, is unit, no clause is constructed,
// 'Internal::clause' has the unit literal (without 0) and the function
// returns 0.
//
// In every other cases a new clause is constructed and the pointer is in
// newest_clause
//
void Internal::add_external_clause (int propagated_elit,
bool no_backtrack) {
assert (original.empty ());
int elit = 0;
bool propagated_lit_found = false;
if (propagated_elit) {
// Propagation reason clauses are by default assumed to be forgettable
// irredundant. In case they would be unforgettably important, the
// propagator can add them as an explicit unforgettable external clause
// or set 'are_reasons_forgettable' to false.
ext_clause_forgettable = external->propagator->are_reasons_forgettable;
#ifndef NDEBUG
LOG ("add external reason of propagated lit: %d", propagated_elit);
#endif
elit = external->propagator->cb_add_reason_clause_lit (propagated_elit);
if (elit == propagated_elit)
propagated_lit_found = true;
} else
elit = external->propagator->cb_add_external_clause_lit ();
REQUIRE (
!elit || ((size_t) abs (elit) < external->is_observed.size () &&
external->is_observed[abs (elit)]),
"external (reason) clause must contain only observed variables.");
// we need to be build a new LRAT chain if we are already in the middle of
// the analysis (like during failed assumptions)
LOG (lrat_chain, "lrat chain before");
std::vector<int64_t> lrat_chain_ext = std::move (lrat_chain);
lrat_chain.clear ();
clause.clear ();
// Read out the external lemma into original and simplify it into clause
assert (clause.empty ());
assert (original.empty ());
assert (!force_no_backtrack);
assert (!from_propagator);
force_no_backtrack = no_backtrack;
from_propagator = true;
while (elit) {
external->add (elit);
if (propagated_elit) {
elit =
external->propagator->cb_add_reason_clause_lit (propagated_elit);
if (elit == propagated_elit)
propagated_lit_found = true;
} else
elit = external->propagator->cb_add_external_clause_lit ();
REQUIRE (
!elit || ((size_t) abs (elit) < external->is_observed.size () &&
external->is_observed[abs (elit)]),
"external (reason) clause must contain only observed variables.");
}
external->add (elit);
REQUIRE (!propagated_elit || propagated_lit_found,
"external reason clause must contain the propagated literal.");
#ifdef NCONTRACTS
(void) propagated_lit_found;
#endif
assert (original.empty ());
assert (clause.empty ());
force_no_backtrack = false;
from_propagator = false;
lrat_chain = std::move (lrat_chain_ext);
LOG (lrat_chain, "lrat chain after");
}
/*----------------------------------------------------------------------------*/
//
// Recursively calls 'learn_external_reason_clause' to explain every
// backward reachable externally propagated literal starting from 'ilit'.
//
void Internal::explain_reason (int ilit, Clause *reason, int &open) {
if (!opts.exteagerreasons)
return;
#ifndef NDEBUG
LOG (reason, "explain_reason of %d (open: %d)", ilit, open);
#endif
assert (reason);
assert (reason != external_reason);
for (const auto &other : *reason) {
if (other == ilit)
continue;
Flags &f = flags (other);
if (f.seen)
continue;
Var &v = var (other);
if (!v.level)
continue;
assert (val (other) < 0);
assert (v.level <= level);
if (v.reason == external_reason) {
v.reason = learn_external_reason_clause (-other, 0, true);
}
if (v.level && v.reason) {
f.seen = true;
open++;
}
}
}
/*----------------------------------------------------------------------------*/
//
// In case external propagation was used, the reason clauses of the relevant
// propagations must be learned lazily before/during conflict analysis.
// While conflict analysis needs to analyze only the current level, lazy
// clause learning must check every clause on every level that is backward
// reachable from the conflicting clause to guarantee that the assignment
// levels of the variables are accurate. So this explanation round is
// separated from the conflict analysis, thereby guranteeing that the flags
// and datastructures can be properly used later.
//
// This function must be called before the conflict analysis, in order to
// guarantee that every relevant reason clause is indeed learned already and
// to be sure that the levels of assignments are set correctly.
//
// Later TODO: experiment with bounded explanation (explain only those
// literals that are directly used during conflict analysis +
// minimizing/shrinking). The assignment levels are then only
// over-approximated.
//
void Internal::explain_external_propagations () {
assert (conflict);
assert (clause.empty ());
Clause *reason = conflict;
std::vector<int> seen_lits;
int open = 0; // Seen but not explained literal
explain_reason (0, reason, open); // marks conflict clause lits as seen
int i = trail.size (); // Start at end-of-trail
while (i > 0) {
const int lit = trail[--i];
if (!flags (lit).seen)
continue;
seen_lits.push_back (lit);
Var &v = var (lit);
if (!v.level)
continue;
if (v.reason) {
open--;
explain_reason (lit, v.reason, open);
}
if (!open)
break;
}
assert (!open);
if (!opts.exteagerrecalc) {
for (auto lit : seen_lits) {
Flags &f = flags (lit);
f.seen = false;
}
#ifndef NDEBUG
for (auto idx : vars) {
assert (!flags (idx).seen);
}
#endif
}
// Traverse now in the opposite direction (from lower to higher levels)
// and calculate the actual assignment level for the seen assignments.
for (auto it = seen_lits.rbegin (); it != seen_lits.rend (); ++it) {
const int lit = *it;
Flags &f = flags (lit);
Var &v = var (lit);
if (v.reason) {
int real_level = 0;
for (const auto &other : *v.reason) {
if (other == lit)
continue;
int tmp = var (other).level;
if (tmp > real_level)
real_level = tmp;
}
if (v.level && !real_level) {
build_chain_for_units (lit, v.reason, 1);
learn_unit_clause (lit);
lrat_chain.clear ();
v.reason = 0;
}
assert (v.level >= real_level);
if (v.level > real_level) {
v.level = real_level;
}
}
f.seen = false;
}
#if 0 // has been fuzzed extensively
for (auto idx : vars) {
assert (!flags (idx).seen);
}
#endif
}
/*----------------------------------------------------------------------------*/
//
// Learns the reason clause of the propagation of ilit from the
// external propagator via 'add_external_clause'.
// In case of falsified propagation steps, if the propagated literal is
// already fixed to the opposite value, externalize will not necessarily
// give back the original elit (but an equivalent one). To avoid that, in
// falsified propagation cases the propagated elit is added as a second
// argument.
//
Clause *Internal::learn_external_reason_clause (int ilit,
int falsified_elit,
bool no_backtrack) {
assert (external->propagator); // REQ is defined by not allowing
// unobserving during conflict
// we cannot modify clause during analysis
auto clause_tmp = std::move (clause);
assert (clause.empty ());
assert (original.empty ());
stats.ext_prop.eprop_expl++;
int elit = 0;
if (!falsified_elit) {
assert (!fixed (ilit));
elit = externalize (ilit);
} else
elit = falsified_elit;
LOG ("ilit: %d, elit: %d", ilit, elit);
add_external_clause (elit, no_backtrack);
#ifndef NDEBUG
if (!falsified_elit && newest_clause) {
// Check if external propagation is correct wrt to the topological order
// defined by the trail. In case it is a falsified external propagation
// step, the order does not matter, the reason simply supposed to be a
// falsified clause.
const int propagated_ilit = ilit;
for (auto const reason_ilit : *newest_clause) {
assert (var (reason_ilit).trail <= var (propagated_ilit).trail);
}
}
#endif
clause = std::move (clause_tmp);
return newest_clause;
}
/*----------------------------------------------------------------------------*/
//
// Helper function to be able to call learn_external_reason_clause when the
// internal clause is already used in the caller side (for example during
// proof checking). These calls are assumed to be without a falsified elit.
// Dont use it in general instead of learn_external_reason_clause because it
// does not support the corner cases where a literal remains in clause.
//
Clause *Internal::wrapped_learn_external_reason_clause (int ilit) {
Clause *res;
std::vector<int64_t> chain_tmp{std::move (lrat_chain)};
lrat_chain.clear ();
if (clause.empty ()) {
res = learn_external_reason_clause (ilit, 0, true);
} else {
std::vector<int> clause_tmp{std::move (clause)};
clause.clear ();
res = learn_external_reason_clause (ilit, 0, true);
// The learn_external_reason clause can leave a literal in clause when
// there is a falsified elit arg. Here it is not allowed to
// happen.
assert (clause.empty ());
clause = std::move (clause_tmp);
clause_tmp.clear ();
}
assert (lrat_chain.empty ());
lrat_chain = std::move (chain_tmp);
chain_tmp.clear ();
return res;
}
/*----------------------------------------------------------------------------*/
//
// Checks if the new clause forces backtracking, new assignments or conflict
// analysis
//
void Internal::handle_external_clause (Clause *res) {
if (from_propagator)
stats.ext_prop.elearned++;
// at level 0 we have to do nothing...
if (!level)
return;
if (!res) {
if (from_propagator)
stats.ext_prop.elearn_prop++;
// new unit clause. For now just backtrack.
assert (!force_no_backtrack);
assert (level);
// if (!opts.chrono) {
backtrack ();
// }
return;
}
if (from_propagator)
stats.ext_prop.elearned++;
assert (res->size >= 2);
const int pos0 = res->literals[0];
const int pos1 = res->literals[1];
if (force_no_backtrack) {
assert (val (pos1) < 0);
assert (val (pos0) >= 0);
return;
// TODO: maybe fix levels
}
const int l1 = var (pos1).level;
if (val (pos0) < 0) { // conflicting or propagating clause
assert (0 < l1 && l1 <= var (pos0).level);
if (!opts.chrono) {
backtrack (l1);
}
if (val (pos0) < 0) {
conflict = res;
if (!from_propagator) {
// its better to backtrack instead of analyze
backtrack (l1 - 1);
conflict = 0;
assert (!val (pos0) && !val (pos1));
}
} else {
search_assign_driving (pos0, res);
}
if (from_propagator)
stats.ext_prop.elearn_conf++;
return;
}
if (val (pos1) < 0 && !val (pos0)) { // propagating clause
if (!opts.chrono) {
backtrack (l1);
}
search_assign_driving (pos0, res);
if (from_propagator)
stats.ext_prop.elearn_conf++;
return;
}
}
/*----------------------------------------------------------------------------*/
//
// Asks the external propagator if the current solution is OK
// by calling 'cb_check_found_model (model)'.
//
// The checked model is built up after everything is restored
// from the reconstruction stack and every variable is reactivated
// and so it is not just simply the trail (i.e. it can be expensive).
//
// If the external propagator approves the model, the function
// returns true.
//
// If the propagator does not approve the model, the solver asks
// the propagator to add an external clause.
// This external clause, however, does NOT have to be falsified by
// the current model. The possible cases and reactions are described
// below in the function. The possible states after that function:
// - A solution was found and accepted by the external propagator
// - A conflicting clause was learned from the external propagator
// - The empty clause was learned due to something new learned from
// the external propagator.
//
// In case only new variables were introduced, but no new clauses were
// added, the function will return without a conflict to the outer CDCL
// loop, where the new (not yet satisfied) variables are recognized and
// the search continues.
bool Internal::external_check_solution () {
if (!external_prop)
return true;
bool trail_changed = true;
bool added_new_clauses = false;
while (trail_changed || added_new_clauses) {
notify_assignments ();
if (!satisfied ())
break;
trail_changed = false; // to be on the safe side
added_new_clauses = false;
LOG ("Final check by external propagator is invoked.");
stats.ext_prop.echeck_call++;
external->reset_extended ();
external->extend ();
std::vector<int> etrail;
// Here the variables must be filtered by external->is_observed,
// because fixed variables are internally not necessarily observed
// anymore.
for (int idx = 1;
idx <= std::min ((int) external->is_observed.size () - 1,
external->max_var);
idx++) {
if (!external->is_observed[idx])
continue;
const int lit = external->ival (idx);
etrail.push_back (lit);
#ifndef NDEBUG
#ifdef LOGGING
bool p = external->vals[idx];
LOG ("evals[%d]: %d ival(%d): %d", idx, p, idx, lit);
#endif
#endif
}
forced_backt_allowed = true;
size_t assigned = num_assigned;
int level_before = level;
bool is_consistent =
external->propagator->cb_check_found_model (etrail);
stats.ext_prop.ext_cb++;
forced_backt_allowed = false;
if (num_assigned != assigned || level != level_before ||
propagated < trail.size ()) {
// In case an external forced backtracking was performed, the CDCL
// loop needs to continue withouth further checks of the model.
trail_changed = true;
return !conflict;
}
if (is_consistent) {
LOG ("Found solution is approved by external propagator.");
return true;
}
bool has_external_clause = ask_external_clause ();
stats.ext_prop.ext_cb++;
stats.ext_prop.elearn_call++;
if (has_external_clause)
LOG (
"Found solution triggered new clauses from external propagator.");
while (has_external_clause) {
level_before = level;
assigned = num_assigned;
add_external_clause (0);
bool trail_changed =
(num_assigned != assigned || level != level_before ||
propagated < trail.size ());
added_new_clauses = true;
//
// There are many possible scenarios here:
// - Learned conflicting clause: return to CDCL loop (conflict true)
// - Learned conflicting unit clause that after backtrack+BCP leads to
// a new complete solution: force the outer loop to check the new
// model (trail_changed is true, but (conflict & unsat) is false)
// - Learned empty clause: return to CDCL loop (unsat true)
// - Learned a non-conflicting unit clause:
// Though it does not invalidate the current solution, the solver
// will backtrack to the root level and will repropagate it. The
// search will start again (saved phases hopefully make it quick),
// but it is needed in order to guarantee that every fixed variable
// is properly handled+notified (important for incremental use
// cases).
// - Otherwise: the solution is considered approved and the CDCL-loop
// can return with res = 10.
//
if (unsat || conflict || trail_changed)
break;
has_external_clause = ask_external_clause ();
stats.ext_prop.ext_cb++;
stats.ext_prop.elearn_call++;
}
LOG ("No more external clause to add.");
if (unsat || conflict)
break;
}
if (!unsat && conflict) {
const int conflict_level = var (conflict->literals[0]).level;
if (conflict_level != level) {
backtrack (conflict_level);
}
}
return !conflict;
}
/*----------------------------------------------------------------------------*/
//
// Notify the external propagator that an observed variable got assigned.
//
void Internal::notify_assignments () {
if (!external_prop || external_prop_is_lazy || private_steps)
return;
const size_t end_of_trail = trail.size ();
if (notified >= end_of_trail)
return;
LOG ("notify external propagator about new assignments");
std::vector<int> assigned;
while (notified < end_of_trail) {
int ilit = trail[notified++];
if (!observed (ilit))
continue;
int elit = externalize (ilit); // TODO: double-check tainting
assert (elit);
if (external->ervars[abs (elit)])
continue;
// Fixed variables might get mapped (during compact) to another
// non-observed but fixed variable.
// This happens on root level, so notification about their assignment is
// already done.
assert (external->observed (elit) ||
(fixed (ilit) && !external->ervars[abs (elit)]));
assigned.push_back (elit);
}
if (assigned.size ())
external->propagator->notify_assignment (assigned);
return;
}
/*----------------------------------------------------------------------------*/
void Internal::connect_propagator () {
if (level)
backtrack ();
}
/*----------------------------------------------------------------------------*/
//
// Notify the external propagator that a new decision level is started.
//
void Internal::notify_decision () {
if (!external_prop || external_prop_is_lazy || private_steps)
return;
external->propagator->notify_new_decision_level ();
}
/*----------------------------------------------------------------------------*/
//
// Notify the external propagator that backtrack to new_level.
//
void Internal::notify_backtrack (size_t new_level) {
if (!external_prop || external_prop_is_lazy || private_steps)
return;
external->propagator->notify_backtrack (new_level);
}
/*----------------------------------------------------------------------------*/
//
// Ask the external propagator if there is a suggested literal as next
// decision.
//
int Internal::ask_decision () {
if (!external_prop || external_prop_is_lazy || private_steps)
return 0;
assert (!unsat);
assert (!conflict);
notify_assignments ();
int level_before = level;
forced_backt_allowed = true;
int elit = external->propagator->cb_decide ();
forced_backt_allowed = false;
stats.ext_prop.ext_cb++;
if (level_before != level) {
propagate ();
assert (!unsat);
assert (!conflict);
notify_assignments ();
// In case the external propagator forced to backtrack below the
// pseduo decision levels, we must go back to the CDCL loop instead of
// making a decision.
if ((size_t) level < assumptions.size () ||
((size_t) level == assumptions.size () && constraint.size ())) {
return 0;
}
}
if (!elit)
return 0;
LOG ("external propagator proposes decision: %d", elit);
REQUIRE ((size_t) abs (elit) < external->is_observed.size () &&
external->is_observed[abs (elit)],
"external decisions are only allowed over observed variables.");
assert (external->is_observed[abs (elit)]);
int ilit = external->e2i[abs (elit)];
if (elit < 0)
ilit = -ilit;
assert (fixed (ilit) || observed (ilit));
LOG ("Asking external propagator for decision returned: %d (internal: "
"%d, fixed: %d, val: %d)",
elit, ilit, fixed (ilit), val (ilit));
REQUIRE (
!fixed (ilit) && !val (ilit),
"external decisions are only allowed over unassigned variables.");
return ilit;
}
/*----------------------------------------------------------------------------*/
//
// Check if the clause is a forgettable clause coming from the external
// propagator.
//
bool Internal::is_external_forgettable (int64_t id) {
assert (opts.check);
return (external->forgettable_original.find (id) !=
external->forgettable_original.end ());
}
/*----------------------------------------------------------------------------*/
//
// When an external forgettable clause is deleted, it is marked in the
// 'forgettable_original' hash, so that the internal model checking can
// ignore it.
//
void Internal::mark_garbage_external_forgettable (int64_t id) {
assert (opts.check);
assert (is_external_forgettable (id));
LOG (external->forgettable_original[id],
"forgettable external lemma is deleted:");
// Mark as removed by flipping the first flag to false.
external->forgettable_original[id][0] = 0;
}
/*----------------------------------------------------------------------------*/
//
// Check that the literals in the clause are properly ordered. Used only
// internally for debug purposes.
//
void Internal::check_watched_literal_invariants () {
#ifndef NDEBUG
int v0 = 0;
int v1 = 0;
if (val (clause[0]) > 0)
v0 = 1;
else if (val (clause[0]) < 0)
v0 = -1;
if (val (clause[1]) > 0)
v1 = 1;
else if (val (clause[1]) < 0)
v1 = -1;
assert (v0 >= v1);
#endif
if (val (clause[0]) > 0) {
if (val (clause[1]) > 0) { // Case 1: Both literals are satisfied
// They are ordered by lower to higher decision level
assert (var (clause[0]).level <= var (clause[1]).level);
// Every other literal of the clause is either
// - satisfied at higher level
// - unassigned
// - falsified
for (size_t i = 2; i < clause.size (); i++)
assert (val (clause[i]) <= 0 ||
(var (clause[1]).level <= var (clause[i]).level));
} else if (val (clause[1]) ==
0) { // Case 2: First satisfied, next unassigned
// Every other literal of the clause is either
// - unassigned
// - falsified
for (size_t i = 2; i < clause.size (); i++)
assert (val (clause[i]) <= 0);
} else { // Case 3: First satisfied, next falsified -> could have been a
// reason of a previous propagation
// Every other literal of the clause is falsified but at a lower
// decision level
for (size_t i = 2; i < clause.size (); i++)
assert (val (clause[i]) < 0 &&
(var (clause[1]).level >= var (clause[i]).level));
}
} else if (val (clause[0]) == 0) {
if (val (clause[1]) == 0) { // Case 4: Both literals are unassigned
// Every other literal of the clause is either
// - unassigned
// - falsified
for (size_t i = 2; i < clause.size (); i++)
assert (val (clause[i]) <= 0);
} else { // Case 5: First unassigned, next falsified -> PROPAGATE
// Every other literal of the clause is falsified but at a lower
// decision level
for (size_t i = 2; i < clause.size (); i++)
assert (val (clause[i]) < 0 &&
(var (clause[1]).level >= var (clause[i]).level));
}
} else {
assert (val (clause[0]) < 0 &&
val (clause[1]) < 0); // Case 6: Both literals are falsified
// They are ordered by higher to lower decision level
assert (var (clause[0]).level >= var (clause[1]).level);
// Every other literal of the clause is falsified, but at a lower level
for (size_t i = 2; i < clause.size (); i++)
assert (val (clause[i]) < 0 &&
(var (clause[1]).level >= var (clause[i]).level));
}
}
#ifndef NDEBUG
/*----------------------------------------------------------------------------*/
//
// An expensive function that can be used for deep-debug trail-related
// issues in mobical. Do not use it unless it is really unavoidable.
//
// eq_class contains all the merged external literals that are currently
// compacted to the internal literal of trail[0] and return true.
//
// In case trail[0] does not exists or is not on the root level, the
// function returns false (indicating that there was no merger literal
// found).
//
bool Internal::get_merged_literals (std::vector<int> &eq_class) {
eq_class.clear ();
if (!trail.size ())
return false;
int ilit = trail[0];
size_t lit_level = var (ilit).level;
if (!lit_level) {
// Collect all the variables that are merged and mapped to that ilit
size_t e2i_size = external->e2i.size ();
int ivar = abs (ilit);
for (size_t i = 0; i < e2i_size; i++) {
int other = abs (external->e2i[i]);
if (other == ivar) {
if (external->e2i[i] == ilit)
eq_class.push_back (i);
else
eq_class.push_back (-1 * i);
}
}
return true;
}
return false;
}
/*----------------------------------------------------------------------------*/
//
// Collect all external variables that are FIXED internally. Again an
// expensive function that should be called only for debugging in mobical.
//
// Do not use it unless it is really unavoidable.
//
void Internal::get_all_fixed_literals (std::vector<int> &fixed_lits) {
fixed_lits.clear ();
if (!trail.size ())
return;
int e2i_size = external->e2i.size ();
int ilit;
for (int eidx = 1; eidx < e2i_size; eidx++) {
ilit = external->e2i[eidx];
if (ilit && !external->ervars[eidx]) {
Flags &f = flags (ilit);
if (f.status == Flags::FIXED) {
fixed_lits.push_back (vals[abs (ilit)] * eidx);
}
}
}
}
#endif
} // namespace CaDiCaL