satgalaxy 0.2.0

satgalaxy-rs is a Rust library that provides Rust bindings for multiple popular SAT solvers
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
495
496
497
498
499
500
501
502
503
504
505
//! The `glucose` module provides access to the `GlucoseSolver`.
//!
//! This module is enabled when the `minisat` feature is activated.
//!
//! # Overview
//! The `GlucoseSolver` struct acts as a wrapper for the [Glucose](https://github.com/audemard/glucose) Solver, allowing users to
//! leverage its functionality for solving SAT problems.
//!
//! # Usage
//! To use the `glucose` module, ensure the `glucose` feature is enabled in your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! satgalaxy = { version = "x.y.z", features = ["glucose"] }
//! ```
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]

mod bindings {
    include!("../../bindings/glucose_bindings.rs");
}
use std::ptr::NonNull;

use crate::{
    errors::SolverError,
    solver::{RawStatus, SatSolver},
};

/// `GlucoseSolver` is a wrapper for the [Glucose](https://github.com/audemard/glucose) SimpSolver.
/// This struct is only available when the `minisat` feature is enabled.
/// # Example
/// ```rust
/// use satgalaxy::solver::{GlucoseSolver, SatStatus, SatSolver};
/// let mut solver = GlucoseSolver::new();
///     solver.add_clause(&vec![1, 2]);
///     solver.add_clause(&vec![-1, -2]);
///     solver.add_clause(&vec![3]);
///
/// match solver.solve_model().unwrap() {
///    SatStatus::Satisfiable(vec) => {
///         println!("Satisfiable solution: {:?}", vec);
///     },
///     SatStatus::Unsatisfiable => {
///         println!("Unsatisfiable");
///     },
///     SatStatus::Unknown => {
///         println!("Unknown");
///     },
/// }
/// ```
///  # Usage
///  To use the `GlucoseSolver`, ensure the `glucose` feature is enabled in your `Cargo.toml`:
///  ```toml
///  [dependencies]
///  satgalaxy = { version = "x.y.z", features = ["glucose"] }
#[derive(Debug, Clone)]
pub struct GlucoseSolver {
    inner: NonNull<bindings::GlucoseSolver>,
}
unsafe impl Sync for GlucoseSolver {}
unsafe impl Send for GlucoseSolver {}
impl Default for GlucoseSolver {
    fn default() -> Self {
        Self::new()
    }
}

macro_rules! glucose_opt_set {
    ($name:ident,$type:ty,$doc:expr) => {
        glucose_opt_set!($name, $name, $type, $doc);
    };
    ($name:ident,$ffi_name:ident,$type:ty,$doc:expr) => {
        paste::paste! {
            #[doc=$doc]
            pub fn [<set_global_opt_$name>](value: $type) -> Result<(), SolverError> {
                let code = unsafe {
                     bindings::[<glucose_set_global_opt_$ffi_name>](value.into())
                    };

                if code!=0{
                    GlucoseSolver::error_msg(code)?;
                }
                Ok(())
            }

            #[doc=$doc]
            pub fn [<set_opt_$name>](&mut self, value: $type) -> Result<(), SolverError> {
                let code = unsafe {
                     bindings::[<glucose_set_opt_$ffi_name>](self.inner.as_ptr(),value.into())
                    };

                if code!=0{
                    GlucoseSolver::error_msg(code)?;
                }
                Ok(())
            }
        }
    };
}

macro_rules! glucose_opt_g_set {
    ($name:ident,$type:ty,$doc:expr) => {
        glucose_opt_g_set!($name, $name, $type, $doc);
    };
    ($name:ident,$ffi_name:ident,$type:ty,$doc:expr) => {
        paste::paste! {
            #[doc=$doc]
            pub fn [<set_global_opt_$name>](value: $type) -> Result<(), SolverError> {
                let code = unsafe {
                     bindings::[<glucose_set_global_opt_$ffi_name>](value.into())
                    };

                if code!=0{
                    GlucoseSolver::error_msg(code)?;
                }
                Ok(())
            }
        }
    };
}

