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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! Which variables are black, and the checks that keep red-black plan repair simple.

mod invertible;
mod strategy;

use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::fmt;

use sas_plus::{CausalGraph, State, Task, VarId};

use crate::lm_cut::contexts::{self, Split};

use invertible::is_rse_invertible;
pub use strategy::Strategy;

/// A choice of black variables for [`crate::RedBlack`]; every other variable is red.
///
/// Black variables must be state variables that no axiom mentions and, after conditional
/// effects are compiled away (see below), no conditional effect mentions (as condition or as
/// effect), and, for [`Painting::new`], no two may be linked in the
/// causal graph ([`Painting::dag`] allows an acyclic causal graph on them). So an operator
/// changes at most one black variable, and an operator that changes a black variable has no
/// precondition on another: moving one black variable never disturbs another, which is what
/// lets the repair insert a path in one variable's domain transition graph at a time. An
/// operator that changes only red variables may still require several black values; the
/// repair moves each in turn.
///
/// The repair is guaranteed to succeed (never to fall back to `h_FF`) when every black
/// variable is also RSE-invertible (see [`Painting::greedy`]): this is the tractable fragment
/// of Katz, Hoffmann & Domshlak (AAAI 2013, Corollary 2 and Theorem 2) and Domshlak, Hoffmann
/// & Katz (AIJ 2015, Theorem 11). [`Painting::new`] and [`Painting::dag`] do not check it; every
/// strategy constructor paints only RSE-invertible variables.
///
/// Conditional effects are compiled away first, in the spirit of the "multiply out"
/// compilation Domshlak, Hoffmann & Katz name for their planners (AIJ 2015, Section 7.3,
/// Table 9): each operator is split into one copy per assignment of the variables its effect
/// conditions mention, whose effects are unconditional. The split is exact (every value of
/// each such variable, no "any other" copy), so the compiled task has exactly the task's
/// plans and a black variable cannot skip an effect it really triggers. All checks run on the compiled task, so a variable that only appears in
/// conditional effects can be black; [`crate::RedBlack`] then repairs on the compiled task.
/// Variables of an operator with more than 64 contexts stay ineligible.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct Painting {
    /// The black variables, ascending.
    black: Vec<VarId>,
    /// Whether this is a [`Painting::bounded`] painting, solved by product search instead of
    /// relaxed plan repair.
    bounded: bool,
}

/// The largest product of the domain sizes of the black variables of a
/// [`Painting::bounded`] painting.
pub const MAX_BOUNDED_PRODUCT: usize = 256;

/// Which end of the causal graph [`Painting::causal_graph_level`] keeps black.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CausalLevel {
    /// Keep the variables that others depend on black, such as the position of a vehicle
    /// that loading and unloading operators need: paint red from the leaves up (strategy L of
    /// Domshlak, Hoffmann & Katz 2015).
    Top,
    /// Keep the variables near the leaves black: paint red from the roots down (their inverse
    /// strategy, a sanity check). The leaves themselves are always red.
    Bottom,
}

/// The tractable fragment a painting strategy targets: the shape of the causal graph induced
/// on the black variables.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Fragment {
    /// No arc between two black variables, as [`Painting::new`] requires (Katz, Hoffmann &
    /// Domshlak, AAAI 2013, Corollary 2; configuration E of Domshlak, Hoffmann & Katz 2015).
    Arcless,
    /// An acyclic black causal graph, as [`Painting::dag`] requires (AAAI 2013, Theorem 2;
    /// AIJ 2015, Theorem 11; configuration D).
    Dag,
}

/// Why a set of variables cannot be painted black; see [`Painting::new`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PaintingError {
    /// The variable does not exist in the task.
    UnknownVariable(VarId),
    /// The variable is derived; derived variables stay red.
    Derived(VarId),
    /// An axiom has a condition on the variable.
    InAxiom(VarId),
    /// A conditional effect that is too large to compile away (more than 64 contexts) sets
    /// the variable or has a condition on it.
    ConditionalEffect(VarId),
    /// The two black variables are linked in the causal graph.
    Dependent(VarId, VarId),
    /// The causal graph on the black variables has a cycle through these two (one arc of it);
    /// see [`Painting::dag`].
    Cyclic(VarId, VarId),
    /// The product of the black domain sizes exceeds [`MAX_BOUNDED_PRODUCT`]; see
    /// [`Painting::bounded`].
    TooLarge,
}

impl fmt::Display for PaintingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::UnknownVariable(v) => write!(f, "variable {v} does not exist"),
            Self::Derived(v) => write!(f, "variable {v} is derived"),
            Self::InAxiom(v) => write!(f, "variable {v} appears in an axiom"),
            Self::ConditionalEffect(v) => {
                write!(
                    f,
                    "variable {v} appears in a conditional effect with too many contexts to compile"
                )
            }
            Self::Dependent(u, v) => {
                write!(f, "variables {u} and {v} are linked in the causal graph")
            }
            Self::Cyclic(u, v) => {
                write!(
                    f,
                    "variables {u} and {v} lie on a cycle of the black causal graph"
                )
            }
            Self::TooLarge => write!(
                f,
                "the black domain sizes multiply to more than {MAX_BOUNDED_PRODUCT}"
            ),
        }
    }
}

impl core::error::Error for PaintingError {}

impl Painting {
    /// All variables red: red-black planning degenerates to the delete relaxation, and
    /// [`crate::RedBlack`] equals `h_FF`.
    #[must_use]
    pub fn all_red() -> Self {
        Self::default()
    }

    /// Paints exactly `black` black, after checking each variable is eligible and no two are
    /// linked in the causal graph.
    ///
    /// # Errors
    ///
    /// - [`PaintingError::UnknownVariable`] if a variable is not in `task`;
    /// - [`PaintingError::Derived`], [`PaintingError::InAxiom`] or
    ///   [`PaintingError::ConditionalEffect`] if a variable is not eligible;
    /// - [`PaintingError::Dependent`] for the first two linked variables.
    pub fn new(task: &Task, black: Vec<VarId>) -> Result<Self, PaintingError> {
        let task = &*compiled(task);
        let black = eligible(task, black)?;

        let graph = CausalGraph::new(task);
        for (i, &u) in black.iter().enumerate() {
            if let Some(&v) = black[i + 1..].iter().find(|&&v| linked(&graph, u, v)) {
                return Err(PaintingError::Dependent(u, v));
            }
        }

        Ok(Self {
            black,
            bounded: false,
        })
    }

    /// Paints exactly `black` black, allowing causal-graph arcs between black variables as long
    /// as the causal graph on them is acyclic (the DAG fragment of Katz, Hoffmann & Domshlak
    /// 2013). An operator then changes at most one black variable (two would link them both
    /// ways), and the repair moves a black variable's causal-graph ancestors on demand before
    /// each transition that needs them, descendants first, so a move never disturbs a value
    /// already in place below it (Figure 7 of Domshlak, Hoffmann & Katz 2015). With
    /// RSE-invertible black variables this always succeeds, but the plan may be exponentially
    /// long in the number of black variables (their Proposition 2); the repair has an operator
    /// budget and falls back to `h_FF` when it runs out, or when a variable is not invertible
    /// and cannot move back.
    ///
    /// # Errors
    ///
    /// - [`PaintingError::UnknownVariable`], [`PaintingError::Derived`],
    ///   [`PaintingError::InAxiom`] or [`PaintingError::ConditionalEffect`] as for
    ///   [`Painting::new`];
    /// - [`PaintingError::Cyclic`] for an arc on a cycle of the black causal graph.
    ///
    /// # Examples
    ///
    /// ```
    /// use planning_heuristics::{Painting, PaintingError};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // Moving `b` needs `a = 1`: an arc a → b, fine for a DAG painting.
    /// let task = Task {
    ///     variables: vec![Variable::new("a", 2), Variable::new("b", 2)],
    ///     operators: vec![
    ///         Operator {
    ///             name: "a".into(),
    ///             pre: vec![],
    ///             eff: vec![Effect::new(Fact::new(0, 1))],
    ///             cost: 1,
    ///         },
    ///         Operator {
    ///             name: "b".into(),
    ///             pre: vec![Fact::new(0, 1)],
    ///             eff: vec![Effect::new(Fact::new(1, 1))],
    ///             cost: 1,
    ///         },
    ///     ],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![Fact::new(1, 1)],
    ///     axioms: vec![],
    /// };
    ///
    /// assert_eq!(Painting::new(&task, vec![0, 1]), Err(PaintingError::Dependent(0, 1)));
    /// assert_eq!(Painting::dag(&task, vec![0, 1]).unwrap().black(), [0, 1]);
    /// ```
    pub fn dag(task: &Task, black: Vec<VarId>) -> Result<Self, PaintingError> {
        let task = &*compiled(task);
        let black = eligible(task, black)?;

        match cycle_arc(&CausalGraph::new(task), &black) {
            Some((u, v)) => Err(PaintingError::Cyclic(u, v)),
            None => Ok(Self {
                black,
                bounded: false,
            }),
        }
    }

    /// A greedy DAG painting of this crate (not one of the papers'): the candidates and order
    /// of [`Painting::greedy`], each painted black unless it closes a cycle in the causal graph
    /// on the variables painted before. Valid for [`Painting::dag`].
    #[must_use]
    pub fn greedy_dag(task: &Task) -> Self {
        let task = &*compiled(task);
        let mut candidates = repairable(task);
        candidates.sort_by_key(|&v| (task.variables[v].domain_size, v));
        let graph = CausalGraph::new(task);
        let mut black: Vec<VarId> = Vec::new();
        for v in candidates {
            let pos = black.binary_search(&v).unwrap_err();
            black.insert(pos, v);
            if cycle_arc(&graph, &black).is_some() {
                black.remove(pos);
            }
        }

        Self {
            black,
            bounded: false,
        }
    }

    /// A greedy painting of this crate (not one of the papers'): among the eligible
    /// RSE-invertible variables, paint black in order of ascending domain size, then variable
    /// id, each one not linked in the causal graph to one painted before. The repair of such
    /// a painting never falls back to `h_FF`.
    ///
    /// A variable is RSE-invertible (relaxed side effects invertible; Katz, Hoffmann &
    /// Domshlak 2013) if every arc `d → d'` of its domain transition graph has an inverse arc
    /// `d' → d` whose conditions on other variables are among the conditions and effects of
    /// the original arc. Moving a black variable back is then always possible once it moved.
    #[must_use]
    pub fn greedy(task: &Task) -> Self {
        let task = &*compiled(task);
        let mut candidates = repairable(task);
        candidates.sort_by_key(|&v| (task.variables[v].domain_size, v));

        Self::independent(&CausalGraph::new(task), candidates)
    }

