Initial commit
This commit is contained in:
commit
959ac83124
5 changed files with 133 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "simple-sat-solver"
|
||||
version = "0.1.0"
|
||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "simple-sat-solver"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
97
src/dpll.rs
Normal file
97
src/dpll.rs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
type Index = i16;
|
||||
|
||||
fn remvove_literal(cnf: &mut Vec<Vec<Index>>, l: Index) {
|
||||
cnf.retain(|c| c.binary_search(&l).is_err());
|
||||
|
||||
for c in cnf {
|
||||
if let Ok(i) = c.binary_search(&(-l)) {
|
||||
c.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn unit_propagate(cnf: &mut Vec<Vec<Index>>, solution: &mut Vec<Index>) {
|
||||
if let Some(i) = cnf
|
||||
.iter()
|
||||
.rev()
|
||||
.position(|c| c.len() == 1)
|
||||
.map(|i| cnf.len() - i - 1)
|
||||
{
|
||||
let l = cnf[i][0];
|
||||
solution.push(l);
|
||||
cnf.swap_remove(i);
|
||||
remvove_literal(cnf, l);
|
||||
unit_propagate(cnf, solution);
|
||||
}
|
||||
}
|
||||
|
||||
fn pure_literal_assign(cnf: &mut Vec<Vec<Index>>, solution: &mut Vec<Index>) {
|
||||
let mut literals: HashSet<Index> = HashSet::new();
|
||||
for c in cnf.iter() {
|
||||
literals.extend(c.iter());
|
||||
}
|
||||
|
||||
for &l in &literals {
|
||||
if !literals.contains(&(-l)) {
|
||||
solution.push(l);
|
||||
remvove_literal(cnf, l);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dpll(cnf: &[Vec<Index>], solution: &mut Vec<Index>) -> bool {
|
||||
let prev_solution = solution.len();
|
||||
let mut own_cnf = cnf.to_owned();
|
||||
|
||||
unit_propagate(&mut own_cnf, solution);
|
||||
pure_literal_assign(&mut own_cnf, solution);
|
||||
|
||||
if own_cnf.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
if own_cnf.iter().any(|c| c.is_empty()) {
|
||||
solution.resize(prev_solution, 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
let literal = own_cnf[0][0];
|
||||
|
||||
own_cnf.push(vec![literal]);
|
||||
if dpll(&own_cnf, solution) {
|
||||
return true;
|
||||
}
|
||||
|
||||
own_cnf.last_mut().unwrap()[0] = -literal;
|
||||
if dpll(&own_cnf, solution) {
|
||||
return true;
|
||||
}
|
||||
|
||||
solution.resize(prev_solution, 0);
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::dpll;
|
||||
|
||||
#[test]
|
||||
fn wikipedia() {
|
||||
let cnf = vec![
|
||||
vec![-1, 2, 3],
|
||||
vec![1, 3, 4],
|
||||
vec![-4, 1, 3],
|
||||
vec![-3, 1, 4],
|
||||
vec![-4, -3, 1],
|
||||
vec![-3, -2, 4],
|
||||
vec![-3, -1, 2],
|
||||
vec![-2, -1, 3],
|
||||
];
|
||||
|
||||
let mut solution = Vec::new();
|
||||
|
||||
assert!(dpll(&cnf, &mut solution));
|
||||
}
|
||||
}
|
||||
22
src/main.rs
Normal file
22
src/main.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use dpll::dpll;
|
||||
|
||||
mod dpll;
|
||||
|
||||
fn main() {
|
||||
let mut solution = Vec::new();
|
||||
|
||||
let cnf = vec![
|
||||
vec![-1, 2, 3],
|
||||
vec![1, 3, 4],
|
||||
vec![-4, 1, 3],
|
||||
vec![-3, 1, 4],
|
||||
vec![-4, -3, 1],
|
||||
vec![-3, -2, 4],
|
||||
vec![-3, -1, 2],
|
||||
vec![-2, -1, 3],
|
||||
];
|
||||
|
||||
dbg!(dpll(&cnf, &mut solution));
|
||||
|
||||
dbg!(solution);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue