Skip to main content

width_search/
iw.rs

1use std::collections::VecDeque;
2
3use crate::{Node, NoveltyTable, SearchResult, WidthProblem, extract_plan};
4
5/// IW(k): breadth-first search that prunes every generated state whose
6/// novelty exceeds `k`. Runs in `O(n^k)` expansions for `n` features.
7pub fn iw<P: WidthProblem>(problem: &P, k: usize) -> SearchResult<P::Action> {
8    iw_from(problem, problem.initial_state(), k, |s| problem.is_goal(s)).0
9}
10
11/// IW(k) from `root` towards the states accepted by `is_goal`.
12///
13/// Returns the result and, if a plan was found, the state it ends in. Goal
14/// states are never pruned, whatever their novelty.
15pub(crate) fn iw_from<P: WidthProblem>(
16    problem: &P,
17    root: P::State,
18    k: usize,
19    is_goal: impl Fn(&P::State) -> bool,
20) -> (SearchResult<P::Action>, Option<P::State>) {
21    let mut table = NoveltyTable::new(problem.num_features(), k);
22    let mut feats = Vec::new();
23    let mut succ = Vec::new();
24    let mut result = SearchResult {
25        plan: None,
26        expanded: 0,
27        generated: 1,
28    };
29
30    problem.features(&root, &mut feats);
31    table.evaluate_and_insert(&feats);
32    if is_goal(&root) {
33        result.plan = Some(Vec::new());
34        return (result, Some(root));
35    }
36
37    let mut nodes = vec![Node {
38        state: root,
39        parent: None,
40        action: None,
41    }];
42    let mut open = VecDeque::from([0usize]);
43
44    while let Some(i) = open.pop_front() {
45        result.expanded += 1;
46        succ.clear();
47        problem.successors(&nodes[i].state, &mut succ);
48        for (action, state) in succ.drain(..) {
49            result.generated += 1;
50            let goal = is_goal(&state);
51            feats.clear();
52            problem.features(&state, &mut feats);
53            if table.evaluate_and_insert(&feats).is_none() && !goal {
54                continue;
55            }
56
57            nodes.push(Node {
58                state,
59                parent: Some(i),
60                action: Some(action),
61            });
62            let j = nodes.len() - 1;
63            if goal {
64                result.plan = Some(extract_plan(&nodes, j));
65                return (result, Some(nodes.swap_remove(j).state));
66            }
67
68            open.push_back(j);
69        }
70    }
71
72    (result, None)
73}
74
75/// Runs IW(1), IW(2), ... up to `max_k`; returns the width that succeeded
76/// (if any) and the accumulated statistics.
77pub fn iterated_width<P: WidthProblem>(
78    problem: &P,
79    max_k: usize,
80) -> (Option<usize>, SearchResult<P::Action>) {
81    let mut total = SearchResult {
82        plan: None,
83        expanded: 0,
84        generated: 0,
85    };
86    for k in 1..=max_k {
87        let r = iw(problem, k);
88        total.expanded += r.expanded;
89        total.generated += r.generated;
90        if r.plan.is_some() {
91            total.plan = r.plan;
92            return (Some(k), total);
93        }
94    }
95    (None, total)
96}