    /// The causal graph level strategy (L, or its inverse) of Domshlak, Hoffmann & Katz
    /// (2015, Section 6.1), for `fragment`.
    ///
    /// Follows their Figure 8: the variables that are ineligible, not RSE-invertible (see
    /// [`Painting::greedy`]) or leaves of the causal graph start red; while the black causal
    /// graph is not in `fragment`, the black variable with the highest causal graph level
    /// (closest to the leaves) is painted red, or with [`CausalLevel::Bottom`] the one with the
    /// lowest; finally the variables painted red are tried black again, last first, and kept
    /// black if the graph stays in `fragment`. The result is maximal.
    ///
    /// The level of a variable is the length of the longest path to it from a source of the
    /// condensed causal graph (its strongly connected components); ties go to the higher
    /// variable id for "highest" and the lower for "lowest", which follows the causal graph
    /// order a Fast Downward translation numbers its variables in.
    ///
    /// # Examples
    ///
    /// ```
    /// use planning_heuristics::{CausalLevel, Fragment, Painting};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // A truck (variable 0) carries a package (variable 1: at a, at b, in the truck) from a
    /// // to b and must end at a again.
    /// let op = |name: &str, pre: Vec<Fact>, eff: Fact| Operator {
    ///     name: name.into(),
    ///     pre,
    ///     eff: vec![Effect::new(eff)],
    ///     cost: 1,
    /// };
    /// let (t, p) = (|v| Fact::new(0, v), |v| Fact::new(1, v));
    /// let task = Task {
    ///     variables: vec![Variable::new("truck", 2), Variable::new("package", 3)],
    ///     operators: vec![
    ///         op("drive-a-b", vec![t(0)], t(1)),
    ///         op("drive-b-a", vec![t(1)], t(0)),
    ///         op("load-a", vec![t(0), p(0)], p(2)),
    ///         op("load-b", vec![t(1), p(1)], p(2)),
    ///         op("unload-a", vec![t(0), p(2)], p(0)),
    ///         op("unload-b", vec![t(1), p(2)], p(1)),
    ///     ],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![p(1), t(0)],
    ///     axioms: vec![],
    /// };
    ///
    /// // The package is a leaf of the causal graph, so it is red under every strategy.
    /// let top = Painting::causal_graph_level(&task, CausalLevel::Top, Fragment::Arcless);
    /// assert_eq!(top.black(), [0]);
    /// ```
    #[must_use]
    pub fn causal_graph_level(task: &Task, keep: CausalLevel, fragment: Fragment) -> Self {
        Self::with_strategy(task, &task.initial_state(), Strategy::Level(keep), fragment)
    }

    /// The incident arcs strategy (A) of Domshlak, Hoffmann & Katz (2015, Section 6.1; also
    /// Katz, Hoffmann & Domshlak, AAAI 2013), for `fragment`: Figure 8 as in
    /// [`Painting::causal_graph_level`], painting red the black variable with the most causal
    /// graph arcs to other black variables (each direction counts), ties to the smaller
    /// domain, then to strategy L. This aims to paint few variables red.
    ///
    /// # Examples
    ///
    /// ```
    /// use planning_heuristics::{Fragment, Painting};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // A hub (variable 0) both needs and is needed by spokes 1 and 2; the hub has the most
    /// // arcs (four) and turns red, the spokes are not linked and stay black.
    /// let flip = |var: usize, from: u32, need: Vec<Fact>| {
    ///     let mut pre = vec![Fact::new(var, from)];
    ///     pre.extend(need);
    ///     Operator {
    ///         name: format!("flip{var}-{from}"),
    ///         pre,
    ///         eff: vec![Effect::new(Fact::new(var, 1 - from))],
    ///         cost: 1,
    ///     }
    /// };
    /// let mut operators = Vec::new();
    /// for from in 0..2 {
    ///     operators.push(flip(0, from, vec![Fact::new(1, 0), Fact::new(2, 0)]));
    ///     operators.push(flip(1, from, vec![Fact::new(0, 0)]));
    ///     operators.push(flip(2, from, vec![Fact::new(0, 0)]));
    /// }
    ///
    /// let task = Task {
    ///     variables: (0..3).map(|i| Variable::new(format!("v{i}"), 2)).collect(),
    ///     operators,
    ///     initial: State(vec![0, 0, 0]),
    ///     goal: vec![Fact::new(1, 1)],
    ///     axioms: vec![],
    /// };
    ///
    /// assert_eq!(Painting::incident_arcs(&task, Fragment::Arcless).black(), [1, 2]);
    /// ```
    #[must_use]
    pub fn incident_arcs(task: &Task, fragment: Fragment) -> Self {
        Self::with_strategy(task, &task.initial_state(), Strategy::MostArcs, fragment)
    }

    /// The conflict-directed strategy (C) of Domshlak, Hoffmann & Katz (2015, Section 6.1;
    /// also Katz, Hoffmann & Domshlak, AAAI 2013), for `fragment`: Figure 8 as in
    /// [`Painting::causal_graph_level`], painting red the black variable with the fewest
    /// conflicts, ties to strategy L, so the most critical variables stay black.
    ///
    /// A *conflict* of a variable is an action of the `h_FF` relaxed plan of `state` (the
    /// papers use the initial state) whose precondition on it is violated when the plan is
    /// executed with only that variable black; the value is then assumed moved to the
    /// precondition. If `state` is a dead end, every variable has no conflicts and the
    /// strategy is L.
    ///
    /// # Panics
    ///
    /// Panics if `state` has fewer values than the task has variables.
    ///
    /// # Examples
    ///
    /// ```
    /// use planning_heuristics::{Fragment, HFf, Painting, RedBlack};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // A truck (variable 0) carries a package (variable 1: at a, at b, in the truck) from a
    /// // to b and must end at a again.
    /// let op = |name: &str, pre: Vec<Fact>, eff: Fact| Operator {
    ///     name: name.into(),
    ///     pre,
    ///     eff: vec![Effect::new(eff)],
    ///     cost: 1,
    /// };
    /// let (t, p) = (|v| Fact::new(0, v), |v| Fact::new(1, v));
    /// let task = Task {
    ///     variables: vec![Variable::new("truck", 2), Variable::new("package", 3)],
    ///     operators: vec![
    ///         op("drive-a-b", vec![t(0)], t(1)),
    ///         op("drive-b-a", vec![t(1)], t(0)),
    ///         op("load-a", vec![t(0), p(0)], p(2)),
    ///         op("load-b", vec![t(1), p(1)], p(2)),
    ///         op("unload-a", vec![t(0), p(2)], p(0)),
    ///         op("unload-b", vec![t(1), p(2)], p(1)),
    ///     ],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![p(1), t(0)],
    ///     axioms: vec![],
    /// };
    /// let state = task.initial_state();
    ///
    /// // The truck stays black and must really drive back to a.
    /// let painting = Painting::conflict_directed(&task, &state, Fragment::Arcless);
    /// assert_eq!(painting.black(), [0]);
    /// assert_eq!(HFf::new(&task).value(&state), Some(3));
    /// assert_eq!(RedBlack::with_painting(&task, &painting).value(&state), Some(4));
    /// ```
    #[must_use]
    pub fn conflict_directed(task: &Task, state: &State, fragment: Fragment) -> Self {
        Self::with_strategy(task, state, Strategy::FewestConflicts, fragment)
    }

    /// The painting of `task` for `fragment` by `strategy`, following Figure 8 of Domshlak,
    /// Hoffmann & Katz (2015) as described at [`Painting::causal_graph_level`]; the conflict
    /// strategies use the relaxed plan of `state` (the paper: the initial state).
    ///
    /// # Panics
    ///
    /// Panics if `state` has fewer values than the task has variables, or
    /// [`Strategy::Mixed`] has a weight above 100.
    ///
    /// # Examples
    ///
    /// ```
    /// use planning_heuristics::{Fragment, Painting, Strategy};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // Two switches, each needing the other off to turn on: a causal graph cycle. Every
    /// // strategy paints exactly one of them red for a DAG.
    /// let flip = |var: usize, from: u32, other: usize| Operator {
    ///     name: format!("flip{var}"),
    ///     pre: vec![Fact::new(var, from), Fact::new(other, 0)],
    ///     eff: vec![Effect::new(Fact::new(var, 1 - from))],
    ///     cost: 1,
    /// };
    /// let task = Task {
    ///     variables: vec![Variable::new("a", 2), Variable::new("b", 2)],
    ///     operators: vec![flip(0, 0, 1), flip(0, 1, 1), flip(1, 0, 0), flip(1, 1, 0)],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![Fact::new(0, 1)],
    ///     axioms: vec![],
    /// };
    /// let state = task.initial_state();
    ///
    /// for strategy in [Strategy::FewestArcs, Strategy::Random { seed: 7 }] {
    ///     let painting = Painting::with_strategy(&task, &state, strategy, Fragment::Dag);
    ///     assert_eq!(painting.black().len(), 1);
    /// }
    /// ```
    #[must_use]
    pub fn with_strategy(
        task: &Task,
        state: &State,
        strategy: Strategy,
        fragment: Fragment,
    ) -> Self {
        Self {
            black: strategy::paint_with(&compiled(task), state, strategy, fragment),
            bounded: false,
        }
    }

