astar: Probe neighbors before adding tem to queue

This commit is contained in:
Mikolaj Wielgus 2023-10-12 00:08:54 +00:00
parent af61a6b120
commit 4e8e9298e6
2 changed files with 31 additions and 20 deletions

View File

@ -101,7 +101,7 @@ where
G::NodeId: Eq + Hash, G::NodeId: Eq + Hash,
{ {
fn is_goal(&mut self, node: G::NodeId, tracker: &PathTracker<G>) -> bool; fn is_goal(&mut self, node: G::NodeId, tracker: &PathTracker<G>) -> bool;
fn edge_cost(&mut self, edge: G::EdgeRef) -> K; fn edge_cost(&mut self, edge: G::EdgeRef) -> Option<K>;
fn estimate_cost(&mut self, node: G::NodeId) -> K; fn estimate_cost(&mut self, node: G::NodeId) -> K;
} }
@ -150,26 +150,28 @@ where
} }
for edge in graph.edges(node) { for edge in graph.edges(node) {
let next = edge.target(); if let Some(edge_cost) = strategy.edge_cost(edge) {
let next_score = node_score + strategy.edge_cost(edge); let next = edge.target();
let next_score = node_score + edge_cost;
match scores.entry(next) { match scores.entry(next) {
Occupied(mut entry) => { Occupied(mut entry) => {
// No need to add neighbors that we have already reached through a // No need to add neighbors that we have already reached through a
// shorter path than now. // shorter path than now.
if *entry.get() <= next_score { if *entry.get() <= next_score {
continue; continue;
}
entry.insert(next_score);
}
Vacant(entry) => {
entry.insert(next_score);
} }
entry.insert(next_score);
} }
Vacant(entry) => {
entry.insert(next_score);
}
}
path_tracker.set_predecessor(next, node); path_tracker.set_predecessor(next, node);
let next_estimate_score = next_score + strategy.estimate_cost(next); let next_estimate_score = next_score + strategy.estimate_cost(next);
visit_next.push(MinScored(next_estimate_score, next)); visit_next.push(MinScored(next_estimate_score, next));
}
} }
} }

View File

@ -50,9 +50,18 @@ impl<'a, RO: RouterObserver> AstarStrategy<&Mesh, u64> for RouterAstarStrategy<'
self.tracer.finish(&mut self.trace, self.to, 5.0).is_ok() self.tracer.finish(&mut self.trace, self.to, 5.0).is_ok()
} }
fn edge_cost(&mut self, edge: MeshEdgeReference) -> u64 { fn edge_cost(&mut self, edge: MeshEdgeReference) -> Option<u64> {
self.observer.on_probe(&self.tracer, edge); if self
1 .tracer
.step(&mut self.trace, edge.target(), 5.0)
.is_ok()
{
self.observer.on_probe(&self.tracer, edge);
self.tracer.undo_step(&mut self.trace);
Some(1)
} else {
None
}
} }
fn estimate_cost(&mut self, vertex: VertexIndex) -> u64 { fn estimate_cost(&mut self, vertex: VertexIndex) -> u64 {