Working cdcl implentation?
This commit is contained in:
parent
63b1350b3b
commit
0faf1284df
3 changed files with 22 additions and 11 deletions
|
|
@ -6,4 +6,3 @@ x2 | x11
|
||||||
!x7 | x8 | !x9
|
!x7 | x8 | !x9
|
||||||
x7 | x8 | !x10
|
x7 | x8 | !x10
|
||||||
x7 | x10 | !x12
|
x7 | x10 | !x12
|
||||||
x1
|
|
||||||
|
|
|
||||||
31
src/cdcl.rs
31
src/cdcl.rs
|
|
@ -69,6 +69,8 @@ impl State {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
|
// println!("Source: {source_slice:?}");
|
||||||
|
// println!("Slice: {slice:?}");
|
||||||
let mut conflict: Vec<_> = source_slice
|
let mut conflict: Vec<_> = source_slice
|
||||||
.iter()
|
.iter()
|
||||||
.copied()
|
.copied()
|
||||||
|
|
@ -112,7 +114,9 @@ impl State {
|
||||||
|
|
||||||
match unknown_literal {
|
match unknown_literal {
|
||||||
Some(l) => ClauseState::UnitPropagate(l),
|
Some(l) => ClauseState::UnitPropagate(l),
|
||||||
None => panic!("Fully false clause."),
|
None => {
|
||||||
|
panic!("Fully false clause. {s:?}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -176,6 +180,7 @@ fn unit_propagate(cnf: &NormalForm, state: &mut State) -> Result<(), Vec<Index>>
|
||||||
|
|
||||||
let l = slice[i];
|
let l = slice[i];
|
||||||
|
|
||||||
|
// println!("Propagating literal: {l:?}");
|
||||||
state.add_literal_propagate(l, slice, cnf)?;
|
state.add_literal_propagate(l, slice, cnf)?;
|
||||||
|
|
||||||
unit_propagate(cnf, state)
|
unit_propagate(cnf, state)
|
||||||
|
|
@ -184,7 +189,11 @@ fn unit_propagate(cnf: &NormalForm, state: &mut State) -> Result<(), Vec<Index>>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cdcl_internal(cnf: &mut NormalForm, mut state: State) -> Result<State, Vec<Index>> {
|
fn cdcl_internal(
|
||||||
|
cnf: &mut NormalForm,
|
||||||
|
mut state: State,
|
||||||
|
depth: usize,
|
||||||
|
) -> Result<State, Vec<Index>> {
|
||||||
unit_propagate(cnf, &mut state)?;
|
unit_propagate(cnf, &mut state)?;
|
||||||
|
|
||||||
let l = match state.get_arbitrary_literal() {
|
let l = match state.get_arbitrary_literal() {
|
||||||
|
|
@ -194,17 +203,21 @@ fn cdcl_internal(cnf: &mut NormalForm, mut state: State) -> Result<State, Vec<In
|
||||||
|
|
||||||
let mut state_clone = state.clone();
|
let mut state_clone = state.clone();
|
||||||
state_clone.add_literal_arbitrary(l);
|
state_clone.add_literal_arbitrary(l);
|
||||||
|
// println!("Choosing arbitrary literal: {l:?}");
|
||||||
|
|
||||||
match cdcl_internal(cnf, state_clone) {
|
match cdcl_internal(cnf, state_clone, depth + 1) {
|
||||||
Ok(s) => Ok(s),
|
Ok(s) => Ok(s),
|
||||||
Err(c) => {
|
Err(c) => {
|
||||||
let count_unknown_literals =
|
let count_unknown_literals = c.iter().filter(|&&i| state.is_unknown_literal(i)).count();
|
||||||
c.iter().filter(|&&i| !state.is_unknown_literal(i)).count();
|
// println!("Count unknown: {count_unknown_literals}; {c:?}");
|
||||||
|
if count_unknown_literals >= 2
|
||||||
if count_unknown_literals >= 2 || (count_unknown_literals == c.len()) {
|
|| (count_unknown_literals == c.len())
|
||||||
|
|| (depth == 0 && count_unknown_literals == 1)
|
||||||
|
{
|
||||||
|
// println!("Adding cclause: {c:?}");
|
||||||
cnf.add_clause(&c);
|
cnf.add_clause(&c);
|
||||||
|
|
||||||
cdcl_internal(cnf, state)
|
cdcl_internal(cnf, state, depth)
|
||||||
} else {
|
} else {
|
||||||
Err(c)
|
Err(c)
|
||||||
}
|
}
|
||||||
|
|
@ -214,7 +227,7 @@ fn cdcl_internal(cnf: &mut NormalForm, mut state: State) -> Result<State, Vec<In
|
||||||
|
|
||||||
pub fn cdcl(cnf: &mut NormalForm) -> Option<Vec<Index>> {
|
pub fn cdcl(cnf: &mut NormalForm) -> Option<Vec<Index>> {
|
||||||
let num_literals = cnf.get_num_literals();
|
let num_literals = cnf.get_num_literals();
|
||||||
match cdcl_internal(cnf, State::new(num_literals)) {
|
match cdcl_internal(cnf, State::new(num_literals), 0) {
|
||||||
Ok(state) => {
|
Ok(state) => {
|
||||||
if state.is_complete() {
|
if state.is_complete() {
|
||||||
Some(state.get_solution())
|
Some(state.get_solution())
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,6 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
Method::Cdcl => {
|
Method::Cdcl => {
|
||||||
let (mut cnf, map) = e.cnf_normal_form();
|
let (mut cnf, map) = e.cnf_normal_form();
|
||||||
dbg!(&map);
|
|
||||||
match crate::cdcl::cdcl(&mut cnf) {
|
match crate::cdcl::cdcl(&mut cnf) {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
solution.extend_from_slice(&s);
|
solution.extend_from_slice(&s);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue