planning-heuristics 0.2.1

Heuristics for classical planning: delete relaxation (h_max, h_add, h_FF), red-black, landmarks, LM-cut, operator counting and abstraction adapters
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Red-black planning with a fixed number of black variables of fixed-size domains (Katz,
//! Hoffmann & Domshlak, ICAPS 2013, Theorem 1 and its tree search; Domshlak, Hoffmann & Katz,
//! AIJ 2015, Theorem 1).
//!
//! The black variables are compiled into one product variable (the papers' first step). The
//! search keeps the values of the product variable visited along its path as a sequence of
//! *groups*: within the last group the variable can switch freely (every value was reached
//! along a cycle of transitions whose red conditions stay true), so operators prevailed by
//! any of its values are usable. Each node closes the red facts under the usable operators
//! (a relaxed fixpoint, executed for real by moving inside the group), stops if the goal
//! holds, and otherwise branches over every transition leaving the group. A transition back
//! into an earlier group closes a cycle and merges all groups from there on, as the ICAPS
//! proof does with its aggregate values. Along one branch at most `k - 1` groups are added and
//! at most `k - 1` merges happen for `k` product values, so the depth is below `2k`.

use alloc::collections::VecDeque;
use alloc::vec::Vec;

use sas_plus::{Fact, OpId, State, Task, VarId};

use crate::Relaxation;

/// Most search nodes one evaluation may expand before giving up (the search is complete but
/// exponential in the product size).
const NODE_BUDGET: usize = 4096;

/// The outcome of one product search.
pub(super) enum Outcome {
    /// A red-black plan and its cost.
    Plan(Vec<OpId>, u64),
    /// The search space is exhausted: the red-black task, and so the task, is unsolvable.
    Unsolvable,
    /// The node budget ran out.
    Exhausted,
}

/// The product compilation of the black variables of one task.
#[derive(Clone, Debug)]
pub(super) struct Product {
    /// Whether each fact (by [`Relaxation::fact_id`]) belongs to a black variable.
    is_black_fact: Vec<bool>,
    /// For each operator, the fact ids of its red preconditions.
    red_pre: Vec<Vec<usize>>,
    /// For each operator, its cost.
    cost: Vec<u64>,
    /// For each operator, the product values its black preconditions allow, ascending.
    matching: Vec<Vec<u32>>,
    /// For each operator with black effects, the value each entry of `matching` moves to;
    /// empty for an operator without black effects.
    next: Vec<Vec<u32>>,
    /// For each product value, the transitions leaving it as `(operator, value reached)`.
    out: Vec<Vec<(OpId, u32)>>,
    /// The product values the goal allows.
    goal_values: Vec<bool>,
    /// The fact ids of the red goal facts.
    red_goal: Vec<usize>,
    /// The black variables, ascending, with the stride of each in a product value.
    strides: Vec<(VarId, u32)>,
}

/// One node of the search: a red-black state, the plan reaching it and the groups.
#[derive(Clone, Debug)]
struct Node {
    /// Whether each red fact holds (black facts are tracked by `current`).
    holds: Vec<bool>,
    /// The plan so far.
    plan: Vec<OpId>,
    /// Its cost.
    cost: u64,
    /// The groups of visited product values; the last is the one the variable is in.
    groups: Vec<Vec<u32>>,
    /// For each product value, the index of its group, if visited.
    group_of: Vec<Option<usize>>,
    /// The current product value.
    current: u32,
}

impl Product {
    /// Compiles the black variables `black` (ascending) of `task`.
    ///
    /// # Panics
    ///
    /// Panics if a variable of `black` is not a variable of `task`, or the product of their
    /// domain sizes does not fit a `u32`.
    pub fn new(task: &Task, relaxation: &Relaxation, black: &[VarId]) -> Self {
        let encoding = Encoding::new(task, black);
        let (matching, next) = encoding.transitions();
        let out = out_edges(encoding.size, &matching, &next);
        let goal_values = (0..encoding.size)
            .map(|x| encoding.allows(x, &task.goal))
            .collect();

        Self {
            is_black_fact: super::tables::black_fact_mask(task, relaxation, black),
            red_pre: task
                .operators
                .iter()
                .map(|op| encoding.red_fact_ids(relaxation, &op.pre))
                .collect(),
            cost: task.operators.iter().map(|op| u64::from(op.cost)).collect(),
            matching,
            next,
            out,
            goal_values,
            red_goal: encoding.red_fact_ids(relaxation, &task.goal),
            strides: encoding.strides,
        }
    }

    /// Searches a red-black plan from `state`.
    ///
    /// # Panics
    ///
    /// Panics if `state` has fewer values than the task has variables.
    pub fn search(&self, relaxation: &Relaxation, state: &State) -> Outcome {
        let mut holds = alloc::vec![false; relaxation.num_facts()];
        for fact in relaxation
            .state_facts(state)
            .chain(relaxation.free_facts().iter().copied())
        {
            holds[fact as usize] = true;
        }

        super::repair::close_axioms(relaxation, &mut holds);
        let current = self
            .strides
            .iter()
            .map(|&(v, stride)| state.0[v] * stride)
            .sum();
        let mut group_of = alloc::vec![None; self.out.len()];
        group_of[current as usize] = Some(0);
        let root = Node {
            holds,
            plan: Vec::new(),
            cost: 0,
            groups: alloc::vec![alloc::vec![current]],
            group_of,
            current,
        };
        let mut expanded = 0;

        match self.expand(relaxation, root, &mut expanded) {
            Some(node) => Outcome::Plan(node.plan, node.cost),
            None if expanded > NODE_BUDGET => Outcome::Exhausted,
            None => Outcome::Unsolvable,
        }
    }

    /// Closes `node` under the operators usable in its group, then returns it if the goal
    /// holds, or else the first plan found by branching over the transitions leaving the
    /// group. `None` if there is none or the budget ran out (then `expanded` exceeds it).
    fn expand(
        &self,
        relaxation: &Relaxation,
        mut node: Node,
        expanded: &mut usize,
    ) -> Option<Node> {
        *expanded += 1;
        if *expanded > NODE_BUDGET {
            return None;
        }

        self.close(relaxation, &mut node);
        if let Some(goal) = self.goal_value(&node) {
            self.move_within(relaxation, &mut node, goal);
            return Some(node);
        }

        let last = node.groups.len() - 1;
        for op in 0..self.matching.len() {
            if self.next[op].is_empty() || !self.red_pre_holds(&node, op) {
                continue;
            }

            for (&x, &y) in self.matching[op].iter().zip(&self.next[op]) {
                if node.group_of[x as usize] != Some(last)
                    || node.group_of[y as usize] == Some(last)
                {
                    continue;
                }

                let mut child = node.clone();
                self.move_within(relaxation, &mut child, x);
                self.apply(relaxation, &mut child, op, Some(y));
                enter(&mut child, y);
                if let Some(found) = self.expand(relaxation, child, expanded) {
                    return Some(found);
                }

                if *expanded > NODE_BUDGET {
                    return None;
                }
            }
        }

        None
    }

    /// Applies, until none adds a red fact, every operator usable in the last group: its red
    /// preconditions hold and it is prevailed by, or moves between, values of the group. The
    /// variable moves inside the group as needed.
    fn close(&self, relaxation: &Relaxation, node: &mut Node) {
        let mut changed = true;
        while changed {
            changed = false;
            for op in 0..self.matching.len() {
                if !self.red_pre_holds(node, op) || !self.adds_red(relaxation, node, op) {
                    continue;
                }

                let Some((from, to)) = self.usable_in_group(node, op) else {
                    continue;
                };

                self.move_within(relaxation, node, from);
                self.apply(relaxation, node, op, to);
                changed = true;
            }
        }
    }

    /// For `op` usable in the last group, the value to apply it from (the current one if
    /// possible) and, for a transition, the value it reaches.
    fn usable_in_group(&self, node: &Node, op: OpId) -> Option<(u32, Option<u32>)> {
        let last = Some(node.groups.len() - 1);
        let in_group = |x: u32| node.group_of[x as usize] == last;
        let candidates = self.matching[op]
            .iter()
            .enumerate()
            .filter(|&(i, &x)| in_group(x) && self.next[op].get(i).is_none_or(|&y| in_group(y)));
        let pick = candidates
            .clone()
            .find(|&(_, &x)| x == node.current)
            .or_else(|| candidates.clone().next())?;

        Some((*pick.1, self.next[op].get(pick.0).copied()))
    }

    /// A value of the last group the goal allows, if the red goal holds.
    fn goal_value(&self, node: &Node) -> Option<u32> {
        if !self.red_goal.iter().all(|&f| node.holds[f]) {
            return None;
        }

        node.groups
            .last()
            .expect("there is a group")
            .iter()
            .copied()
            .find(|&x| self.goal_values[x as usize])
    }

    /// Moves the product variable to `target` in the last group along a shortest path (in
    /// operators) of transitions inside the group whose red preconditions hold. Such a path
    /// exists: every group is strongly connected by transitions applied before.
    fn move_within(&self, relaxation: &Relaxation, node: &mut Node, target: u32) {
        if node.current == target {
            return;
        }

        let last = Some(node.groups.len() - 1);
        let mut via: Vec<Option<(u32, OpId)>> = alloc::vec![None; self.out.len()];
        let mut queue = VecDeque::from([node.current]);
        while let Some(x) = queue.pop_front() {
            if x == target {
                break;
            }

            for &(op, y) in &self.out[x as usize] {
                let fresh = y != node.current && via[y as usize].is_none();
                if fresh && node.group_of[y as usize] == last && self.red_pre_holds(node, op) {
                    via[y as usize] = Some((x, op));
                    queue.push_back(y);
                }
            }
        }

        let mut path = Vec::new();
        let mut value = target;
        while value != node.current {
            let (from, op) = via[value as usize].expect("groups are strongly connected");
            path.push((op, value));
            value = from;
        }

        for &(op, to) in path.iter().rev() {
            self.apply(relaxation, node, op, Some(to));
        }
    }

    /// Applies `op` in `node`: red effects whose red conditions hold are added (black
    /// conditions are preconditions, which hold), the product variable moves to `to`, and the
    /// red facts are closed under the axioms.
    fn apply(&self, relaxation: &Relaxation, node: &mut Node, op: OpId, to: Option<u32>) {
        let fired: Vec<usize> = relaxation
            .op_rules(op)
            .iter()
            .filter(|rule| !self.is_black_fact[rule.head])
            .filter(|rule| self.red_conditions_hold(node, &rule.conditions))
            .map(|rule| rule.head)
            .collect();
        for head in fired {
            node.holds[head] = true;
        }

        if let Some(to) = to {
            node.current = to;
        }

        super::repair::close_axioms(relaxation, &mut node.holds);
        node.plan.push(op);
        node.cost = node.cost.saturating_add(self.cost[op]);
    }

    /// Whether some red effect of `op` would add a fact not yet true.
    fn adds_red(&self, relaxation: &Relaxation, node: &Node, op: OpId) -> bool {
        relaxation.op_rules(op).iter().any(|rule| {
            !self.is_black_fact[rule.head]
                && !node.holds[rule.head]
                && self.red_conditions_hold(node, &rule.conditions)
        })
    }

    /// Whether the red facts of `conditions` hold.
    fn red_conditions_hold(&self, node: &Node, conditions: &[usize]) -> bool {
        conditions
            .iter()
            .all(|&c| self.is_black_fact[c] || node.holds[c])
    }

    /// Whether the red preconditions of `op` hold.
    fn red_pre_holds(&self, node: &Node, op: OpId) -> bool {
        self.red_pre[op].iter().all(|&f| node.holds[f])
    }
}

/// Records that the product variable entered `value` from the last group: a value of an
/// earlier group closes a cycle and merges that group and all later ones; a new value starts
/// a group.
fn enter(node: &mut Node, value: u32) {
    if let Some(i) = node.group_of[value as usize] {
        let merged: Vec<u32> = node.groups.drain(i..).flatten().collect();
        for &x in &merged {
            node.group_of[x as usize] = Some(i);
        }

        node.groups.push(merged);
    } else {
        node.group_of[value as usize] = Some(node.groups.len());
        node.groups.push(alloc::vec![value]);
    }
}

/// The mixed-radix encoding of the black variables' values as one product value.
struct Encoding<'t> {
    /// The task whose variables are encoded.
    task: &'t Task,
    /// The black variables, ascending, with the stride of each in a product value.
    strides: Vec<(VarId, u32)>,
    /// The number of product values (the product of the black domain sizes).
    size: u32,
}

impl<'t> Encoding<'t> {
    /// Assigns strides to the black variables `black` (ascending) of `task`.
    ///
    /// # Panics
    ///
    /// Panics if a variable of `black` is not a variable of `task`, or the product of their
    /// domain sizes does not fit a `u32`.
    fn new(task: &'t Task, black: &[VarId]) -> Self {
        let mut strides = Vec::with_capacity(black.len());
        let mut size: u32 = 1;
        for &v in black {
            strides.push((v, size));
            size = size
                .checked_mul(task.variables[v].domain_size)
                .expect("the product fits u32");
        }

        Self {
            task,
            strides,
            size,
        }
    }

    /// The stride of `v` in a product value, or `None` if `v` is red.
    fn stride(&self, v: VarId) -> Option<u32> {
        self.strides
            .binary_search_by_key(&v, |&(var, _)| var)
            .ok()
            .map(|i| self.strides[i].1)
    }

    /// The value of the black variable `v` in the product value `x`.
    ///
    /// # Panics
    ///
    /// Panics if `v` is red.
    fn digit(&self, x: u32, v: VarId) -> u32 {
        let stride = self.stride(v).expect("a black variable");

        x / stride % self.task.variables[v].domain_size
    }

    /// Whether the product value `x` agrees with every black fact of `facts`.
    fn allows(&self, x: u32, facts: &[Fact]) -> bool {
        facts
            .iter()
            .all(|f| self.stride(f.var).is_none() || self.digit(x, f.var) == f.value)
    }

