use alloc::borrow::Cow;
use alloc::vec::Vec;
use sas_plus::{OpId, State, Task};
use super::Labels;
use crate::{Heuristic, Relaxation};
#[derive(Clone, Debug)]
pub struct LandmarkCount<'r> {
relaxation: Cow<'r, Relaxation>,
labels: Labels,
}
impl LandmarkCount<'static> {
#[must_use]
pub fn new(task: &Task) -> Self {
Self::from_cow(Cow::Owned(Relaxation::new(task)))
}
}
impl<'r> LandmarkCount<'r> {
#[must_use]
pub fn with_relaxation(relaxation: &'r Relaxation) -> Self {
Self::from_cow(Cow::Borrowed(relaxation))
}
fn from_cow(relaxation: Cow<'r, Relaxation>) -> Self {
Self {
relaxation,
labels: Labels::default(),
}
}
#[must_use]
pub fn value(&mut self, state: &State) -> Option<u64> {
if !self.labels.compute(&self.relaxation, state) {
return None;
}
Some(self.labels.pending_landmarks().count() as u64)
}
}
impl Heuristic<State> for LandmarkCount<'_> {
fn estimate(&mut self, state: &State) -> Option<u32> {
self.value(state).map(saturate)
}
}
#[derive(Clone, Debug)]
pub struct UniformLandmarkCost<'r> {
pub(super) relaxation: Cow<'r, Relaxation>,
pub(super) achievers: Vec<Vec<OpId>>,
pub(super) costs: Vec<u64>,
pub(super) labels: Labels,
shares: Vec<u32>,
}
impl UniformLandmarkCost<'static> {
#[must_use]
pub fn new(task: &Task) -> Self {
Self::from_cow(Cow::Owned(Relaxation::new(task)))
}
}
impl<'r> UniformLandmarkCost<'r> {
#[must_use]
pub fn with_relaxation(relaxation: &'r Relaxation) -> Self {
Self::from_cow(Cow::Borrowed(relaxation))
}
fn from_cow(relaxation: Cow<'r, Relaxation>) -> Self {
let num_ops = relaxation.num_operators();
let mut achievers = alloc::vec![Vec::new(); relaxation.num_facts()];
let mut costs = alloc::vec![0; num_ops];
for (op, cost) in costs.iter_mut().enumerate() {
for rule in relaxation.op_rules(op) {
*cost = rule.cost;
if achievers[rule.head].last() != Some(&op) {
achievers[rule.head].push(op);
}
}
}
Self {
relaxation,
achievers,
costs,
labels: Labels::default(),
shares: Vec::new(),
}
}
#[must_use]
pub fn value(&mut self, state: &State) -> Option<u64> {
if !self.labels.compute(&self.relaxation, state) {
return None;
}
self.shares.clear();
self.shares.resize(self.costs.len(), 0);
let mut sum = 0.0;
for landmark in self.labels.pending_landmarks() {
let achievers = &self.achievers[landmark];
sum += cheapest_share(
&self.relaxation,
&self.labels,
achievers,
&self.costs,
&mut self.shares,
);
}
Some(round_up_admissible(sum))
}
}
impl Heuristic<State> for UniformLandmarkCost<'_> {
fn estimate(&mut self, state: &State) -> Option<u32> {
self.value(state).map(saturate)
}
}
fn cheapest_share(
relaxation: &Relaxation,
labels: &Labels,
achievers: &[OpId],
costs: &[u64],
shares: &mut [u32],
) -> f64 {
let mut best = f64::INFINITY;
for &op in achievers {
if shares[op] == 0 {
shares[op] = pending_heads(relaxation, labels, op);
}
best = best.min(to_f64(costs[op]) / f64::from(shares[op]));
}
if best.is_finite() { best } else { 0.0 }
}
fn pending_heads(relaxation: &Relaxation, labels: &Labels, op: OpId) -> u32 {
let rules = relaxation.op_rules(op);
let pending = |i: usize| labels.is_pending(rules[i].head);
let distinct = (0..rules.len())
.filter(|&i| pending(i) && !rules[..i].iter().any(|r| r.head == rules[i].head))
.count();
u32::try_from(distinct).expect("an operator has fewer than 2^32 effects")
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the value is positive; `as` truncates toward zero and saturates"
)]
pub(super) fn round_up_admissible(sum: f64) -> u64 {
let value = sum - 1e-9 * (1.0 + sum);
if value <= 0.0 {
return 0;
}
let truncated = value as u64;
truncated + u64::from(to_f64(truncated) < value)
}
#[expect(
clippy::cast_precision_loss,
reason = "operator costs are u32 and sums stay below 2^53, exact in f64"
)]
pub(super) fn to_f64(cost: u64) -> f64 {
cost as f64
}
pub(super) fn saturate(value: u64) -> u32 {
u32::try_from(value).unwrap_or(u32::MAX)
}