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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
//! Compute a shortest path (or all shorted paths) using the [A* search //! algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm). use indexmap::map::Entry::{Occupied, Vacant}; use indexmap::IndexMap; use num_traits::Zero; use std::cmp::Ordering; use std::collections::{BinaryHeap, HashSet}; use std::hash::Hash; use std::usize; use super::reverse_path; /// Compute a shortest path using the [A* search /// algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm). /// /// The shortest path starting from `start` up to a node for which `success` returns `true` is /// computed and returned along with its total cost, in a `Some`. If no path can be found, `None` /// is returned instead. /// /// - `start` is the starting node. /// - `successors` returns a list of successors for a given node, along with the cost for moving /// from the node to the successor. /// - `heuristic` returns an approximation of the cost from a given node to the goal. The /// approximation must not be greater than the real cost, or a wrong shortest path may be returned. /// - `success` checks whether the goal has been reached. It is not a node as some problems require /// a dynamic solution instead of a fixed node. /// /// A node will never be included twice in the path as determined by the `Eq` relationship. /// /// The returned path comprises both the start and end node. /// /// # Example /// /// We will search the shortest path on a chess board to go from (1, 1) to (4, 6) doing only knight /// moves. /// /// The first version uses an explicit type `Pos` on which the required traits are derived. /// /// ``` /// use pathfinding::prelude::{absdiff, astar}; /// /// #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] /// struct Pos(i32, i32); /// /// impl Pos { /// fn distance(&self, other: &Pos) -> u32 { /// (absdiff(self.0, other.0) + absdiff(self.1, other.1)) as u32 /// } /// /// fn successors(&self) -> Vec<(Pos, u32)> { /// let &Pos(x, y) = self; /// vec![Pos(x+1,y+2), Pos(x+1,y-2), Pos(x-1,y+2), Pos(x-1,y-2), /// Pos(x+2,y+1), Pos(x+2,y-1), Pos(x-2,y+1), Pos(x-2,y-1)] /// .into_iter().map(|p| (p, 1)).collect() /// } /// } /// /// static GOAL: Pos = Pos(4, 6); /// let result = astar(&Pos(1, 1), |p| p.successors(), |p| p.distance(&GOAL) / 3, /// |p| *p == GOAL); /// assert_eq!(result.expect("no path found").1, 4); /// ``` /// /// The second version does not declare a `Pos` type, makes use of more closures, /// and is thus shorter. /// /// ``` /// use pathfinding::prelude::{absdiff, astar}; /// /// static GOAL: (i32, i32) = (4, 6); /// let result = astar(&(1, 1), /// |&(x, y)| vec![(x+1,y+2), (x+1,y-2), (x-1,y+2), (x-1,y-2), /// (x+2,y+1), (x+2,y-1), (x-2,y+1), (x-2,y-1)] /// .into_iter().map(|p| (p, 1)), /// |&(x, y)| absdiff(x, GOAL.0) + absdiff(y, GOAL.1), /// |&p| p == GOAL); /// assert_eq!(result.expect("no path found").1, 4); /// ``` pub fn astar<N, C, FN, IN, FH, FS>( start: &N, mut successors: FN, mut heuristic: FH, mut success: FS, ) -> Option<(Vec<N>, C)> where N: Eq + Hash + Clone, C: Zero + Ord + Copy, FN: FnMut(&N) -> IN, IN: IntoIterator<Item = (N, C)>, FH: FnMut(&N) -> C, FS: FnMut(&N) -> bool, { let mut to_see = BinaryHeap::new(); to_see.push(SmallestCostHolder { estimated_cost: Zero::zero(), cost: Zero::zero(), index: 0, }); let mut parents: IndexMap<N, (usize, C)> = IndexMap::new(); parents.insert(start.clone(), (usize::max_value(), Zero::zero())); while let Some(SmallestCostHolder { cost, index, .. }) = to_see.pop() { let successors = { let (node, &(_, c)) = parents.get_index(index).unwrap(); if success(node) { let path = reverse_path(&parents, |&(p, _)| p, index); return Some((path, cost)); } // We may have inserted a node several time into the binary heap if we found // a better way to access it. Ensure that we are currently dealing with the // best path and discard the others. if cost > c { continue; } successors(node) }; for (successor, move_cost) in successors { let new_cost = cost + move_cost; let h; // heuristic(&successor) let n; // index for successor match parents.entry(successor) { Vacant(e) => { h = heuristic(e.key()); n = e.index(); e.insert((index, new_cost)); } Occupied(mut e) => { if e.get().1 > new_cost { h = heuristic(e.key()); n = e.index(); e.insert((index, new_cost)); } else { continue; } } } to_see.push(SmallestCostHolder { estimated_cost: new_cost + h, cost: new_cost, index: n, }); } } None } /// Compute all shortest paths using the [A* search /// algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm). Whereas `astar` /// (non-deterministic-ally) returns a single shortest path, `astar_bag` returns all shortest paths /// (in a non-deterministic order). /// /// The shortest paths starting from `start` up to a node for which `success` returns `true` are /// computed and returned in an iterator along with the cost (which, by definition, is the same for /// each shortest path), wrapped in a `Some`. If no paths are found, `None` is returned. /// /// - `start` is the starting node. /// - `successors` returns a list of successors for a given node, along with the cost for moving /// from the node to the successor. /// - `heuristic` returns an approximation of the cost from a given node to the goal. The /// approximation must not be greater than the real cost, or a wrong shortest path may be returned. /// - `success` checks whether the goal has been reached. It is not a node as some problems require /// a dynamic solution instead of a fixed node. /// /// A node will never be included twice in the path as determined by the `Eq` relationship. /// /// Each path comprises both the start and an end node. Note that while every path shares the same /// start node, different paths may have different end nodes. pub fn astar_bag<N, C, FN, IN, FH, FS>( start: &N, mut successors: FN, mut heuristic: FH, mut success: FS, ) -> Option<(AstarSolution<N>, C)> where N: Eq + Hash + Clone, C: Zero + Ord + Copy, FN: FnMut(&N) -> IN, IN: IntoIterator<Item = (N, C)>, FH: FnMut(&N) -> C, FS: FnMut(&N) -> bool, { let mut to_see = BinaryHeap::new(); let mut min_cost = None; let mut sinks = HashSet::new(); to_see.push(SmallestCostHolder { estimated_cost: Zero::zero(), cost: Zero::zero(), index: 0, }); let mut parents: IndexMap<N, (HashSet<usize>, C)> = IndexMap::new(); parents.insert(start.clone(), (HashSet::new(), Zero::zero())); while let Some(SmallestCostHolder { cost, index, estimated_cost, .. }) = to_see.pop() { if let Some(min_cost) = min_cost { if estimated_cost > min_cost { break; } } let successors = { let (node, &(_, c)) = parents.get_index(index).unwrap(); if success(node) { min_cost = Some(cost); sinks.insert(index); } // We may have inserted a node several time into the binary heap if we found // a better way to access it. Ensure that we are currently dealing with the // best path and discard the others. if cost > c { continue; } successors(node) }; for (successor, move_cost) in successors { let new_cost = cost + move_cost; let h; // heuristic(&successor) let n; // index for successor match parents.entry(successor) { Vacant(e) => { h = heuristic(e.key()); n = e.index(); let mut p = HashSet::new(); p.insert(index); e.insert((p, new_cost)); } Occupied(mut e) => { if e.get().1 > new_cost { h = heuristic(e.key()); n = e.index(); let s = e.get_mut(); s.0.clear(); s.0.insert(index); s.1 = new_cost; } else { if e.get().1 == new_cost { // New parent with an identical cost, this is not // considered as an insertion. e.get_mut().0.insert(index); } continue; } } } to_see.push(SmallestCostHolder { estimated_cost: new_cost + h, cost: new_cost, index: n, }); } } min_cost.map(|cost| { let parents = parents .into_iter() .map(|(k, (ps, _))| (k, ps.into_iter().collect())) .collect(); ( AstarSolution { sinks: sinks.into_iter().collect(), parents, current: vec![], terminated: false, }, cost, ) }) } /// Compute all shortest paths using the [A* search /// algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm). Whereas `astar` /// (non-deterministic-ally) returns a single shortest path, `astar_bag` returns all shortest paths /// (in a non-deterministic order). /// /// This is a utility function which collects the results of the `astar_bag` function into a /// vector. Most of the time, it is more appropriate to use `astar_bag` directly. /// /// ### Warning /// /// The number of results with the same value might be very large in some graphs. Use with caution. pub fn astar_bag_collect<N, C, FN, IN, FH, FS>( start: &N, successors: FN, heuristic: FH, success: FS, ) -> Option<(Vec<Vec<N>>, C)> where N: Eq + Hash + Clone, C: Zero + Ord + Copy, FN: FnMut(&N) -> IN, IN: IntoIterator<Item = (N, C)>, FH: FnMut(&N) -> C, FS: FnMut(&N) -> bool, { astar_bag(start, successors, heuristic, success) .map(|(solutions, cost)| (solutions.collect(), cost)) } struct SmallestCostHolder<K> { estimated_cost: K, cost: K, index: usize, } impl<K: PartialEq> PartialEq for SmallestCostHolder<K> { fn eq(&self, other: &Self) -> bool { self.estimated_cost.eq(&other.estimated_cost) && self.cost.eq(&other.cost) } } impl<K: PartialEq> Eq for SmallestCostHolder<K> {} impl<K: Ord> PartialOrd for SmallestCostHolder<K> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) } } impl<K: Ord> Ord for SmallestCostHolder<K> { fn cmp(&self, other: &Self) -> Ordering { match other.estimated_cost.cmp(&self.estimated_cost) { Ordering::Equal => self.cost.cmp(&other.cost), s => s, } } } /// Iterator structure created by the `astar_bag` function. #[derive(Clone)] pub struct AstarSolution<N> { sinks: Vec<usize>, parents: Vec<(N, Vec<usize>)>, current: Vec<Vec<usize>>, terminated: bool, } impl<N: Clone + Eq + Hash> AstarSolution<N> { fn complete(&mut self) { loop { let ps = match self.current.last() { None => self.sinks.clone(), Some(last) => { let &top = last.last().unwrap(); self.parents(top).clone() } }; if ps.is_empty() { break; } self.current.push(ps); } } fn next_vec(&mut self) { while self.current.last().map(Vec::len) == Some(1) { self.current.pop(); } self.current.last_mut().map(Vec::pop); } fn node(&self, i: usize) -> &N { &self.parents[i].0 } fn parents(&self, i: usize) -> &Vec<usize> { &self.parents[i].1 } } impl<N: Clone + Eq + Hash> Iterator for AstarSolution<N> { type Item = Vec<N>; fn next(&mut self) -> Option<Self::Item> { if self.terminated { return None; } self.complete(); let path = self .current .iter() .rev() .map(|v| v.last().cloned().unwrap()) .map(|i| self.node(i).clone()) .collect::<Vec<_>>(); self.next_vec(); self.terminated = self.current.is_empty(); Some(path) } }