decoupled-search 0.3.1

Decoupled (star-topology) search for factored SAS+ planning: star factorings, cost-optimal and greedy search, dominance pruning and decoupled heuristics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! One compiled leaf factor: ranking of leaf states and leaf reachability.
//!
//! A leaf state is a mixed-radix rank over the leaf's state variables. Its
//! *slots* are those variables, followed by the derived variables attached
//! to the leaf, whose values depend on the rank and the current center. A
//! [`View`] fixes the center, evaluates the derived slots once for every
//! rank, and offers all rank-level operations.

use alloc::collections::BinaryHeap;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::Reverse;

use sas_plus::{Fact, OpId, Value, VarId};

use super::Prices;
use super::axioms::{Axioms, Scope};

/// The cost of reaching a leaf state; [`UNREACHED`] when it is not reached.
pub(crate) type Price = u64;

/// The price of a leaf state that is not reached.
pub(crate) const UNREACHED: Price = Price::MAX;

/// How a leaf state was first reached at its final price during a closure:
/// from leaf state `from` (a rank) by leaf action `op`.
pub(crate) type Step = Option<(usize, OpId)>;

/// A condition or assignment on one leaf slot.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct LeafFact {
    /// Position in the leaf: a variable of [`crate::Factoring::leaves`]
    /// below [`Leaf::vars`]`.len()`, an attached derived variable above.
    pub(crate) slot: usize,
    /// The value, already widened for rank arithmetic.
    pub(crate) value: usize,
}

/// One effect of an operator on a leaf variable, possibly conditional.
#[derive(Clone, Debug)]
pub(crate) struct LeafEffect {
    /// Effect conditions on the center; `var` is the center slot. Evaluated
    /// on the center the operator is applied in.
    pub(crate) center_cond: Vec<Fact>,
    /// Effect conditions on this leaf, evaluated on the leaf state the
    /// operator is applied in.
    pub(crate) cond: Vec<LeafFact>,
    /// The assignment; its slot is always a state variable of the leaf.
    pub(crate) fact: LeafFact,
}

impl LeafEffect {
    /// Whether the effect has conditions on the center.
    pub(crate) fn reads_center(&self) -> bool {
        !self.center_cond.is_empty()
    }
}

/// The part of an operator that reads or writes one leaf.
#[derive(Clone, Debug)]
pub(crate) struct LeafPart {
    /// Preconditions on this leaf's slots.
    pub(crate) pre: Vec<LeafFact>,
    /// Effects on this leaf's variables, in operator order: a later firing
    /// effect on the same variable wins.
    pub(crate) eff: Vec<LeafEffect>,
}

impl LeafPart {
    /// Whether any effect depends on the center.
    pub(crate) fn reads_center(&self) -> bool {
        self.eff.iter().any(LeafEffect::reads_center)
    }
}

/// An operator whose effects all lie in one leaf and whose preconditions and
/// effect conditions lie in the center and that leaf.
#[derive(Clone, Debug)]
pub(crate) struct LeafAction {
    /// The operator in the original task.
    pub(crate) op: OpId,
    /// The operator cost.
    pub(crate) cost: Price,
    /// Preconditions on the center; `var` is the center slot, not the task
    /// variable. Sorted, so that subset tests are linear.
    pub(crate) center_pre: Vec<Fact>,
    /// Preconditions and effects on the leaf.
    pub(crate) part: LeafPart,
}

