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
//! Activity management for variables and clauses
//!
//! This module provides efficient activity tracking and decay for both
//! variables and clauses. Activity scores are used for decision heuristics
//! and clause deletion policies.
//!
//! References:
//! - "Chaff: Engineering an Efficient SAT Solver" (VSIDS)
//! - "Glucose: Combining Fast Local Search and Heuristics"

use crate::clause::ClauseId;
use crate::literal::Var;
#[allow(unused_imports)]
use crate::prelude::*;

/// Statistics for activity management
#[derive(Debug, Clone, Default)]
pub struct ActivityStats {
    /// Total number of bumps performed
    pub total_bumps: u64,
    /// Total number of decays performed
    pub total_decays: u64,
    /// Number of rescales performed
    pub rescales: u64,
    /// Current activity increment
    pub current_increment: f64,
}

impl ActivityStats {
    /// Display statistics
    pub fn display(&self) {
        println!("Activity Manager Statistics:");
        println!("  Total bumps: {}", self.total_bumps);
        println!("  Total decays: {}", self.total_decays);
        println!("  Rescales: {}", self.rescales);
        println!("  Current increment: {:.2e}", self.current_increment);
    }
}

/// Variable activity manager
///
/// Manages activity scores for variables using VSIDS-style decay.
#[derive(Debug)]
pub struct VariableActivityManager {
    /// Activity score for each variable
    activities: Vec<f64>,
    /// Activity increment (increased on each decay)
    increment: f64,
    /// Decay factor (< 1.0)
    decay: f64,
    /// Threshold for rescaling
    rescale_threshold: f64,
    /// Statistics
    stats: ActivityStats,
}

impl Default for VariableActivityManager {
    fn default() -> Self {
        Self::new()
    }
}

impl VariableActivityManager {
    /// Create a new variable activity manager
    ///
    /// Default decay factor: 0.95
    #[must_use]
    pub fn new() -> Self {
        Self::with_decay(0.95)
    }

    /// Create with custom decay factor
    ///
    /// Decay should be in range (0.0, 1.0):
    /// - Higher values (e.g., 0.99) decay more slowly
    /// - Lower values (e.g., 0.8) decay more quickly
    #[must_use]
    pub fn with_decay(decay: f64) -> Self {
        Self {
            activities: Vec::new(),
            increment: 1.0,
            decay: decay.clamp(0.0, 1.0),
            rescale_threshold: 1e100,
            stats: ActivityStats {
                current_increment: 1.0,
                ..Default::default()
            },
        }
    }

    /// Resize for new number of variables
    pub fn resize(&mut self, num_vars: usize) {
        self.activities.resize(num_vars, 0.0);
    }

    /// Bump (increase) activity of a variable
    pub fn bump(&mut self, var: Var) {
        let idx = var.index();

        // Ensure we have space
        if idx >= self.activities.len() {
            self.activities.resize(idx + 1, 0.0);
        }

        self.activities[idx] += self.increment;
        self.stats.total_bumps += 1;

        // Rescale if needed
        if self.activities[idx] > self.rescale_threshold {
            self.rescale();
        }
    }

    /// Decay all activities and increase increment
    pub fn decay(&mut self) {
        self.increment /= self.decay;
        self.stats.total_decays += 1;
        self.stats.current_increment = self.increment;

        // Rescale if increment gets too large
        if self.increment > self.rescale_threshold {
            self.rescale();
        }
    }

    /// Rescale all activities to prevent overflow
    fn rescale(&mut self) {
        const RESCALE_FACTOR: f64 = 1e-100;

        for activity in &mut self.activities {
            *activity *= RESCALE_FACTOR;
        }
        self.increment *= RESCALE_FACTOR;
        self.stats.rescales += 1;
        self.stats.current_increment = self.increment;
    }

    /// Get activity of a variable
    #[must_use]
    pub fn activity(&self, var: Var) -> f64 {
        let idx = var.index();
        if idx < self.activities.len() {
            self.activities[idx]
        } else {
            0.0
        }
    }

    /// Set activity of a variable
    pub fn set_activity(&mut self, var: Var, activity: f64) {
        let idx = var.index();
        if idx >= self.activities.len() {
            self.activities.resize(idx + 1, 0.0);
        }
        self.activities[idx] = activity;
    }

    /// Get all activities
    #[must_use]
    pub fn activities(&self) -> &[f64] {
        &self.activities
    }

    /// Get statistics
    #[must_use]
    pub fn stats(&self) -> &ActivityStats {
        &self.stats
    }

    /// Reset all activities
    pub fn reset(&mut self) {
        for activity in &mut self.activities {
            *activity = 0.0;
        }
        self.increment = 1.0;
        self.stats = ActivityStats {
            current_increment: 1.0,
            ..Default::default()
        };
    }
}

/// Clause activity manager
///
/// Manages activity scores for clauses, used in clause deletion policies.
#[derive(Debug)]
pub struct ClauseActivityManager {
    /// Activity score for each clause
    activities: Vec<f64>,
    /// Activity increment
    increment: f64,
    /// Decay factor
    decay: f64,
    /// Rescale threshold
    rescale_threshold: f64,
    /// Statistics
    stats: ActivityStats,
}

impl Default for ClauseActivityManager {
    fn default() -> Self {
        Self::new()
    }
}

impl ClauseActivityManager {
    /// Create a new clause activity manager
    ///
    /// Default decay factor: 0.999 (slower than variable decay)
    #[must_use]
    pub fn new() -> Self {
        Self::with_decay(0.999)
    }

