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
//! Plan representation.
//!
//! A plan is a totally-ordered sequence of grounded operator applications
//! whose execution transforms the initial state into a goal-satisfying state.
//! Plans here are **sequential** — there is no partial ordering or parallelism.
//! They are produced by the planners in [`crate::search`].
//!
//! The [`Plan`] struct carries two invariants:
//!
//! - `steps` records *which* grounded operators to apply and in what order.
//! - `cost` is the numeric sum of the executed operators' costs, so empty
//! plans always have `cost == 0.0`.
//!
//! The [`Display`](std::fmt::Display) impl on [`Plan`] emits a PDDL-style
//! human-readable format.
use fmt;
use crateOpId;
/// A single step in a [`Plan`].
///
/// A `PlanStep` names one grounded-operator application inside a plan.
/// The `op_id` is the authoritative identifier (an index into the
/// [`Task`](crate::task::Task)'s operator list), while `op_name` is a
/// redundant, pretty-printed copy carried along so that plans can be
/// displayed or serialised without needing the originating `Task`.
/// The output produced by a planner when search succeeds.
///
/// This is the structure wrapped by [`SearchOutcome::Plan`](crate::search::SearchOutcome::Plan).
///
/// # Invariants
///
/// - `steps` is ordered: `steps[0]` is applied first.
/// - `cost` equals the sum of the applied operators' cost values; it is not
/// necessarily `steps.len()` because operator costs need not be unit.
/// - An empty plan (`steps` is empty, `cost == 0.0`) is a valid result when
/// the initial state already satisfies the goal.
///
/// # Examples
///
/// ```
/// use miniplan::plan::{Plan, PlanStep};
/// use miniplan::task::OpId;
///
/// let plan = Plan::new();
/// assert!(plan.is_empty());
/// assert_eq!(plan.len(), 0);
/// assert_eq!(plan.cost, 0.0);
///
/// let mut plan = Plan::new();
/// plan.steps.push(PlanStep { op_id: OpId(0), op_name: "pick".into() });
/// plan.cost = 1.0;
/// assert!(!plan.is_empty());
/// assert_eq!(plan.len(), 1);
///
/// assert_eq!(
/// plan.to_string(),
/// "; cost = 1\n; length = 1\n(pick)\n"
/// );
/// ```
/// Human-readable display format for a [`Plan`].
///
/// Emits a PDDL-style plan file:
///
/// - Line 1: `; cost = <cost>` (semicolons are PDDL plan comments).
/// - Line 2: `; length = <len>`.
/// - Lines 3..: one `(<op_name>)` per step, each terminated by `\n`.
///
/// The trailing newline after the last step matches the `writeln!` macro.
///
/// # Examples
///
/// ```
/// use miniplan::plan::{Plan, PlanStep};
/// use miniplan::task::OpId;
///
/// let mut plan = Plan::new();
/// plan.steps.push(PlanStep { op_id: OpId(0), op_name: "pick".into() });
/// plan.steps.push(PlanStep { op_id: OpId(1), op_name: "place".into() });
/// plan.cost = 2.0;
///
/// assert_eq!(
/// plan.to_string(),
/// "; cost = 2\n; length = 2\n(pick)\n(place)\n"
/// );
/// ```