From cd1eadf13217c4cf2eafadd63dfbf6f84b6fb954 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Fri, 26 Jul 2024 10:05:36 +0200 Subject: Fix nearest open node search --- astar.rs | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/astar.rs b/astar.rs index bc48d39..3bbe1a5 100755 --- a/astar.rs +++ b/astar.rs @@ -1,23 +1,32 @@ -# ![allow(unused_attributes)] -# ![allow()] /* +#[allow(non_camel_case_types)] /* +#/ ================================================================ +#/ +#/ astar.rs +#/ +#/ Iterative general implementation for +#/ the A* graph search algorithm +#/ +#/ ---------------------------------------------------------------- +#/ +#/ To-Do list +#/ +#/ - Decouple `neighbor` proc +#/ +#/ ---------------------------------------------------------------- +#/ +#/ (C) 2024 Mitya Selivanov , MIT License +#/ +#/ ================================================================ +#/ +#/ Self-compilation shell script +#/ SRC=${0##*./} BIN=${SRC%.*} rustc --test -o $BIN.tmp $SRC && ./$BIN.tmp $@ && rm $BIN.tmp exit $? # */ - -// ================================================================ -// -// Iterative general implementation for -// the A* graph search algorithm -// -// ================================================================ -// -// (C) 2024 Mitya Selivanov , MIT License -// // ================================================================ -#[allow(non_camel_case_types)] -pub mod astar { +mod astar_internal { use std::{ops::Add, fmt::Debug}; #[derive(Debug, Clone, PartialEq)] @@ -165,7 +174,10 @@ pub mod astar { let mut index_in_open : usize = 0; for index in 1..state.num_open { - if open[index].estimate < open[index_in_open].estimate { + let node = open[index].clone(); + let nearest = open[index_in_open].clone(); + if node .exact_distance + node .estimate < + nearest.exact_distance + nearest.estimate { index_in_open = index; } } @@ -305,6 +317,8 @@ pub mod astar { } } +pub use astar_internal::*; + // ================================================================ // // Testing @@ -313,7 +327,7 @@ pub mod astar { #[cfg(test)] mod tests { - use super::astar::*; + use super::*; #[test] fn path_exists() { @@ -574,9 +588,9 @@ mod tests { #[test] fn cyclic() { let graph : Vec<((i64, i64), i64)> = vec![ - ((0, 1), 5), + ((0, 1), 10), ((0, 2), 3), - ((1, 3), 4), + ((1, 3), 20), ((2, 4), 1), ((3, 5), 1), ((4, 6), 10), @@ -629,7 +643,7 @@ mod tests { let n = path(&closed, &state, &mut v); v.resize(n, 0); - assert_eq!(steps, 5); + assert_eq!(steps, 6); assert_eq!(v.len(), 6); assert_eq!(v[0], 0); assert_eq!(v[1], 2); -- cgit v1.2.3