oxiz-sat 0.2.0

High-performance CDCL SAT Solver 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
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
use crate::literal::Lit;
/// Cube-and-Conquer: Advanced parallel SAT solving via search space partitioning.
///
/// This module implements the Cube-and-Conquer technique, which partitions the search
/// space into "cubes" (partial assignments) that can be solved independently in parallel.
///
/// The algorithm works in two phases:
/// 1. **Cube Phase**: Use lookahead to generate a set of cubes that partition the space
/// 2. **Conquer Phase**: Solve each cube in parallel using CDCL
///
/// This approach is particularly effective for hard combinatorial problems.
#[allow(unused_imports)]
use crate::prelude::*;

/// A cube represents a partial assignment (conjunction of literals).
///
/// Cubes partition the search space - if all cubes are UNSAT, the formula is UNSAT.
/// If any cube is SAT, the formula is SAT.
#[derive(Debug, Clone)]
pub struct Cube {
    /// The literals in this cube (partial assignment)
    pub literals: Vec<Lit>,
    /// Estimated difficulty score (higher = harder)
    pub difficulty: f64,
    /// Depth at which this cube was created
    pub depth: usize,
}

impl Cube {
    /// Creates a new cube from literals.
    pub fn new(literals: Vec<Lit>) -> Self {
        Self {
            depth: literals.len(),
            literals,
            difficulty: 0.0,
        }
    }

    /// Creates a cube with estimated difficulty.
    pub fn with_difficulty(literals: Vec<Lit>, difficulty: f64) -> Self {
        Self {
            depth: literals.len(),
            literals,
            difficulty,
        }
    }

    /// Returns the number of literals in the cube.
    pub fn len(&self) -> usize {
        self.literals.len()
    }

    /// Checks if the cube is empty.
    pub fn is_empty(&self) -> bool {
        self.literals.is_empty()
    }

    /// Extends the cube with an additional literal.
    pub fn extend(&self, lit: Lit) -> Self {
        let mut new_lits = self.literals.clone();
        new_lits.push(lit);
        Self {
            literals: new_lits,
            difficulty: 0.0,
            depth: self.depth + 1,
        }
    }

    /// Checks if the cube contains conflicting literals.
    pub fn is_consistent(&self) -> bool {
        let mut seen = HashSet::new();
        for &lit in &self.literals {
            if seen.contains(&lit.negate()) {
                return false;
            }
            seen.insert(lit);
        }
        true
    }
}

/// Strategy for cube generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CubeSplittingStrategy {
    /// Split based on variable activity (VSIDS-style)
    Activity,
    /// Split based on lookahead scores
    Lookahead,
    /// Split using the most constrained variable (smallest domain)
    MostConstrained,
    /// Balanced splitting to create similar-sized cubes
    Balanced,
}

/// Configuration for cube generation.
#[derive(Debug, Clone)]
pub struct CubeConfig {
    /// Maximum depth for cube splitting
    pub max_depth: usize,
    /// Target number of cubes to generate
    pub target_cubes: usize,
    /// Minimum literals per cube
    pub min_cube_size: usize,
    /// Maximum literals per cube
    pub max_cube_size: usize,
    /// Splitting strategy
    pub strategy: CubeSplittingStrategy,
    /// Enable adaptive depth adjustment
    pub adaptive_depth: bool,
}

impl Default for CubeConfig {
    fn default() -> Self {
        Self {
            max_depth: 10,
            target_cubes: 100,
            min_cube_size: 3,
            max_cube_size: 15,
            strategy: CubeSplittingStrategy::Lookahead,
            adaptive_depth: true,
        }
    }
}

/// Cube generator using recursive splitting.
pub struct CubeGenerator {
    /// Configuration
    config: CubeConfig,
    /// Generated cubes
    cubes: Vec<Cube>,
    /// Number of variables
    num_vars: usize,
}

impl CubeGenerator {
    /// Creates a new cube generator.
    pub fn new(num_vars: usize, config: CubeConfig) -> Self {
        Self {
            config,
            cubes: Vec::new(),
            num_vars,
        }
    }

    /// Generates cubes by recursive splitting.
    ///
    /// Starting from an empty cube, recursively splits by choosing a variable
    /// and creating two cubes: one with the positive literal, one with negative.
    pub fn generate(&mut self, variable_scores: &[f64]) -> Vec<Cube> {
        self.cubes.clear();

        // Start with empty cube
        let initial = Cube::new(Vec::new());
        self.split_recursive(initial, variable_scores);

        // If adaptive depth is enabled and we have too few cubes, try again with more depth
        if self.config.adaptive_depth && self.cubes.len() < self.config.target_cubes / 2 {
            self.config.max_depth = (self.config.max_depth * 3) / 2;
            self.cubes.clear();
            let initial = Cube::new(Vec::new());
            self.split_recursive(initial, variable_scores);
        }

        core::mem::take(&mut self.cubes)
    }

