width_search/siw.rs
1use crate::iw::iw_plus_from;
2use crate::{GoalCount, RelaxedPlan, SearchResult, WidthProblem};
3
4/// Serialized IW (SIW): reaches the goal one subgoal at a time.
5///
6/// From the current state it runs IW(1), IW(2), ... up to `max_k` until a
7/// state with fewer unsatisfied goals (`#g`) is found, or a goal state. The
8/// plans of these searches are concatenated; each search starts with an
9/// empty novelty table.
10///
11/// This is a simplified SIW. LAPKT's SIW (after Lipovetzky & Geffner,
12/// ECAI 2012) tracks goal atoms: a subgoal state must keep every goal achieved
13/// so far, and a newly achieved goal is accepted only if the full goal stays
14/// reachable in the delete relaxation without actions that delete achieved
15/// goals. [`GoalCount`] exposes only the number of unsatisfied goals, so here
16/// any drop in `#g` counts. A subgoal may then undo earlier goals or commit to
17/// one that must be undone later, which gives longer plans (blocksworld).
18///
19/// SIW is incomplete: it gives up (`plan: None`) when no subgoal is reachable
20/// within width `max_k`. That happens in dead ends left by earlier subplans,
21/// but also in live states whose next subgoal needs a width above `max_k`.
22/// Node counts add up over all IW runs, failed ones included.
23pub fn siw<P: GoalCount>(problem: &P, max_k: usize) -> SearchResult<P::Action> {
24 serialize(
25 problem,
26 max_k,
27 None::<fn(&P::State, &mut Vec<u32>)>,
28 GoalCountDrop(problem),
29 )
30}
31
32/// SIW+ (Lipovetzky & Geffner, ECAI 2014): [`siw`] with IW+ in place of IW.
33///
34/// IW+ computes novelty separately for each `#r`: the number of relaxed-plan
35/// features reached on the path from the root of the current IW run, as in
36/// [`bfws_r`](crate::bfws_r). Features already true at that root never
37/// count, even if the path makes them false and true again. The relaxed plan
38/// comes from
39/// [`RelaxedPlan::relaxed_plan_features`] and is computed once at the root of
40/// every IW run. A state is pruned only if it is not novel among the states
41/// with the same `#r`, so IW+ keeps more states than IW and can reach a
42/// subgoal at a lower width, at the price of more nodes per run.
43///
44/// Subgoals are found as in [`siw`]: any drop in `#g` counts, and the search
45/// gives up when no subgoal is reachable within width `max_k`. LAPKT's SIW+
46/// differs in four ways, so node counts and plans differ too: it tracks goal
47/// atoms (see [`siw`]), it skips states already generated in the current run,
48/// it stops widening early when a run pruned nothing by novelty, and its `#r`
49/// counts add effects on the path, so a root fact that an action adds again
50/// counts too.
51pub fn siw_plus<P: RelaxedPlan>(problem: &P, max_k: usize) -> SearchResult<P::Action> {
52 serialize(
53 problem,
54 max_k,
55 Some(|s: &P::State, out: &mut Vec<u32>| problem.relaxed_plan_features(s, out)),
56 GoalCountDrop(problem),
57 )
58}
59
60/// How a serialized search picks its subgoals.
61pub(crate) trait Subgoals<S> {
62 /// What the test needs to know about a subproblem's start state,
63 /// computed once per subproblem.
64 type Anchor;
65
66 /// Summarizes `from`, the state a subproblem starts from.
67 fn anchor(&mut self, from: &S) -> Self::Anchor;
68
69 /// Whether `state` solves the subproblem that started at `anchor`.
70 fn reached(&self, anchor: &Self::Anchor, state: &S) -> bool;
71
72 /// Records that the search moved on to the subgoal `state`; returns
73 /// `false` to make the search give up instead.
74 fn advance(&mut self, _state: &S) -> bool {
75 true
76 }
77}
78
79/// The subgoals of [`siw`] and [`siw_plus`]: a goal state, or any state with
80/// fewer unsatisfied goals than the start of the subproblem.
81struct GoalCountDrop<'p, P>(&'p P);
82
83impl<P: GoalCount> Subgoals<P::State> for GoalCountDrop<'_, P> {
84 /// `#g` of the start state.
85 type Anchor = usize;
86
87 fn anchor(&mut self, from: &P::State) -> usize {
88 self.0.unsatisfied_goals(from)
89 }
90
91 fn reached(&self, g: &usize, state: &P::State) -> bool {
92 self.0.is_goal(state) || self.0.unsatisfied_goals(state) < *g
93 }
94}
95
96/// The loop behind [`siw`], [`siw_plus`] and [`siw_r`](crate::siw_r):
97/// reaches one subgoal after another and concatenates the subplans.
98///
99/// Each IW run is IW+ when `oracle` is set, plain IW otherwise. `subgoals`
100/// decides which states end a subproblem; only [`siw_r`](crate::siw_r)'s
101/// subgoals also stop the search when it moves on to a subgoal it passed
102/// before (a cycling sketch).
103pub(crate) fn serialize<P, F, G>(
104 problem: &P,
105 max_k: usize,
106 mut oracle: Option<F>,
107 mut subgoals: G,
108) -> SearchResult<P::Action>
109where
110 P: WidthProblem,
111 F: FnMut(&P::State, &mut Vec<u32>),
112 G: Subgoals<P::State>,
113{
114 // The initial state counts once, even if no IW run is needed.
115 let mut total = SearchResult {
116 plan: None,
117 expanded: 0,
118 generated: 0,
119 };
120 let mut state = problem.initial_state();
121 if problem.is_goal(&state) {
122 total.generated = 1;
123 }
124
125 let mut plan = Vec::new();
126
127 while !problem.is_goal(&state) {
128 let anchor = subgoals.anchor(&state);
129 let is_subgoal = |s: &P::State| subgoals.reached(&anchor, s);
130 let Some((subplan, next)) = reach_subgoal(
131 problem,
132 &state,
133 max_k,
134 oracle.as_mut(),
135 is_subgoal,
136 &mut total,
137 ) else {
138 return total;
139 };
140 if !subgoals.advance(&next) {
141 return total;
142 }
143
144 plan.extend(subplan);
145 state = next;
146 }
147
148 total.plan = Some(plan);
149 total
150}
151
152/// Iterated width from `state` until `is_subgoal` holds; returns the subplan
153/// and the state it reaches. Adds every run's counts to `total`.
154/// Each run is IW+ with `oracle`'s relaxed plan when it is set.
155fn reach_subgoal<P, F>(
156 problem: &P,
157 state: &P::State,
158 max_k: usize,
159 mut oracle: Option<&mut F>,
160 is_subgoal: impl Fn(&P::State) -> bool + Copy,
161 total: &mut SearchResult<P::Action>,
162) -> Option<(Vec<P::Action>, P::State)>
163where
164 P: WidthProblem,
165 F: FnMut(&P::State, &mut Vec<u32>),
166{
167 for k in 1..=max_k {
168 let run_oracle = oracle.as_deref_mut();
169 let (r, end) = iw_plus_from(problem, state.clone(), k, is_subgoal, run_oracle);
170 total.expanded += r.expanded;
171 total.generated += r.generated;
172 if let (Some(subplan), Some(end)) = (r.plan, end) {
173 return Some((subplan, end));
174 }
175 }
176
177 None
178}