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
|
use std::{
cmp::{self},
collections::BinaryHeap,
fmt::{self, Debug},
hash::{BuildHasherDefault, Hash},
time::{Duration, Instant},
};
use indexmap::IndexMap;
use num_format::ToFormattedString;
use rustc_hash::FxHasher;
use tracing::{debug, trace, warn};
pub struct Path<P, M>
where
P: Eq + Hash + Copy + Debug,
{
pub movements: Vec<Movement<P, M>>,
pub is_partial: bool,
}
// used for better results when timing out
// see https://github.com/cabaletta/baritone/blob/1.19.4/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java#L68
const COEFFICIENTS: [f32; 7] = [1.5, 2., 2.5, 3., 4., 5., 10.];
const MIN_IMPROVEMENT: f32 = 0.01;
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
// Sources:
// - https://en.wikipedia.org/wiki/A*_search_algorithm
// - https://github.com/evenfurther/pathfinding/blob/main/src/directed/astar.rs
// - https://github.com/cabaletta/baritone/blob/1.19.4/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java
pub fn a_star<P, M, HeuristicFn, SuccessorsFn, SuccessFn>(
start: P,
heuristic: HeuristicFn,
mut successors: SuccessorsFn,
success: SuccessFn,
min_timeout: PathfinderTimeout,
max_timeout: PathfinderTimeout,
) -> Path<P, M>
where
P: Eq + Hash + Copy + Debug,
HeuristicFn: Fn(P) -> f32,
SuccessorsFn: FnMut(P) -> Vec<Edge<P, M>>,
SuccessFn: Fn(P) -> bool,
{
let start_time = Instant::now();
let mut open_set = BinaryHeap::<WeightedNode>::new();
open_set.push(WeightedNode {
g_score: 0.,
f_score: 0.,
index: 0,
});
let mut nodes: FxIndexMap<P, Node> = IndexMap::default();
nodes.insert(
start,
Node {
came_from: usize::MAX,
g_score: 0.,
},
);
let mut best_paths: [usize; 7] = [0; 7];
let mut best_path_scores: [f32; 7] = [heuristic(start); 7];
let mut num_nodes = 0_usize;
let mut num_movements = 0;
while let Some(WeightedNode { index, g_score, .. }) = open_set.pop() {
num_nodes += 1;
let (&node, node_data) = nodes.get_index(index).unwrap();
if success(node) {
debug!("Nodes considered: {num_nodes}");
return Path {
movements: reconstruct_path(nodes, index, successors),
is_partial: false,
};
}
if g_score > node_data.g_score {
continue;
}
for neighbor in successors(node) {
let tentative_g_score = g_score + neighbor.cost;
// let neighbor_heuristic = heuristic(neighbor.movement.target);
let neighbor_heuristic;
let neighbor_index;
num_movements += 1;
match nodes.entry(neighbor.movement.target) {
indexmap::map::Entry::Occupied(mut e) => {
if e.get().g_score > tentative_g_score {
neighbor_heuristic = heuristic(*e.key());
neighbor_index = e.index();
e.insert(Node {
came_from: index,
g_score: tentative_g_score,
});
} else {
continue;
}
}
indexmap::map::Entry::Vacant(e) => {
neighbor_heuristic = heuristic(*e.key());
neighbor_index = e.index();
e.insert(Node {
came_from: index,
g_score: tentative_g_score,
});
}
}
open_set.push(WeightedNode {
index: neighbor_index,
g_score: tentative_g_score,
f_score: tentative_g_score + neighbor_heuristic,
});
for (coefficient_i, &coefficient) in COEFFICIENTS.iter().enumerate() {
let node_score = neighbor_heuristic + tentative_g_score / coefficient;
if best_path_scores[coefficient_i] - node_score > MIN_IMPROVEMENT {
best_paths[coefficient_i] = neighbor_index;
best_path_scores[coefficient_i] = node_score;
}
}
}
// check for timeout every ~10ms
if num_nodes.is_multiple_of(10_000) {
let min_timeout_reached = match min_timeout {
PathfinderTimeout::Time(max_duration) => start_time.elapsed() >= max_duration,
PathfinderTimeout::Nodes(max_nodes) => num_nodes >= max_nodes,
};
if min_timeout_reached {
// means we have a non-empty path
if best_paths[6] != 0 {
break;
}
if min_timeout_reached {
let max_timeout_reached = match max_timeout {
PathfinderTimeout::Time(max_duration) => {
start_time.elapsed() >= max_duration
}
PathfinderTimeout::Nodes(max_nodes) => num_nodes >= max_nodes,
};
if max_timeout_reached {
// timeout, we're gonna be returning an empty path :(
trace!("A* couldn't find a path in time, returning best path");
break;
}
}
}
}
}
let best_path = determine_best_path(best_paths, 0);
let elapsed_seconds = start_time.elapsed().as_secs_f64();
let nodes_per_second = (num_nodes as f64 / elapsed_seconds) as u64;
let num_movements_per_second = (num_movements as f64 / elapsed_seconds) as u64;
debug!(
"A* ran at {} nodes per second and {} movements per second",
nodes_per_second.to_formatted_string(&num_format::Locale::en),
num_movements_per_second.to_formatted_string(&num_format::Locale::en),
);
Path {
movements: reconstruct_path(nodes, best_path, successors),
is_partial: true,
}
}
fn determine_best_path(best_paths: [usize; 7], start: usize) -> usize {
// this basically makes sure we don't create a path that's really short
for node in best_paths {
if node != start {
return node;
}
}
warn!("No best node found, returning first node");
best_paths[0]
}
fn reconstruct_path<P, M, SuccessorsFn>(
nodes: FxIndexMap<P, Node>,
mut current_index: usize,
mut successors: SuccessorsFn,
) -> Vec<Movement<P, M>>
where
P: Eq + Hash + Copy + Debug,
SuccessorsFn: FnMut(P) -> Vec<Edge<P, M>>,
{
let mut path = Vec::new();
while let Some((&node_position, node)) = nodes.get_index(current_index) {
if node.came_from == usize::MAX {
break;
}
let came_from_position = *nodes.get_index(node.came_from).unwrap().0;
// find the movement data for this successor, we have to do this again because
// we don't include the movement data in the Node (as an optimization)
let mut best_successor = None;
let mut best_successor_cost = f32::INFINITY;
for successor in successors(came_from_position) {
if successor.movement.target == node_position && successor.cost < best_successor_cost {
best_successor_cost = successor.cost;
best_successor = Some(successor);
}
}
let Some(found_successor) = best_successor else {
warn!(
"a successor stopped being possible while reconstructing the path, returning empty path"
);
return vec![];
};
path.push(Movement {
target: node_position,
data: found_successor.movement.data,
});
current_index = node.came_from;
}
path.reverse();
path
}
pub struct Node {
pub came_from: usize,
pub g_score: f32,
}
#[derive(Clone, Debug)]
pub struct Edge<P: Hash + Copy, M> {
pub movement: Movement<P, M>,
pub cost: f32,
}
pub struct Movement<P: Hash + Copy, M> {
pub target: P,
pub data: M,
}
impl<P: Hash + Copy + Debug, M: Debug> Debug for Movement<P, M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Movement")
.field("target", &self.target)
.field("data", &self.data)
.finish()
}
}
impl<P: Hash + Copy + Clone, M: Clone> Clone for Movement<P, M> {
fn clone(&self) -> Self {
Self {
target: self.target,
data: self.data.clone(),
}
}
}
#[derive(PartialEq)]
#[repr(C)]
pub struct WeightedNode {
/// Sum of the g_score and heuristic
pub f_score: f32,
/// The actual cost to get to this node
pub g_score: f32,
pub index: usize,
}
impl Ord for WeightedNode {
#[inline]
fn cmp(&self, other: &Self) -> cmp::Ordering {
// intentionally inverted to make the BinaryHeap a min-heap
match other.f_score.total_cmp(&self.f_score) {
cmp::Ordering::Equal => self.g_score.total_cmp(&other.g_score),
s => s,
}
}
}
impl Eq for WeightedNode {}
impl PartialOrd for WeightedNode {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PathfinderTimeout {
/// Time out after a certain duration has passed. This is a good default so
/// you don't waste too much time calculating a path if you're on a slow
/// computer.
Time(Duration),
/// Time out after this many nodes have been considered.
///
/// This is useful as an alternative to a time limit if you're doing
/// something like running tests where you want consistent results.
Nodes(usize),
}
impl Default for PathfinderTimeout {
fn default() -> Self {
Self::Time(Duration::from_secs(1))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn weighted_node(f: f32, g: f32) -> WeightedNode {
WeightedNode {
f_score: f,
g_score: g,
index: 0,
}
}
#[test]
fn test_weighted_node_eq() {
let a = weighted_node(0., 0.);
let b = weighted_node(0., 0.);
assert!(a == b);
}
#[test]
fn test_weighted_node_le() {
let a = weighted_node(1., 0.);
let b = weighted_node(0., 0.);
assert_eq!(a.cmp(&b), cmp::Ordering::Less);
assert!(a.le(&b));
}
#[test]
fn test_weighted_node_le_g() {
let a = weighted_node(0., 1.);
let b = weighted_node(0., 0.);
assert_eq!(a.cmp(&b), cmp::Ordering::Greater);
assert!(!a.le(&b));
}
}
|