Skip to main content

width_search/
lib.rs

1#![doc = include_str!("../README.md")]
2#![forbid(unsafe_code)]
3
4/// Best-first width search: BFWS(⟨w_{#g}, #g⟩) and BFWS(⟨w_{#g,#r}, #g⟩).
5mod bfws;
6/// IW(k) and iterated width.
7mod iw;
8/// The novelty table shared by all searches.
9mod novelty;
10/// Rollout IW(1): depth-first rollouts with depth-aware novelty.
11mod rollout;
12/// Adapter that runs width-based search on a [`sas_plus::Task`].
13#[cfg(feature = "sas-plus")]
14pub mod sas;
15/// Serialized IW: one IW search per achieved goal.
16mod siw;
17
18pub use bfws::{bfws, bfws_r};
19pub use iw::{iterated_width, iw};
20pub use novelty::NoveltyTable;
21pub use rollout::rollout_iw;
22pub use siw::siw;
23
24/// A search problem described by successors and integer features.
25///
26/// Width-based search never inspects states directly. It only needs the
27/// successors of a state, the goal test, and the features that are true in a
28/// state, numbered `0..num_features()`. Novelty is computed over those
29/// features, so their choice decides what counts as "new".
30pub trait WidthProblem {
31    /// A search state. Cloned when a node is stored.
32    type State: Clone;
33    /// The label of a transition; plans are sequences of actions.
34    type Action: Clone;
35
36    /// The state the search starts from.
37    fn initial_state(&self) -> Self::State;
38    /// Push all `(action, successor)` pairs of `state` into `out`.
39    fn successors(&self, state: &Self::State, out: &mut Vec<(Self::Action, Self::State)>);
40    /// Push the feature ids (`< num_features()`) true in `state` into `out`.
41    fn features(&self, state: &Self::State, out: &mut Vec<u32>);
42    /// Number of distinct features; every id from [`WidthProblem::features`]
43    /// must be below it.
44    fn num_features(&self) -> usize;
45    /// Whether `state` satisfies the goal.
46    fn is_goal(&self, state: &Self::State) -> bool;
47}
48
49/// Goal-directed information used by BFWS.
50pub trait GoalCount: WidthProblem {
51    /// Number of goal conditions not yet satisfied (`#g`).
52    fn unsatisfied_goals(&self, state: &Self::State) -> usize;
53}
54
55/// Relaxed-plan information used by [`bfws_r`] to compute `#r`.
56pub trait RelaxedPlan: GoalCount {
57    /// Push into `out` the features a relaxed plan from `state` makes true,
58    /// for example the add effects of its actions (LAPKT's choice).
59    ///
60    /// Push nothing when the goal is unreachable even under the relaxation.
61    /// Features already true in `state` are ignored by the search.
62    fn relaxed_plan_features(&self, state: &Self::State, out: &mut Vec<u32>);
63}
64
65/// The outcome of a search: a plan, if one was found, and node counts.
66#[derive(Clone, Debug)]
67pub struct SearchResult<A> {
68    /// The actions from the initial state to a goal, or `None` if the search
69    /// gave up (pruning) or exhausted the state space.
70    pub plan: Option<Vec<A>>,
71    /// Number of nodes whose successors were generated.
72    pub expanded: usize,
73    /// Number of states generated, the initial state included.
74    pub generated: usize,
75}
76
77/// A search node, stored in an arena and linked to its parent by index.
78pub(crate) struct Node<S, A> {
79    /// The state this node represents.
80    pub state: S,
81    /// Arena index of the parent node; `None` only for the root.
82    pub parent: Option<usize>,
83    /// The action that led from the parent here; `None` only for the root.
84    pub action: Option<A>,
85}
86
87/// Follows parent links from node `i` back to the root and returns the
88/// actions along the way, in execution order.
89pub(crate) fn extract_plan<S, A: Clone>(nodes: &[Node<S, A>], mut i: usize) -> Vec<A> {
90    let mut plan = Vec::new();
91    while let Some(p) = nodes[i].parent {
92        plan.push(
93            nodes[i]
94                .action
95                .clone()
96                .expect("non-root node has an action"),
97        );
98        i = p;
99    }
100
101    plan.reverse();
102    plan
103}