    /// Paints `candidates` black in order, skipping each one linked in `graph` to one painted
    /// before.
    fn independent(graph: &CausalGraph, candidates: Vec<VarId>) -> Self {
        let mut black: Vec<VarId> = Vec::new();
        for v in candidates {
            if !black.iter().any(|&u| linked(graph, u, v)) {
                black.push(v);
            }
        }

        black.sort_unstable();
        Self {
            black,
            bounded: false,
        }
    }

    /// Paints exactly `black` black, with any causal graph on them, for the fragment of a
    /// fixed number of black variables with fixed-size domains (Katz, Hoffmann & Domshlak,
    /// ICAPS 2013, Theorem 1; Domshlak, Hoffmann & Katz 2015, Theorem 1). [`crate::RedBlack`]
    /// then searches red-black plans over the product of the black domains instead of
    /// repairing a relaxed plan: polynomial for a fixed product size, exponential in it. A
    /// state it proves unsolvable is a dead end even if the relaxed plan exists.
    ///
    /// # Errors
    ///
    /// - [`PaintingError::UnknownVariable`], [`PaintingError::Derived`],
    ///   [`PaintingError::InAxiom`] or [`PaintingError::ConditionalEffect`] as for
    ///   [`Painting::new`];
    /// - [`PaintingError::TooLarge`] if the domain sizes multiply to more than
    ///   [`MAX_BOUNDED_PRODUCT`].
    ///
    /// # Examples
    ///
    /// ```
    /// use planning_heuristics::{Painting, PaintingError};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // Two variables that need each other: a cycle, fine for a bounded painting.
    /// let flip = |var: usize, other: usize| Operator {
    ///     name: format!("flip{var}"),
    ///     pre: vec![Fact::new(var, 0), Fact::new(other, 0)],
    ///     eff: vec![Effect::new(Fact::new(var, 1))],
    ///     cost: 1,
    /// };
    /// let task = Task {
    ///     variables: vec![Variable::new("a", 2), Variable::new("b", 2)],
    ///     operators: vec![flip(0, 1), flip(1, 0)],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![Fact::new(0, 1)],
    ///     axioms: vec![],
    /// };
    ///
    /// assert!(matches!(Painting::dag(&task, vec![0, 1]), Err(PaintingError::Cyclic(..))));
    /// assert!(Painting::bounded(&task, vec![0, 1]).unwrap().is_bounded());
    /// ```
    pub fn bounded(task: &Task, black: Vec<VarId>) -> Result<Self, PaintingError> {
        let black = eligible(&compiled(task), black)?;
        let mut product: usize = 1;
        for &v in &black {
            let size = task.variables[v].domain_size as usize;
            product = product
                .checked_mul(size)
                .filter(|&p| p <= MAX_BOUNDED_PRODUCT)
                .ok_or(PaintingError::TooLarge)?;
        }

        Ok(Self {
            black,
            bounded: true,
        })
    }

    /// Whether this is a [`Painting::bounded`] painting.
    #[must_use]
    pub fn is_bounded(&self) -> bool {
        self.bounded
    }

    /// The black variables, ascending.
    #[must_use]
    pub fn black(&self) -> &[VarId] {
        &self.black
    }
}

/// Most context copies per operator when conditional effects are compiled away for
/// red-black planning.
const MAX_CONTEXTS: usize = 64;

/// `task` with its conditional effects compiled away by context splitting (operators with too
/// many contexts keep theirs); `None` if `task` has no conditional effects.
pub(super) fn split(task: &Task) -> Option<Split> {
    contexts::split_exact(task, MAX_CONTEXTS)
}

