1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
//! # space-search
//!
//! A library providing basic utilities for performing generic depth-first, breadth-first, and heuristic-guided search space exploration algorithms.
//!
//! Implement [`Searchable`] to perform breadth-first or depth-first searching, and implement [`ScoredSearchable`] to perform heuristically guided search space exploration. Pass them to the [`Searcher`] and [`ScoredSearcher`] structs respectively to create iterators that will search the space for a solution.
//!
//! Implement `Eq + Hash + Clone` for your search space state type to benefit from prior explored state checking optimization; if youre unable to, then use the [`ScoredSearcher`] or [`ScoredUnhashableSearcher`] iterators, which do not require these additional bounds, but will likely explore the space much less efficiently.
//!
//! When implementing [`ScoredSearcher`], make sure that higher scoring states are closer to a solution.
//!
//! ```
//! use space_search::*;
//! use std::{vec, hash::Hash};
//!
//! #[derive(Clone, Debug, PartialEq, Eq, Hash)]
//! struct Pos(i32, i32);
//!
//! impl Searchable for Pos {
//! type NextMoveIterator = vec::IntoIter<Pos>;
//!
//! fn next_states(&self) -> Self::NextMoveIterator {
//! let &Pos(x, y) = self;
//! vec![
//! Pos(x - 1, y),
//! Pos(x, y - 1),
//! Pos(x + 1, y),
//! Pos(x, y + 1),
//! ].into_iter()
//! }
//!
//! fn is_solution(&self) -> bool {
//! let &Pos(x, y) = self;
//! x == 5 && y == 5
//! }
//! }
//!
//! let mut searcher = Searcher::new(Pos(0, 0));
//! assert_eq!(searcher.next(), Some(Pos(5, 5)));
//! ```
use std::{
collections::{BinaryHeap, HashSet, VecDeque},
hash::Hash,
};
/// Basic trait for depth-first and breadth-first search space exploration.
pub trait Searchable {
type NextMoveIterator: Iterator<Item = Self>;
/// Yield all adjacent explorable states reachable from this state.
fn next_states(&self) -> Self::NextMoveIterator;
/// Return `true` if this state is a solution state.
fn is_solution(&self) -> bool;
}
/// Optimized breadth-first / depth-first state space exploration iterator.
pub struct Searcher<S> {
explored: HashSet<S>,
fringe: VecDeque<S>,
/// Toggle depth-first searching on. By default, breadth-first search is used.
/// Enable this flag to perform depth-first search instead.
pub depth_first: bool,
}
impl<S> Searcher<S> {
/// Create a new search iterator from an initial state.
pub fn new(initial_state: S) -> Self {
Self {
explored: HashSet::new(),
fringe: VecDeque::from([initial_state]),
depth_first: false,
}
}
/// Create a new search iterator from a default initial state.
pub fn new_with_default() -> Self
where
S: Default,
{
Self::new(Default::default())
}
}
impl<S> Iterator for Searcher<S>
where
S: Searchable + Clone + Hash + Eq,
{
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
loop {
let current_state = self.fringe.pop_back()?;
if current_state.is_solution() {
return Some(current_state);
}
for state in current_state.next_states() {
if !self.explored.contains(&state) {
self.explored.insert(state.clone());
if self.depth_first {
self.fringe.push_back(state);
} else {
self.fringe.push_front(state);
}
}
}
}
}
}
/// Unoptimized breadth-first / depth-first search space exploration iterator.
///
/// Use this instead of [`Searcher`] if implementing `Clone + Eq + Hash` for your [`Searchable`] type
/// is infeasible or impractical for whatever reason, or if you're running into memory
/// limitations from the optimized implementation.
pub struct UnhashableSearcher<S> {
fringe: VecDeque<S>,
/// Toggle depth-first searching on. By default, breadth-first search is used.
/// Enable this flag to perform depth-first search instead.
pub depth_first: bool,
}
impl<S> UnhashableSearcher<S> {
/// Create a new unoptimized iterator from an initial state.
pub fn new(initial_state: S) -> Self {
Self {
fringe: VecDeque::from([initial_state]),
depth_first: false,
}
}
/// Create a new unoptimized iterator from a default state.
pub fn new_with_default() -> Self
where
S: Default,
{
Self::new(Default::default())
}
}
impl<S> Iterator for UnhashableSearcher<S>
where
S: Searchable,
{
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
loop {
let current_state = self.fringe.pop_back()?;
if current_state.is_solution() {
return Some(current_state);
}
for state in current_state.next_states() {
if self.depth_first {
self.fringe.push_back(state);
} else {
self.fringe.push_front(state);
}
}
}
}
}
/// Trait for search space exploration guided by a heuristic.
///
/// New states are explored in the order of
/// highest-scoring first, biasing the search exploration in the direction of a solution. Ensure the scores
/// returned by `score(self)` are increasing with the proximity to a solution.
pub trait ScoredSearchable: Searchable {
type Score: Ord;
/// Score function used for heuristic exploration. New states are explored in the order of
/// highest-scoring first; ensure the scores
/// returned by this function increase with the proximity to a solution.
fn score(&self) -> Self::Score;
}
struct OrderedSearchable<T, C> {
state: T,
score: C,
}
impl<T, C> PartialEq for OrderedSearchable<T, C>
where
C: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.score == other.score
}
}
impl<T, C> Eq for OrderedSearchable<T, C> where C: Eq {}
impl<T, C> PartialOrd for OrderedSearchable<T, C>
where
C: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.score.partial_cmp(&other.score)
}
}
impl<T, C> Ord for OrderedSearchable<T, C>
where
C: Ord,
{
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.score.cmp(&other.score)
}
}
/// Optimized heuristic-guided search space exploration iterator.
pub struct ScoredSearcher<S: ScoredSearchable> {
explored: HashSet<S>,
fringe: BinaryHeap<OrderedSearchable<S, S::Score>>,
}
impl<S: ScoredSearchable> ScoredSearcher<S> {
/// Create a new guided search iterator from an initial state.
pub fn new(initial_state: S) -> Self {
let score = initial_state.score();
Self {
explored: HashSet::new(),
fringe: BinaryHeap::from([OrderedSearchable {
state: initial_state,
score,
}]),
}
}
/// Create a new guided search iterator from a default state.
pub fn new_with_default() -> Self
where
S: Default,
{
Self::new(Default::default())
}
}
impl<S: ScoredSearchable> Iterator for ScoredSearcher<S>
where
S: Clone + Hash + Eq,
{
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
loop {
let current_state = self.fringe.pop()?.state;
if current_state.is_solution() {
return Some(current_state);
}
for state in current_state.next_states() {
if !self.explored.contains(&state) {
self.explored.insert(state.clone());
let score = state.score();
self.fringe.push(OrderedSearchable { state, score });
}
}
}
}
}
/// Unoptimized heuristic-guided search space exploration iterator.
///
/// Use this instead of [`ScoredSearcher`] if implementing `Clone + Eq + Hash` for your [`ScoredSearchable`] type
/// is infeasible or impractical for whatever reason, or if you're running into memory
/// limitations from the optimized implementation.
pub struct ScoredUnhashableSearcher<S: ScoredSearchable> {
fringe: BinaryHeap<OrderedSearchable<S, S::Score>>,
}
impl<S: ScoredSearchable> ScoredUnhashableSearcher<S> {
/// Create a new unoptimizd guided search iterator from an initial state.
pub fn new(initial_state: S) -> Self {
let score = initial_state.score();
Self {
fringe: BinaryHeap::from([OrderedSearchable {
state: initial_state,
score,
}]),
}
}
/// Create a new unoptimizd guided search iterator from a default state.
pub fn new_with_default() -> Self
where
S: Default,
{
Self::new(Default::default())
}
}
impl<S: ScoredSearchable> Iterator for ScoredUnhashableSearcher<S> {
type Item = S;
fn next(&mut self) -> Option<Self::Item> {
loop {
let current_state = self.fringe.pop()?.state;
if current_state.is_solution() {
return Some(current_state);
}
for state in current_state.next_states() {
let score = state.score();
self.fringe.push(OrderedSearchable { state, score });
}
}
}
}