/// A leaf factor whose states are mixed-radix ranks over its variables.
#[derive(Clone, Debug)]
pub(crate) struct Leaf {
    /// The task variable of each state-variable slot.
    pub(crate) vars: Vec<VarId>,
    /// Rank weight of each slot: the product of the domain sizes of all earlier slots.
    pub(crate) strides: Vec<usize>,
    /// Domain size of each slot.
    pub(crate) domains: Vec<usize>,
    /// Number of leaf states, the product of [`Leaf::domains`]. At most
    /// [`crate::MAX_LEAF_STATES`].
    pub(crate) size: usize,
    /// Rank of the leaf's part of the initial state.
    pub(crate) initial: usize,
    /// The derived variables attached to this leaf, by slot after
    /// [`Leaf::vars`], with the rules that evaluate them.
    pub(crate) derived: Scope,
    /// The task goal restricted to this leaf.
    pub(crate) goal: Vec<LeafFact>,
    /// All leaf actions of this leaf.
    pub(crate) actions: Vec<LeafAction>,
    /// Leaf transitions removed by simulation-based leaf reduction, by
    /// `rank * actions.len() + action`; empty if none are. Leaf closure
    /// skips them.
    pub(crate) disabled: Vec<bool>,
    /// The center slots leaf closure reads (center preconditions of leaf
    /// actions and center conditions of their effects), sorted; `None` if
    /// derived variables attached to the leaf read the center too. Filled
    /// by [`Leaf::index_center_reads`] once the actions are in.
    pub(crate) center_reads: Option<Vec<usize>>,
}

impl Leaf {
    /// The leaf over state variables `vars` with `domains` and the initial
    /// leaf state `initial` (one value per variable); attached derived
    /// variables, goal and actions are filled in later. `None` if it has
    /// more than [`crate::MAX_LEAF_STATES`] states.
    pub(crate) fn new(vars: Vec<VarId>, domains: Vec<usize>, initial: &[usize]) -> Option<Self> {
        let mut strides = Vec::with_capacity(domains.len());
        let mut size = 1_usize;
        for &d in &domains {
            strides.push(size);
            size = size
                .checked_mul(d)
                .filter(|&s| s <= crate::MAX_LEAF_STATES)?;
        }

        let initial = initial.iter().zip(&strides).map(|(v, s)| v * s).sum();
        Some(Self {
            vars,
            strides,
            domains,
            size,
            initial,
            derived: Scope::default(),
            goal: Vec::new(),
            actions: Vec::new(),
            disabled: Vec::new(),
            center_reads: None,
        })
    }

    /// Records which center slots leaf closure reads, from the leaf
    /// actions; call after adding the last one.
    pub(crate) fn index_center_reads(&mut self) {
        if !self.derived.is_empty() {
            self.center_reads = None;
            return;
        }

        let pre = self.actions.iter().flat_map(|a| &a.center_pre);
        let cond = (self.actions.iter())
            .flat_map(|a| &a.part.eff)
            .flat_map(|e| &e.center_cond);
        let mut slots: Vec<usize> = pre.chain(cond).map(|f| f.var).collect();
        slots.sort_unstable();
        slots.dedup();
        self.center_reads = Some(slots);
    }

    /// Whether leaf closure may differ between centers `before` and
    /// `after`: some slot it reads changed, or it is not indexed.
    pub(crate) fn sees_change(&self, before: &[Value], after: &[Value]) -> bool {
        self.center_reads
            .as_ref()
            .is_none_or(|slots| slots.iter().any(|&s| before[s] != after[s]))
    }

    /// Price vector with only the initial leaf state reached, at price 0.
    pub(crate) fn initial_prices(&self) -> Vec<Price> {
        let mut prices = vec![UNREACHED; self.size];
        prices[self.initial] = 0;
        prices
    }

    /// The value of state-variable slot `slot` in leaf state `rank`.
    pub(crate) fn var_value(&self, rank: usize, slot: usize) -> usize {
        rank / self.strides[slot] % self.domains[slot]
    }

    /// Leaf state `rank` with state-variable slot `slot` set to `value`.
    fn with_value(&self, rank: usize, slot: usize, value: usize) -> usize {
        rank - self.var_value(rank, slot) * self.strides[slot] + value * self.strides[slot]
    }

    /// The view of this leaf under center state `center` (values by center
    /// slot, as in [`crate::Factoring::center`] order without attached
    /// derived variables). Evaluates the attached derived variables for
    /// every rank, which costs `O(size * rules)` if there are any.
    pub(crate) fn view<'a>(&'a self, axioms: &Axioms, center: &'a [Value]) -> View<'a> {
        let derived = if self.derived.is_empty() {
            Vec::new()
        } else {
            self.derived_values(axioms, center)
        };

        View {
            leaf: self,
            center,
            derived,
        }
    }

