use alloc::borrow::Cow;
use alloc::vec::Vec;
use sas_plus::{OpId, State, Task};
use crate::{Heuristic, Relaxation};
const WORD: usize = 64;
#[derive(Clone, Debug)]
pub struct HSetAdd<'r> {
relaxation: Cow<'r, Relaxation>,
costs: Vec<u64>,
words: usize,
sets: Vec<u64>,
cost: Vec<Option<u64>>,
offer: Vec<u64>,
}
impl HSetAdd<'static> {
#[must_use]
pub fn new(task: &Task) -> Self {
Self::from_cow(Cow::Owned(Relaxation::new(task)))
}
}
impl<'r> HSetAdd<'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 costs: Vec<u64> = (0..relaxation.num_operators())
.map(|op| relaxation.op_rules(op).first().map_or(0, |r| r.cost))
.collect();
let words = costs.len().div_ceil(WORD);
Self {
relaxation,
costs,
words,
sets: Vec::new(),
cost: Vec::new(),
offer: Vec::new(),
}
}
#[must_use]
pub fn value(&mut self, state: &State) -> Option<u64> {
self.compute(state)?;
Some(self.set_cost(&self.offer))
}
#[must_use]
pub fn plan(&mut self, state: &State) -> Option<Vec<OpId>> {
self.compute(state)?;
Some(
(0..self.costs.len())
.filter(|&op| self.offer[op / WORD] & (1 << (op % WORD)) != 0)
.collect(),
)
}
fn compute(&mut self, state: &State) -> Option<()> {
let relaxation = &*self.relaxation;
let facts = relaxation.num_facts();
self.sets.clear();
self.sets.resize(facts * self.words, 0);
self.cost.clear();
self.cost.resize(facts, None);
self.offer.clear();
self.offer.resize(self.words, 0);
for f in relaxation
.state_facts(state)
.chain(relaxation.free_facts().iter().copied())
{
self.cost[f as usize] = Some(0);
}
let rules = relaxation.rules().len();
let mut changed = true;
while changed {
changed = false;
for r in 0..rules {
changed |= self.offer_rule(r);
}
}
self.offer.fill(0);
for &g in self.relaxation.goal_facts() {
self.cost[g]?;
let set = &self.sets[g * self.words..(g + 1) * self.words];
for (o, &s) in self.offer.iter_mut().zip(set) {
*o |= s;
}
}
Some(())
}
fn offer_rule(&mut self, r: usize) -> bool {
let rule = &self.relaxation.rules()[r];
if rule.conditions.iter().any(|&c| self.cost[c].is_none()) {
return false;
}
self.offer.fill(0);
for &c in &rule.conditions {
let set = &self.sets[c * self.words..(c + 1) * self.words];
for (o, &s) in self.offer.iter_mut().zip(set) {
*o |= s;
}
}
if let Some(op) = rule.op {
self.offer[op / WORD] |= 1 << (op % WORD);
}
let cost = self.set_cost(&self.offer);
let head = rule.head;
if self.cost[head].is_some_and(|old| old <= cost) {
return false;
}
self.cost[head] = Some(cost);
self.sets[head * self.words..(head + 1) * self.words].copy_from_slice(&self.offer);
true
}
fn set_cost(&self, set: &[u64]) -> u64 {
let mut total = 0_u64;
for (w, &word) in set.iter().enumerate() {
let mut word = word;
while word != 0 {
let op = w * WORD + word.trailing_zeros() as usize;
total = total.saturating_add(self.costs[op]);
word &= word - 1;
}
}
total
}
}
impl Heuristic<State> for HSetAdd<'_> {
fn estimate(&mut self, state: &State) -> Option<u32> {
self.value(state)
.map(|v| u32::try_from(v).unwrap_or(u32::MAX))
}
}