    /// Recursively splits a cube into smaller cubes.
    fn split_recursive(&mut self, cube: Cube, variable_scores: &[f64]) {
        // Stop if we've reached max depth or target number of cubes
        if cube.depth >= self.config.max_depth || self.cubes.len() >= self.config.target_cubes {
            if cube.len() >= self.config.min_cube_size && cube.is_consistent() {
                self.cubes.push(cube);
            }
            return;
        }

        // Select next variable to split on
        if let Some(var) = self.select_splitting_variable(&cube, variable_scores) {
            use crate::literal::Var;
            let v = Var::new(var as u32);

            // Create two cubes: one with positive literal, one with negative
            let pos_cube = cube.extend(Lit::pos(v));
            let neg_cube = cube.extend(Lit::neg(v));

            // Recursively split both branches
            self.split_recursive(pos_cube, variable_scores);
            self.split_recursive(neg_cube, variable_scores);
        } else {
            // No more variables to split on, save this cube
            if cube.len() >= self.config.min_cube_size && cube.is_consistent() {
                self.cubes.push(cube);
            }
        }
    }

    /// Selects the best variable to split on based on the strategy.
    fn select_splitting_variable(&self, cube: &Cube, variable_scores: &[f64]) -> Option<usize> {
        // Get variables already assigned in cube
        let mut assigned = HashSet::new();
        for lit in &cube.literals {
            assigned.insert(lit.var().index());
        }

        // Find best unassigned variable
        let mut best_var = None;
        let mut best_score = f64::NEG_INFINITY;

        for var in 0..self.num_vars {
            if assigned.contains(&var) {
                continue;
            }

            let score = if var < variable_scores.len() {
                variable_scores[var]
            } else {
                0.0
            };

            if score > best_score {
                best_score = score;
                best_var = Some(var);
            }
        }

        best_var
    }

    /// Estimates the difficulty of a cube based on its size and depth.
    #[allow(dead_code)]
    fn estimate_difficulty(&self, cube: &Cube) -> f64 {
        // Smaller cubes are generally harder (less constrained)
        let size_factor = 1.0 / (cube.len() as f64 + 1.0);
        // Deeper cubes represent more specific search spaces
        let depth_factor = cube.depth as f64;

        size_factor * depth_factor
    }

    /// Returns the current configuration.
    pub fn config(&self) -> &CubeConfig {
        &self.config
    }

    /// Returns the number of cubes generated.
    pub fn num_cubes(&self) -> usize {
        self.cubes.len()
    }
}

/// Statistics for cube generation.
#[derive(Debug, Clone, Default)]
pub struct CubeStats {
    /// Total cubes generated
    pub total_cubes: usize,
    /// Average cube size
    pub avg_cube_size: f64,
    /// Minimum cube size
    pub min_cube_size: usize,
    /// Maximum cube size
    pub max_cube_size: usize,
    /// Maximum depth reached
    pub max_depth: usize,
    /// Average difficulty
    pub avg_difficulty: f64,
}

impl CubeStats {
    /// Creates statistics from a set of cubes.
    pub fn from_cubes(cubes: &[Cube]) -> Self {
        if cubes.is_empty() {
            return Self::default();
        }

        let total = cubes.len();
        let sizes: Vec<usize> = cubes.iter().map(|c| c.len()).collect();
        let avg_size = sizes.iter().sum::<usize>() as f64 / total as f64;
        let min_size = sizes.iter().copied().min().unwrap_or(0);
        let max_size = sizes.iter().copied().max().unwrap_or(0);
        let max_depth = cubes.iter().map(|c| c.depth).max().unwrap_or(0);
        let avg_diff = cubes.iter().map(|c| c.difficulty).sum::<f64>() / total as f64;

        Self {
            total_cubes: total,
            avg_cube_size: avg_size,
            min_cube_size: min_size,
            max_cube_size: max_size,
            max_depth,
            avg_difficulty: avg_diff,
        }
    }

    /// Displays the statistics.
    pub fn display(&self) -> String {
        format!(
            "Cube Generation Statistics:\n\
             - Total Cubes: {}\n\
             - Avg Size: {:.2}\n\
             - Size Range: [{}, {}]\n\
             - Max Depth: {}\n\
             - Avg Difficulty: {:.4}",
            self.total_cubes,
            self.avg_cube_size,
            self.min_cube_size,
            self.max_cube_size,
            self.max_depth,
            self.avg_difficulty
        )
    }
}

