use alloc::vec::Vec;
use sas_plus::State;
use super::queue::Queue;
use super::workspace::{FactSlot, NO_RULE};
use super::{Relaxation, Workspace};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Combine {
Max,
Add,
}
impl Combine {
fn apply(self, acc: u64, cost: u64) -> u64 {
match self {
Self::Max => acc.max(cost),
Self::Add => acc.saturating_add(cost),
}
}
}
#[derive(Clone, Debug)]
pub struct Exploration {
cost: Vec<Option<u64>>,
goal_cost: Option<u64>,
}
impl Exploration {
#[must_use]
pub fn goal_cost(&self) -> Option<u64> {
self.goal_cost
}
#[must_use]
pub fn cost(&self, fact: usize) -> Option<u64> {
self.cost[fact]
}
}
impl Relaxation {
#[must_use]
pub fn explore(&self, state: &State, combine: Combine) -> Exploration {
let mut workspace = Workspace::default();
let goal_cost = self.run(state, combine, &mut workspace, false);
Exploration {
cost: workspace
.facts
.iter()
.map(|slot| slot.cost(workspace.epoch))
.collect(),
goal_cost,
}
}
pub(crate) fn goal_cost_in(
&self,
state: &State,
combine: Combine,
workspace: &mut Workspace,
) -> Option<u64> {
self.run(state, combine, workspace, true)
}
fn run(
&self,
state: &State,
combine: Combine,
workspace: &mut Workspace,
stop_at_goal: bool,
) -> Option<u64> {
workspace.reset(self);
match combine {
Combine::Max => Run::<false>::new(self, workspace).complete(state, stop_at_goal),
Combine::Add => Run::<true>::new(self, workspace).complete(state, stop_at_goal),
}
}
}
struct Run<'a, const ADD: bool> {
relaxation: &'a Relaxation,
ws: &'a mut Workspace,
open_goals: usize,
}
impl<'a, const ADD: bool> Run<'a, ADD> {
fn new(relaxation: &'a Relaxation, ws: &'a mut Workspace) -> Self {
Self {
relaxation,
ws,
open_goals: relaxation.goal.len(),
}
}
fn complete(mut self, state: &State, stop_at_goal: bool) -> Option<u64> {
self.seed(state);
self.drain(stop_at_goal);
self.goal_cost()
}
const COMBINE: Combine = if ADD { Combine::Add } else { Combine::Max };
fn seed(&mut self, state: &State) {
let relaxation = self.relaxation;
for fact in relaxation.state_facts(state) {
self.close_at_zero(fact);
}
for &fact in &relaxation.free {
self.close_at_zero(fact);
}
self.ws.start_facts = self.ws.order.len();
let (facts, epoch) = (&self.ws.facts, self.ws.epoch);
self.open_goals = relaxation
.goal
.iter()
.filter(|&&g| !facts[g].is_closed(epoch))
.count();
for i in 0..self.ws.order.len() {
self.trigger_watchers(self.ws.order[i] as usize, 0);
}
for &r in &relaxation.unconditioned {
let rule = self.ws.rules[r as usize];
self.offer(rule.head, u64::from(rule.cost), r);
}
}
#[inline]
fn close_at_zero(&mut self, fact: u32) {
let epoch = self.ws.epoch;
let slot = &mut self.ws.facts[fact as usize];
if slot.is_closed(epoch) {
return;
}
*slot = FactSlot::open(0, NO_RULE, epoch);
slot.close(epoch);
self.ws.order.push(fact);
}
fn drain(&mut self, stop_at_goal: bool) {
while !(stop_at_goal && self.open_goals == 0) {
let Some((cost, fact)) = self.ws.queue.pop() else {
break;
};
let epoch = self.ws.epoch;
let slot = &mut self.ws.facts[fact as usize];
if slot.is_closed(epoch) {
continue;
}
slot.close(epoch);
self.ws.order.push(fact);
if self.relaxation.is_goal[fact as usize] {
self.open_goals -= 1;
}
self.trigger_watchers(fact as usize, cost);
}
}
#[inline]
fn trigger_watchers(&mut self, fact: usize, cost: u64) {
let Workspace {
epoch,
facts,
rules,
queue,
..
} = &mut *self.ws;
for &r in self.relaxation.watchers(fact) {
let slot = &mut rules[r as usize];
slot.touch(*epoch);
slot.accumulated = Self::COMBINE.apply(slot.accumulated, cost);
slot.unsatisfied -= 1;
if slot.unsatisfied == 0 {
let total = slot.accumulated.saturating_add(u64::from(slot.cost));
offer(facts, queue, *epoch, slot.head, total, r);
}
}
}
fn offer(&mut self, fact: u32, cost: u64, supporter: u32) {
offer(
&mut self.ws.facts,
&mut self.ws.queue,
self.ws.epoch,
fact,
cost,
supporter,
);
}
fn goal_cost(&self) -> Option<u64> {
let combine = Self::COMBINE;
self.relaxation.goal.iter().try_fold(0, |acc, &g| {
self.ws.facts[g]
.cost(self.ws.epoch)
.map(|c| combine.apply(acc, c))
})
}
}
#[inline]
fn offer(
facts: &mut [FactSlot],
queue: &mut Queue,
epoch: u32,
fact: u32,
cost: u64,
supporter: u32,
) {
let slot = &mut facts[fact as usize];
if !slot.is_reached(epoch) || cost < slot.cost {
*slot = FactSlot::open(cost, supporter, epoch);
queue.push(cost, fact);
}
}