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
//! Abstraction heuristics: goal distances of an explicit abstraction, looked up per state.
//!
//! [`AbstractionHeuristic`] covers any [`sas_plus::Abstraction`], such as a
//! [`sas_plus::Projection`] (a pattern database). With the `merge-and-shrink` and
//! `cost-partition` features, the heuristics of those crates implement [`GoalDistance`] and
//! become a [`Heuristic`] through [`Adapter`].
use Vec;
use ;
use crateHeuristic;
/// The goal distances of an abstraction as a heuristic: admissible, since every transition of
/// the task maps to a path of at most its cost in the abstraction.
///
/// `None` means the abstract state of the state is a dead end, or that the abstraction
/// pruned it. Projections never prune. An abstraction that prunes unreachable states (such
/// as merge-and-shrink with `prune: true`) is only sound for states reachable from the
/// initial state, which is all a forward search ever evaluates.
///
/// # Examples
///
/// ```
/// use planning_heuristics::{AbstractionHeuristic, Heuristic};
/// use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
///
/// // Two independent switches, each set by an operator of cost 2 and 3.
/// let set = |var: usize, cost: u32| Operator {
/// name: format!("set-{var}"),
/// pre: vec![],
/// eff: vec![Effect::new(Fact::new(var, 1))],
/// cost,
/// };
/// let task = Task {
/// variables: vec![Variable::new("a", 2), Variable::new("b", 2)],
/// operators: vec![set(0, 2), set(1, 3)],
/// initial: State(vec![0, 0]),
/// goal: vec![Fact::new(0, 1), Fact::new(1, 1)],
/// axioms: vec![],
/// };
/// let state = task.initial_state();
///
/// // The pattern database of `b` alone sees only the cost of setting `b`.
/// assert_eq!(AbstractionHeuristic::projection(&task, vec![1]).estimate(&state), Some(3));
/// // The full pattern is exact.
/// assert_eq!(AbstractionHeuristic::projection(&task, vec![0, 1]).estimate(&state), Some(5));
/// ```
/// Narrows an estimate to `u32`, saturating at [`u32::MAX`].
/// A heuristic of another crate with an inherent `value(&State) -> Option<u64>`, which
/// [`Adapter`] turns into a [`Heuristic`].
///
/// [`Heuristic`] is the shared `search_core` trait, so the orphan rule forbids implementing it
/// here for another crate's type; this local trait plus [`Adapter`] stands in for that impl.
/// Implemented, behind the matching features, for `merge_and_shrink::MergeAndShrinkHeuristic`
/// (`merge-and-shrink`) and for `cost_partition::CostPartitionedHeuristic` and
/// `cost_partition::MaxOverOrders` (`cost-partition`).
/// A [`GoalDistance`] used as a [`Heuristic`]: forwards to [`GoalDistance::value`] and
/// saturates at [`u32::MAX`], like every heuristic of this crate.
///
/// # Examples
///
/// ```
/// use planning_heuristics::{Adapter, GoalDistance, Heuristic};
/// use sas_plus::State;
///
/// /// A stand-in for an external heuristic: the first variable's value, as a distance.
/// struct FirstValue;
///
/// impl GoalDistance for FirstValue {
/// fn value(&self, state: &State) -> Option<u64> {
/// state.0.first().map(|&v| v as u64)
/// }
/// }
///
/// let mut h = Adapter(FirstValue);
/// assert_eq!(h.estimate(&State(vec![4])), Some(4));
/// assert_eq!(h.estimate(&State(vec![])), None);
/// ```
;
/// The final merge-and-shrink abstraction's goal distances.
///
/// Admissible. `None` means a dead end or, when built with `prune: true`, a state the
/// construction found unreachable from the initial state; build with `prune: false` to
/// evaluate arbitrary states.
/// A saturated cost partitioning over abstractions: the sum of their goal distances under
/// their cost shares. Admissible.
/// The maximum over several saturated cost partitionings. Admissible.