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
//! Planning module for GOAP (Goal-Oriented Action Planning) system.
//!
//! This module provides the core planning algorithms that find optimal sequences
//! of actions to transition from an initial world state to a desired goal state.
//! It uses the A* pathfinding algorithm with custom heuristics and state
//! transition logic.
use crateNode;
use crate::;
/// Heuristic function for A* pathfinding.
///
/// Estimates the cost to reach the goal from the given node.
/// Uses the `distance_to_goal` method of the world state to calculate
/// how far the current state is from satisfying all goal requirements.
///
/// # Arguments
/// * `node` - Current node in the search graph
/// * `goal` - Target goal state
///
/// # Returns
/// Estimated cost (as usize) to reach the goal from this node
/// Generates successor nodes for the A* pathfinding algorithm.
///
/// For a given node, returns all possible next nodes by applying
/// valid actions from the available action list. Each successor
/// includes the cost of applying the action's effect.
///
/// # Arguments
/// * `node` - Current node to expand
/// * `actions` - List of available actions
///
/// # Returns
/// Iterator over (successor_node, transition_cost) pairs
+ 'a
/// Checks if a node satisfies all goal requirements.
///
/// Compares the world state in the node against all requirements
/// specified in the goal. Returns true only if all requirements
/// are satisfied according to their assertion rules.
///
/// # Arguments
/// * `node` - Node to check
/// * `goal` - Goal containing requirements to satisfy
///
/// # Returns
/// `true` if the node's state satisfies all goal requirements, `false` otherwise
/// Planning strategies for finding paths from start to goal.
///
/// Different strategies can be used depending on the planning requirements,
/// though currently only `StartToGoal` is implemented.
/// Creates a plan using a specified planning strategy.
///
/// This is the lower-level planning function that allows specifying
/// which planning strategy to use. For most use cases, prefer [`make_plan`].
///
/// # Arguments
/// * `strategy` - Planning strategy to use
/// * `start` - Initial world state
/// * `actions` - Available actions that can be performed
/// * `goal` - Desired goal state
///
/// # Returns
/// * `Some((path, total_cost))` if a plan is found, where `path` is a sequence
/// of nodes from start to goal and `total_cost` is the sum of all action costs
/// * `None` if no valid plan exists
///
/// # See Also
/// [`make_plan`] - Higher-level function that uses the default strategy
/// Creates an optimal plan from start state to goal state.
///
/// This is the main planning function that finds the lowest-cost sequence
/// of actions to transition from the initial world state to a state that
/// satisfies all goal requirements.
///
/// Uses the A* pathfinding algorithm with custom heuristics to efficiently
/// search through the space of possible action sequences.
///
/// # Arguments
/// * `start` - Initial world state
/// * `actions` - Available actions that can be performed
/// * `goal` - Desired goal state with requirements
///
/// # Returns
/// * `Some((path, total_cost))` if a plan is found
/// * `None` if no valid plan exists
///
/// # Example
/// ```rust
/// use rust_goap::prelude::*;
///
/// let start = WorldState::new().set("has_food", false).set("is_hungry", true);
/// let goal = Goal::new().with("is_hungry", Assert::eq(false));
/// let eat_action = Action {
/// key: "eat".to_string(),
/// preconditions: vec![("has_food".to_string(), Assert::eq(true))],
/// effect: Some(Effect {
/// mutations: vec![Mutation::set("is_hungry", false)],
/// cost: 1,
/// }),
/// };
///
/// if let Some((plan, cost)) = make_plan(&start, &[eat_action], &goal) {
/// println!("Found plan with cost: {}", cost);
/// }
/// ```
/// Extracts all effects from a plan, filtering out initial state nodes.
///
/// Converts a plan (sequence of nodes) into an iterator over the actual
/// actions and their effects. This is useful for executing the plan
/// or analyzing the specific actions that need to be performed.
///
/// # Arguments
/// * `plan` - Plan containing both state and effect nodes
///
/// # Returns
/// Iterator over tuples of (action_key, effect, resulting_state)
///
/// # Note
/// Initial state nodes (Node::State) are filtered out since they
/// don't represent actions that need to be executed.
/// Formats a plan into a human-readable string for debugging or display.
///
/// Creates a detailed textual representation of a plan showing:
/// - Initial world state
/// - Each action to execute with its mutations
/// - Intermediate states after each action
/// - Final state and total plan cost
///
/// # Arguments
/// * `plan` - Tuple containing the node sequence and total cost
///
/// # Returns
/// Formatted string representation of the plan
///
/// # Example Output
/// ```text
/// = INITIAL STATE
/// is_hungry = Value:Bool(true)
///
/// ---
/// = DO ACTION "eat"
/// MUTATES:
/// set: is_hungry = Value:Bool(false)
/// current state:
/// WorldState({"is_hungry": Bool(false)})
///
/// ---
/// = FINAL STATE (COST: 1)
/// is_hungry = Value:Bool(false)
/// ```