mod exploration;
mod plan;
mod queue;
mod workspace;
use alloc::vec::Vec;
use core::ops::Range;
use sas_plus::{Fact, OpId, Task};
pub use exploration::{Combine, Exploration};
pub use plan::RelaxedPlan;
pub(crate) use workspace::Workspace;
use workspace::{FactSlot, RuleSlot};
#[derive(Clone, Debug)]
pub struct Relaxation {
offsets: Vec<u32>,
num_facts: usize,
rules: Vec<Rule>,
watcher_starts: Vec<usize>,
watcher_rules: Vec<u32>,
axiom_watcher_starts: Vec<usize>,
axiom_watcher_axioms: Vec<u32>,
rule_init: Vec<RuleSlot>,
fact_init: Vec<FactSlot>,
unconditioned: Vec<u32>,
op_rules: Vec<Range<usize>>,
first_axiom: usize,
pre_starts: Vec<usize>,
pre_facts: Vec<u32>,
free: Vec<u32>,
goal: Vec<usize>,
is_goal: Vec<bool>,
}
#[derive(Clone, Debug)]
pub(crate) struct Rule {
pub(crate) conditions: Vec<usize>,
pub(crate) head: usize,
pub(crate) op: Option<OpId>,
pub(crate) cost: u64,
}
impl Relaxation {
#[must_use]
pub fn new(task: &Task) -> Self {
let offsets = fact_offsets(task);
let num_facts = task.variables.iter().map(|v| v.domain_size as usize).sum();
assert!(u32::try_from(num_facts).is_ok(), "fewer than 2^32 facts");
let id = |f: &Fact| (offsets[f.var] + f.value) as usize;
let (mut rules, op_rules) = operator_rules(task, &id);
let first_axiom = rules.len();
rules.extend(
task.axioms
.iter()
.filter(|a| consistent(&a.conditions))
.map(|a| Rule {
conditions: sorted_ids(a.conditions.iter().map(id)),
head: id(&a.head),
op: None,
cost: 0,
}),
);
let free = (0..task.num_variables())
.filter(|&var| task.variables[var].is_derived())
.map(|var| offsets[var] + task.initial.0[var])
.collect();
let goal = sorted_ids(task.goal.iter().map(id));
let mut is_goal = alloc::vec![false; num_facts];
for &g in &goal {
is_goal[g] = true;
}
let (watcher_starts, watcher_rules) = watchers(&rules, num_facts);
let (axiom_watcher_starts, axiom_watcher_axioms) =
watchers(&rules[first_axiom..], num_facts);
let (rule_init, unconditioned) = worklist_tables(&rules);
let mut pre_starts = Vec::with_capacity(task.operators.len() + 1);
let mut pre_facts = Vec::new();
pre_starts.push(0);
for operator in &task.operators {
pre_facts.extend(operator.pre.iter().map(|f| to_u32(id(f))));
pre_starts.push(pre_facts.len());
}
Self {
offsets,
num_facts,
rules,
watcher_starts,
watcher_rules,
axiom_watcher_starts,
axiom_watcher_axioms,
rule_init,
fact_init: alloc::vec![FactSlot::UNREACHED; num_facts],
unconditioned,
op_rules,
first_axiom,
pre_starts,
pre_facts,
free,
goal,
is_goal,
}
}
#[must_use]
pub fn num_facts(&self) -> usize {
self.num_facts
}
#[must_use]
pub fn fact_id(&self, fact: Fact) -> usize {
(self.offsets[fact.var] + fact.value) as usize
}
#[must_use]
pub fn fact(&self, id: usize) -> Fact {
assert!(id < self.num_facts, "fact id {id} out of range");
let id = u32::try_from(id).expect("fact ids fit u32");
let var = self.offsets.partition_point(|&offset| offset <= id) - 1;
Fact::new(var, id - self.offsets[var])
}
#[must_use]
pub fn is_applicable(&self, op: OpId, state: &sas_plus::State) -> bool {
self.pre_facts[self.pre_starts[op]..self.pre_starts[op + 1]]
.iter()
.all(|&f| {
let fact = self.fact(f as usize);
state.0[fact.var] == fact.value
})
}
pub(crate) fn state_facts<'s>(
&'s self,
state: &'s sas_plus::State,
) -> impl Iterator<Item = u32> + 's {
state
.0
.iter()
.zip(&self.offsets)
.map(|(&value, &offset)| offset + value)
}
fn start_facts<'s>(&'s self, state: &'s sas_plus::State) -> impl Iterator<Item = u32> + 's {
self.state_facts(state).chain(self.free.iter().copied())
}
pub(crate) fn watchers(&self, fact: usize) -> &[u32] {
&self.watcher_rules[self.watcher_starts[fact]..self.watcher_starts[fact + 1]]
}
pub(crate) fn rules(&self) -> &[Rule] {
&self.rules
}
pub(crate) fn axiom_rules(&self) -> &[Rule] {
&self.rules[self.first_axiom..]
}
pub(crate) fn op_rules(&self, op: OpId) -> &[Rule] {
&self.rules[self.op_rules[op].clone()]
}
pub(crate) fn num_operators(&self) -> usize {
self.op_rules.len()
}
pub(crate) fn unconditioned(&self) -> &[u32] {
&self.unconditioned
}
pub(crate) fn free_facts(&self) -> &[u32] {
&self.free
}
pub(crate) fn goal_facts(&self) -> &[usize] {
&self.goal
}
fn axiom_watchers(&self, fact: usize) -> &[u32] {
&self.axiom_watcher_axioms
[self.axiom_watcher_starts[fact]..self.axiom_watcher_starts[fact + 1]]
}
}
fn fact_offsets(task: &Task) -> Vec<u32> {
task.variables
.iter()
.scan(0, |next, v| {
let offset = *next;
*next += v.domain_size as usize;
Some(to_u32(offset))
})
.collect()
}
fn operator_rules(task: &Task, id: &impl Fn(&Fact) -> usize) -> (Vec<Rule>, Vec<Range<usize>>) {
let mut rules = Vec::new();
let mut ranges = Vec::with_capacity(task.operators.len());
let mut facts = Vec::new();
for (op, operator) in task.operators.iter().enumerate() {
let start = rules.len();
for e in &operator.eff {
facts.clear();
facts.extend(operator.pre.iter().chain(&e.conditions).copied());
if consistent(&facts) {
rules.push(Rule {
conditions: sorted_ids(facts.iter().map(id)),
head: id(&e.fact),
op: Some(op),
cost: u64::from(operator.cost),
});
}
}
ranges.push(start..rules.len());
}
(rules, ranges)
}
fn consistent(facts: &[Fact]) -> bool {
facts.iter().enumerate().all(|(i, f)| {
facts[i + 1..]
.iter()
.all(|g| g.var != f.var || g.value == f.value)
})
}
fn sorted_ids(ids: impl Iterator<Item = usize>) -> Vec<usize> {
let mut ids: Vec<usize> = ids.collect();
ids.sort_unstable();
ids.dedup();
ids
}
fn watchers(rules: &[Rule], num_facts: usize) -> (Vec<usize>, Vec<u32>) {
let mut starts = alloc::vec![0; num_facts + 1];
for rule in rules {
for &c in &rule.conditions {
starts[c + 1] += 1;
}
}
for f in 0..num_facts {
starts[f + 1] += starts[f];
}
let mut next = starts.clone();
let mut flat = alloc::vec![0; starts[num_facts]];
for (r, rule) in rules.iter().enumerate() {
for &c in &rule.conditions {
flat[next[c]] = to_u32(r);
next[c] += 1;
}
}
(starts, flat)
}
fn worklist_tables(rules: &[Rule]) -> (Vec<RuleSlot>, Vec<u32>) {
let rule_init = rules
.iter()
.map(|r| {
let cost = u32::try_from(r.cost).expect("rule costs come from u32 operator costs");
RuleSlot::new(to_u32(r.conditions.len()), to_u32(r.head), cost)
})
.collect();
let unconditioned = (0..rules.len())
.filter(|&r| rules[r].conditions.is_empty())
.map(to_u32)
.collect();
(rule_init, unconditioned)
}
fn to_u32(id: usize) -> u32 {
u32::try_from(id).expect("fewer than 2^32 facts and rules")
}