mod context;
mod node;
mod search;
pub(crate) mod state;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use crate::{Actions, Map, SearchError};
use context::Context;
#[derive(Clone, Copy, Eq, PartialEq, Debug, Default)]
pub enum Strategy {
#[default]
Quick,
PushOptimal,
MoveOptimal,
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum Algorithm {
AStar,
IDAStar,
Bfs,
}
#[derive(Clone, Debug)]
pub struct Solver {
ctx: Context,
stop_flag: Arc<AtomicBool>,
}
impl Solver {
pub fn new(map: Map, strategy: Strategy) -> Self {
Self {
ctx: Context::new(map, strategy),
stop_flag: Arc::new(AtomicBool::new(false)),
}
}
pub fn search(&self, algorithm: Algorithm) -> Result<Actions, SearchError> {
self.stop_flag.store(false, Ordering::Relaxed);
match algorithm {
Algorithm::AStar => search::a_star_search(&self.ctx, &self.stop_flag),
Algorithm::IDAStar => search::ida_star_search(&self.ctx, &self.stop_flag),
Algorithm::Bfs => search::bfs_search(&self.ctx, &self.stop_flag),
}
}
pub fn request_stop(&self) {
self.stop_flag.store(true, Ordering::Relaxed);
}
pub fn context(&self) -> &Context {
&self.ctx
}
}