use alloc::vec::Vec;
use sas_plus::OpId;
use super::Relaxation;
use super::queue::Queue;
pub(super) const NO_RULE: u32 = u32::MAX;
pub(super) const MAX_EPOCH: u32 = (1 << 31) - 1;
#[derive(Clone, Copy, Debug)]
pub(super) struct FactSlot {
pub cost: u64,
pub supporter: u32,
stamp: u32,
}
impl FactSlot {
pub const UNREACHED: Self = Self {
cost: 0,
supporter: NO_RULE,
stamp: 0,
};
pub fn open(cost: u64, supporter: u32, epoch: u32) -> Self {
Self {
cost,
supporter,
stamp: epoch << 1,
}
}
pub fn is_reached(self, epoch: u32) -> bool {
self.stamp >> 1 == epoch
}
pub fn is_closed(self, epoch: u32) -> bool {
self.stamp == (epoch << 1 | 1)
}
pub fn close(&mut self, epoch: u32) {
debug_assert!(self.is_reached(epoch), "only reached facts close");
self.stamp = epoch << 1 | 1;
}
pub fn cost(self, epoch: u32) -> Option<u64> {
self.is_reached(epoch).then_some(self.cost)
}
pub fn supporter(self, epoch: u32) -> Option<usize> {
(self.is_reached(epoch) && self.supporter != NO_RULE).then_some(self.supporter as usize)
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct RuleSlot {
pub conditions: u32,
pub unsatisfied: u32,
pub head: u32,
pub cost: u32,
pub stamp: u32,
pub accumulated: u64,
}
impl RuleSlot {
pub fn new(conditions: u32, head: u32, cost: u32) -> Self {
Self {
conditions,
unsatisfied: conditions,
head,
cost,
stamp: 0,
accumulated: 0,
}
}
#[inline]
pub fn touch(&mut self, epoch: u32) {
if self.stamp != epoch {
self.stamp = epoch;
self.unsatisfied = self.conditions;
self.accumulated = 0;
}
}
}
#[derive(Clone, Debug, Default)]
pub(super) struct Marks {
stamps: Vec<u32>,
epoch: u32,
}
impl Marks {
pub fn reset(&mut self, len: usize, epoch: u32) {
if self.stamps.len() != len || epoch <= self.epoch {
refill(&mut self.stamps, len, 0);
}
self.epoch = epoch;
}
#[inline]
pub fn contains(&self, i: usize) -> bool {
self.stamps[i] == self.epoch
}
#[inline]
pub fn insert(&mut self, i: usize) -> bool {
core::mem::replace(&mut self.stamps[i], self.epoch) != self.epoch
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct Workspace {
pub(super) epoch: u32,
pub(super) facts: Vec<FactSlot>,
pub(super) rules: Vec<RuleSlot>,
pub(super) queue: Queue,
pub(super) order: Vec<u32>,
pub(super) start_facts: usize,
pub(super) marked: Marks,
pub(super) seen: Marks,
pub(super) stack: Vec<usize>,
pub(super) reached: Marks,
pub(super) fired: Vec<usize>,
pub(crate) plan: Vec<OpId>,
}
impl Workspace {
pub(super) fn reset(&mut self, relaxation: &Relaxation) {
let sized = self.facts.len() == relaxation.num_facts
&& self.rules.len() == relaxation.rule_init.len();
if !sized || self.epoch == MAX_EPOCH {
self.facts.clear();
self.facts.extend_from_slice(&relaxation.fact_init);
self.rules.clear();
self.rules.extend_from_slice(&relaxation.rule_init);
self.epoch = 0;
}
self.epoch += 1;
self.queue.clear();
self.order.clear();
}
}
pub(super) fn refill<T: Clone>(buffer: &mut Vec<T>, len: usize, value: T) {
buffer.clear();
buffer.resize(len, value);
}
#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use sas_plus::random::{RandomTaskParams, random_task};
use super::{MAX_EPOCH, Workspace};
use crate::{Combine, Relaxation};
#[test]
fn evaluations_survive_the_epoch_wrap() {
let params = RandomTaskParams {
max_conditions: 2,
num_derived: 2,
..RandomTaskParams::default()
};
for seed in 0..16 {
let task = random_task(seed, params);
let relaxation = Relaxation::new(&task);
let initial = task.initial_state();
let mut states: Vec<_> = task
.applicable_operators(&initial)
.map(|op| task.apply(op, &initial))
.collect();
states.push(initial);
let mut reused = Workspace::default();
reused.reset(&relaxation);
reused.epoch = MAX_EPOCH - 2;
for _ in 0..3 {
for state in &states {
for combine in [Combine::Max, Combine::Add] {
let expected = relaxation.explore(state, combine).goal_cost();
assert_eq!(
relaxation.goal_cost_in(state, combine, &mut reused),
expected
);
}
let cost = relaxation.relaxed_plan_in(state, &mut reused);
let fresh = relaxation.relaxed_plan(state);
assert_eq!(cost, fresh.as_ref().map(crate::RelaxedPlan::cost));
if let Some(fresh) = fresh {
assert_eq!(reused.plan, fresh.ops());
}
}
}
assert!(reused.epoch < MAX_EPOCH - 2, "the epoch wrapped");
}
}
}