/// Result of solving cubes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CubeResult {
    /// All cubes are UNSAT - formula is UNSAT
    Unsat,
    /// At least one cube is SAT - formula is SAT
    Sat,
    /// Unknown (timeout or resource limit)
    Unknown,
}

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

    #[test]
    fn test_cube_creation() {
        let lit1 = Lit::pos(Var::new(0));
        let lit2 = Lit::neg(Var::new(1));
        let cube = Cube::new(vec![lit1, lit2]);

        assert_eq!(cube.len(), 2);
        assert!(!cube.is_empty());
        assert_eq!(cube.depth, 2);
    }

    #[test]
    fn test_cube_extend() {
        let lit1 = Lit::pos(Var::new(0));
        let cube1 = Cube::new(vec![lit1]);

        let lit2 = Lit::neg(Var::new(1));
        let cube2 = cube1.extend(lit2);

        assert_eq!(cube1.len(), 1);
        assert_eq!(cube2.len(), 2);
        assert_eq!(cube2.depth, 2);
    }

    #[test]
    fn test_cube_consistency() {
        let lit1 = Lit::pos(Var::new(0));
        let lit2 = Lit::neg(Var::new(1));
        let cube = Cube::new(vec![lit1, lit2]);

        assert!(cube.is_consistent());
    }

    #[test]
    fn test_cube_inconsistency() {
        let lit1 = Lit::pos(Var::new(0));
        let lit2 = Lit::neg(Var::new(0));
        let cube = Cube::new(vec![lit1, lit2]);

        assert!(!cube.is_consistent());
    }

    #[test]
    fn test_empty_cube() {
        let cube = Cube::new(Vec::new());

        assert_eq!(cube.len(), 0);
        assert!(cube.is_empty());
        assert!(cube.is_consistent());
    }

    #[test]
    fn test_cube_config_default() {
        let config = CubeConfig::default();

        assert_eq!(config.max_depth, 10);
        assert_eq!(config.target_cubes, 100);
        assert!(config.adaptive_depth);
    }

    #[test]
    fn test_cube_generator_creation() {
        let config = CubeConfig::default();
        let generator = CubeGenerator::new(10, config);

        assert_eq!(generator.num_vars, 10);
        assert_eq!(generator.num_cubes(), 0);
    }

    #[test]
    fn test_cube_generation() {
        let config = CubeConfig {
            max_depth: 3,
            target_cubes: 10,
            min_cube_size: 1,
            max_cube_size: 5,
            strategy: CubeSplittingStrategy::Activity,
            adaptive_depth: false,
        };
        let mut generator = CubeGenerator::new(5, config);
        let scores = vec![1.0, 0.9, 0.8, 0.7, 0.6];

        let cubes = generator.generate(&scores);

        assert!(!cubes.is_empty());
        assert!(cubes.len() <= 10);

        // All cubes should be consistent
        for cube in &cubes {
            assert!(cube.is_consistent());
        }
    }

    #[test]
    fn test_cube_stats() {
        let lit1 = Lit::pos(Var::new(0));
        let lit2 = Lit::neg(Var::new(1));
        let lit3 = Lit::pos(Var::new(2));

        let cubes = vec![
            Cube::new(vec![lit1]),
            Cube::new(vec![lit1, lit2]),
            Cube::new(vec![lit1, lit2, lit3]),
        ];

        let stats = CubeStats::from_cubes(&cubes);

        assert_eq!(stats.total_cubes, 3);
        assert_eq!(stats.min_cube_size, 1);
        assert_eq!(stats.max_cube_size, 3);
        assert_eq!(stats.avg_cube_size, 2.0);
    }

    #[test]
    fn test_empty_cube_stats() {
        let stats = CubeStats::from_cubes(&[]);

        assert_eq!(stats.total_cubes, 0);
        assert_eq!(stats.avg_cube_size, 0.0);
    }

    #[test]
    fn test_cube_splitting_strategies() {
        let strategies = vec![
            CubeSplittingStrategy::Activity,
            CubeSplittingStrategy::Lookahead,
            CubeSplittingStrategy::MostConstrained,
            CubeSplittingStrategy::Balanced,
        ];

        for strategy in strategies {
            let config = CubeConfig {
                strategy,
                max_depth: 2,
                target_cubes: 4,
                min_cube_size: 1,
                max_cube_size: 5,
                adaptive_depth: false,
            };

            let mut generator = CubeGenerator::new(3, config);
            let scores = vec![1.0, 0.5, 0.2];
            let cubes = generator.generate(&scores);

            assert!(!cubes.is_empty());
        }
    }

    #[test]
    fn test_adaptive_depth() {
        let config = CubeConfig {
            max_depth: 2,
            target_cubes: 100,
            min_cube_size: 1,
            max_cube_size: 10,
            strategy: CubeSplittingStrategy::Activity,
            adaptive_depth: true,
        };

        let mut generator = CubeGenerator::new(3, config);
        let scores = vec![1.0, 0.5, 0.2];
        let _cubes = generator.generate(&scores);

        // Adaptive depth should increase max_depth if needed
        assert!(generator.config().max_depth >= 2);
    }
}