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
//! Conflict analysis, clause minimization, and assumption handling
use super::*;
use smallvec::SmallVec;
/// Compute LBD (Literals per Block Distance / "glue" score) from a set of clause literals.
///
/// LBD = number of distinct decision levels among the literals, excluding level 0.
/// Level-0 literals are excluded because they are consequences of unit propagation at the
/// root level and are always true — they do not contribute to the "block distance" that
/// measures how spread across the search tree a learned clause is.
///
/// This is an O(n) computation with no heap allocation in the common case
/// (`SmallVec<[u32; 32]>` avoids a heap allocation for clauses up to 32 distinct decision
/// levels, which covers the overwhelming majority of real CDCL learned clauses).
///
/// This is the standard Glucose/MiniSat LBD definition applied to the **actual learned
/// (1-UIP) clause literals**, so the value it returns satisfies `lbd <= literals.len()`.
/// It is a pure function (no shared scratch state) so it can be called at sites where a
/// `&mut self` borrow of the solver is unavailable — in particular after `self.learnt`
/// has been finalized but while `&self.trail` is borrowed to fire the external hook.
fn compute_lbd_from_literals(literals: &[Lit], trail: &Trail) -> u32 {
let mut levels: SmallVec<[u32; 32]> = SmallVec::new();
for &lit in literals {
let level = trail.level(lit.var());
if level > 0 && !levels.contains(&level) {
levels.push(level);
}
}
levels.len() as u32
}
impl Solver {
/// Analyze conflict and learn clause
pub(super) fn analyze(&mut self, conflict: ClauseId) -> (u32, SmallVec<[Lit; 16]>) {
// Debug: print conflict info (only with analyze-debug feature)
#[cfg(feature = "analyze-debug")]
if self.num_vars <= 5 {
eprintln!("[ANALYZE] Conflict clause id={:?}", conflict);
if let Some(c) = self.clauses.get(conflict) {
let lits_str: Vec<String> = c
.lits
.iter()
.map(|lit| {
let val = self.trail.lit_value(*lit);
let level = self.trail.level(lit.var());
let sign = if lit.is_pos() { "" } else { "~" };
format!("{}v{}@{}={:?}", sign, lit.var().index(), level, val)
})
.collect();
eprintln!("[ANALYZE] Conflict clause: ({})", lits_str.join(" | "));
}
eprintln!("[ANALYZE] Trail:");
for &lit in self.trail.assignments() {
let var = lit.var();
let level = self.trail.level(var);
let reason = self.trail.reason(var);
let sign = if lit.is_pos() { "" } else { "~" };
eprintln!(" {}v{}@{} reason={:?}", sign, var.index(), level, reason);
}
}
self.learnt.clear();
self.learnt.push(Lit::from_code(0)); // Placeholder for asserting literal
let mut counter = 0;
let mut p = None;
let mut index = self.trail.assignments().len();
let current_level = self.trail.decision_level();
// Reset seen flags
for s in &mut self.seen {
*s = false;
}
// Collect variables to bump in batch (avoids repeated heap sift-ups)
let mut vars_to_bump: SmallVec<[Var; 32]> = SmallVec::new();
let mut reason_clause = conflict;
while let Some(clause) = self.clauses.get(reason_clause) {
// Process reason clause (must exist, as it's either conflict or a propagation reason)
let start = if p.is_some() { 1 } else { 0 };
let is_learned = clause.learned;
// Record clause usage for tier promotion and bump activity (if it's a learned clause)
if is_learned && let Some(clause_mut) = self.clauses.get_mut(reason_clause) {
clause_mut.record_usage();
// Promote to Core if LBD ≤ 2 (GLUE clause)
if clause_mut.lbd <= 2 {
clause_mut.promote_to_core();
}
// Bump clause activity (MapleSAT-style)
clause_mut.activity += self.clause_bump_increment;
}
let Some(clause) = self.clauses.get(reason_clause) else {
break;
};
for &lit in &clause.lits[start..] {
let var = lit.var();
let level = self.trail.level(var);
if !self.seen[var.index()] && level > 0 {
self.seen[var.index()] = true;
// Collect variable for batch bumping instead of individual bumps
vars_to_bump.push(var);
if level == current_level {
counter += 1;
} else {
// Add the literal itself (not negated) to the learned clause.
// The conflict clause has all literals FALSE. To prevent this
// conflict, we need at least one of these literals to become TRUE.
self.learnt.push(lit);
}
}
}
// Find next literal to analyze
let mut current_lit = Lit::from_code(0); // sentinel default
let mut found_next = false;
loop {
if index == 0 {
// Guard against underflow: this should not happen in a
// well-formed conflict, but theory-conflict injection can
// occasionally produce a degenerate state. Break out to
// avoid a usize overflow panic.
break;
}
index -= 1;
current_lit = self.trail.assignments()[index];
p = Some(current_lit);
if self.seen[current_lit.var().index()] {
found_next = true;
break;
}
}
if !found_next {
break;
}
counter -= 1;
if counter == 0 {
break;
}
let var = current_lit.var();
match self.trail.reason(var) {
Reason::Propagation(c) => reason_clause = c,
_ => break,
}
}
// Batch bump all collected variables at once (single heap rebuild)
self.vsids.bump_batch(&vars_to_bump);
self.chb.bump_batch(&vars_to_bump);
self.lrb.on_reason_batch(&vars_to_bump);
// Set asserting literal (p is guaranteed to be Some at this point)
if let Some(lit) = p {
self.learnt[0] = lit.negate();
}
// Minimize learnt clause using recursive resolution
self.minimize_learnt_clause();
// Compute the real LBD from the FINAL learned (1-UIP) clause literals.
// This is the standard Glucose definition: the number of distinct decision
// levels in the learned clause itself (level 0 excluded), not the larger
// `vars_to_bump` set. It is computed AFTER minimization so it reflects the
// exact clause that will be stored, and therefore satisfies lbd <= clause len.
let lbd = compute_lbd_from_literals(&self.learnt, &self.trail);
// Notify external heuristic of each conflict-involved variable with the
// learned-clause LBD score.
if let Some(ref ext) = self.config.external_branching
&& let Ok(mut h) = ext.lock()
{
for &var in &vars_to_bump {
h.on_conflict_var_with_lbd(var, lbd);
}
}
// Calculate assertion level (traditional backtrack level)
let assertion_level = if self.learnt.len() == 1 {
0
} else {
// Find second highest level
let mut max_level = 0;
let mut max_idx = 1;
for (i, &lit) in self.learnt.iter().enumerate().skip(1) {
let level = self.trail.level(lit.var());
if level > max_level {
max_level = level;
max_idx = i;
}
}
// Move second watch to position 1
self.learnt.swap(1, max_idx);
max_level
};
// Apply chronological backtracking if enabled
let backtrack_level = self.chrono_backtrack.compute_backtrack_level(
&self.trail,
&self.learnt,
assertion_level,
);
// Track chronological vs non-chronological backtracks
if backtrack_level != assertion_level {
self.stats.chrono_backtracks += 1;
} else {
self.stats.non_chrono_backtracks += 1;
}
// Debug: print learned clause (only with analyze-debug feature)
#[cfg(feature = "analyze-debug")]
if self.num_vars <= 5 {
let lits_str: Vec<String> = self
.learnt
.iter()
.map(|lit| {
let sign = if lit.is_pos() { "" } else { "~" };
format!("{}v{}", sign, lit.var().index())
})
.collect();
eprintln!(
"[ANALYZE] Learned clause: ({}), backtrack_level={}",
lits_str.join(" | "),
backtrack_level
);
}
(backtrack_level, self.learnt.clone())
}
/// Minimize the learned clause by removing redundant literals
///
/// A literal can be removed if it is implied by the remaining literals.
/// We use a recursive check: a literal l is redundant if its reason clause
/// contains only literals that are either:
/// - Already in the learnt clause (marked as seen)
/// - At decision level 0 (always true in the learned clause context)
/// - Themselves redundant (recursive check)
///
/// This also performs clause strengthening by checking for stronger implications
pub(super) fn minimize_learnt_clause(&mut self) {
if self.learnt.len() <= 2 {
// Don't minimize very small clauses
return;
}
let original_len = self.learnt.len();
// Mark all literals in the learned clause as "in clause"
// We use analyze_stack to track literals to check
self.analyze_stack.clear();
// Phase 1: Basic minimization - remove redundant literals
let mut j = 1; // Write position
for i in 1..self.learnt.len() {
let lit = self.learnt[i];
if self.lit_is_redundant(lit) {
// Skip this literal (it's redundant)
} else {
// Keep this literal
self.learnt[j] = lit;
j += 1;
}
}
self.learnt.truncate(j);
// Phase 2: Clause strengthening - check for self-subsuming resolution
// If the clause contains both l and ~l' where l' is in a reason clause,
// we might be able to strengthen the clause
self.strengthen_learnt_clause();
// Track minimization statistics
let final_len = self.learnt.len();
if final_len < original_len {
self.stats.minimizations += 1;
self.stats.literals_removed += (original_len - final_len) as u64;
}
}
/// Strengthen the learned clause using on-the-fly self-subsuming resolution
pub(super) fn strengthen_learnt_clause(&mut self) {
if self.learnt.len() <= 2 {
return;
}
// Check each literal to see if we can strengthen by resolution
let mut j = 1;
for i in 1..self.learnt.len() {
let lit = self.learnt[i];
let var = lit.var();
// Check if this literal can be strengthened
if let Reason::Propagation(reason_id) = self.trail.reason(var)
&& let Some(reason_clause) = self.clauses.get(reason_id)
&& reason_clause.lits.len() == 2
{
// Binary reason: one literal is lit, the other is the implied literal
let other_lit = if reason_clause.lits[0] == lit.negate() {
reason_clause.lits[1]
} else if reason_clause.lits[1] == lit.negate() {
reason_clause.lits[0]
} else {
// Keep the literal
self.learnt[j] = lit;
j += 1;
continue;
};
// If other_lit is already in the learned clause at level 0,
// we can remove lit
if self.trail.level(other_lit.var()) == 0 && self.seen[other_lit.var().index()] {
// Skip this literal (strengthened)
continue;
}
}
// Keep this literal
self.learnt[j] = lit;
j += 1;
}
self.learnt.truncate(j);
}
/// Check if a literal is redundant in the learned clause
///
/// A literal is redundant if its reason clause only contains:
/// - Literals marked as seen (in the learned clause)
/// - Literals at decision level 0
/// - Literals that are themselves redundant (recursive)
pub(super) fn lit_is_redundant(&mut self, lit: Lit) -> bool {
let var = lit.var();
// Decision variables and theory propagations are not redundant
let reason = match self.trail.reason(var) {
Reason::Decision => return false,
Reason::Theory => return false, // Theory propagations can't be minimized
Reason::Propagation(c) => c,
};
let reason_clause = match self.clauses.get(reason) {
Some(c) => c,
None => return false,
};
// Check all literals in the reason clause
for &reason_lit in &reason_clause.lits {
if reason_lit == lit.negate() {
// Skip the literal we're analyzing
continue;
}
let reason_var = reason_lit.var();
// Level 0 literals are always OK
if self.trail.level(reason_var) == 0 {
continue;
}
// If the literal is in the learned clause (seen), it's OK
if self.seen[reason_var.index()] {
continue;
}
// Otherwise, this literal prevents minimization
// (A full recursive check would be more powerful but more expensive)
return false;
}
true
}
/// Analyze a theory conflict (given as a list of literals that are all false)
pub(super) fn analyze_theory_conflict(
&mut self,
conflict_lits: &[Lit],
) -> (u32, SmallVec<[Lit; 16]>) {
self.learnt.clear();
self.learnt.push(Lit::from_code(0)); // Placeholder
let mut counter = 0;
let current_level = self.trail.decision_level();
// Reset seen flags
for s in &mut self.seen {
*s = false;
}
// Collect variables for batch bumping
let mut vars_to_bump: SmallVec<[Var; 32]> = SmallVec::new();
// Process conflict literals
let mut all_level_zero = true;
for &lit in conflict_lits {
let var = lit.var();
let level = self.trail.level(var);
if !self.seen[var.index()] && level > 0 {
all_level_zero = false;
self.seen[var.index()] = true;
vars_to_bump.push(var);
if level == current_level {
counter += 1;
} else {
// Add the literal itself (not negated) to the learned clause.
// The conflict clause has all literals FALSE. To prevent this
// conflict, we need at least one of these literals to become TRUE.
// So we add the literal directly to the learned clause.
self.learnt.push(lit);
}
}
}
// If ALL conflict literals are at level 0, this is a fundamental UNSAT
// that cannot be resolved by backtracking. Return an empty learned clause
// with backtrack_level=0 as a signal.
if !conflict_lits.is_empty() && all_level_zero {
return (0, SmallVec::new());
}
// Find UIP by walking back through trail
let mut index = self.trail.assignments().len();
let mut p = None;
while counter > 0 {
if index == 0 {
break; // Avoid underflow — no more trail entries
}
index -= 1;
if index >= self.trail.assignments().len() {
break; // Guard against stale length
}
let current_lit = self.trail.assignments()[index];
p = Some(current_lit);
let var = current_lit.var();
if self.seen[var.index()] {
counter -= 1;
if counter > 0
&& let Reason::Propagation(reason_clause) = self.trail.reason(var)
&& let Some(clause) = self.clauses.get(reason_clause)
{
// Get reason and process its literals
for &lit in &clause.lits[1..] {
let reason_var = lit.var();
let level = self.trail.level(reason_var);
if !self.seen[reason_var.index()] && level > 0 {
self.seen[reason_var.index()] = true;
vars_to_bump.push(reason_var);
if level == current_level {
counter += 1;
} else {
// Add the literal itself to the learned clause
self.learnt.push(lit);
}
}
}
}
}
}
// Batch bump all collected variables
self.vsids.bump_batch(&vars_to_bump);
self.chb.bump_batch(&vars_to_bump);
self.lrb.on_reason_batch(&vars_to_bump);
// Set asserting literal
if let Some(uip) = p {
self.learnt[0] = uip.negate();
}
// Minimize
self.minimize_learnt_clause();
// Compute the real LBD from the FINAL learned clause literals (post-minimization),
// matching the standard Glucose definition rather than using the larger
// `vars_to_bump` proxy. For theory conflicts the learned clause shape may differ
// from Boolean conflicts, but the distinct-decision-level count of the actual
// learned clause is the correct glue score and never exceeds the clause length.
let lbd = compute_lbd_from_literals(&self.learnt, &self.trail);
// Notify external heuristic of each conflict-involved variable with the
// learned-clause LBD score.
if let Some(ref ext) = self.config.external_branching
&& let Ok(mut h) = ext.lock()
{
for &var in &vars_to_bump {
h.on_conflict_var_with_lbd(var, lbd);
}
}
// Calculate backtrack level
let backtrack_level = if self.learnt.len() == 1 {
0
} else {
let mut max_level = 0;
let mut max_idx = 1;
for (i, &lit) in self.learnt.iter().enumerate().skip(1) {
let level = self.trail.level(lit.var());
if level > max_level {
max_level = level;
max_idx = i;
}
}
self.learnt.swap(1, max_idx);
max_level
};
(backtrack_level, self.learnt.clone())
}
/// Extract a core of assumptions that caused a conflict
pub(super) fn extract_assumption_core(
&self,
assumptions: &[Lit],
conflict_idx: usize,
) -> Vec<Lit> {
// The conflicting assumption and any assumptions it depends on
let mut core = Vec::new();
let conflict_lit = assumptions[conflict_idx];
// Find assumptions that led to this conflict
for &lit in &assumptions[..=conflict_idx] {
if self.seen.get(lit.var().index()).copied().unwrap_or(false) || lit == conflict_lit {
core.push(lit);
}
}
// If core is empty, just return the conflicting assumption
if core.is_empty() {
core.push(conflict_lit);
}
core
}
/// Analyze conflict to find assumptions in the unsat core
pub(super) fn analyze_assumption_conflict(&mut self, assumptions: &[Lit]) -> Vec<Lit> {
// Use seen flags to mark which assumptions are in the conflict
let mut core = Vec::new();
// Walk back through the trail to find conflicting assumptions
for &lit in assumptions {
let var = lit.var();
if var.index() < self.trail.assignments().len() {
let value = self.trail.lit_value(lit);
// If the negation of an assumption is implied, it's in the core
if value.is_false() || self.seen.get(var.index()).copied().unwrap_or(false) {
core.push(lit);
}
}
}
// If no specific core found, return all assumptions
if core.is_empty() {
core.extend(assumptions.iter().copied());
}
core
}
/// Get the minimum backtrack level for a conflict
pub(super) fn analyze_conflict_level(&self, conflict: ClauseId) -> u32 {
let clause = match self.clauses.get(conflict) {
Some(c) => c,
None => return 0,
};
let mut min_level = u32::MAX;
for lit in clause.lits.iter().copied() {
let level = self.trail.level(lit.var());
if level > 0 && level < min_level {
min_level = level;
}
}
if min_level == u32::MAX { 0 } else { min_level }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::trail::Trail;
// ---------------------------------------------------------------------------
// Tests for compute_lbd_from_literals
// ---------------------------------------------------------------------------
#[test]
fn test_compute_lbd_all_same_level() {
// Three literals whose vars are all assigned at level 3 → LBD = 1.
let n = 4;
let mut trail = Trail::new(n);
// Level 0 is implicit; push 3 levels.
trail.new_decision_level(); // → level 1
trail.new_decision_level(); // → level 2
trail.new_decision_level(); // → level 3
let v0 = Var::new(0);
let v1 = Var::new(1);
let v2 = Var::new(2);
trail.assign_decision(Lit::pos(v0));
trail.assign_decision(Lit::pos(v1));
trail.assign_decision(Lit::pos(v2));
let lits = [Lit::pos(v0), Lit::neg(v1), Lit::pos(v2)];
let lbd = compute_lbd_from_literals(&lits, &trail);
assert_eq!(lbd, 1, "all literals at same level → LBD should be 1");
}
#[test]
fn test_compute_lbd_distinct_levels() {
// Three literals at levels 1, 2, 3 → LBD = 3.
let n = 4;
let mut trail = Trail::new(n);
let v0 = Var::new(0);
let v1 = Var::new(1);
let v2 = Var::new(2);
trail.new_decision_level(); // → level 1
trail.assign_decision(Lit::pos(v0));
trail.new_decision_level(); // → level 2
trail.assign_decision(Lit::pos(v1));
trail.new_decision_level(); // → level 3
trail.assign_decision(Lit::pos(v2));
let lits = [Lit::pos(v0), Lit::pos(v1), Lit::neg(v2)];
let lbd = compute_lbd_from_literals(&lits, &trail);
assert_eq!(lbd, 3, "literals at levels 1, 2, 3 → LBD should be 3");
}
#[test]
fn test_compute_lbd_excludes_level_zero() {
// Literals: one var at level 0 (unit prop), two at level 2 → LBD = 1.
// Level-0 variables must not be counted.
let n = 4;
let mut trail = Trail::new(n);
let v0 = Var::new(0); // Will be at level 0
let v1 = Var::new(1); // Will be at level 2
let v2 = Var::new(2); // Will be at level 2
// Assign v0 at level 0 (root decision level, no new_decision_level call).
trail.assign_decision(Lit::pos(v0));
trail.new_decision_level(); // → level 1 (unused)
trail.new_decision_level(); // → level 2
trail.assign_decision(Lit::pos(v1));
trail.assign_decision(Lit::pos(v2));
let lits = [Lit::pos(v0), Lit::pos(v1), Lit::pos(v2)];
let lbd = compute_lbd_from_literals(&lits, &trail);
assert_eq!(
lbd, 1,
"level-0 var must be excluded; only level-2 vars count → LBD = 1"
);
}
#[test]
fn test_compute_lbd_mixed_duplicates_and_zero() {
// v0 @ level 0 (excluded), v1 @ level 2, v2 @ level 4, v3 @ level 2 (duplicate)
// → distinct non-zero levels: {2, 4} → LBD = 2.
let n = 5;
let mut trail = Trail::new(n);
let v0 = Var::new(0);
let v1 = Var::new(1);
let v2 = Var::new(2);
let v3 = Var::new(3);
trail.assign_decision(Lit::pos(v0)); // level 0
trail.new_decision_level(); // → 1
trail.new_decision_level(); // → 2
trail.assign_decision(Lit::pos(v1));
trail.assign_decision(Lit::pos(v3));
trail.new_decision_level(); // → 3
trail.new_decision_level(); // → 4
trail.assign_decision(Lit::pos(v2));
let lits = [Lit::pos(v0), Lit::pos(v1), Lit::neg(v2), Lit::pos(v3)];
let lbd = compute_lbd_from_literals(&lits, &trail);
assert_eq!(lbd, 2, "levels {{2, 4}} → LBD = 2");
}
#[test]
fn test_compute_lbd_empty_literals() {
// Empty literal set → LBD = 0.
let trail = Trail::new(0);
let lits: [Lit; 0] = [];
let lbd = compute_lbd_from_literals(&lits, &trail);
assert_eq!(lbd, 0, "empty literal set → LBD = 0");
}
// ---------------------------------------------------------------------------
// Integration test: conflict analysis passes LBD to the external hook
// ---------------------------------------------------------------------------
#[test]
fn test_conflict_analysis_passes_lbd_to_hook() {
// Solve PHP(3,2) — the same UNSAT formula used in the external_branching tests.
// A ConflictLbdRecordingHeuristic records all LBD values received via
// on_conflict_var_with_lbd. After solving, assert:
// 1. at least one call was made (conflicts happened)
// 2. all recorded LBD values are > 0 (no degenerate LBD-0 passed through)
use crate::solver::heuristic::BranchingHeuristic;
use crate::{Solver, SolverConfig, SolverResult};
use std::sync::{Arc, Mutex};
struct ConflictLbdRecordingHeuristic {
lbd_values: Arc<Mutex<Vec<u32>>>,
}
impl BranchingHeuristic for ConflictLbdRecordingHeuristic {
fn select(&mut self, _candidates: &[Var], _scores: &[f64]) -> Option<Var> {
None // always defer — VSIDS drives the solve
}
fn on_conflict_var_with_lbd(&mut self, _var: Var, lbd: u32) {
self.lbd_values
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(lbd);
}
}
let lbd_values: Arc<Mutex<Vec<u32>>> = Arc::new(Mutex::new(Vec::new()));
let heuristic = Arc::new(Mutex::new(ConflictLbdRecordingHeuristic {
lbd_values: Arc::clone(&lbd_values),
}));
let config = SolverConfig {
external_branching: Some(heuristic),
..SolverConfig::default()
};
let mut solver = Solver::with_config(config);
// PHP(3,2): 6 variables
for _ in 0..6 {
solver.new_var();
}
// Each pigeon must be in at least one hole
solver.add_clause_dimacs(&[1, 2]);
solver.add_clause_dimacs(&[3, 4]);
solver.add_clause_dimacs(&[5, 6]);
// At most one pigeon per hole
solver.add_clause_dimacs(&[-1, -3]);
solver.add_clause_dimacs(&[-1, -5]);
solver.add_clause_dimacs(&[-3, -5]);
solver.add_clause_dimacs(&[-2, -4]);
solver.add_clause_dimacs(&[-2, -6]);
solver.add_clause_dimacs(&[-4, -6]);
let result = solver.solve();
assert_eq!(result, SolverResult::Unsat, "PHP(3,2) must be UNSAT");
let values = lbd_values.lock().unwrap_or_else(|e| e.into_inner());
assert!(
!values.is_empty(),
"on_conflict_var_with_lbd must have been called at least once"
);
for &lbd in values.iter() {
assert!(
lbd > 0,
"LBD passed to hook must be > 0 (got {lbd}); level-0 vars should be excluded"
);
}
}
#[test]
fn test_lbd_matches_learned_clause_glue() {
// The LBD passed to the hook must be the glue score of the ACTUAL learned
// (1-UIP) clause — i.e. the distinct decision-level count of `self.learnt` —
// NOT the distinct-level count of the larger `vars_to_bump` union.
//
// We solve a crafted UNSAT instance with clause deletion effectively disabled
// so every learned clause persists. The hook records the set of LBD values it
// receives; the solver stores `clause.lbd` (computed independently in
// learn.rs::compute_lbd from the same final clause). Since a 1-UIP learned
// clause never contains level-0 literals, the two definitions coincide, so the
// set of hook LBDs must be a SUBSET of the stored learned-clause LBD set
// (plus 1 for unit learned clauses, whose single literal sits at the current
// decision level). The old `vars_to_bump` proxy would routinely report values
// ABSENT from any stored clause's LBD, so a subset relation is decisive.
use crate::solver::heuristic::BranchingHeuristic;
use crate::{Solver, SolverConfig, SolverResult};
use std::collections::BTreeSet;
use std::sync::{Arc, Mutex};
struct SetRecordingHeuristic {
lbd_set: Arc<Mutex<BTreeSet<u32>>>,
}
impl BranchingHeuristic for SetRecordingHeuristic {
fn select(&mut self, _candidates: &[Var], _scores: &[f64]) -> Option<Var> {
None
}
fn on_conflict_var_with_lbd(&mut self, _var: Var, lbd: u32) {
self.lbd_set
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(lbd);
}
}
let lbd_set: Arc<Mutex<BTreeSet<u32>>> = Arc::new(Mutex::new(BTreeSet::new()));
let heuristic = Arc::new(Mutex::new(SetRecordingHeuristic {
lbd_set: Arc::clone(&lbd_set),
}));
let config = SolverConfig {
external_branching: Some(heuristic),
// Disable clause deletion so every learned clause survives for inspection.
clause_deletion_threshold: usize::MAX,
..SolverConfig::default()
};
let mut solver = Solver::with_config(config);
// PHP(3,2): 6 variables, UNSAT, produces multi-level conflicts.
for _ in 0..6 {
solver.new_var();
}
solver.add_clause_dimacs(&[1, 2]);
solver.add_clause_dimacs(&[3, 4]);
solver.add_clause_dimacs(&[5, 6]);
solver.add_clause_dimacs(&[-1, -3]);
solver.add_clause_dimacs(&[-1, -5]);
solver.add_clause_dimacs(&[-3, -5]);
solver.add_clause_dimacs(&[-2, -4]);
solver.add_clause_dimacs(&[-2, -6]);
solver.add_clause_dimacs(&[-4, -6]);
let result = solver.solve();
assert_eq!(result, SolverResult::Unsat, "PHP(3,2) must be UNSAT");
// Gather the LBD of every surviving learned clause from the solver's
// internal database (these fields are crate-visible). Unit learned clauses
// (len == 1) keep the default lbd 0 but their hook LBD is 1, so we add 1 for
// every unit clause to the allowed set.
let mut stored_lbds: BTreeSet<u32> = BTreeSet::new();
for &cid in &solver.learned_clause_ids {
if let Some(clause) = solver.clauses.get(cid) {
if clause.lits.len() == 1 {
stored_lbds.insert(1);
} else {
stored_lbds.insert(clause.lbd);
}
}
}
let hook_lbds = lbd_set.lock().unwrap_or_else(|e| e.into_inner());
assert!(!hook_lbds.is_empty(), "hook must have received LBD values");
// Decisive check: every LBD the hook reported is the glue score of a real
// learned clause (subset relation). With the old vars_to_bump proxy this
// would fail because vars_to_bump-derived LBDs exceed any stored clause's LBD.
for &lbd in hook_lbds.iter() {
assert!(
stored_lbds.contains(&lbd),
"hook LBD {lbd} must match the glue score of an actual learned clause; \
stored learned-clause LBDs = {stored_lbds:?}"
);
}
}
#[test]
fn test_lbd_le_clause_size() {
// The LBD of a learned clause can never exceed its literal count: each literal
// contributes at most one distinct decision level. The fix computes LBD from
// the actual learned clause, so this invariant must hold for the value handed
// to the hook (unlike the old vars_to_bump proxy, which could exceed the clause
// length). We verify it on every surviving learned clause and also confirm the
// hook never reported an LBD larger than the largest learned clause.
use crate::solver::heuristic::BranchingHeuristic;
use crate::{Solver, SolverConfig, SolverResult};
use std::sync::{Arc, Mutex};
struct MaxRecordingHeuristic {
max_lbd: Arc<Mutex<u32>>,
}
impl BranchingHeuristic for MaxRecordingHeuristic {
fn select(&mut self, _candidates: &[Var], _scores: &[f64]) -> Option<Var> {
None
}
fn on_conflict_var_with_lbd(&mut self, _var: Var, lbd: u32) {
let mut m = self.max_lbd.lock().unwrap_or_else(|e| e.into_inner());
if lbd > *m {
*m = lbd;
}
}
}
let max_lbd: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
let heuristic = Arc::new(Mutex::new(MaxRecordingHeuristic {
max_lbd: Arc::clone(&max_lbd),
}));
let config = SolverConfig {
external_branching: Some(heuristic),
clause_deletion_threshold: usize::MAX,
..SolverConfig::default()
};
let mut solver = Solver::with_config(config);
// PHP(4,3): 12 variables, UNSAT, deeper search → larger learned clauses.
for _ in 0..12 {
solver.new_var();
}
// Each of 4 pigeons in at least one of 3 holes (pigeon p, hole h → var 3*(p-1)+h).
solver.add_clause_dimacs(&[1, 2, 3]);
solver.add_clause_dimacs(&[4, 5, 6]);
solver.add_clause_dimacs(&[7, 8, 9]);
solver.add_clause_dimacs(&[10, 11, 12]);
// At most one pigeon per hole (for each hole h, no two pigeons share it).
for hole in 0..3 {
let h = hole + 1;
let occupants = [h, h + 3, h + 6, h + 9];
for i in 0..occupants.len() {
for j in (i + 1)..occupants.len() {
solver.add_clause_dimacs(&[-occupants[i], -occupants[j]]);
}
}
}
let result = solver.solve();
assert_eq!(result, SolverResult::Unsat, "PHP(4,3) must be UNSAT");
// Invariant on every surviving learned clause: lbd <= number of literals.
let mut max_learnt_len: usize = 0;
for &cid in &solver.learned_clause_ids {
if let Some(clause) = solver.clauses.get(cid) {
let len = clause.lits.len();
max_learnt_len = max_learnt_len.max(len);
// Unit clauses store the default lbd 0; the invariant lbd <= len is
// trivially satisfied. For len >= 2 the stored lbd was computed from
// the clause literals and must not exceed the literal count.
assert!(
clause.lbd as usize <= len,
"learned clause LBD {} exceeds its literal count {len}",
clause.lbd
);
}
}
let observed_max = *max_lbd.lock().unwrap_or_else(|e| e.into_inner());
assert!(observed_max > 0, "hook must have received a positive LBD");
assert!(
observed_max as usize <= max_learnt_len,
"max hook LBD {observed_max} must not exceed the largest learned clause length \
{max_learnt_len} — proves LBD is computed from the learned clause, not vars_to_bump"
);
}
}