Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cadical: Add ability to clone solver #35

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/pindakaas-cadical/src/ccadical_override.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,10 @@ void ccadical_phase(CCaDiCaL *slv, int lit) {
void ccadical_unphase(CCaDiCaL *slv, int lit) {
((Wrapper *)slv)->solver->unphase(lit);
}

CCaDiCaL *ccadical_copy(CCaDiCaL *slv) {
auto *cp = new Wrapper();
((Wrapper *)slv)->solver->copy(*cp->solver);
return (CCaDiCaL *)cp;
}
}
1 change: 1 addition & 0 deletions crates/pindakaas-cadical/src/ccadical_up.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void ccadical_prop_set_add_external_clause_lit(

void ccadical_phase(CCaDiCaL *, int lit);
void ccadical_unphase(CCaDiCaL *, int lit);
CCaDiCaL *ccadical_copy(CCaDiCaL *slv);

/*------------------------------------------------------------------------*/

Expand Down
1 change: 1 addition & 0 deletions crates/pindakaas-cadical/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ extern "C" {
pub fn ccadical_simplify(slv: *mut c_void) -> c_int;
pub fn ccadical_terminate(slv: *mut c_void);
pub fn ccadical_unphase(slv: *mut c_void, lit: i32);
pub fn ccadical_copy(slv: *const c_void) -> *mut c_void;
}
25 changes: 23 additions & 2 deletions crates/pindakaas/src/solver/cadical.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use pindakaas_cadical::{ccadical_copy, ccadical_phase, ccadical_unphase};
use pindakaas_derive::IpasirSolver;

use super::VarFactory;
Expand All @@ -23,13 +24,25 @@ impl Default for Cadical {
}
}

impl Clone for Cadical {
fn clone(&self) -> Self {
let ptr = unsafe { ccadical_copy(self.ptr) };
Self {
ptr,
vars: self.vars,
#[cfg(feature = "ipasir-up")]
prop: None,
}
}
}

impl Cadical {
pub fn phase(&mut self, lit: Lit) {
unsafe { pindakaas_cadical::ccadical_phase(self.ptr, lit.0.get()) }
unsafe { ccadical_phase(self.ptr, lit.0.get()) }
}

pub fn unphase(&mut self, lit: Lit) {
unsafe { pindakaas_cadical::ccadical_unphase(self.ptr, lit.0.get()) }
unsafe { ccadical_unphase(self.ptr, lit.0.get()) }
}
}

Expand Down Expand Up @@ -68,6 +81,14 @@ mod tests {
)
});
assert_eq!(res, SolveResult::Sat);
// Test clone implementation
let mut cp = slv.clone();
cp.solve(|value| {
assert!(
(value(!a).unwrap() && value(b).unwrap())
|| (value(a).unwrap() && value(!b).unwrap()),
)
});
}

#[cfg(feature = "ipasir-up")]
Expand Down