use super::{AitStar, Kind, VertexEntry};
use crate::{NearestNeighbors, Sampler, StateSpace, ValidityChecker};
impl<Sp, Sa, C, N> AitStar<Sp, Sa, C, N>
where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
C: ValidityChecker<Sp>,
N: NearestNeighbors<Sp> + Clone,
{
pub(super) fn restart_reverse_search(&mut self) {
self.reverse_queue.clear();
for node in &mut self.nodes {
node.h_con = f64::INFINITY;
node.h_exp = f64::INFINITY;
node.reverse_key = None;
}
for x in 0..self.nodes.len() {
if self.is_reverse_root(x) {
self.nodes[x].h_con = 0.0;
self.queue_state(x);
}
}
}
fn is_reverse_root(&self, x: usize) -> bool {
self.nodes[x].kind != Kind::Pruned && self.nodes[x].in_goal()
}
pub(super) fn is_consistent(&self, x: usize) -> bool {
self.nodes[x].h_con.total_cmp(&self.nodes[x].h_exp).is_eq()
}
fn reverse_key(&self, x: usize) -> [f64; 2] {
let node = &self.nodes[x];
let h = node.h_con.min(node.h_exp);
[h + node.g_hat, h]
}
fn queue_state(&mut self, x: usize) {
let key = self.reverse_key(x);
if self.nodes[x].reverse_key == Some(key) {
return;
}
self.nodes[x].reverse_key = Some(key);
self.reverse_queue.push(VertexEntry {
key: key[0],
cost: key[1],
vertex: x,
});
}
pub(super) fn peek_state(&mut self) -> Option<VertexEntry> {
while let Some(&entry) = self.reverse_queue.peek() {
if self.nodes[entry.vertex].reverse_key == Some([entry.key, entry.cost]) {
return Some(entry);
}
self.reverse_queue.pop();
}
None
}
pub(super) fn advance_reverse_search(&mut self) {
while let Some(edge) = self.peek_edge() {
let Some(top) = self.peek_state() else {
return;
};
if self.is_consistent(edge.to) && edge.key[0] < top.key {
return;
}
self.expand_state(top.vertex);
}
}
pub(super) fn expand_state(&mut self, x: usize) {
self.reverse_queue.pop();
self.nodes[x].reverse_key = None;
let neighbors = core::mem::take(&mut self.nodes[x].adjacent);
let node = &mut self.nodes[x];
let overconsistent = node.h_con < node.h_exp;
if overconsistent {
node.h_exp = node.h_con;
self.lower_neighbors(x, &neighbors);
} else {
let old = core::mem::replace(&mut node.h_exp, f64::INFINITY);
self.raise_neighbors(x, old, &neighbors);
}
self.nodes[x].adjacent = neighbors;
if !overconsistent {
self.update_state(x);
}
}
fn lower_neighbors(&mut self, x: usize, neighbors: &[usize]) {
let h_exp = self.nodes[x].h_exp;
for &y in neighbors {
if self.is_invalid(y, x) || self.is_reverse_root(y) {
continue;
}
let through = h_exp + self.cost_estimate(y, x);
if through < self.nodes[y].h_con {
self.set_h_con(y, through);
}
}
}
fn raise_neighbors(&mut self, x: usize, old: f64, neighbors: &[usize]) {
for &y in neighbors {
if self.is_invalid(y, x) {
continue;
}
let through = old + self.cost_estimate(y, x);
if through.total_cmp(&self.nodes[y].h_con).is_eq() {
self.update_state(y);
}
}
}
fn update_state(&mut self, x: usize) {
if self.is_reverse_root(x) {
return;
}
let h_con = self.nodes[x]
.adjacent
.iter()
.filter(|&&y| !self.is_invalid(x, y))
.map(|&y| self.nodes[y].h_exp + self.cost_estimate(x, y))
.fold(f64::INFINITY, f64::min);
self.set_h_con(x, h_con);
}
fn set_h_con(&mut self, x: usize, h_con: f64) {
if h_con.total_cmp(&self.nodes[x].h_con).is_ne() {
self.nodes[x].h_con = h_con;
self.rekey_incoming(x);
}
if self.is_consistent(x) {
self.nodes[x].reverse_key = None;
} else {
self.queue_state(x);
}
}
pub(super) fn update_heuristic(&mut self, v: usize, x: usize) {
self.nodes[v].invalid.push(x);
self.update_state(v);
}
}