Skip to content

Commit

Permalink
Add basic modelling and reformulation of propositional logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekker1 committed May 7, 2024
1 parent dc776fd commit 747815b
Show file tree
Hide file tree
Showing 9 changed files with 604 additions and 100 deletions.
63 changes: 35 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions crates/fzn-huub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use std::{
use ::tracing::Level;
use clap::Parser;
use flatzinc_serde::{FlatZinc, Literal};
use huub::{SlvTermSignal, SolveResult, Solver, SolverView, Valuation};
use huub::{
FlatZincError, ReformulationError, SlvTermSignal, SolveResult, Solver, SolverView, Valuation,
};
use miette::{IntoDiagnostic, Result, WrapErr};
use tracing::{FmtLitFields, LitName};
use tracing_subscriber::fmt::time::uptime;
Expand Down Expand Up @@ -67,10 +69,20 @@ fn main() -> Result<()> {
})?;

// Convert FlatZinc model to internal Solver representation
let (mut slv, var_map): (Solver, _) = Solver::from_fzn(&fzn)
.into_diagnostic()
.wrap_err("Error while processing FlatZinc")?;
let var_map: UstrMap<SolverView> = var_map.into_iter().collect();
let res = Solver::from_fzn(&fzn);

Check warning on line 72 in crates/fzn-huub/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/fzn-huub/src/main.rs#L72

Added line #L72 was not covered by tests
// Resolve any errors that may have occurred during the conversion
let (mut slv, var_map): (Solver, UstrMap<SolverView>) = match res {

Check warning on line 74 in crates/fzn-huub/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/fzn-huub/src/main.rs#L74

Added line #L74 was not covered by tests
Err(FlatZincError::ReformulationError(ReformulationError::TrivialUnsatisfiable)) => {
println!("=====UNSATISFIABLE=====");
return Ok(());

Check warning on line 77 in crates/fzn-huub/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/fzn-huub/src/main.rs#L76-L77

Added lines #L76 - L77 were not covered by tests
}
Err(err) => {
return Err(err)

Check warning on line 80 in crates/fzn-huub/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/fzn-huub/src/main.rs#L79-L80

Added lines #L79 - L80 were not covered by tests
.into_diagnostic()
.wrap_err("Error while processing FlatZinc")
}
Ok((slv, var_map)) => (slv, var_map.into_iter().collect()),

Check warning on line 84 in crates/fzn-huub/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/fzn-huub/src/main.rs#L84

Added line #L84 was not covered by tests
};

// Create reverse map for solver variables if required
if args.verbose > 0 {
Expand Down Expand Up @@ -114,15 +126,15 @@ fn main() -> Result<()> {
for ident in &fzn.output {
if let Some(arr) = fzn.arrays.get(ident) {
println!(
"{ident} = [{}]",
"{ident} = [{}];",
arr.contents
.iter()
.map(|lit| print_lit(value, &var_map, lit))
.collect::<Vec<_>>()
.join(",")
)
} else {
println!("{ident} = {}", value(var_map[ident]).unwrap())
println!("{ident} = {};", value(var_map[ident]).unwrap())

Check warning on line 137 in crates/fzn-huub/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/fzn-huub/src/main.rs#L137

Added line #L137 was not covered by tests
}
}
println!("----------");
Expand Down
13 changes: 8 additions & 5 deletions crates/huub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub(crate) mod model;
pub(crate) mod propagator;
pub(crate) mod solver;

pub use model::{Constraint, Model, Variable};
pub use model::{
bool::BoolExpr, flatzinc::FlatZincError, int::IntExpr, reformulate::ReformulationError,
Constraint, Model, Variable,
};
pub use pindakaas::solver::{SlvTermSignal, SolveResult};
use pindakaas::Lit as RawLit;
pub use solver::{
Expand All @@ -19,18 +22,18 @@ type Conjunction<L = RawLit> = Vec<L>;

#[cfg(test)]
mod tests {
use crate::{Constraint, Model, SolveResult, Solver, Variable};
use crate::{BoolExpr, Model, SolveResult, Solver, Variable};

#[test]
fn it_works() {
let mut prb = Model::default();
let a = prb.new_bool_var();
let b = prb.new_bool_var();

prb += Constraint::Clause(vec![(!a).into(), (!b).into()]);
prb += Constraint::Clause(vec![a.into(), b.into()]);
prb += BoolExpr::Or(vec![(!a).into(), (!b).into()]);
prb += BoolExpr::Or(vec![a.into(), b.into()]);

let (mut slv, map): (Solver, _) = prb.to_solver();
let (mut slv, map): (Solver, _) = prb.to_solver().unwrap();
let a = map.get(&Variable::Bool(a));
let b = map.get(&Variable::Bool(b));

Expand Down
Loading

0 comments on commit 747815b

Please sign in to comment.