    /// The view of a leaf that does not read the center (see
    /// [`Leaf::reads_center`]), valid under every center. Center
    /// preconditions of leaf actions are not checked by its rank-level
    /// operations, only by [`View::close`], which must not be called on it
    /// ([`View::close_by`] may, with its own choice of actions).
    pub(crate) fn static_view(&self) -> View<'_> {
        debug_assert!(self.derived.is_empty(), "derived slots read the center");
        View {
            leaf: self,
            center: &[],
            derived: Vec::new(),
        }
    }

    /// Whether the leaf's behaviour depends on the center beyond the center
    /// preconditions of its leaf actions: an effect on it (of a leaf action
    /// or of a global action in `parts`) has center conditions, or a derived
    /// variable is attached to it. Dominance analyses that hold "for every
    /// center" fall back to plain price comparison on such leaves.
    pub(crate) fn reads_center<'p>(&self, parts: impl IntoIterator<Item = &'p LeafPart>) -> bool {
        !self.derived.is_empty()
            || self.actions.iter().any(|a| a.part.reads_center())
            || parts.into_iter().any(LeafPart::reads_center)
    }

    /// The values of the attached derived variables for every rank under
    /// `center`, rank-major.
    fn derived_values(&self, axioms: &Axioms, center: &[Value]) -> Vec<usize> {
        let mut scratch = axioms.scratch(center);
        let mut out = Vec::with_capacity(self.size * self.derived.vars.len());
        for rank in 0..self.size {
            for (slot, &var) in self.vars.iter().enumerate() {
                scratch[var] = u32::try_from(self.var_value(rank, slot)).expect("values fit u32");
            }

            axioms.evaluate(&self.derived, &mut scratch);
            out.extend(self.derived.vars.iter().map(|&v| super::widen(scratch[v])));
        }

        out
    }
}

/// A [`Leaf`] under a fixed center state.
#[derive(Debug)]
pub(crate) struct View<'a> {
    /// The leaf.
    pub(crate) leaf: &'a Leaf,
    /// The center state, by center slot.
    center: &'a [Value],
    /// The value of every attached derived variable in every rank,
    /// rank-major; empty if the leaf has none.
    derived: Vec<usize>,
}

