oxiz-solver 0.2.0

Main CDCL(T) Solver API for OxiZ
Documentation
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
//! UIP (Unique Implication Point) Selection for Conflict Analysis.
#![allow(dead_code)] // Under development
//!
//! Identifies UIPs in the implication graph for effective clause learning.
//!
//! ## UIP Strategies
//!
//! - **First UIP**: Closest UIP to conflict (default in modern SAT solvers)
//! - **Last UIP**: Decision literal (produces longest clauses)
//! - **All UIPs**: Enumerate all UIPs for analysis
//! - **Dominator-based**: Use dominator tree to find UIPs efficiently
//!
//! ## References
//!
//! - "Chaff: Engineering an Efficient SAT Solver" (Moskewicz et al., 2001)
//! - Z3's `sat/sat_cut_simplifier.cpp`

#[allow(unused_imports)]
use crate::prelude::*;
use oxiz_sat::Lit;

/// Decision level.
pub type Level = usize;

/// Node in the implication graph.
#[derive(Debug, Clone)]
pub struct ImplicationNode {
    /// The literal assigned.
    pub literal: Lit,
    /// Decision level of this assignment.
    pub level: Level,
    /// Reason for this assignment (None for decisions).
    pub reason: Option<Vec<Lit>>,
}

impl ImplicationNode {
    /// Create a new implication node.
    pub fn new(literal: Lit, level: Level, reason: Option<Vec<Lit>>) -> Self {
        Self {
            literal,
            level,
            reason,
        }
    }

    /// Check if this is a decision node.
    pub fn is_decision(&self) -> bool {
        self.reason.is_none()
    }
}

/// UIP selection strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UipStrategy {
    /// First UIP (closest to conflict).
    FirstUIP,
    /// Last UIP (decision literal).
    LastUIP,
    /// All UIPs (for analysis).
    AllUIPs,
    /// Dominator-based UIP.
    Dominator,
}

/// Configuration for UIP selection.
#[derive(Debug, Clone)]
pub struct UipConfig {
    /// UIP selection strategy.
    pub strategy: UipStrategy,
    /// Enable dominator tree computation.
    pub use_dominators: bool,
    /// Maximum distance from conflict to search.
    pub max_distance: Option<usize>,
}

impl Default for UipConfig {
    fn default() -> Self {
        Self {
            strategy: UipStrategy::FirstUIP,
            use_dominators: false,
            max_distance: Some(1000),
        }
    }
}

/// Statistics for UIP selection.
#[derive(Debug, Clone, Default)]
pub struct UipStats {
    /// UIPs found.
    pub uips_found: u64,
    /// First UIPs selected.
    pub first_uips: u64,
    /// Last UIPs selected.
    pub last_uips: u64,
    /// Average distance to conflict.
    pub avg_distance: f64,
    /// Dominator computations.
    pub dominator_computations: u64,
}

/// UIP selector for conflict analysis.
pub struct UipSelector {
    /// Configuration.
    config: UipConfig,
    /// Statistics.
    stats: UipStats,
    /// Implication graph.
    graph: FxHashMap<Lit, ImplicationNode>,
    /// Current decision level.
    current_level: Level,
}

impl UipSelector {
    /// Create a new UIP selector.
    pub fn new(config: UipConfig) -> Self {
        Self {
            config,
            stats: UipStats::default(),
            graph: FxHashMap::default(),
            current_level: 0,
        }
    }

    /// Create with default configuration.
    pub fn default_config() -> Self {
        Self::new(UipConfig::default())
    }

    /// Add an implication to the graph.
    pub fn add_implication(&mut self, literal: Lit, level: Level, reason: Option<Vec<Lit>>) {
        let node = ImplicationNode::new(literal, level, reason);
        self.graph.insert(literal, node);

        if level > self.current_level {
            self.current_level = level;
        }
    }

    /// Find UIP for a conflict clause.
    ///
    /// Returns the UIP literal according to the configured strategy.
    pub fn find_uip(&mut self, conflict: &[Lit]) -> Option<Lit> {
        if conflict.is_empty() {
            return None;
        }

        match self.config.strategy {
            UipStrategy::FirstUIP => self.find_first_uip(conflict),
            UipStrategy::LastUIP => self.find_last_uip(conflict),
            UipStrategy::AllUIPs => self.find_first_uip(conflict), // Return first as default
            UipStrategy::Dominator => {
                if self.config.use_dominators {
                    self.find_dominator_uip(conflict)
                } else {
                    self.find_first_uip(conflict)
                }
            }
        }
    }

    /// Find the first UIP (closest to conflict).
    fn find_first_uip(&mut self, conflict: &[Lit]) -> Option<Lit> {
        // Get conflict level (highest level in conflict)
        let conflict_level = conflict
            .iter()
            .filter_map(|&lit| self.graph.get(&lit).map(|node| node.level))
            .max()?;

        // Count literals at conflict level
        let mut current_lits: FxHashSet<Lit> = conflict
            .iter()
            .filter(|&&lit| {
                self.graph
                    .get(&lit)
                    .is_some_and(|node| node.level == conflict_level)
            })
            .copied()
            .collect();

        // Traverse backward until we find UIP (single literal at conflict level)
        while current_lits.len() > 1 {
            // Pick a literal to resolve
            let &lit = current_lits.iter().next()?;
            current_lits.remove(&lit);

            // Add its reason clause
            if let Some(node) = self.graph.get(&lit)
                && let Some(reason) = &node.reason
            {
                for &reason_lit in reason {
                    if let Some(reason_node) = self.graph.get(&reason_lit)
                        && reason_node.level == conflict_level
                    {
                        current_lits.insert(reason_lit);
                    }
                }
            }
        }

        self.stats.uips_found += 1;
        self.stats.first_uips += 1;

        current_lits.iter().next().copied()
    }