/// The task the painting checks run on: `task` with its conditional effects compiled away.
fn compiled(task: &Task) -> Cow<'_, Task> {
    split(task).map_or(Cow::Borrowed(task), |s| Cow::Owned(s.task))
}

/// `black` sorted and deduplicated, after checking each variable exists and is eligible.
fn eligible(task: &Task, mut black: Vec<VarId>) -> Result<Vec<VarId>, PaintingError> {
    black.sort_unstable();
    black.dedup();
    let excluded = ineligible(task);
    for &v in &black {
        if v >= task.num_variables() {
            return Err(PaintingError::UnknownVariable(v));
        }

        if let Some(reason) = excluded[v] {
            return Err(reason);
        }
    }

    Ok(black)
}

/// For each variable, why it cannot be black; `None` if it can.
fn ineligible(task: &Task) -> Vec<Option<PaintingError>> {
    let mut reason: Vec<Option<PaintingError>> = (0..task.num_variables())
        .map(|v| {
            task.variables[v]
                .is_derived()
                .then_some(PaintingError::Derived(v))
        })
        .collect();
    let mut exclude = |v: VarId, why: PaintingError| {
        reason[v].get_or_insert(why);
    };
    for axiom in &task.axioms {
        for c in &axiom.conditions {
            exclude(c.var, PaintingError::InAxiom(c.var));
        }
    }

    for effect in task.operators.iter().flat_map(|op| &op.eff) {
        if !effect.conditions.is_empty() {
            let v = effect.fact.var;
            exclude(v, PaintingError::ConditionalEffect(v));
            for c in &effect.conditions {
                exclude(c.var, PaintingError::ConditionalEffect(c.var));
            }
        }
    }

    reason
}

/// The eligible RSE-invertible variables, ascending.
fn repairable(task: &Task) -> Vec<VarId> {
    let excluded = ineligible(task);
    (0..task.num_variables())
        .filter(|&v| excluded[v].is_none() && is_rse_invertible(task, v))
        .collect()
}

/// The variable of `candidates` (not empty) with the largest `key`.
fn max_by_key<K: Ord>(candidates: &[VarId], key: impl Fn(VarId) -> K) -> VarId {
    candidates
        .iter()
        .copied()
        .max_by_key(|&v| key(v))
        .expect("there are candidates")
}

/// For each of the `n` nodes of a graph with `successors`, the length of the longest path to
/// it from a source of the graph's condensation (each strongly connected component counts as
/// one node).
pub(super) fn levels<'g>(n: usize, successors: impl Fn(usize) -> &'g [usize]) -> Vec<usize> {
    let component = components(n, &successors);
    let count = component.iter().max().map_or(0, |&c| c + 1);
    let mut arcs: Vec<(usize, usize)> = (0..n)
        .flat_map(|u| successors(u).iter().map(move |&v| (u, v)))
        .map(|(u, v)| (component[u], component[v]))
        .filter(|(a, b)| a != b)
        .collect();
    arcs.sort_unstable();
    arcs.dedup();

    // Longest path by relaxation in topological order; `components` numbers the
    // components in reverse topological order, so sources have the highest numbers.
    let mut level = alloc::vec![0; count];
    for c in (0..count).rev() {
        for &(a, b) in arcs.iter().filter(|(a, _)| *a == c) {
            level[b] = level[b].max(level[a] + 1);
        }
    }

    component.iter().map(|&c| level[c]).collect()
}

