use std::time::Duration; use criterion::{criterion_group, criterion_main, Criterion}; type STATE = (usize, usize); const BLOCKED: [[usize; 8]; 8] = [ [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], ]; fn successors(state: &STATE) -> Vec { let mut states = vec![]; if state.0 > 0 { states.push((state.0 - 1, state.1)); } if state.0 < 7 { states.push((state.0 + 1, state.1)); } if state.1 > 0 { states.push((state.0, state.1 - 1)); } if state.1 < 7 { states.push((state.0, state.1 + 1)); } states .into_iter() .filter(|(x, y)| BLOCKED[*x][*y] == 0) .collect() } fn heuristic(state: &STATE, goal: &STATE) -> usize { ((state.0.abs_diff(goal.0).pow(2) as f64 + state.1.abs_diff(goal.1).pow(2) as f64).sqrt()) .ceil() as usize } fn bfs(init: &STATE, goal: &STATE) -> Option> { searchlib::bfs::solve(init, successors, |s| s == goal) } fn dfs(init: &STATE, goal: &STATE) -> Option> { searchlib::dfs::solve(init, successors, |s| s == goal) } fn gbfs(init: &STATE, goal: &STATE) -> Option> { searchlib::gbfs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn lgbfs(init: &STATE, goal: &STATE) -> Option> { searchlib::lgbfs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn hcs(init: &STATE, goal: &STATE) -> Option> { searchlib::hcs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn ehcs(init: &STATE, goal: &STATE) -> Option> { searchlib::ehcs::solve(init, successors, |s| s == goal, |s| heuristic(s, goal)) } fn criterion_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("maze"); group.warm_up_time(Duration::from_secs(1)); group.measurement_time(Duration::from_secs(1)); group.bench_function("BFS", |b| { b.iter(|| assert!(bfs(&(0, 0), &(7, 7)).is_some())) }); group.bench_function("DFS", |b| { b.iter(|| assert!(dfs(&(0, 0), &(7, 7)).is_some())) }); group.bench_function("GBFS", |b| { b.iter(|| assert!(gbfs(&(0, 0), &(7, 7)).is_some())) }); group.bench_function("LGBFS", |b| { b.iter(|| assert!(lgbfs(&(0, 0), &(7, 7)).is_some())) }); group.bench_function("HCS", |b| { b.iter(|| assert!(hcs(&(0, 0), &(7, 7)).is_none())) }); group.bench_function("EHCS", |b| { b.iter(|| assert!(ehcs(&(0, 0), &(7, 7)).is_some())) }); group.finish(); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);