use alloc::vec::Vec;
use sas_plus::{OpId, State};
use super::{Combine, Relaxation, Rule, Workspace};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RelaxedPlan {
ops: Vec<OpId>,
cost: u64,
}
impl RelaxedPlan {
pub(crate) fn new(ops: Vec<OpId>, cost: u64) -> Self {
Self { ops, cost }
}
#[must_use]
pub fn ops(&self) -> &[OpId] {
&self.ops
}
#[must_use]
pub fn cost(&self) -> u64 {
self.cost
}
#[must_use]
pub fn preferred_operators(&self, relaxation: &Relaxation, state: &State) -> Vec<OpId> {
relaxation.applicable_once(&self.ops, state)
}
}
impl Relaxation {
pub(crate) fn applicable_once(&self, ops: &[OpId], state: &State) -> Vec<OpId> {
let mut preferred: Vec<OpId> = Vec::new();
for &op in ops {
if !preferred.contains(&op) && self.is_applicable(op, state) {
preferred.push(op);
}
}
preferred
}
#[must_use]
pub fn relaxed_plan(&self, state: &State) -> Option<RelaxedPlan> {
let mut workspace = Workspace::default();
let cost = self.relaxed_plan_in(state, &mut workspace)?;
Some(RelaxedPlan::new(workspace.plan, cost))
}
pub(crate) fn relaxed_plan_in(&self, state: &State, workspace: &mut Workspace) -> Option<u64> {
self.goal_cost_in(state, Combine::Add, workspace)?;
self.mark_supporters(workspace);
Some(self.replay(state, workspace))
}
fn mark_supporters(&self, ws: &mut Workspace) {
ws.marked.reset(self.rules.len(), ws.epoch);
ws.seen.reset(self.num_facts, ws.epoch);
ws.stack.clear();
ws.stack.extend_from_slice(&self.goal);
while let Some(fact) = ws.stack.pop() {
if !ws.seen.insert(fact) {
continue;
}
if let Some(r) = ws.facts[fact].supporter(ws.epoch) {
ws.marked.insert(r);
ws.stack.extend_from_slice(&self.rules[r].conditions);
}
}
}
fn replay(&self, state: &State, ws: &mut Workspace) -> u64 {
self.relaxed_state(state, ws);
let start_facts = ws.start_facts;
debug_assert!(
ws.order[..start_facts]
.iter()
.all(|&f| ws.facts[f as usize].supporter(ws.epoch).is_none()),
"the exploration closes the start facts first, without supporters"
);
ws.plan.clear();
let mut cost = 0_u64;
let order = core::mem::take(&mut ws.order);
for &fact in &order[start_facts..] {
let fact = fact as usize;
let Some(r) = ws.facts[fact]
.supporter(ws.epoch)
.filter(|&r| ws.marked.contains(r))
else {
continue;
};
let rule = &self.rules[r];
let Some(op) = rule.op else {
continue;
};
if ws.reached.contains(rule.head) {
continue;
}
self.apply_relaxed(op, ws);
debug_assert!(
ws.reached.contains(rule.head),
"a marked rule fires once its conditions hold"
);
ws.plan.push(op);
cost = cost.saturating_add(rule.cost);
}
ws.order = order;
cost
}
fn relaxed_state(&self, state: &State, ws: &mut Workspace) {
ws.reached.reset(self.num_facts, ws.epoch);
for fact in self.start_facts(state) {
ws.reached.insert(fact as usize);
}
if !self.has_axioms() {
return;
}
ws.stack.clear();
for rule in &self.rules[self.first_axiom..] {
fire_if_enabled(rule, ws);
}
self.close_axioms(ws);
}
fn has_axioms(&self) -> bool {
self.first_axiom < self.rules.len()
}
fn apply_relaxed(&self, op: OpId, ws: &mut Workspace) {
ws.fired.clear();
ws.fired.extend(
self.rules[self.op_rules[op].clone()]
.iter()
.filter(|rule| rule.conditions.iter().all(|&c| ws.reached.contains(c)))
.map(|rule| rule.head),
);
for &head in &ws.fired {
ws.reached.insert(head);
}
if self.has_axioms() {
ws.stack.extend_from_slice(&ws.fired);
self.close_axioms(ws);
}
}
fn close_axioms(&self, ws: &mut Workspace) {
while let Some(fact) = ws.stack.pop() {
for &a in self.axiom_watchers(fact) {
fire_if_enabled(&self.rules[self.first_axiom + a as usize], ws);
}
}
}
}
#[inline]
fn fire_if_enabled(rule: &Rule, ws: &mut Workspace) {
if !ws.reached.contains(rule.head) && rule.conditions.iter().all(|&c| ws.reached.contains(c)) {
ws.reached.insert(rule.head);
ws.stack.push(rule.head);
}
}