/// The strongly connected component of each of the `n` nodes (Tarjan), numbered in reverse
/// topological order of the condensation: an arc between components goes from a higher to a
/// lower number.
fn components<'g>(n: usize, successors: &impl Fn(usize) -> &'g [usize]) -> Vec<usize> {
    /// Tarjan's bookkeeping.
    struct Tarjan {
        /// Discovery index per node; `usize::MAX` if unvisited.
        index: Vec<usize>,
        /// Lowest reachable discovery index on the stack per node.
        low: Vec<usize>,
        /// Whether each node is on the stack.
        on_stack: Vec<bool>,
        /// The Tarjan stack.
        stack: Vec<usize>,
        /// Component per node; `usize::MAX` until assigned.
        component: Vec<usize>,
        /// Next discovery index.
        next: usize,
        /// Next component number.
        count: usize,
    }

    let mut t = Tarjan {
        index: alloc::vec![usize::MAX; n],
        low: alloc::vec![0; n],
        on_stack: alloc::vec![false; n],
        stack: Vec::new(),
        component: alloc::vec![usize::MAX; n],
        next: 0,
        count: 0,
    };
    for root in 0..n {
        if t.index[root] != usize::MAX {
            continue;
        }

        // Iterative DFS: (node, next successor position).
        let mut work = alloc::vec![(root, 0)];
        t.index[root] = t.next;
        t.low[root] = t.next;
        t.next += 1;
        t.stack.push(root);
        t.on_stack[root] = true;
        while let Some(&mut (u, ref mut pos)) = work.last_mut() {
            if let Some(&v) = successors(u).get(*pos) {
                *pos += 1;
                if t.index[v] == usize::MAX {
                    t.index[v] = t.next;
                    t.low[v] = t.next;
                    t.next += 1;
                    t.stack.push(v);
                    t.on_stack[v] = true;
                    work.push((v, 0));
                } else if t.on_stack[v] {
                    t.low[u] = t.low[u].min(t.index[v]);
                }

                continue;
            }

            work.pop();
            if let Some(&(parent, _)) = work.last() {
                t.low[parent] = t.low[parent].min(t.low[u]);
            }

            if t.low[u] == t.index[u] {
                while let Some(w) = t.stack.pop() {
                    t.on_stack[w] = false;
                    t.component[w] = t.count;
                    if w == u {
                        break;
                    }
                }

                t.count += 1;
            }
        }
    }

    t.component
}

/// An arc `(u, v)` of the causal graph between variables of `black` (ascending) that lies on
/// a cycle of the graph induced on them; `None` if that graph is acyclic.
fn cycle_arc(graph: &CausalGraph, black: &[VarId]) -> Option<(VarId, VarId)> {
    let successors: Vec<Vec<usize>> = black
        .iter()
        .map(|&v| {
            graph
                .successors(v)
                .iter()
                .filter_map(|w| black.binary_search(w).ok())
                .collect()
        })
        .collect();
    let component = components(black.len(), &|b| successors[b].as_slice());

    (0..black.len())
        .flat_map(|u| successors[u].iter().map(move |&v| (u, v)))
        .find(|&(u, v)| component[u] == component[v])
        .map(|(u, v)| (black[u], black[v]))
}

/// Whether `u` and `v` are linked by an arc of the causal graph in either direction.
fn linked(graph: &CausalGraph, u: VarId, v: VarId) -> bool {
    graph.successors(u).binary_search(&v).is_ok() || graph.successors(v).binary_search(&u).is_ok()
}

#[cfg(test)]
mod tests {
    //! The causal graph levels on small graphs.

    use alloc::vec::Vec;

    use super::levels;

    /// Levels of a graph given as successor lists.
    fn of(successors: &[&[usize]]) -> Vec<usize> {
        levels(successors.len(), |v| successors[v])
    }

    /// A chain has levels 0, 1, 2; a cycle collapses to one level; isolated nodes are sources.
    #[test]
    fn levels_of_chains_and_cycles() {
        assert_eq!(of(&[&[1], &[2], &[]]), [0, 1, 2]);
        assert_eq!(of(&[&[1], &[2], &[1], &[]]), [0, 1, 1, 0]);
        assert_eq!(of(&[&[2], &[2], &[3], &[]]), [0, 0, 1, 2]);
        assert_eq!(of(&[&[1, 2], &[3], &[], &[]]), [0, 1, 1, 2]);
    }
}