-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Example minimization of compiled knowledge via vtree fragments. | ||
use sddrs::{ | ||
literal::literal::Polarity, | ||
manager::{ | ||
options::{self, FragmentHeuristic, GarbageCollection, MinimizationCutoff, VTreeStrategy}, | ||
SddManager, | ||
}, | ||
}; | ||
|
||
fn main() { | ||
let opts = options::SddOptions::builder() | ||
.vtree_strategy(VTreeStrategy::LeftLinear) | ||
.garbage_collection(GarbageCollection::Automatic) | ||
.variables(&["A".to_string(), "B".to_string(), "C".to_string()]) | ||
.build(); | ||
let manager = SddManager::new(&opts); | ||
|
||
let a = manager.literal("A", Polarity::Positive).unwrap(); | ||
let b = manager.literal("B", Polarity::Positive).unwrap(); | ||
let c = manager.literal("C", Polarity::Positive).unwrap(); | ||
|
||
let conjunction = manager.conjoin(&a, &manager.conjoin(&b, &c)); | ||
let size_before = conjunction.size(); | ||
println!("size for right-linear: {}", size_before); | ||
|
||
manager | ||
.minimize( | ||
MinimizationCutoff::BreakAfterFirstImprovement, | ||
&FragmentHeuristic::Root, | ||
&conjunction, | ||
) | ||
.unwrap(); | ||
|
||
let size_after = conjunction.size(); | ||
println!("size after minimizing via fragments: {}", size_after); | ||
|
||
// Ideally, this should hold: | ||
assert!(size_after <= size_before); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use sddrs::manager::{ | ||
options::{self, GarbageCollection, VTreeStrategy}, | ||
SddManager, | ||
}; | ||
|
||
fn main() { | ||
let opts = options::SddOptions::builder() | ||
.vtree_strategy(VTreeStrategy::LeftLinear) | ||
.garbage_collection(GarbageCollection::Automatic) | ||
.variables(&[ | ||
"A".to_string(), | ||
"B".to_string(), | ||
"C".to_string(), | ||
"D".to_string(), | ||
]) | ||
.build(); | ||
let manager = SddManager::new(&opts); | ||
|
||
let dimacs = "c | ||
p cnf 4 3 | ||
1 3 -4 0 | ||
4 0 | ||
2 -3 0 | ||
"; | ||
let sdd = manager.from_dimacs(&mut dimacs.as_bytes()).unwrap(); | ||
|
||
println!("Compiled SDD in DOT Graphviz format:"); | ||
manager.draw_sdd(&mut std::io::stdout(), &sdd).unwrap(); | ||
|
||
println!("\n---\nVtree in DOT Graphviz format:"); | ||
manager.draw_vtree(&mut std::io::stdout()).unwrap(); | ||
println!(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Example of manual minimization of compiled knowledge. | ||
use sddrs::{ | ||
literal::literal::Polarity, | ||
manager::{ | ||
options::{self, GarbageCollection, VTreeStrategy}, | ||
SddManager, | ||
}, | ||
}; | ||
|
||
fn main() { | ||
let opts = options::SddOptions::builder() | ||
// x | ||
// / \ | ||
// A y | ||
// / \ | ||
// B C | ||
.vtree_strategy(VTreeStrategy::RightLinear) | ||
.garbage_collection(GarbageCollection::Automatic) | ||
.variables(&["A".to_string(), "B".to_string(), "C".to_string()]) | ||
.build(); | ||
let manager = SddManager::new(&opts); | ||
|
||
let a = manager.literal("A", Polarity::Positive).unwrap(); | ||
let b = manager.literal("B", Polarity::Positive).unwrap(); | ||
let c = manager.literal("C", Polarity::Positive).unwrap(); | ||
|
||
// A && B && C | ||
let conjunction = manager.conjoin(&a, &manager.conjoin(&b, &c)); | ||
println!("size for right-linear: {}", conjunction.size()); | ||
|
||
// Rotate 'x' to obtain this vtree: | ||
// x | ||
// / \ | ||
// y C | ||
// / \ | ||
// A B | ||
manager | ||
.rotate_left(&manager.root().right_child().unwrap()) | ||
.unwrap(); | ||
println!( | ||
"size after rotating 'x' to the left: {}", | ||
conjunction.size() | ||
); | ||
|
||
// Swap children of 'y' to obtain this vtree: | ||
// x | ||
// / \ | ||
// y C | ||
// / \ | ||
// B A | ||
manager.swap(&manager.root().left_child().unwrap()).unwrap(); | ||
println!( | ||
"size after swapping children of 'y': {}", | ||
conjunction.size() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! Example of simple knowledge compilation. | ||
use sddrs::{ | ||
literal::literal::Polarity, | ||
manager::{ | ||
options::{self, GarbageCollection, VTreeStrategy}, | ||
SddManager, | ||
}, | ||
}; | ||
|
||
fn main() { | ||
let opts = options::SddOptions::builder() | ||
.vtree_strategy(VTreeStrategy::RightLinear) | ||
.garbage_collection(GarbageCollection::Automatic) | ||
.variables(&["A".to_string(), "B".to_string(), "C".to_string()]) | ||
.build(); | ||
|
||
let manager = SddManager::new(&opts); | ||
|
||
let a = manager.literal("A", Polarity::Positive).unwrap(); | ||
let n_b = manager.literal("B", Polarity::Negative).unwrap(); | ||
let c = manager.literal("C", Polarity::Positive).unwrap(); | ||
|
||
// A && !B | ||
let fst = manager.conjoin(&a, &n_b); | ||
assert_eq!(manager.model_count(&fst), 2); | ||
println!("A && !B:\n{}\n", manager.model_enumeration(&fst)); | ||
|
||
// C => (A && !B) | ||
let snd = manager.imply(&c, &fst); | ||
assert_eq!(manager.model_count(&snd), 5); | ||
println!("C => (A && !B):\n{}\n", manager.model_enumeration(&snd)); | ||
|
||
// !(C => (A && !B)) | ||
let n_snd = manager.negate(&snd); | ||
assert_eq!(manager.model_count(&n_snd), 3); | ||
println!("!(C => A && !B):\n{}\n", manager.model_enumeration(&n_snd)); | ||
|
||
// (C => (A && !B)) <=> !(C => (A && !B)) == False | ||
let ff = manager.equiv(&snd, &n_snd); | ||
assert_eq!(manager.model_count(&ff), 0); | ||
assert!(ff.is_false()); | ||
println!("False:\n{}\n", manager.model_enumeration(&ff)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters