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
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
//! Uniform-cost, A* and greedy best-first decoupled search with dominance
//! pruning.
//!
//! All three share one driver and differ in the open-list key: `g`,
//! `g + h` or `h`. Uniform-cost search and A* stop once the popped key
//! reaches the best solution cost found so far; greedy search stops at the
//! first solution. [`Config`] selects the dominance criterion (see
//! [`Pruning`]) and whether leaves keep prices ([`Leaves`]).
//!
//! ```mermaid
//! graph LR
//!     pop[pop lowest key] --> stop{done?}
//!     stop -- yes --> done[reconstruct best]
//!     stop -- no --> expand[global successors]
//!     expand --> prune{dominated or dead end?}
//!     prune -- no --> store[store, record goal cost, push]
//!     store --> pop
//!     prune -- yes --> pop
//! ```

mod dominance;
mod reconstruct;
mod stubborn;

use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::Reverse;

use sas_plus::{Task, Value};

use crate::decoupled::{DecoupledState, DecoupledTask, Price};
use crate::{DecoupledHeuristic, Error, Factoring, Plan};
pub(crate) use dominance::simulation;
use dominance::{Bounds, Dominance};
use stubborn::StubbornSets;

/// How the open list is ordered.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Order {
    /// By `g + h`, cost-optimal for admissible `h` (`h = 0` without a
    /// heuristic, which is uniform-cost search).
    Cost,
    /// By `h` alone, stopping at the first solution.
    Greedy,
    /// Anytime decoupled A*: by `g` plus a root heuristic (remaining global
    /// cost only), pruning by `g + h` against the best solution, stopping
    /// once the popped key plus the least possible goal price reaches it.
    Anytime,
}

/// The dominance criterion that prunes generated decoupled states.
///
/// A stored state `t` *dominates* a generated state `n` with the same
/// center if every completion of `n` can be turned into a completion of `t`
/// with the same remaining global actions and no higher total cost; `n` is
/// then pruned. The criteria are those of Torralba, Gnad, Dubbert and
/// Hoffmann (IJCAI 2016), generalised to this crate's global actions, which
/// may read and write leaves under any partition, and to partial leaf
/// goals. [`Pruning::Frontier`] subsumes [`Pruning::Basic`],
/// [`Pruning::EffectivePrices`] subsumes [`Pruning::Frontier`],
/// [`Pruning::Simulation`] subsumes [`Pruning::Basic`], and
/// [`Pruning::EffectiveSimulation`] subsumes all; effective prices and
/// simulation are incomparable (as in the paper). Stronger criteria prune
/// more but cost more: simulation is computed once per search and leaf.
///
/// # Anchored leaf states
///
/// The paper has one goal leaf state, and only leaf actions move a leaf.
/// Here, a leaf state is *anchored* if it satisfies the leaf goal or the
/// leaf preconditions of some global action's part on the leaf (every state
/// is, if some part has no leaf precondition). An unanchored state can only
/// be left by leaf actions; a completion's leaf path ends in a goal leaf
/// state and meets every global action that touches the leaf in an
/// anchored state.
///
/// # The per-leaf property
///
/// Each criterion yields, per leaf `L`, a slack `δ_L` such that:
///
/// > (★) for every center path `π` from `n` and every `π`-compliant leaf
/// > path `ρ` that a completion of `n` takes from a reached leaf state `x`,
/// > `t` has a `π`-compliant leaf path that ends in a goal leaf state and
/// > satisfies the same global preconditions, at total leaf cost at most
/// > `price_n(x) + δ_L + cost(ρ)`.
///
/// All criteria but the frontier one give a *bound* `D_t(x)` with "`t` can
/// do what `ρ` does from `x` at leaf cost at most `D_t(x) + cost(ρ)`"; then
/// `δ_L = max_x (D_t(x) - price_n(x))` over the reached states `x` of `n`
/// satisfies (★).
///
/// - [`Pruning::Basic`]: `D_t(x) = price_t(x)`. `t` reaches `x` at that
///   price and follows `ρ`.
/// - [`Pruning::Simulation`]: `D_t(x) = min_{x ⪯ y} price_t(y)` over the
///   reached `y` that simulate `x` (see the variant). `t` reaches `y` and
///   mirrors `ρ`.
/// - [`Pruning::EffectivePrices`]: `D_t = p`, the point-wise least function
///   with `p(x) = S(x)` on anchored states and
///   `p(x) = min(S(x), max_{x -a-> y} p(y) - cost(a))` elsewhere, over leaf
///   actions regardless of their center preconditions, where
///   `S = price_t`. Proof: let `ρ` visit `x = s_0, ..., s_m` and take the
///   first `i` with `p(s_i) = S(s_i)`; it exists because `ρ` ends in a goal
///   leaf state, which is anchored. Before `i` all states are unanchored,
///   so every step is a leaf action and `p(s_j) >= p(s_{j+1}) - cost(a_{j+1})`;
///   summing, `S(s_i) = p(s_i) <= p(x) + cost(s_0 .. s_i)`. `t` reaches `s_i`
///   at `S(s_i)` and follows the rest of `ρ`. States whose least value is
///   `-∞` reach no anchored state, so no completion uses them.
/// - [`Pruning::EffectiveSimulation`]: the same with `S` the simulation
///   bound; at `s_i`, `t` reaches a simulating `y` and mirrors the rest.
/// - [`Pruning::Frontier`]: `δ_L = max_x (price_t(x) - price_n(x))` over the
///   reached states `x` on the frontier of `n`: anchored, or left by some
///   leaf action `x -a-> y` with `price_n(x) + cost(a) < price_n(y)`. Proof:
///   if `ρ` starts outside the frontier, every leaf action out of a
///   non-frontier state `s_j` reaches `s_{j+1}` with
///   `price_n(s_{j+1}) <= price_n(s_j) + cost(a_{j+1})`, so the first
///   frontier state `s_k` of `ρ` (it exists, the goal state is anchored)
///   has `price_n(s_k) <= price_n(x) + cost(s_0 .. s_k)`; `t` reaches `s_k`
///   at `price_t(s_k) <= price_n(s_k) + δ_L` and follows the rest of `ρ`.
///
/// # The `g` budget
///
/// Leaf prices exclude `g`. A completion of `n` through leaf states `x_L`
/// costs `g(n) + Σ_L (price_n(x_L) + cost(ρ_L)) + c`, with `c` the cost of
/// the remaining global actions, and by (★) `t` has one costing at most
/// `g(t) + Σ_L (price_n(x_L) + δ_L + cost(ρ_L)) + c`. So `t` dominates `n`
/// for cost-optimal search if `g(t) + Σ_L δ_L <= g(n)`, which
/// [`Config::fold_g`] uses; without it, the classic condition `g(t) <= g(n)`
/// and `δ_L <= 0` for every leaf. Comparing `g + price` per leaf state
/// instead is unsound with two or more leaves, because `g` is paid once but
/// would be credited per leaf: with `g(t) = 0` and both leaves at price 10,
/// `g(n) = 10` and both at 0, every leaf ties at 10, yet completing both
/// costs 20 from `t` and 10 from `n`. Greedy search only needs
/// reachability: `t` dominates `n` if every `δ_L` is finite.
///
/// # Optimality
///
/// Say a stored node *covers* cost `C` via global actions `π` if `π` leads
/// it to a goal state with total cost at most `C`. While uniform-cost search
/// or A* with an admissible heuristic has not recorded the optimal cost
/// `C*`, take a node covering `C*` with the shortest `π`. If `π` is empty,
/// the node is a goal and its cost was recorded when it was stored. If the
/// node were closed, its successor along `π[0]` was either stored (covering
/// via a shorter path), pruned by a stored node covering via the same
/// shorter `π[1..]` (dominance keeps the remaining global actions, so the
/// paper's condition on the number of center actions is not needed), or a
/// dead end of the heuristic, which is impossible for a state with a
/// completion. So it is open with `f <= C*`, and the search cannot stop
/// before recording `C*`. Termination follows from Dickson's lemma for the
/// basic criterion, which every other one subsumes.
///
/// Leaves whose transitions depend on the center (center-conditioned
/// effects, derived variables attached to them) or that have more than
/// [`Config::max_analysis_states`] states use the basic bound.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Pruning {
    /// Same center and no higher price for any leaf state (Gnad and
    /// Hoffmann's criterion).
    Basic,
    /// Prices compared only on the frontier of the generated state: leaf
    /// states that are goals, satisfy a global precondition, or could still
    /// lower another state's price.
    Frontier,
    /// Prices of the stored state replaced by effective prices, which credit
    /// a leaf state with the cheapest way to get to where it leads.
    EffectivePrices,
    /// A leaf state may take the price of any reached leaf state that
    /// simulates it.
    ///
    /// The leaf simulation `⪯` (Definition 5 of the paper, with global
    /// actions added) is the coarsest relation on leaf states where
    /// `x ⪯ y` implies: if `x` satisfies the leaf goal, so does `y`; for
    /// every global action's part `P` on the leaf, if `x` satisfies `P`'s
    /// leaf preconditions, so does `y`, and `P(x) ⪯ P(y)`; and for every
    /// leaf action `x -a-> x'`, either `x' ⪯ y` (the leaf stays at `y`) or
    /// some leaf action `b` with `cost(b) <= cost(a)` and center
    /// preconditions contained in `a`'s leads `y -b-> y'` with `x' ⪯ y'`.
    /// By induction on a compliant leaf path from `x`, `y` has one that
    /// complies with the same center path (each `b` is applicable wherever
    /// `a` was, each global action's leaf preconditions hold), ends in a
    /// goal leaf state whenever the original does, and costs no more.
    Simulation,
    /// Effective prices over simulation bounds; the strongest criterion.
    EffectiveSimulation,
    /// [`Pruning::EffectivePrices`] until the search has spent about as
    /// much as computing the leaf simulations would cost, then
    /// [`Pruning::EffectiveSimulation`] (the default).
    ///
    /// Computing a leaf simulation takes time quadratic in the leaf size,
    /// which dominates searches on small tasks. The switch follows the
    /// ski-rental rule: the work unit is one leaf state of a generated
    /// decoupled state (leaf closure touches every leaf state of every
    /// successor), and the simulation's estimated cost is the sum of
    /// `size^2 * (1 + leaf actions)` over the analysed leaves. A search that
    /// ends earlier never pays for simulation; otherwise it pays at most
    /// about twice the better of the two choices. Both criteria are sound,
    /// so mixing them in one search is too; bounds cached under effective
    /// prices are recomputed after the switch. With
    /// [`Config::reduce_leaves`], the simulation is needed up front, and
    /// this is [`Pruning::EffectiveSimulation`] from the start.
    #[default]
    Adaptive,
}

/// What a decoupled state keeps per leaf state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Leaves {
    /// The price of every reached leaf state: the cheapest cost of leaf
    /// actions that reaches it along the current center path. Needed for
    /// cost-optimal search.
    #[default]
    Priced,
    /// Only which leaf states are reached (all at price 0); leaf action
    /// costs enter the plan, not the search. Smaller states and more
    /// pruning, for greedy search only.
    Reachability,
}

/// Options for decoupled search.
///
/// # Examples
///
/// ```
/// use decoupled_search::{Config, Factoring, Pruning};
/// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
///
/// let op = |pre: Vec<Fact>, eff: Fact| Operator {
///     name: "op".into(),
///     pre,
///     eff: vec![Effect::new(eff)],
///     cost: 1,
/// };
/// let task = Task {
///     variables: vec![Variable::new("truck", 2), Variable::new("pkg", 3)],
///     operators: vec![
///         op(vec![Fact::new(0, 0)], Fact::new(0, 1)),
///         op(vec![Fact::new(0, 1)], Fact::new(0, 0)),
///         op(vec![Fact::new(0, 0), Fact::new(1, 0)], Fact::new(1, 2)),
///         op(vec![Fact::new(0, 1), Fact::new(1, 2)], Fact::new(1, 1)),
///     ],
///     initial: State(vec![0, 0]),
///     goal: vec![Fact::new(1, 1)],
///     axioms: vec![],
/// };
///
/// let config = Config {
///     pruning: Pruning::Basic,
///     ..Config::default()
/// };
/// let outcome = config.search(&task, &Factoring::fork(&task))?;
/// assert_eq!(outcome.plan.map(|p| p.cost), Some(3));
/// assert!(outcome.statistics.expanded >= 1);
/// # Ok::<(), decoupled_search::Error>(())
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[expect(
    clippy::struct_excessive_bools,
    reason = "independent on/off options; struct-literal construction is public API"
)]
pub struct Config {
    /// The dominance criterion. Default: [`Pruning::Adaptive`].
    pub pruning: Pruning,
    /// Whether a stored state may dominate one reached more cheaply, when
    /// its cheaper prices pay for the difference in `g` (one budget shared
    /// by all leaves). Without it, the stored state needs no higher `g`.
    /// Default: `true`.
    pub fold_g: bool,
    /// Prices or reachability only. Default: [`Leaves::Priced`].
    pub leaves: Leaves,
    /// The largest leaf (in leaf states) that frontier, effective-price and
    /// simulation analyses run on; larger leaves use the basic comparison.
    /// Simulation takes time quadratic in the leaf size per refinement
    /// round, effective prices keep one bound per leaf state in every
    /// stored state. Default: 256.
    pub max_analysis_states: usize,
    /// Whether to shrink the leaf state spaces by leaf simulation before
    /// searching: the superfluous-transition rule of Torralba, Gnad,
    /// Dubbert and Hoffmann (IJCAI 2016, Section 6 of the extended
    /// technical report), with a removal order that makes it sound.
    /// Default: `false`.
    ///
    /// On every analysed leaf (see [`Config::max_analysis_states`]), the
    /// leaf simulation `⪯` of [`Pruning::Simulation`] is computed once.
    /// Say that `x -b-> y'` *replaces* `x -a-> x'` if `cost(b) <= cost(a)`,
    /// `b`'s center preconditions are contained in `a`'s, and `x' ⪯ y'`. A
    /// leaf transition `x -a-> x'` is removed if
    ///
    /// 1. `x' ⪯ x`: staying at `x` is as good, or
    /// 2. another leaf action `b` applicable in `x` replaces it, and either
    ///    `a` does not replace `b` in turn or `b` has the smaller index.
    ///
    /// The report states rule 2 without the last condition and without a
    /// removal order; read as one simultaneous step, it would remove both
    /// of two transitions that replace each other (for example two copies
    /// of one action), which is unsound. Removing one transition at a time,
    /// each justified by one still present, removes exactly what the rules
    /// above remove, up to which of two mutually replacing transitions
    /// stays. The report also drops leaf states that become unreachable and
    /// leaf actions without transitions; here leaf closure skips removed
    /// transitions, so those states are never reached and stored
    /// ([`Statistics::reduced_leaf_transitions`] and
    /// [`Statistics::unreachable_leaf_states`] report the effect).
    /// [`Config::reduced_task`] drops the operators left without a useful
    /// transition, for building heuristics.
    ///
    /// *Soundness* (the report gives no proof). "Replaces" is reflexive and
    /// transitive on the transitions leaving `x`; ordering by it, and by
    /// index between transitions that replace each other, is a strict
    /// partial order on a finite set, and rule 2 removes only transitions
    /// below a greater one. Take any compliant leaf path `ρ` of the full
    /// leaf system, and its first removed step `s_i -a-> s_{i+1}`.
    /// Following rule 2 upwards ends in a maximal transition `s_i -c-> z`,
    /// which rule 2 keeps, with `s_{i+1} ⪯ z` by transitivity; if rule 1
    /// removes it, staying at `s_i` (`z ⪯ s_i`) is as good. `c` is
    /// applicable wherever `a` was (weaker center preconditions) and no
    /// dearer. By the simulation, the rest of `ρ` is mirrored from `z` (or
    /// `s_i`) at no higher cost, in no more steps, meeting the same global
    /// preconditions and ending in a goal leaf state. Repeating at the next
    /// removed step terminates: each round either fixes one more step or
    /// (staying) shortens the path. So every completion of the full system
    /// has one of the reduced system at no higher cost, which preserves
    /// optimality and solvability, and every reduced completion is a real
    /// one. The dominance analyses still use the full transitions, which
    /// only makes frontier and effective-price bounds more conservative,
    /// and mirrored paths in the simulation bound are converted the same
    /// way.
    pub reduce_leaves: bool,
    /// Whether to apply, in every expanded state, only the global actions
    /// of a decoupled strong stubborn set (Gnad, Hoffmann and Wehrle, JAIR
    /// 2019, Definitions 4 to 8, general star topologies, with the paper's
    /// approximations). Default: `false`.
    ///
    /// The set is the closure of: in a non-goal state, the achievers of an
    /// open part of the goal (a leaf fact no reached leaf state has, else a
    /// leaf's goal part no single reached leaf state has, else a center
    /// fact); in a goal state, the actions that could still lower some
    /// leaf's goal price (none: the state is not expanded); for an
    /// unreached action in the set, the achievers of an open part of its
    /// precondition; for a reached one, every action that interferes with
    /// it and has a consistent precondition, and every action leaving a
    /// reached leaf state towards its leaf preconditions. The paper proves
    /// that this keeps a strongly optimal decoupled plan, so uniform-cost
    /// search, A* and anytime A* stay optimal and every search stays
    /// complete. [`Statistics::stubborn_pruned`] counts what it removes.
    ///
    /// Where the paper's guarantee stops:
    ///
    /// - Tasks with conditional effects or axioms are searched with full
    ///   expansion; the construction is not defined for them.
    /// - The paper covers duplicate detection, not dominance pruning. For
    ///   [`Pruning::Basic`], [`Pruning::Frontier`] and
    ///   [`Pruning::EffectivePrices`] with [`Config::fold_g`] off, a
    ///   dominating state can follow the pruned one's center path at no
    ///   higher cost, which is the property the proof needs (an argument of
    ///   this crate, not a proof). The other criteria, `fold_g`, and
    ///   [`Config::reduce_leaves`] rest on the randomized cross-checks
    ///   against Dijkstra only.
    pub stubborn_sets: bool,
    /// Whether to keep the pricing function of every leaf with at least
    /// [`Config::symbolic_min_states`] states as binary decision diagrams,
    /// one per price, instead of a price per enumerated leaf state (Gnad,
    /// Torralba and Hoffmann 2017); smaller leaves stay enumerated. Needs
    /// the `symbolic-leaves` feature. Default: `false`.
    ///
    /// The decoupled state space is the same as with explicit leaves, so
    /// costs, plans and the number of expanded states agree; leaves larger
    /// than [`crate::MAX_LEAF_STATES`] become searchable when their reached
    /// sets have small diagrams. Supports every search and heuristic and
    /// both [`Leaves`] modes, with [`Pruning::Basic`] (with or without
    /// [`Config::fold_g`]) only; other criteria, [`Config::reduce_leaves`],
    /// [`Config::stubborn_sets`], conditional effects and axioms are
    /// rejected with [`Error::SymbolicUnsupported`].
    pub symbolic_leaves: bool,
    /// The fewest leaf states for which [`Config::symbolic_leaves`] uses
    /// diagrams; 0 makes every leaf symbolic. Default: 1024, about where
    /// diagrams start to pay off on the crate's benchmark.
    pub symbolic_min_states: usize,
    /// Whether to expand sets of center states at once: a search node is a
    /// set of center states, kept as a BDD, that share one pricing function
    /// and one `g`. Needs the `symbolic-leaves` feature; leaves follow
    /// [`Config::symbolic_leaves`] and [`Config::symbolic_min_states`].
    /// Default: `false`.
    ///
    /// This is the crate's own formulation, not a published one;
    /// `docs/design.md` argues why it is sound, and the randomized
    /// cross-checks against Dijkstra test it. Each member is an ordinary
    /// decoupled state, so cost-optimal searches stay optimal; the nodes
    /// differ from the explicit search's, so the counters do too. Supports uniform-cost search, A* and GBFS with
    /// [`Pruning::Basic`] and [`Config::fold_g`] off, under the same
    /// restrictions as [`Config::symbolic_leaves`]; anytime A* and `fold_g`
    /// are rejected with [`Error::SymbolicUnsupported`]. Heuristics see a
    /// center fact as reached when some member has it.
    pub symbolic_center: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            pruning: Pruning::default(),
            fold_g: true,
            leaves: Leaves::default(),
            max_analysis_states: 256,
            reduce_leaves: false,
            stubborn_sets: false,
            symbolic_leaves: false,
            symbolic_min_states: 1024,
            symbolic_center: false,
        }
    }
}

/// Counters of one search run.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Statistics {
    /// Decoupled states expanded (popped and not past the stopping key).
    pub expanded: u64,
    /// Successors generated, the initial state included.
    pub generated: u64,
    /// Generated states pruned because a stored state dominates them.
    pub dominated: u64,
    /// Generated states pruned because the heuristic declared them dead
    /// ends.
    pub dead_ends: u64,
    /// States stored (and pushed on the open list).
    pub stored: u64,
    /// Leaf simulations computed (one per analysed leaf, if any criterion or
    /// [`Config::reduce_leaves`] needed them).
    pub simulated_leaves: u64,
    /// Leaf transitions removed by [`Config::reduce_leaves`].
    pub reduced_leaf_transitions: u64,
    /// Generated states pruned by anytime A* ([`Config::anytime_astar`])
    /// because `g` plus the fork heuristic reaches the best solution cost.
    pub bound_pruned: u64,
    /// Leaf states that [`Config::reduce_leaves`] made unreachable from the
    /// initial leaf states (under every center).
    pub unreachable_leaf_states: u64,
    /// Applicable global actions that [`Config::stubborn_sets`] left out,
    /// summed over the expanded states.
    pub stubborn_pruned: u64,
}

/// The result of a search run: the plan, if any, and counters.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Outcome {
    /// The plan; `None` if the task is unsolvable.
    pub plan: Option<Plan>,
    /// What the search did.
    pub statistics: Statistics,
}

/// A generated decoupled state.
#[derive(Debug)]
struct Node {
    /// The decoupled state.
    state: DecoupledState,
    /// Cost of the global actions from the initial state.
    g: Price,
    /// Predecessor node and the index of the global action that led here;
    /// `None` for the initial node.
    parent: Option<(usize, usize)>,
    /// The node's dominance bounds, computed on its first comparison.
    bounds: Option<Bounds>,
}

/// Search bookkeeping: all nodes, the open list, the dominance store and the
/// best solution found so far.
struct Search<'t> {
    /// The task being searched.
    task: &'t DecoupledTask,
    /// The heuristic; `None` means `h = 0` without computing fact prices.
    /// Under [`Order::Anytime`], the fork heuristic that prunes against the
    /// best solution.
    heuristic: Option<&'t dyn DecoupledHeuristic>,
    /// The root heuristic of [`Order::Anytime`], evaluated with every
    /// reached fact at price 0; `None` otherwise.
    root: Option<&'t dyn DecoupledHeuristic>,
    /// A lower bound on the goal price of every decoupled goal state (see
    /// [`min_goal_price`]); 0 unless [`Order::Anytime`].
    min_goal_price: Price,
    /// The open-list order.
    order: Order,
    /// The dominance criterion.
    dominance: Dominance,
    /// Every node that survived pruning, indexed by node id.
    nodes: Vec<Node>,
    /// Node ids ordered by their key (`g + h` or `h`).
    open: BinaryHeap<Reverse<(Price, usize)>>,
    /// Node ids per center state, for dominance checks.
    by_center: BTreeMap<Vec<Value>, Vec<usize>>,
    /// Cheapest solution cost so far and the node it ends in.
    best: Option<(Price, usize)>,
    /// Counters.
    statistics: Statistics,
    /// The most expansions to make; `None` for no limit.
    limit: Option<u64>,
    /// What a sample search measures.
    probe: Probe,
    /// The stubborn set construction; `None` for full expansion.
    stubborn: Option<StubbornSets>,
}

/// Finds a cheapest plan for `task` by uniform-cost decoupled search over
/// `factoring`, with the default [`Config`].
///
/// Returns `Ok(None)` if the task is unsolvable. A generated state is pruned
/// when a stored state with the same center dominates it (see [`Pruning`]).
///
/// # Errors
///
/// - [`Error::InvalidTask`] if `task` does not validate.
/// - [`Error::CenterEffectReadsLeaf`], [`Error::EffectConditionAcrossLeaves`]
///   or [`Error::AxiomAcrossLeaves`] for conditional effects and axioms that
///   decoupled search cannot express under `factoring`.
/// - [`Error::DerivedInLeaf`] if `factoring` puts a derived variable in a
///   leaf.
/// - [`Error::FactoringMismatch`] or [`Error::LeafTooLarge`] if `factoring`
///   was built for a different task.
///
/// # Examples
///
/// ```
/// use decoupled_search::{Factoring, search};
/// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
///
/// // A truck (variable 0) and a package (variable 1) that rides along.
/// let op = |name: &str, pre: Vec<Fact>, eff: Fact| Operator {
///     name: name.into(),
///     pre,
///     eff: vec![Effect::new(eff)],
///     cost: 1,
/// };
/// let task = Task {
///     variables: vec![Variable::new("truck", 2), Variable::new("pkg", 3)],
///     operators: vec![
///         op("drive", vec![Fact::new(0, 0)], Fact::new(0, 1)),
///         op("load", vec![Fact::new(0, 0), Fact::new(1, 0)], Fact::new(1, 2)),
///         op("unload", vec![Fact::new(0, 1), Fact::new(1, 2)], Fact::new(1, 1)),
///     ],
///     initial: State(vec![0, 0]),
///     goal: vec![Fact::new(1, 1)],
///     axioms: vec![],
/// };
///
/// let factoring = Factoring::fork(&task);
/// assert_eq!(factoring.leaves(), &[vec![1]]);
///
/// let plan = search(&task, &factoring)?.expect("solvable");
/// assert_eq!(plan.cost, 3);
/// assert_eq!(plan.validate(&task), Ok(()));
/// # Ok::<(), decoupled_search::Error>(())
/// ```
pub fn search(task: &Task, factoring: &Factoring) -> Result<Option<Plan>, Error> {
    Config::default()
        .search(task, factoring)
        .map(|outcome| outcome.plan)
}

/// Finds a cheapest plan for `task` by decoupled A* over `factoring`, guided
/// by `heuristic`, with the default [`Config`].
///
/// The plan is cost-optimal if `heuristic` is admissible in the sense of
/// [`DecoupledHeuristic::estimate`], as [`crate::HMax`] is. Pruning is the
/// same as in [`search()`]; states the heuristic declares dead ends are
/// pruned too. Returns `Ok(None)` if the task is unsolvable.
///
/// # Errors
///
/// As [`search()`].
///
/// # Examples
///
/// See [`crate::HMax`].
pub fn astar(
    task: &Task,
    factoring: &Factoring,
    heuristic: &(impl DecoupledHeuristic + ?Sized),
) -> Result<Option<Plan>, Error> {
    Config::default()
        .astar(task, factoring, heuristic)
        .map(|outcome| outcome.plan)
}

/// Finds some plan for `task` by decoupled greedy best-first search over
/// `factoring`, ordered by `heuristic` alone, with the default [`Config`],
/// and returns the first one found. The plan is valid but not necessarily
/// cheapest.
///
/// Dominance ignores costs here: a stored state dominates a generated one
/// if it can do everything the generated one can, which preserves
/// solvability. Returns `Ok(None)` if the task is unsolvable.
///
/// # Errors
///
/// As [`search()`].
///
/// # Examples
///
/// See [`crate::HFf`].
pub fn gbfs(
    task: &Task,
    factoring: &Factoring,
    heuristic: &(impl DecoupledHeuristic + ?Sized),
) -> Result<Option<Plan>, Error> {
    Config::default()
        .gbfs(task, factoring, heuristic)
        .map(|outcome| outcome.plan)
}

impl Config {
    /// Uniform-cost decoupled search with this configuration; see
    /// [`search()`].
    ///
    /// # Errors
    ///
    /// As [`search()`], and [`Error::ReachabilityNotOptimal`] with
    /// [`Leaves::Reachability`].
    pub fn search(&self, task: &Task, factoring: &Factoring) -> Result<Outcome, Error> {
        self.run(task, factoring, (None, None), Order::Cost)
    }

    /// Decoupled A* with this configuration; see [`astar`].
    ///
    /// # Errors
    ///
    /// As [`Config::search`].
    pub fn astar(
        &self,
        task: &Task,
        factoring: &Factoring,
        heuristic: &(impl DecoupledHeuristic + ?Sized),
    ) -> Result<Outcome, Error> {
        self.run(task, factoring, (Some(&heuristic), None), Order::Cost)
    }

    /// Decoupled greedy best-first search with this configuration; see
    /// [`gbfs`]. With [`Leaves::Reachability`], states keep no prices.
    ///
    /// # Errors
    ///
    /// As [`search()`].
    ///
    /// # Examples
    ///
    /// ```
    /// use decoupled_search::{Config, Factoring, HFf, Leaves};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// let op = |pre: Vec<Fact>, eff: Fact, cost| Operator {
    ///     name: "op".into(),
    ///     pre,
    ///     eff: vec![Effect::new(eff)],
    ///     cost,
    /// };
    /// let task = Task {
    ///     variables: vec![Variable::new("truck", 2), Variable::new("pkg", 3)],
    ///     operators: vec![
    ///         op(vec![Fact::new(0, 0)], Fact::new(0, 1), 4),
    ///         op(vec![Fact::new(0, 0), Fact::new(1, 0)], Fact::new(1, 2), 2),
    ///         op(vec![Fact::new(0, 1), Fact::new(1, 2)], Fact::new(1, 1), 3),
    ///     ],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![Fact::new(1, 1)],
    ///     axioms: vec![],
    /// };
    ///
    /// let config = Config { leaves: Leaves::Reachability, ..Config::default() };
    /// let outcome = config.gbfs(&task, &Factoring::fork(&task), &HFf::new(&task))?;
    /// let plan = outcome.plan.expect("solvable");
    /// assert_eq!(plan.cost, 9, "costs are summed from the plan's operators");
    /// assert_eq!(plan.validate(&task), Ok(()));
    /// # Ok::<(), decoupled_search::Error>(())
    /// ```
    pub fn gbfs(
        &self,
        task: &Task,
        factoring: &Factoring,
        heuristic: &(impl DecoupledHeuristic + ?Sized),
    ) -> Result<Outcome, Error> {
        self.run(task, factoring, (Some(&heuristic), None), Order::Greedy)
    }

    /// Anytime decoupled A* (Gnad and Hoffmann's Anytime Fork-Root A*,
    /// ICAPS 2015, generalised to star factorings): A* ordered by `g` plus
    /// the `root` heuristic, which estimates only the cost of the remaining
    /// global actions, with `fork` (an estimate of the whole remaining cost,
    /// as for [`Config::astar`]) used to prune nodes that cannot beat the
    /// best solution found so far.
    ///
    /// The root heuristic sees every reached leaf fact at price 0. Build it
    /// on [`Factoring::with_free_leaf_actions`], for example
    /// `HMax::new(&factoring.with_free_leaf_actions(&task)?)`: any
    /// completion of a member state is then a plan of that task costing
    /// only its global actions, so `h^max` there is root-admissible.
    ///
    /// The search records solutions when they are generated and stops once
    /// the popped key plus the least goal price any decoupled goal state can
    /// have (per leaf, the cheapest leaf-action cost from the initial leaf
    /// state to a goal leaf state, global parts free, ignoring the center)
    /// reaches the best solution cost, or when the open list is empty. This
    /// subsumes the paper's early termination (an expanded goal whose goal
    /// price equals that minimum) and is sound by the same argument as
    /// [`Config::astar`]: while the optimum `C*` is not recorded, a node
    /// covering it is open, with key at most `C*` minus its goal price.
    /// Nodes with `g + fork` at least the best cost are pruned
    /// ([`Statistics::bound_pruned`]), which cannot cut below `C*`.
    ///
    /// The plan is cost-optimal if `root` is root-admissible and `fork` is
    /// admissible.
    ///
    /// # Errors
    ///
    /// As [`Config::search`].
    ///
    /// # Examples
    ///
    /// ```
    /// use decoupled_search::{Config, Factoring, HMax};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// let op = |pre: Vec<Fact>, eff: Fact| Operator {
    ///     name: "op".into(),
    ///     pre,
    ///     eff: vec![Effect::new(eff)],
    ///     cost: 1,
    /// };
    /// let task = Task {
    ///     variables: vec![Variable::new("truck", 2), Variable::new("pkg", 3)],
    ///     operators: vec![
    ///         op(vec![Fact::new(0, 0)], Fact::new(0, 1)),
    ///         op(vec![Fact::new(0, 0), Fact::new(1, 0)], Fact::new(1, 2)),
    ///         op(vec![Fact::new(0, 1), Fact::new(1, 2)], Fact::new(1, 1)),
    ///     ],
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![Fact::new(1, 1)],
    ///     axioms: vec![],
    /// };
    ///
    /// let factoring = Factoring::fork(&task);
    /// let root = HMax::new(&factoring.with_free_leaf_actions(&task)?);
    /// let outcome = Config::default().anytime_astar(&task, &factoring, &root, &HMax::new(&task))?;
    /// assert_eq!(outcome.plan.map(|p| p.cost), Some(3));
    /// # Ok::<(), decoupled_search::Error>(())
    /// ```
    pub fn anytime_astar(
        &self,
        task: &Task,
        factoring: &Factoring,
        root: &(impl DecoupledHeuristic + ?Sized),
        fork: &(impl DecoupledHeuristic + ?Sized),
    ) -> Result<Outcome, Error> {
        self.run(task, factoring, (Some(&fork), Some(&root)), Order::Anytime)
    }

    /// The search driver shared by all entry points.
    ///
    /// # Errors
    ///
    /// As [`Config::search`].
    fn run(
        &self,
        task: &Task,
        factoring: &Factoring,
        (heuristic, root): (
            Option<&dyn DecoupledHeuristic>,
            Option<&dyn DecoupledHeuristic>,
        ),
        order: Order,
    ) -> Result<Outcome, Error> {
        self.probe(task, factoring, (heuristic, root), order, None)
            .map(|(outcome, _)| outcome)
    }

    /// [`Config::run`] that stops after `limit` expansions, if given, and
    /// also reports what a sample search measures.
    ///
    /// # Errors
    ///
    /// As [`Config::search`].
    fn probe(
        &self,
        task: &Task,
        factoring: &Factoring,
        (heuristic, root): (
            Option<&dyn DecoupledHeuristic>,
            Option<&dyn DecoupledHeuristic>,
        ),
        order: Order,
        limit: Option<u64>,
    ) -> Result<(Outcome, Probe), Error> {
        if self.symbolic_leaves || self.symbolic_center {
            #[cfg(feature = "symbolic-leaves")]
            return crate::symbolic::run(self, task, factoring, (heuristic, root), order)
                .map(|outcome| (outcome, Probe::default()));
            #[cfg(not(feature = "symbolic-leaves"))]
            return Err(Error::SymbolicUnsupported {
                option: "symbolic leaves or center without the symbolic-leaves feature",
            });
        }

        let mut decoupled = DecoupledTask::new(task, factoring)?;
        if self.leaves == Leaves::Reachability {
            if order != Order::Greedy {
                return Err(Error::ReachabilityNotOptimal);
            }

            decoupled = decoupled.into_reachability_only();
        }

        let dominance = Dominance::new(&decoupled, self, order == Order::Greedy);
        let mut statistics = Statistics::default();
        if self.reduce_leaves {
            reduce_leaves(&mut decoupled, &dominance, &mut statistics);
        }

        let min_goal_price = if order == Order::Anytime {
            min_goal_price(&decoupled)
        } else {
            0
        };
        let stubborn = self
            .stubborn_sets
            .then(|| StubbornSets::new(task, &decoupled))
            .flatten();
        let mut search = Search {
            task: &decoupled,
            heuristic,
            root,
            min_goal_price,
            order,
            dominance,
            nodes: Vec::new(),
            open: BinaryHeap::new(),
            by_center: BTreeMap::new(),
            best: None,
            statistics,
            limit,
            probe: Probe::default(),
            stubborn,
        };
        search.run();
        search.statistics.simulated_leaves = search.dominance.simulated_leaves();

        let plan = search.best.map(|(cost, id)| {
            let plan = reconstruct::plan(&decoupled, &search.path(id), cost);
            with_true_cost(task, plan, decoupled.is_reachability_only())
        });

        let outcome = Outcome {
            plan,
            statistics: search.statistics,
        };
        Ok((outcome, search.probe))
    }
}

/// What a sample search measures (see [`sample`]).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Probe {
    /// Whether the search stopped at its expansion limit rather than by
    /// finishing.
    pub(crate) cut_off: bool,
    /// Work units spent: per generated decoupled state, one plus the number
    /// of leaf states its leaf closures range over.
    pub(crate) work: u64,
    /// The heuristic value of the initial state; `None` if it was pruned.
    pub(crate) initial_h: Option<Price>,
    /// The least heuristic value of any stored state.
    pub(crate) best_h: Option<Price>,
}

/// A sample search for factoring selection: decoupled A* with `h^max`
/// (cost-optimal) or greedy best-first search with `h^FF` (not), each with
/// the default [`Config`], stopped after `expansions` expansions. Returns
/// the probe and the number of expansions.
///
/// # Errors
///
/// As [`Config::search`].
pub(crate) fn sample(
    task: &Task,
    factoring: &Factoring,
    optimal: bool,
    expansions: u64,
) -> Result<(Probe, u64), Error> {
    let config = Config::default();
    let (outcome, probe) = if optimal {
        let h = crate::HMax::new(task);
        config.probe(
            task,
            factoring,
            (Some(&h), None),
            Order::Cost,
            Some(expansions),
        )?
    } else {
        let h = crate::HFf::new(task);
        config.probe(
            task,
            factoring,
            (Some(&h), None),
            Order::Greedy,
            Some(expansions),
        )?
    };

    Ok((probe, outcome.statistics.expanded))
}

impl Config {
    /// `task` without the operators that [`Config::reduce_leaves`] makes
    /// useless under `factoring`, for building heuristics (the last step of
    /// the leaf reduction in Torralba, Gnad, Dubbert and Hoffmann's
    /// technical report).
    ///
    /// An operator is removed if it is a leaf action of an analysed leaf
    /// (see [`Config::max_analysis_states`]) and none of its kept leaf
    /// transitions starts in a leaf state that the reduced leaf can still
    /// reach (over kept leaf actions and global parts, under any center).
    /// Variables, goal and the remaining operators are unchanged, so fact
    /// prices of the search fit heuristics built on the result.
    ///
    /// *Soundness.* Only use heuristics built on the result in searches
    /// with `reduce_leaves: true`, the same `factoring` and the same
    /// `max_analysis_states`. There, every reached leaf state is reachable
    /// in the reduced leaf, and by the soundness argument on
    /// [`Config::reduce_leaves`] every decoupled state has a cheapest
    /// completion that uses only kept transitions from such states, hence
    /// only operators of the result. A heuristic admissible on the result
    /// is therefore admissible for the search, and fewer operators can only
    /// raise its estimates. Without leaf reduction in the search the
    /// estimates may exceed the true cost.
    ///
    /// The result is the same for both [`Leaves`] modes: the simulation
    /// and the removed transitions do not depend on it.
    ///
    /// # Errors
    ///
    /// As [`Config::search`], except that [`Config::leaves`] is ignored.
    ///
    /// # Examples
    ///
    /// ```
    /// use decoupled_search::{Config, Factoring, HMax, Pruning};
    /// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
    ///
    /// // A truck over three locations and a package (3: in the truck) that
    /// // must go from location 0 to location 1.
    /// let op = |pre: Vec<Fact>, eff: Fact| Operator {
    ///     name: "op".into(),
    ///     pre,
    ///     eff: vec![Effect::new(eff)],
    ///     cost: 1,
    /// };
    /// let mut operators = Vec::new();
    /// for x in 0..3 {
    ///     for y in (0..3).filter(|&y| y != x) {
    ///         operators.push(op(vec![Fact::new(0, x)], Fact::new(0, y)));
    ///     }
    ///     operators.push(op(vec![Fact::new(0, x), Fact::new(1, x)], Fact::new(1, 3)));
    ///     operators.push(op(vec![Fact::new(0, x), Fact::new(1, 3)], Fact::new(1, x)));
    /// }
    /// let task = Task {
    ///     variables: vec![Variable::new("truck", 3), Variable::new("pkg", 4)],
    ///     operators,
    ///     initial: State(vec![0, 0]),
    ///     goal: vec![Fact::new(1, 1)],
    ///     axioms: vec![],
    /// };
    ///
    /// let config = Config { reduce_leaves: true, ..Config::default() };
    /// let factoring = Factoring::fork(&task);
    /// let reduced = config.reduced_task(&task, &factoring)?;
    /// // Only loading at 0 and unloading at 1 remain of the package's six.
    /// assert_eq!(reduced.operators.len(), task.operators.len() - 4);
    ///
    /// let outcome = config.astar(&task, &factoring, &HMax::new(&reduced))?;
    /// assert_eq!(outcome.plan.map(|p| p.cost), Some(3));
    /// # Ok::<(), decoupled_search::Error>(())
    /// ```
    pub fn reduced_task(&self, task: &Task, factoring: &Factoring) -> Result<Task, Error> {
        let mut decoupled = DecoupledTask::new(task, factoring)?;
        let config = Config {
            reduce_leaves: true,
            ..*self
        };
        let dominance = Dominance::new(&decoupled, &config, false);
        reduce_leaves(&mut decoupled, &dominance, &mut Statistics::default());

        let mut removed = vec![false; task.operators.len()];
        for (index, leaf) in decoupled.leaves.iter().enumerate() {
            if dominance.simulators(index).is_none() {
                continue;
            }

            let parts: Vec<_> = decoupled
                .globals
                .iter()
                .filter_map(|g| g.parts[index].as_ref())
                .collect();
            let live = simulation::live_actions(leaf, &parts);
            for (action, live) in leaf.actions.iter().zip(live) {
                removed[action.op] |= !live;
            }
        }

        let mut reduced = task.clone();
        let mut keep = removed.iter().map(|&r| !r);
        reduced.operators.retain(|_| keep.next().unwrap_or(true));
        Ok(reduced)
    }
}

/// A lower bound on the goal price of every decoupled goal state of `task`:
/// per leaf, the cheapest leaf-action cost from the initial leaf state to a
/// goal leaf state, global parts free, ignoring the center (0 for leaves
/// whose transitions read the center); [`Price::MAX`] if some leaf can
/// never reach its goal.
fn min_goal_price(task: &DecoupledTask) -> Price {
    task.leaves
        .iter()
        .enumerate()
        .map(|(index, leaf)| {
            let parts: Vec<_> = task
                .globals
                .iter()
                .filter_map(|g| g.parts[index].as_ref())
                .collect();
            if leaf.reads_center(parts.iter().copied()) {
                return 0;
            }

            simulation::cheapest_goal(leaf, &parts).unwrap_or(Price::MAX)
        })
        .fold(0, Price::saturating_add)
}

/// Removes the redundant leaf transitions of every leaf with a simulation
/// (see [`Config::reduce_leaves`]) and counts the effect.
fn reduce_leaves(task: &mut DecoupledTask, dominance: &Dominance, statistics: &mut Statistics) {
    let widen = |n: usize| u64::try_from(n).unwrap_or(u64::MAX);
    for leaf in 0..task.leaves.len() {
        let Some(rel) = dominance.simulators(leaf) else {
            continue;
        };

        let parts: Vec<_> = task
            .globals
            .iter()
            .filter_map(|g| g.parts[leaf].as_ref())
            .collect();
        let (disabled, lost) = simulation::redundant_transitions(&task.leaves[leaf], &parts, rel);
        statistics.reduced_leaf_transitions += widen(disabled.iter().filter(|&&d| d).count());
        statistics.unreachable_leaf_states += widen(lost);
        task.leaves[leaf].disabled = disabled;
    }
}

