Skip to main content

readme_example/
readme_example.rs

1use std::collections::HashMap;
2use pldag::{Bound, Pldag};
3
4fn main() {
5    // Build your PL-DAG
6    // For example, we create a model of three boolean variables x, y and z.
7    // We bind them to an OR constraint.
8    let mut pldag: Pldag = Pldag::new();
9
10    // First setup the primitive variables
11    pldag.set_primitive("x", (0, 1));
12    pldag.set_primitive("y", (0, 1));
13    pldag.set_primitive("z", (0, 1));
14
15    // A reference ID is returned
16    let root = pldag.set_or(vec!["x", "y", "z"]).unwrap();
17
18    // Export a sub dag
19    let mut dag = pldag.sub_dag(vec![root.clone()]).unwrap();
20
21    // 1. Validate a combination:
22    let mut inputs: HashMap<&str, Bound> = HashMap::new();
23    let validated = Pldag::propagate_dag(&mut dag, inputs.clone()).unwrap();
24    // Since nothing is given, and all other variables implicitly have bounds (0, 1) from the pldag model,
25    // the root will be (0,1) since there's not enough information to evaluate the root `or` node.
26    println!("Root valid? {}", *validated.get(&root).unwrap() == (1, 1)); // This will be false
27
28    // If we however fix x to be zero, then we can check the result
29    inputs.insert("x", (0, 0));
30    let revalidated = pldag.propagate(inputs.clone()).unwrap();
31    println!("Root valid? {}", *revalidated.get(&root).unwrap() == (1, 1)); // This will be false
32
33    // However, fixing y and z to 1 will yield the root node to be true (since the root will be true if any of x, y or z is true).
34    inputs.insert("y", (1, 1));
35    inputs.insert("z", (1, 1));
36    let revalidated = pldag.propagate(inputs.clone()).unwrap();
37    println!("Root valid? {}", *revalidated.get(&root).unwrap() == (1, 1)); // This will be true
38
39    // Build a simple OR‑of‑three model
40    let mut pldag = Pldag::new();
41    pldag.set_primitive("x", (0, 1));
42    pldag.set_primitive("y", (0, 1));
43    pldag.set_primitive("z", (0, 1));
44    let root = pldag.set_or(vec!["x", "y", "z"]).unwrap();
45    let mut dag = pldag.sub_dag(vec![root.clone()]).unwrap();
46
47    // 1. Validate a combination
48    let validated = dag.propagate(inputs).unwrap();
49    println!("root bound = {:?}", validated[&root]);
50}