    /// Find the last UIP (decision literal at conflict level).
    fn find_last_uip(&mut self, conflict: &[Lit]) -> Option<Lit> {
        // Get conflict level
        let conflict_level = conflict
            .iter()
            .filter_map(|&lit| self.graph.get(&lit).map(|node| node.level))
            .max()?;

        // Find decision literal at this level
        for (lit, node) in &self.graph {
            if node.level == conflict_level && node.is_decision() {
                self.stats.uips_found += 1;
                self.stats.last_uips += 1;
                return Some(*lit);
            }
        }

        // Fallback to first UIP
        self.find_first_uip(conflict)
    }

    /// Find UIP using dominator analysis.
    fn find_dominator_uip(&mut self, conflict: &[Lit]) -> Option<Lit> {
        self.stats.dominator_computations += 1;

        // Simplified: Compute dominators in implication graph
        // In real implementation, would use Lengauer-Tarjan algorithm

        // For now, fall back to first UIP
        self.find_first_uip(conflict)
    }

    /// Find all UIPs in the implication graph.
    pub fn find_all_uips(&mut self, conflict: &[Lit]) -> Vec<Lit> {
        let mut uips = Vec::new();

        // Get conflict level
        let conflict_level = match conflict
            .iter()
            .filter_map(|&lit| self.graph.get(&lit).map(|node| node.level))
            .max()
        {
            Some(level) => level,
            None => return uips,
        };

        // Find all literals at conflict level
        let conflict_level_lits: Vec<Lit> = conflict
            .iter()
            .filter(|&&lit| {
                self.graph
                    .get(&lit)
                    .is_some_and(|node| node.level == conflict_level)
            })
            .copied()
            .collect();

        // Check each for UIP property
        for &candidate in &conflict_level_lits {
            if self.is_uip(candidate, conflict_level) {
                uips.push(candidate);
            }
        }

        self.stats.uips_found += uips.len() as u64;

        uips
    }

    /// Check if a literal is a UIP at a given level.
    fn is_uip(&self, candidate: Lit, level: Level) -> bool {
        // A literal is a UIP if all paths from decision to conflict go through it

        // Simplified check: is it on the conflict side and single at its level?
        // Real implementation would do proper reachability analysis

        if let Some(node) = self.graph.get(&candidate) {
            node.level == level
        } else {
            false
        }
    }

    /// Compute distance from a literal to the conflict.
    pub fn distance_to_conflict(&self, lit: Lit, conflict: &[Lit]) -> Option<usize> {
        // Simplified: BFS from lit to any conflict literal

        let mut visited = FxHashSet::default();
        let mut queue = vec![(lit, 0)];
        visited.insert(lit);

        while let Some((current, dist)) = queue.pop() {
            if conflict.contains(&current) {
                return Some(dist);
            }

            if let Some(node) = self.graph.get(&current)
                && let Some(reason) = &node.reason
            {
                for &next_lit in reason {
                    if visited.insert(next_lit) {
                        queue.push((next_lit, dist + 1));
                    }
                }
            }
        }

        None
    }

    /// Update average distance statistics.
    fn update_distance_stats(&mut self, distance: usize) {
        let count = self.stats.uips_found;
        let old_avg = self.stats.avg_distance;
        self.stats.avg_distance = (old_avg * (count - 1) as f64 + distance as f64) / count as f64;
    }

    /// Clear the implication graph.
    pub fn clear(&mut self) {
        self.graph.clear();
        self.current_level = 0;
    }

    /// Get statistics.
    pub fn stats(&self) -> &UipStats {
        &self.stats
    }

    /// Reset statistics.
    pub fn reset_stats(&mut self) {
        self.stats = UipStats::default();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxiz_sat::Var;

    #[test]
    fn test_uip_selector_creation() {
        let selector = UipSelector::default_config();
        assert_eq!(selector.stats().uips_found, 0);
    }

    #[test]
    fn test_add_implication() {
        let mut selector = UipSelector::default_config();

        let lit = Lit::pos(Var::new(0));
        selector.add_implication(lit, 1, None);

        assert_eq!(selector.current_level, 1);
        assert!(selector.graph.contains_key(&lit));
    }

    #[test]
    fn test_decision_node() {
        let node = ImplicationNode::new(Lit::pos(Var::new(0)), 1, None);
        assert!(node.is_decision());

        let node2 = ImplicationNode::new(Lit::pos(Var::new(1)), 1, Some(vec![]));
        assert!(!node2.is_decision());
    }

    #[test]
    fn test_find_last_uip() {
        let mut selector = UipSelector::new(UipConfig {
            strategy: UipStrategy::LastUIP,
            ..Default::default()
        });

        // Add decision at level 1
        let decision = Lit::pos(Var::new(0));
        selector.add_implication(decision, 1, None);

        // Add propagated literals
        let prop1 = Lit::pos(Var::new(1));
        selector.add_implication(prop1, 1, Some(vec![decision]));

        let conflict = vec![prop1];

        let uip = selector.find_uip(&conflict);
        assert!(uip.is_some());
        assert_eq!(selector.stats().last_uips, 1);
    }

    #[test]
    fn test_stats() {
        let mut selector = UipSelector::default_config();

        let lit = Lit::pos(Var::new(0));
        selector.add_implication(lit, 1, None);

        let conflict = vec![lit];
        let _ = selector.find_uip(&conflict);

        assert!(selector.stats().uips_found > 0);
    }
}