/// `plan` with its cost summed from its operators. Without leaf costs in
/// the search this is the only correct cost; otherwise it equals the search
/// cost, which a debug assertion checks.
pub(crate) fn with_true_cost(task: &Task, mut plan: Plan, reachability_only: bool) -> Plan {
    let summed = plan
        .ops
        .iter()
        .map(|&op| u64::from(task.operators[op].cost))
        .sum();
    debug_assert!(
        reachability_only || summed == plan.cost,
        "search cost {} differs from plan cost {summed}",
        plan.cost
    );
    plan.cost = summed;
    plan
}

impl Search<'_> {
    /// Inserts the initial state and expands nodes until the open list is
    /// empty or [`Search::is_done`].
    fn run(&mut self) {
        self.insert(self.task.initial(), 0, None);

        while let Some(Reverse((key, id))) = self.open.pop() {
            if self.is_done(key) {
                break;
            }

            if self
                .limit
                .is_some_and(|limit| self.statistics.expanded >= limit)
            {
                self.probe.cut_off = true;
                break;
            }

            self.statistics.expanded += 1;
            let node = &self.nodes[id];
            let g = node.g;
            let successors = match self.stubborn.as_mut() {
                None => self.task.successors(&node.state),
                Some(stubborn) => {
                    let (allowed, pruned) = stubborn.allowed(self.task, &node.state);
                    self.statistics.stubborn_pruned += pruned;
                    (0..allowed.len())
                        .filter(|&i| allowed[i])
                        .filter_map(|i| self.task.successor(i, &node.state).map(|s| (i, s)))
                        .collect()
                }
            };
            for (action, state) in successors {
                let g = g.saturating_add(self.task.globals[action].cost);
                self.insert(state, g, Some((id, action)));
            }
        }
    }

    /// Whether the search can stop before expanding a node with open-list
    /// key `key`: greedy search once any solution is known, otherwise once
    /// `key` reaches the best solution cost (keys are lower bounds).
    fn is_done(&self, key: Price) -> bool {
        match self.order {
            Order::Cost => self.best.is_some_and(|(cost, _)| key >= cost),
            Order::Anytime => self
                .best
                .is_some_and(|(cost, _)| key.saturating_add(self.min_goal_price) >= cost),
            Order::Greedy => self.best.is_some(),
        }
    }

    /// The heuristic estimate of `state`; 0 without a heuristic, `None` for
    /// a dead end.
    fn estimate(&self, state: &DecoupledState) -> Option<Price> {
        self.heuristic
            .map_or(Some(0), |h| h.estimate(&self.task.fact_prices(state)))
    }

    /// The open-list key of `state` reached at `g` with heuristic value `h`;
    /// `None` if the root heuristic of [`Order::Anytime`] declares it a dead
    /// end.
    fn key(&self, state: &DecoupledState, g: Price, h: Price) -> Option<Price> {
        match self.order {
            Order::Cost => Some(g.saturating_add(h)),
            Order::Greedy => Some(h),
            Order::Anytime => {
                let root = self.root.map_or(Some(0), |r| {
                    r.estimate(&self.task.fact_prices(state).reached_for_free())
                })?;
                Some(g.saturating_add(root))
            }
        }
    }

    /// Adds `state` reached at cost `g` unless a stored node dominates it or
    /// the heuristic declares it a dead end, and records it as the best
    /// solution if it is one.
    fn insert(&mut self, state: DecoupledState, g: Price, parent: Option<(usize, usize)>) {
        self.statistics.generated += 1;
        let work = self.task.leaves.iter().map(|l| l.size).sum::<usize>() + 1;
        let work = u64::try_from(work).unwrap_or(u64::MAX);
        self.probe.work = self.probe.work.saturating_add(work);
        if self.dominance.record_work(self.task, work) {
            for node in &mut self.nodes {
                node.bounds = None;
            }
        }

        if self.is_dominated(&state, g) {
            self.statistics.dominated += 1;
            return;
        }

        let Some(h) = self.estimate(&state) else {
            self.statistics.dead_ends += 1;
            return;
        };

        self.probe.initial_h.get_or_insert(h);
        let Some(key) = self.key(&state, g, h) else {
            self.statistics.dead_ends += 1;
            return;
        };

        if self.order == Order::Anytime
            && self
                .best
                .is_some_and(|(best, _)| g.saturating_add(h) >= best)
        {
            self.statistics.bound_pruned += 1;
            return;
        }

        let id = self.nodes.len();
        if let Some(rest) = self.task.goal_price(&state) {
            let cost = g.saturating_add(rest);
            if self.best.is_none_or(|(best, _)| cost < best) {
                self.best = Some((cost, id));
            }
        }

        self.by_center
            .entry(state.center.clone())
            .or_default()
            .push(id);
        self.probe.best_h = Some(self.probe.best_h.map_or(h, |b| b.min(h)));
        self.open.push(Reverse((key, id)));
        self.nodes.push(Node {
            state,
            g,
            parent,
            bounds: None,
        });
        self.statistics.stored += 1;
    }

    /// Whether a stored node with the same center dominates `state`
    /// reached at `g`, under the configured criterion.
    ///
    /// shortcut: linear scan per center state; index the store (for
    /// example by reached-leaf-state sets) when stores get long.
    fn is_dominated(&mut self, state: &DecoupledState, g: Price) -> bool {
        let Some(ids) = self.by_center.get(&state.center) else {
            return false;
        };

        let frontier = self.dominance.frontier(self.task, state);
        for &id in ids {
            let node = &mut self.nodes[id];
            let bounds = node
                .bounds
                .get_or_insert_with(|| self.dominance.bounds(self.task, &node.state));
            if self
                .dominance
                .dominates((&node.state, node.g, bounds), (state, g, &frontier))
            {
                return true;
            }
        }

        false
    }

    /// The path from the initial node to node `id`, as
    /// `(global action index that led here, state)`, initial node first.
    fn path(&self, mut id: usize) -> Vec<(Option<usize>, &DecoupledState)> {
        let mut path = Vec::new();
        loop {
            let node = &self.nodes[id];
            path.push((node.parent.map(|(_, a)| a), &node.state));
            match node.parent {
                Some((parent, _)) => id = parent,
                None => break,
            }
        }

        path.reverse();
        path
    }
}