use std::time::Instant;
use sas_plus::Task;
use sas_plus::fdr::SasFile;
use width_search::sas::SasProblem;
use width_search::{SearchResult, bfws, bfws_r, ihiw, iterated_width, siw, siw_plus};
const DEFAULT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/translated/gripper-4/output.sas"
);
fn run(task: &Task, name: &str, search: impl FnOnce() -> SearchResult<usize>) {
let start = Instant::now();
let r = search();
let seconds = start.elapsed().as_secs_f64();
let plan = match &r.plan {
Some(plan) => {
let cost = task
.validate_plan(plan)
.expect("every returned plan is valid");
format!("cost {cost}")
}
None => "no plan".to_string(),
};
println!(
"{name:<24} {plan:>10} {:>9} expanded {seconds:>8.3} s",
r.expanded
);
}
fn main() {
let path = std::env::args()
.nth(1)
.unwrap_or_else(|| DEFAULT.to_string());
let text = std::fs::read_to_string(&path).unwrap_or_else(|e| {
panic!("{path}: {e}\npass the path to a Fast Downward output.sas as the first argument")
});
let file: SasFile = text.parse().unwrap_or_else(|e| panic!("{path}: {e:?}"));
let task = file.to_task();
let problem = SasProblem::new(&task);
println!(
"{path}\n{} variables, {} operators, {} goal facts\n",
task.variables.len(),
task.operators.len(),
task.goal.len()
);
run(&task, "IW(1), IW(2)", || iterated_width(&problem, 2).1);
run(&task, "SIW, max width 2", || siw(&problem, 2));
run(&task, "SIW+, max width 1", || siw_plus(&problem, 1));
run(&task, "IHIW(1, 1)", || ihiw(&problem, 1, 1, 0));
run(&task, "BFWS, k=2", || bfws(&problem, 2));
run(&task, "BFWS with #r, k=2", || bfws_r(&problem, 2));
}