#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
mod bfws;
mod hiw;
mod iw;
mod novelty;
mod relevance;
mod rng;
mod rollout;
#[cfg(feature = "sas-plus")]
pub mod sas;
mod siw;
mod sketch;
pub use bfws::{bfws, bfws_r};
pub use hiw::{hiw, ihiw, pi_hiw};
pub use iw::{iterated_width, iw};
pub use novelty::NoveltyTable;
pub use rollout::{pi_iw, rollout_iw};
pub use siw::{siw, siw_plus};
pub use sketch::{Condition, Effect, Rule, siw_r};
pub trait WidthProblem {
type State: Clone;
type Action: Clone;
fn initial_state(&self) -> Self::State;
fn successors(&self, state: &Self::State, out: &mut Vec<(Self::Action, Self::State)>);
fn features(&self, state: &Self::State, out: &mut Vec<u32>);
fn num_features(&self) -> usize;
fn is_goal(&self, state: &Self::State) -> bool;
}
pub trait GoalCount: WidthProblem {
fn unsatisfied_goals(&self, state: &Self::State) -> usize;
}
pub trait RelaxedPlan: GoalCount {
fn relaxed_plan_features(&self, state: &Self::State, out: &mut Vec<u32>);
}
#[derive(Clone, Debug)]
pub struct SearchResult<A> {
pub plan: Option<Vec<A>>,
pub expanded: usize,
pub generated: usize,
}
pub(crate) struct Node<S, A> {
pub state: S,
pub parent: Option<usize>,
pub action: Option<A>,
}
pub(crate) fn sorted_set(fill: impl FnOnce(&mut Vec<u32>)) -> Vec<u32> {
let mut ids = Vec::new();
fill(&mut ids);
ids.sort_unstable();
ids.dedup();
ids
}
pub(crate) fn extract_plan<S, A: Clone>(nodes: &[Node<S, A>], mut i: usize) -> Vec<A> {
let mut plan = Vec::new();
while let Some(p) = nodes[i].parent {
plan.push(
nodes[i]
.action
.clone()
.expect("non-root node has an action"),
);
i = p;
}
plan.reverse();
plan
}
#[cfg(test)]
pub(crate) mod test_problems {
use crate::WidthProblem;
pub(crate) struct Line {
pub len: u32,
}
impl WidthProblem for Line {
type State = u32;
type Action = i8;
fn initial_state(&self) -> u32 {
0
}
fn successors(&self, s: &u32, out: &mut Vec<(i8, u32)>) {
if *s > 0 {
out.push((-1, s - 1));
}
if *s < self.len {
out.push((1, s + 1));
}
}
fn features(&self, s: &u32, out: &mut Vec<u32>) {
out.push(*s);
}
fn num_features(&self) -> usize {
self.len as usize + 1
}
fn is_goal(&self, s: &u32) -> bool {
*s == self.len
}
}
}