    /// The fact ids of the red facts of `facts`.
    fn red_fact_ids(&self, relaxation: &Relaxation, facts: &[Fact]) -> Vec<usize> {
        facts
            .iter()
            .filter(|f| self.stride(f.var).is_none())
            .map(|&f| relaxation.fact_id(f))
            .collect()
    }

    /// The product value `x` with the black facts `black_eff` applied.
    fn apply(&self, x: u32, black_eff: &[&Fact]) -> u32 {
        black_eff.iter().fold(x, |y, f| {
            let stride = self.stride(f.var).expect("a black variable");

            y - self.digit(y, f.var) * stride + f.value * stride
        })
    }

    /// For each operator of the task, the product values its black preconditions allow
    /// (ascending), and the value each of them moves to (empty without black effects).
    fn transitions(&self) -> (Vec<Vec<u32>>, Vec<Vec<u32>>) {
        let operators = &self.task.operators;
        let mut matching = Vec::with_capacity(operators.len());
        let mut next = Vec::with_capacity(operators.len());
        for op in operators {
            let values: Vec<u32> = (0..self.size)
                .filter(|&x| self.allows(x, &op.pre))
                .collect();
            let black_eff: Vec<&Fact> = op
                .eff
                .iter()
                .map(|e| &e.fact)
                .filter(|f| self.stride(f.var).is_some())
                .collect();
            let moved = if black_eff.is_empty() {
                Vec::new()
            } else {
                values.iter().map(|&x| self.apply(x, &black_eff)).collect()
            };

            matching.push(values);
            next.push(moved);
        }

        (matching, next)
    }
}

/// For each of the `size` product values, the transitions leaving it as
/// `(operator, value reached)`, from the per-operator `matching` and `next` values.
fn out_edges(size: u32, matching: &[Vec<u32>], next: &[Vec<u32>]) -> Vec<Vec<(OpId, u32)>> {
    let mut out = alloc::vec![Vec::new(); size as usize];
    for (op, (from, to)) in matching.iter().zip(next).enumerate() {
        for (&x, &y) in from.iter().zip(to) {
            if x != y {
                out[x as usize].push((op, y));
            }
        }
    }

    out
}