Skip to content

Commit 1887681

Browse files
committed
update graph heuristic functions
1 parent 5598fa9 commit 1887681

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

Diff for: src/algorithm/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub fn dijkstra<W: GraphWeightTrait, const NODES: usize>(
292292
/// ]);
293293
/// ```
294294
//noinspection DuplicatedCode
295-
pub fn a_star<W: GraphWeightTrait, const NODES: usize, H: Fn(usize, usize) -> W>(
295+
pub fn a_star<W: GraphWeightTrait, const NODES: usize, H: Fn(usize, usize, usize) -> W>(
296296
graph: &Graph<W, NODES>,
297297
source: usize,
298298
destination: usize,
@@ -326,7 +326,7 @@ pub fn a_star<W: GraphWeightTrait, const NODES: usize, H: Fn(usize, usize) -> W>
326326
for adj in 0..NODES {
327327
if let Some(weight) = graph[(current, adj)] {
328328
let new_cost = costs[current].unwrap() + weight;
329-
let new_heu = new_cost + heu(adj, destination);
329+
let new_heu = new_cost + heu(adj, destination, NODES);
330330
match costs[adj] {
331331
None => {
332332
costs[adj] = Some(new_cost);

Diff for: src/algorithm/graph/heuristics.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
use crate::structure::graph::graph_trait::GraphWeightTrait;
22

33
/// Manhattan distance heuristic for A*
4-
pub fn manhattan_heuristic<W: GraphWeightTrait + From<i32>>(node: usize, goal: usize) -> W {
5-
let ((x1, y1), (x2, y2)) = get_points(node, goal);
4+
pub fn manhattan_heuristic<W: GraphWeightTrait + From<i32>>(
5+
node: usize,
6+
goal: usize,
7+
nodes: usize,
8+
) -> W {
9+
let ((x1, y1), (x2, y2)) = get_points(node, goal, nodes);
610
W::from((x1 - x2).abs() + (y1 - y2).abs())
711
}
812

913
/// Euclidean distance heuristic for A*
10-
pub fn euclidean_heuristic<W: GraphWeightTrait + From<i32>>(node: usize, goal: usize) -> W {
11-
let ((x1, y1), (x2, y2)) = get_points(node, goal);
14+
pub fn euclidean_heuristic<W: GraphWeightTrait + From<i32>>(
15+
node: usize,
16+
goal: usize,
17+
nodes: usize,
18+
) -> W {
19+
let ((x1, y1), (x2, y2)) = get_points(node, goal, nodes);
1220
W::from(((x1 as f64 - x2 as f64).powi(2) + (y1 as f64 - y2 as f64).powi(2)).sqrt() as i32)
1321
}
1422

1523
/// Chebyshev distance heuristic for A*
16-
pub fn chebyshev_heuristic<W: GraphWeightTrait + From<i32>>(node: usize, goal: usize) -> W {
17-
let ((x1, y1), (x2, y2)) = get_points(node, goal);
24+
pub fn chebyshev_heuristic<W: GraphWeightTrait + From<i32>>(
25+
node: usize,
26+
goal: usize,
27+
nodes: usize,
28+
) -> W {
29+
let ((x1, y1), (x2, y2)) = get_points(node, goal, nodes);
1830
W::from((x1 - x2).abs().max((y1 - y2).abs()))
1931
}
2032

2133
/// Octile distance heuristic for A*
22-
pub fn octile_heuristic<W: GraphWeightTrait + From<i32>>(node: usize, goal: usize) -> W {
23-
let ((x1, y1), (x2, y2)) = get_points(node, goal);
34+
pub fn octile_heuristic<W: GraphWeightTrait + From<i32>>(
35+
node: usize,
36+
goal: usize,
37+
nodes: usize,
38+
) -> W {
39+
let ((x1, y1), (x2, y2)) = get_points(node, goal, nodes);
2440
let (dx, dy) = ((x1 - x2).abs(), (y1 - y2).abs());
2541
W::from((dx + dy) + ((2 - 2_i32.isqrt()) * dx.min(dy)))
2642
}
2743

2844
/// Dijkstra's Heuristic
29-
pub fn dijkstra_heuristic<W: GraphWeightTrait>(_: usize, _: usize) -> W {
45+
pub fn dijkstra_heuristic<W: GraphWeightTrait>(_: usize, _: usize, _: usize) -> W {
3046
W::zero()
3147
}
3248

33-
fn get_points(node: usize, goal: usize) -> ((i32, i32), (i32, i32)) {
49+
fn get_points(node: usize, goal: usize, nodes: usize) -> ((i32, i32), (i32, i32)) {
3450
(
35-
(node as i32 % 3, node as i32 / 3),
36-
(goal as i32 % 3, goal as i32 / 3),
51+
((node % nodes) as i32, (node / nodes) as i32),
52+
((goal % nodes) as i32, (goal / nodes) as i32),
3753
)
3854
}

0 commit comments

Comments
 (0)