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