    /// Create with custom decay factor
    #[must_use]
    pub fn with_decay(decay: f64) -> Self {
        Self {
            activities: Vec::new(),
            increment: 1.0,
            decay: decay.clamp(0.0, 1.0),
            rescale_threshold: 1e20,
            stats: ActivityStats {
                current_increment: 1.0,
                ..Default::default()
            },
        }
    }

    /// Bump activity of a clause
    pub fn bump(&mut self, clause_id: ClauseId) {
        let idx = clause_id.0 as usize;

        // Ensure we have space
        if idx >= self.activities.len() {
            self.activities.resize(idx + 1, 0.0);
        }

        self.activities[idx] += self.increment;
        self.stats.total_bumps += 1;

        if self.activities[idx] > self.rescale_threshold {
            self.rescale();
        }
    }

    /// Decay all clause activities
    pub fn decay(&mut self) {
        self.increment /= self.decay;
        self.stats.total_decays += 1;
        self.stats.current_increment = self.increment;

        if self.increment > self.rescale_threshold {
            self.rescale();
        }
    }

    /// Rescale to prevent overflow
    fn rescale(&mut self) {
        const RESCALE_FACTOR: f64 = 1e-20;

        for activity in &mut self.activities {
            *activity *= RESCALE_FACTOR;
        }
        self.increment *= RESCALE_FACTOR;
        self.stats.rescales += 1;
        self.stats.current_increment = self.increment;
    }

    /// Get activity of a clause
    #[must_use]
    pub fn activity(&self, clause_id: ClauseId) -> f64 {
        let idx = clause_id.0 as usize;
        if idx < self.activities.len() {
            self.activities[idx]
        } else {
            0.0
        }
    }

    /// Set activity of a clause
    pub fn set_activity(&mut self, clause_id: ClauseId, activity: f64) {
        let idx = clause_id.0 as usize;
        if idx >= self.activities.len() {
            self.activities.resize(idx + 1, 0.0);
        }
        self.activities[idx] = activity;
    }

    /// Get statistics
    #[must_use]
    pub fn stats(&self) -> &ActivityStats {
        &self.stats
    }

    /// Reset all activities
    pub fn reset(&mut self) {
        self.activities.clear();
        self.increment = 1.0;
        self.stats = ActivityStats {
            current_increment: 1.0,
            ..Default::default()
        };
    }
}

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

    #[test]
    fn test_variable_activity_manager_creation() {
        let manager = VariableActivityManager::new();
        assert_eq!(manager.decay, 0.95);
        assert_eq!(manager.increment, 1.0);
    }

    #[test]
    fn test_bump_variable() {
        let mut manager = VariableActivityManager::new();
        manager.resize(10);

        manager.bump(Var::new(0));
        assert_eq!(manager.activity(Var::new(0)), 1.0);

        manager.bump(Var::new(0));
        assert_eq!(manager.activity(Var::new(0)), 2.0);
    }

    #[test]
    fn test_decay_variable() {
        let mut manager = VariableActivityManager::with_decay(0.5);
        manager.resize(10);

        manager.bump(Var::new(0));
        let initial_activity = manager.activity(Var::new(0));

        manager.decay();
        manager.bump(Var::new(1));

        // After decay, new bumps should have higher increment
        assert!(manager.activity(Var::new(1)) > initial_activity);
        assert_eq!(manager.stats().total_decays, 1);
    }

    #[test]
    fn test_rescale_variable() {
        let mut manager = VariableActivityManager::new();
        manager.resize(10);
        manager.rescale_threshold = 10.0; // Low threshold for testing

        // Bump until rescale
        for _ in 0..20 {
            manager.bump(Var::new(0));
        }

        // Should have rescaled
        assert!(manager.stats().rescales > 0);
        assert!(manager.activity(Var::new(0)) < 100.0);
    }

    #[test]
    fn test_set_activity() {
        let mut manager = VariableActivityManager::new();
        manager.resize(10);

        manager.set_activity(Var::new(0), 42.0);
        assert_eq!(manager.activity(Var::new(0)), 42.0);
    }

    #[test]
    fn test_reset_variable() {
        let mut manager = VariableActivityManager::new();
        manager.resize(10);

        manager.bump(Var::new(0));
        manager.decay();

        manager.reset();

        assert_eq!(manager.activity(Var::new(0)), 0.0);
        assert_eq!(manager.increment, 1.0);
        assert_eq!(manager.stats().total_bumps, 0);
    }

    #[test]
    fn test_clause_activity_manager_creation() {
        let manager = ClauseActivityManager::new();
        assert_eq!(manager.decay, 0.999);
    }

    #[test]
    fn test_bump_clause() {
        let mut manager = ClauseActivityManager::new();

        manager.bump(ClauseId(0));
        assert_eq!(manager.activity(ClauseId(0)), 1.0);

        manager.bump(ClauseId(0));
        assert_eq!(manager.activity(ClauseId(0)), 2.0);
    }

    #[test]
    fn test_decay_clause() {
        let mut manager = ClauseActivityManager::with_decay(0.5);

        manager.bump(ClauseId(0));
        manager.decay();
        manager.bump(ClauseId(1));

        assert!(manager.activity(ClauseId(1)) > manager.activity(ClauseId(0)));
    }

    #[test]
    fn test_clause_rescale() {
        let mut manager = ClauseActivityManager::new();
        manager.rescale_threshold = 5.0;

        for _ in 0..10 {
            manager.bump(ClauseId(0));
        }

        assert!(manager.stats().rescales > 0);
    }

    #[test]
    fn test_reset_clause() {
        let mut manager = ClauseActivityManager::new();

        manager.bump(ClauseId(0));
        manager.reset();

        assert_eq!(manager.activity(ClauseId(0)), 0.0);
        assert_eq!(manager.increment, 1.0);
    }
}