impl View<'_> {
    /// The value of slot `slot` in leaf state `rank`.
    fn value(&self, rank: usize, slot: usize) -> usize {
        let vars = self.leaf.vars.len();
        if slot < vars {
            self.leaf.var_value(rank, slot)
        } else {
            self.derived[rank * self.leaf.derived.vars.len() + slot - vars]
        }
    }

    /// Whether leaf state `rank` satisfies all `facts`.
    pub(crate) fn satisfies(&self, rank: usize, facts: &[LeafFact]) -> bool {
        facts.iter().all(|f| self.value(rank, f.slot) == f.value)
    }

    /// Whether all center `facts` (by center slot) hold.
    fn center_holds(&self, facts: &[Fact]) -> bool {
        facts.iter().all(|f| self.center[f.var] == f.value)
    }

    /// The leaf state after applying the effects `eff` to `rank`: every
    /// effect whose conditions hold in `rank` and the center fires, in
    /// order.
    pub(crate) fn apply(&self, rank: usize, eff: &[LeafEffect]) -> usize {
        eff.iter()
            .filter(|e| self.center_holds(&e.center_cond) && self.satisfies(rank, &e.cond))
            .fold(rank, |r, e| {
                self.leaf.with_value(r, e.fact.slot, e.fact.value)
            })
    }

    /// The prices after a global action with leaf part `part`, applied under
    /// this view's center: reached states that satisfy its preconditions,
    /// mapped through its effects, keeping the cheapest price when two states
    /// map to the same one.
    pub(crate) fn transfer(&self, part: &LeafPart, prices: &[Price]) -> Vec<Price> {
        let mut next = vec![UNREACHED; self.leaf.size];
        for (rank, &price) in prices.iter().enumerate() {
            if price == UNREACHED || !self.satisfies(rank, &part.pre) {
                continue;
            }

            let target = self.apply(rank, &part.eff);
            next[target] = next[target].min(price);
        }

        next
    }

    /// A reached leaf state of `prices` that [`View::transfer`] maps onto
    /// `seed` at exactly `price`.
    ///
    /// # Panics
    ///
    /// Panics if there is none, which means `price` did not come from a
    /// transfer of `prices` under this view.
    pub(crate) fn transfer_source(
        &self,
        part: &LeafPart,
        prices: &[Price],
        seed: usize,
        price: Price,
    ) -> usize {
        (0..self.leaf.size)
            .find(|&rank| {
                prices[rank] == price
                    && self.satisfies(rank, &part.pre)
                    && self.apply(rank, &part.eff) == seed
            })
            .expect("a transferred price has a source leaf state")
    }

    /// Leaf reachability under the view's center: Dijkstra from all reached
    /// states of `prices` over the leaf actions whose center preconditions
    /// hold. Lowers `prices` in place and returns the step that set each
    /// improved price (`None` for unchanged states).
    ///
    /// Steps change only on strict improvement, so they form a forest even
    /// with zero-cost actions. Without `costs`, every action counts as free,
    /// so only reachability is recorded.
    pub(crate) fn close(&self, prices: &mut [Price], costs: bool) -> Vec<Step> {
        self.close_by(prices, costs, |a| {
            self.center_holds(&self.leaf.actions[a].center_pre)
        })
    }

    /// [`View::close`] over the leaf actions whose index `usable` accepts,
    /// whatever the view's center.
    pub(crate) fn close_by(
        &self,
        prices: &mut [Price],
        costs: bool,
        usable: impl Fn(usize) -> bool,
    ) -> Vec<Step> {
        let count = self.leaf.actions.len();
        let actions: Vec<(usize, &LeafAction)> = self
            .leaf
            .actions
            .iter()
            .enumerate()
            .filter(|&(i, _)| usable(i))
            .collect();
        let disabled = |rank: usize, index: usize| {
            self.leaf
                .disabled
                .get(rank * count + index)
                .is_some_and(|&d| d)
        };
        let mut steps = vec![None; self.leaf.size];
        let mut open: BinaryHeap<Reverse<(Price, usize)>> = prices
            .iter()
            .enumerate()
            .filter(|&(_, &p)| p != UNREACHED)
            .map(|(rank, &p)| Reverse((p, rank)))
            .collect();

        while let Some(Reverse((price, rank))) = open.pop() {
            if price > prices[rank] {
                continue;
            }

            for &(_, action) in actions
                .iter()
                .filter(|&&(i, a)| !disabled(rank, i) && self.satisfies(rank, &a.part.pre))
            {
                let target = self.apply(rank, &action.part.eff);
                let cost = if costs { action.cost } else { 0 };
                let reached = price.saturating_add(cost);
                if reached < prices[target] {
                    prices[target] = reached;
                    steps[target] = Some((rank, action.op));
                    open.push(Reverse((reached, target)));
                }
            }
        }

        steps
    }

    /// Lowers `out[offsets[var] + value]` to the cheapest price among the
    /// `reached` leaf states (as `(rank, price)`) with `var = value`, for
    /// every leaf variable and attached derived variable.
    pub(crate) fn lower_fact_prices(
        &self,
        reached: impl Iterator<Item = (usize, Price)>,
        offsets: &[usize],
        out: &mut [Price],
    ) {
        let slots = self.leaf.vars.iter().chain(&self.leaf.derived.vars);
        for (rank, price) in reached {
            for (slot, &var) in slots.clone().enumerate() {
                let fact = &mut out[offsets[var] + self.value(rank, slot)];
                *fact = (*fact).min(price);
            }
        }
    }

    /// Whether leaf state `rank` satisfies the leaf goal.
    pub(crate) fn is_goal(&self, rank: usize) -> bool {
        self.satisfies(rank, &self.leaf.goal)
    }

    /// The cheapest price of a reached leaf state that satisfies the leaf
    /// goal, with that state's rank; `None` if no goal state is reached.
    pub(crate) fn cheapest_goal(&self, prices: &Prices) -> Option<(Price, usize)> {
        prices
            .iter()
            .filter(|&(rank, _)| self.is_goal(rank))
            .map(|(rank, p)| (p, rank))
            .min()
    }
}