macro_rules! ffi_bind {
    (
        $(#[$doc:meta])*
        $c_name:ident ($($arg:ident: $arg_ty:ty),*) -> $ret:ty;
        as $rust_name:ident
    ) => {
        $(#[$doc])*
        pub fn $rust_name(&mut self, $($arg: $arg_ty),*) -> Result<$ret, SolverError> {
            unsafe {
                let ret = bindings::$c_name(self.inner.as_ptr() $(, $arg.into())*);
                self.error()?;
                Ok(ret.into())
            }
        }
    };
    (
        $(#[$doc:meta])*
        $c_name:ident ($($arg:ident: $arg_ty:ty),*) -> $ret:ty => |$raw_var:ident| $convert:expr;
        as $rust_name:ident
    ) => {
        $(#[$doc])*
        pub fn $rust_name(&mut self, $($arg: $arg_ty),*) -> Result<$ret, SolverError> {
            unsafe {
                let $raw_var = bindings::$c_name(self.inner.as_ptr() $(, $arg.into())*);
                self.error()?;
                Ok($convert)
            }
        }
    };
}

impl GlucoseSolver {
    fn error_msg(code: i32) -> Result<(), SolverError> {
        unsafe {
            let msg: *const ::std::os::raw::c_char = bindings::glucose_error_msg(code);
            let msg = std::ffi::CStr::from_ptr(msg);
            return Err(SolverError(msg.to_str().unwrap()));
        }
    }
    fn error(&mut self) -> Result<(), SolverError> {
        unsafe {
            let code = bindings::glucose_error(self.inner.as_mut());
            if code != 0 {
                return GlucoseSolver::error_msg(code);
            }
        }
        Ok(())
    }
    glucose_opt_set!(
        k,
        K,
        f64,
        "The constant used to force restart.\n value must be in (0, 1)"
    );
    glucose_opt_set!(
        r,
        R,
        f64,
        "The constant used to block restart\n\n# Arguments\n* `value` - must be in (0, 5)"
    );
    glucose_opt_set!(size_lbd_queue, i32, "The size of moving average for LBD (restarts)\n\n# Arguments\n* `value` - must be at least 10");
    glucose_opt_set!(size_trail_queue, i32, "The size of moving average for trail (block restarts)\n\n# Arguments\n* `value` - must be at least 10");
    glucose_opt_set!(first_reduce_db, i32, "The number of conflicts before first reduce DB\n\n# Arguments\n* `value` - must be non-negative");
    glucose_opt_set!(
        inc_reduce_db,
        i32,
        "Increment for reduce DB\n\n# Arguments\n* `value` - must be non-negative"
    );
    glucose_opt_g_set!(
        spec_inc_reduce_db,
        i32,
        "Special increment for reduce DB\n\n# Arguments\n* `value` - must be non-negative"
    );
    glucose_opt_set!(lb_lbd_frozen_clause, i32, "Protect clauses if LBD decreases below this\n\n# Arguments\n* `value` - must be non-negative");
    glucose_opt_g_set!(chanseok_hack, bool, "Use Chanseok Oh strategy for LBD");
    glucose_opt_set!(
        chanseok_limit,
        i32,
        "Chanseok: permanent clauses with LBD<=limit\n\n# Arguments\n* `value` - must be > 1"
    );
    glucose_opt_set!(
        lb_size_minimzing_clause,
        i32,
        "Min size required to minimize clause\n\n# Arguments\n* `value` - must be >= 3"
    );
    glucose_opt_set!(
        lb_lbd_minimzing_clause,
        i32,
        "Min LBD required to minimize clause\n\n# Arguments\n* `value` - must be >= 3"
    );
    glucose_opt_set!(lcm, bool, "Use inprocessing vivif (ijcai17)");
    glucose_opt_set!(lcm_update_lbd, bool, "Update LBD when doing LCM");
    glucose_opt_set!(
        var_decay,
        f64,
        "Variable activity decay factor\n\n# Arguments\n* `value` - must be in (0, 1)"
    );
    glucose_opt_set!(
        max_var_decay,
        f64,
        "Max variable activity decay factor\n\n# Arguments\n* `value` - must be in (0, 1)"
    );
    glucose_opt_set!(
        clause_decay,
        f64,
        "Clause activity decay factor\n\n# Arguments\n* `value` - must be in (0, 1)"
    );
    glucose_opt_set!(
        random_var_freq,
        f64,
        "Frequency for random variable selection\n\n# Arguments\n* `value` - must be in [0, 1]"
    );
    glucose_opt_set!(
        random_seed,
        f64,
        "Seed for random variable selection\n\n# Arguments\n* `value` - must be positive"
    );
    glucose_opt_set!(ccmin_mode, i32, "Conflict clause minimization (0=none,1=basic,2=deep)\n\n# Arguments\n* `value` - must be 0-2");
    glucose_opt_set!(
        phase_saving,
        i32,
        "Phase saving (0=none,1=basic,2=deep)\n\n# Arguments\n* `value` - must be 0-2"
    );
    glucose_opt_set!(rnd_init_act, bool, "Randomize initial activity\n\n");
    glucose_opt_set!(
        garbage_frac,
        f64,
        "Memory waste allowed before GC\n\n# Arguments\n* `value` - must be positive"
    );
    glucose_opt_g_set!(glu_reduction, bool, "Glucose reduction strategy");
    glucose_opt_g_set!(luby_restart, bool, "Use Luby restart sequence");
    glucose_opt_g_set!(
        restart_inc,
        f64,
        "Restart interval increase factor\n\n# Arguments\n* `value` - must be >= 1.0"
    );
    glucose_opt_g_set!(
        luby_restart_factor,
        i32,
        "Luby restart factor\n\n# Arguments\n* `value` - must be positive"
    );
    glucose_opt_g_set!(
        randomize_phase_on_restarts,
        i32,
        "Randomization level on restarts (0-3)\n\n# Arguments\n* `value` - must be 0-3"
    );
    glucose_opt_g_set!(
        fixed_randomize_phase_on_restarts,
        bool,
        "Fix first 7 levels at random phase"
    );
    glucose_opt_g_set!(adapt, bool, "Adapt strategies after 100000 conflicts\n\n# Arguments\n* `value` - boolean (1=true, 0=false)");
    glucose_opt_g_set!(forceunsat, bool, "Force phase for UNSAT");
    glucose_opt_set!(use_asymm, bool, "Shrink clauses by asymmetric branching");
    glucose_opt_set!(use_rcheck, bool, "Check if clause is already implied");
    glucose_opt_set!(use_elim, bool, "Perform variable elimination");
    glucose_opt_set!(
        grow,
        i32,
        "Allow clause growth in elimination\n\n# Arguments\n* `value` - must be an integer"
    );
    glucose_opt_set!(clause_lim, i32, "Max resolvent length for elimination (-1=no limit)\n\n# Arguments\n* `value` - must be -1 or positive");
    glucose_opt_set!(subsumption_lim, i32, "Max clause size for subsumption (-1=no limit)\n\n# Arguments\n* `value` - must be -1 or positive");
    glucose_opt_set!(
        simp_garbage_frac,
        f64,
        "Memory waste allowed during simplification\n\n# Arguments\n* `value` - must be positive"
    );
    glucose_opt_set!(
        verbosity,
        i32,
        "Verbosity level (0=silent,1=some,2=more)\n\n# Arguments\n* `value` - must be 0-2"
    );

    pub fn new() -> Self {
        unsafe {
            GlucoseSolver {
                inner: NonNull::new(bindings::glucose_new_solver()).unwrap(),
            }
        }
    }
    ffi_bind! {
        /// Add a new variable to the solver.
        glucose_new_var() -> i32;
        as new_var
    }

    /// Add a clause to the solver.
    pub fn add_clause(&mut self, clause: &[i32]) -> Result<(), SolverError> {
        unsafe {
            bindings::glucose_add_clause(self.inner.as_ptr(), clause.as_ptr(), clause.len());
        }
        self.error()?;
        Ok(())
    }
    ffi_bind! {
        /// Add the empty clause to the solver.
        glucose_add_empty_clause() -> i32;
        as add_empty_clause
    }

    ffi_bind! {
        /// Get the value of a literal.
        glucose_value(x: i32) -> i32;
        as value
    }

    ffi_bind! {
        /// Get the value of a literal in the model.
        glucose_model_value(x: i32) -> bool=>|v|v!=0;
        as model_value
    }

    /// Solve the problem with assumptions.
    pub fn solve_assumps(
        &mut self,
        clause: &[i32],
        do_simp: bool,
        turn_off_simp: bool,
    ) -> Result<RawStatus, SolverError> {
        let status = unsafe {
            bindings::glucose_solve_assumps(
                self.inner.as_ptr(),
                clause.as_ptr(),
                clause.len(),
                do_simp.into(),
                turn_off_simp.into(),
            )
        }
        .into();
        self.error()?;
        Ok(status)
    }
    /// Solve the problem with limited.
    pub fn solve_limited(
        &mut self,
        clause: &[i32],
        do_simp: bool,
        turn_off_simp: bool,
    ) -> Result<RawStatus, SolverError> {
        let status = unsafe {
            bindings::glucose_solve_limited(
                self.inner.as_ptr(),
                clause.as_ptr(),
                clause.len(),
                do_simp.into(),
                turn_off_simp.into(),
            )
        }
        .into();
        self.error()?;
        Ok(status)
    }
    /// Solve the problem with limited.

    pub fn glucose_solve_limited(
        &mut self,
        clause: &[i32],
        do_simp: bool,
        turn_off_simp: bool,
    ) -> Result<RawStatus, SolverError> {
        let status = unsafe {
            bindings::glucose_solve_limited(
                self.inner.as_ptr(),
                clause.as_ptr(),
                clause.len(),
                do_simp.into(),
                turn_off_simp.into(),
            )
        }
        .into();
        self.error()?;
        Ok(status)
    }

    ffi_bind! {
        /// Solve the problem.
        glucose_solve(do_simp: bool, turn_off_simp: bool) -> i32;
        as solve
    }

    ffi_bind! {
        /// Perform variable elimination based simplification.
        glucose_eliminate(turn_off_elim: bool) -> i32;
        as eliminate
    }

    ffi_bind! {
        /// The current number of assigned literals.
        glucose_nassigns() -> i32;
        as nassigns
    }

    ffi_bind! {
        /// The current number of original clauses.
        glucose_nclauses() -> i32;
        as nclauses
    }

    ffi_bind! {
        /// The current number of learnt clauses.
        glucose_nlearnts() -> i32;
        as nlearnts
    }

    ffi_bind! {
        /// The current number of variables.
        glucose_nvars() -> i32;
        as nvars
    }

    ffi_bind! {
        /// The current number of free variables.
        glucose_nfree_vars() -> i32;
        as nfree_vars
    }

    ffi_bind! {
        /// Destroy the solver.
        glucose_destroy() -> ();
        as destroy
    }

    ffi_bind! {
        /// Check if the solver is okay.
        glucose_okay() -> i32;
        as okay
    }
}

impl SatSolver for GlucoseSolver {
    fn push_clause(&mut self, clause: &[i32]) -> Result<(), SolverError> {
        GlucoseSolver::add_clause(self, clause)?;
        Ok(())
    }
    fn solve_sat(&mut self) -> Result<RawStatus, SolverError> {
        self.eliminate(true)?;
        self.solve_limited(&[], true, false)
    }

    fn model(&mut self) -> Result<Vec<i32>, SolverError> {
        let mut model = vec![];
        for lit in 1..=self.nvars()? {
            if self.model_value(lit)? {
                model.push(lit);
            }
        }
        Ok(model)
    }
}
impl Drop for GlucoseSolver {
    fn drop(&mut self) {
        unsafe {
            bindings::glucose_destroy(self.inner.as_ptr());
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::solver::SatStatus;

    use super::*;
    #[test]
    fn unsat() {
        let mut solver = GlucoseSolver::new();
        solver.push_clause(&vec![1]).unwrap();
        solver.push_clause(&vec![-1]).unwrap();
        assert!(matches!(
            solver.solve_model().unwrap(),
            SatStatus::Unsatisfiable
        ));
    }
    #[test]
    fn sat() {
        let mut solver = GlucoseSolver::new();
        solver.push_clause(&vec![1, 2]).unwrap();
        solver.push_clause(&vec![-1]).unwrap();
        assert!(
            matches!(solver.solve_model().unwrap(),SatStatus::Satisfiable(x) if x.eq(&vec![2]))
        );
    }
}