goap-ai 0.2.0

Goal-Oriented Action Planning (GOAP) AI.
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
use serde::Deserialize;

use std::{
    cmp::Ordering,
    collections::{BinaryHeap, HashMap},
};

use crate::{Action, Model, State};

#[derive(Debug, Clone, Copy, Deserialize)]
pub enum Algorithm {
    Traditional,
    Efficient,
    Hybrid,
}

#[derive(Debug, Clone, Copy, Deserialize)]
pub enum Solution {
    Fast,
    Best,
}

#[derive(Debug, Clone)]
pub struct Plan {
    pub total_discontentment: f32,
    pub total_time: i32,
    pub actions: Vec<(String, Action)>,
}

#[derive(Debug, Clone)]
pub struct Planner {
    algorithm: Algorithm,
    solution: Solution,
    max_depth: usize,
    actions: HashMap<String, Action>,
}

impl Planner {
    /// Construct a new planner instance.
    pub fn new(
        algorithm: Algorithm,
        solution: Solution,
        max_depth: usize,
        actions: HashMap<String, Action>,
    ) -> Self {
        Self {
            algorithm,
            solution,
            max_depth,
            actions,
        }
    }

    pub fn plan(&self, model: &Model) -> Plan {
        match (self.algorithm, self.solution) {
            (Algorithm::Traditional, Solution::Fast) => self.fast_total_plan(model),
            (Algorithm::Efficient, Solution::Fast) => self.fast_efficiency_plan(model),
            (Algorithm::Hybrid, Solution::Fast) => self.fast_hybrid_plan(model),
            (Algorithm::Traditional, Solution::Best) => {
                let mut memo = HashMap::new();
                self.best_total_plan(model, self.max_depth, &mut memo)
            }
            (Algorithm::Efficient, Solution::Best) => {
                let mut memo = HashMap::new();
                self.best_efficiency_plan(model, self.max_depth, &mut memo)
            }
            (Algorithm::Hybrid, Solution::Best) => {
                let mut memo = HashMap::new();
                self.best_hybrid_plan(model, self.max_depth, &mut memo)
            }
        }
    }

    /// A* fast plan (traditional) focusing on lowering discontentment quickly.
    pub fn fast_total_plan(&self, start_model: &Model) -> Plan {
        // Heuristic: how much discontentment remains?
        fn heuristic(model: &Model) -> f32 {
            model.calculate_discontentment()
        }

        let mut visited: HashMap<State, f32> = HashMap::new();
        let mut frontier = BinaryHeap::new();

        // Initialize
        let start_discontent = start_model.calculate_discontentment();
        let start_h = heuristic(start_model);
        frontier.push(AStarNode {
            cost_so_far: start_discontent,
            estimated_total: start_discontent + start_h,
            time: 0,
            model: start_model.clone(),
            plan: vec![],
        });

        // A* loop
        while let Some(node) = frontier.pop() {
            if let Some(&best_known) = visited.get(&node.model.state) {
                if node.cost_so_far > best_known {
                    continue;
                }
            }
            let depth_so_far = node.plan.len();
            if node.model.calculate_discontentment() < f32::EPSILON
                || depth_so_far >= self.max_depth
            {
                return Plan {
                    total_discontentment: node.model.calculate_discontentment(),
                    total_time: node.time,
                    actions: node.plan.clone(),
                };
            }
            visited.insert(node.model.state.clone(), node.cost_so_far);

            // Expand actions
            for (label, action) in &self.actions {
                if let Some(next_model) = node.model.apply(label.clone(), action) {
                    let new_g = node.cost_so_far + next_model.calculate_discontentment();
                    let new_time = node.time + action.duration;
                    if !visited.contains_key(&next_model.state)
                        || new_g < visited[&next_model.state]
                    {
                        let mut new_plan = node.plan.clone();
                        new_plan.push((label.clone(), action.clone()));
                        let new_h = heuristic(&next_model);
                        frontier.push(AStarNode {
                            cost_so_far: new_g,
                            estimated_total: new_g + new_h,
                            time: new_time,
                            model: next_model,
                            plan: new_plan,
                        });
                    }
                }
            }
        }

        Plan {
            total_discontentment: start_discontent,
            total_time: 0,
            actions: vec![],
        }
    }

    /// A* plan optimizing efficiency (discontentment reduction per time).
    pub fn fast_efficiency_plan(&self, start_model: &Model) -> Plan {
        // For efficiency, we'll invert "efficiency" into a cost.
        // Higher efficiency => lower cost => A* prioritizes those paths.
        fn efficiency_heuristic(model: &Model) -> f32 {
            // Could still be the raw discontentment as a guiding heuristic.
            model.calculate_discontentment()
        }

        let mut visited: HashMap<State, f32> = HashMap::new();
        let mut frontier = BinaryHeap::new();

        let start_discontent = start_model.calculate_discontentment();
        let start_h = efficiency_heuristic(start_model);
        frontier.push(AStarNode {
            cost_so_far: 0.0, // We'll accumulate "inefficiency" as cost
            estimated_total: start_h,
            time: 0,
            model: start_model.clone(),
            plan: vec![],
        });

        // A* loop
        while let Some(node) = frontier.pop() {
            if let Some(&best_known) = visited.get(&node.model.state) {
                if node.cost_so_far > best_known {
                    continue;
                }
            }
            let depth_so_far = node.plan.len();
            if node.model.calculate_discontentment() < f32::EPSILON
                || depth_so_far >= self.max_depth
            {
                return Plan {
                    total_discontentment: node.model.calculate_discontentment(),
                    total_time: node.time,
                    actions: node.plan.clone(),
                };
            }
            visited.insert(node.model.state.clone(), node.cost_so_far);

            // Expand actions
            for (label, action) in &self.actions {
                if let Some(next_model) = node.model.apply(label.clone(), action) {
                    let discontent_delta = node.model.calculate_discontentment()
                        - next_model.calculate_discontentment();
                    let efficiency = discontent_delta / action.duration.max(1) as f32;
                    // Accumulate cost as the inverse of efficiency
                    let new_cost = node.cost_so_far + 1.0 / (efficiency + 1e-6);
                    let new_time = node.time + action.duration;

                    if !visited.contains_key(&next_model.state)
                        || new_cost < visited[&next_model.state]
                    {
                        let mut new_plan = node.plan.clone();
                        new_plan.push((label.clone(), action.clone()));
                        let new_h = efficiency_heuristic(&next_model);
                        frontier.push(AStarNode {
                            cost_so_far: new_cost,
                            estimated_total: new_cost + new_h,
                            time: new_time,
                            model: next_model,
                            plan: new_plan,
                        });
                    }
                }
            }
        }

        Plan {
            total_discontentment: start_discontent,
            total_time: 0,
            actions: vec![],
        }
    }

    /// A* plan mixing efficiency and raw discontentment (hybrid).
    pub fn fast_hybrid_plan(&self, start_model: &Model) -> Plan {
        fn hybrid_heuristic(model: &Model) -> f32 {
            model.calculate_discontentment()
        }

        let mut visited: HashMap<State, f32> = HashMap::new();
        let mut frontier = BinaryHeap::new();

        let start_discontent = start_model.calculate_discontentment();
        let start_h = hybrid_heuristic(start_model);
        frontier.push(AStarNode {
            cost_so_far: 0.0,
            estimated_total: start_h,
            time: 0,
            model: start_model.clone(),
            plan: vec![],
        });

        // A* loop
        while let Some(node) = frontier.pop() {
            if let Some(&best_known) = visited.get(&node.model.state) {
                if node.cost_so_far > best_known {
                    continue;
                }
            }
            let depth_so_far = node.plan.len();
            if node.model.calculate_discontentment() < f32::EPSILON
                || depth_so_far >= self.max_depth
            {
                return Plan {
                    total_discontentment: node.model.calculate_discontentment(),
                    total_time: node.time,
                    actions: node.plan.clone(),
                };
            }
            visited.insert(node.model.state.clone(), node.cost_so_far);

            for (label, action) in &self.actions {
                if let Some(next_model) = node.model.apply(label.clone(), action) {
                    let discontent_delta = node.model.calculate_discontentment()
                        - next_model.calculate_discontentment();
                    let efficiency = discontent_delta / action.duration.max(1) as f32;

                    // Decide if we prioritize efficiency or raw discontentment
                    let use_efficiency = depth_so_far > 2 && efficiency > 0.1;
                    let metric = if use_efficiency {
                        1.0 / (efficiency + 1e-6)
                    } else {
                        next_model.calculate_discontentment()
                    };

                    let new_cost = node.cost_so_far + metric;
                    let new_time = node.time + action.duration;

                    if !visited.contains_key(&next_model.state)
                        || new_cost < visited[&next_model.state]
                    {
                        let mut new_plan = node.plan.clone();
                        new_plan.push((label.clone(), action.clone()));
                        let new_h = hybrid_heuristic(&next_model);
                        frontier.push(AStarNode {
                            cost_so_far: new_cost,
                            estimated_total: new_cost + new_h,
                            time: new_time,
                            model: next_model,
                            plan: new_plan,
                        });
                    }
                }
            }
        }

        Plan {
            total_discontentment: start_discontent,
            total_time: 0,
            actions: vec![],
        }
    }

    /// Exhaustive best plan (traditional), using memoized search.
    fn best_total_plan(
        &self,
        model: &Model,
        depth: usize,
        memo: &mut HashMap<(State, usize), Plan>,
    ) -> Plan {
        let key = (model.state.clone(), depth);
        if let Some(result) = memo.get(&key) {
            return result.clone();
        }
        if depth == 0 {
            let score = model.calculate_discontentment();
            let res = Plan {
                total_discontentment: score,
                total_time: 0,
                actions: vec![],
            };
            memo.insert(key, res.clone());
            return res;
        }

        let current_score = model.calculate_discontentment();
        let mut best_score = current_score;
        let mut best_time = 0;
        let mut best_plan = vec![];

        for (label, action) in &self.actions {
            if let Some(next_model) = model.apply(label.clone(), action) {
                let mut sub_plan = self.best_total_plan(&next_model, depth - 1, memo);

                // Prioritize lower discontentment, then shorter time
                if sub_plan.total_discontentment < best_score
                    || (sub_plan.total_discontentment == best_score
                        && sub_plan.total_time + action.duration < best_time)
                {
                    best_score = sub_plan.total_discontentment;
                    best_time = sub_plan.total_time + action.duration;
                    sub_plan.actions.insert(0, (label.clone(), action.clone()));
                    best_plan = sub_plan.actions.clone();
                }
            }
        }

        let res = Plan {
            total_discontentment: best_score,
            total_time: best_time,
            actions: best_plan,
        };
        memo.insert(key, res.clone());
        res
    }

    /// Exhaustive best plan focusing on efficiency.
    fn best_efficiency_plan(
        &self,
        model: &Model,
        depth: usize,
        memo: &mut HashMap<(State, usize), Plan>,
    ) -> Plan {
        let key = (model.state.clone(), depth);
        if let Some(result) = memo.get(&key) {
            return result.clone();
        }
        if depth == 0 {
            let score = model.calculate_discontentment();
            let res = Plan {
                total_discontentment: score,
                total_time: 0,
                actions: vec![],
            };
            memo.insert(key, res.clone());
            return res;
        }

        let current_score = model.calculate_discontentment();
        let mut best_efficiency = f32::MIN;
        let mut best_time = 0;
        let mut best_discontent = current_score;
        let mut best_plan = vec![];

        for (label, action) in &self.actions {
            if let Some(next_model) = model.apply(label.clone(), action) {
                let sub_plan = self.best_efficiency_plan(&next_model, depth - 1, memo);

                let total_discontent_delta = current_score - sub_plan.total_discontentment;
                let total_time = sub_plan.total_time + action.duration;

                // Calculate efficiency: discontent delta per unit time
                let total_efficiency = total_discontent_delta / total_time.max(1) as f32;

                // Pick the path that yields better efficiency
                if total_efficiency > best_efficiency
                    || (total_efficiency == best_efficiency && total_time < best_time)
                {
                    best_efficiency = total_efficiency;
                    best_time = total_time;
                    best_discontent = sub_plan.total_discontentment;
                    let mut new_plan = sub_plan.actions.clone();
                    new_plan.insert(0, (label.clone(), action.clone()));
                    best_plan = new_plan;
                }
            }
        }

        let res = Plan {
            total_discontentment: best_discontent,
            total_time: best_time,
            actions: best_plan,
        };
        memo.insert(key, res.clone());
        res
    }

    /// Exhaustive best plan using a hybrid strategy.
    fn best_hybrid_plan(
        &self,
        model: &Model,
        depth: usize,
        memo: &mut HashMap<(State, usize), Plan>,
    ) -> Plan {
        let key = (model.state.clone(), depth);
        if let Some(result) = memo.get(&key) {
            return result.clone();
        }
        if depth == 0 {
            let score = model.calculate_discontentment();
            let res = Plan {
                total_discontentment: score,
                total_time: 0,
                actions: vec![],
            };
            memo.insert(key, res.clone());
            return res;
        }

        let current_score = model.calculate_discontentment();
        let mut best_metric = f32::MAX;
        let mut best_time = 0;
        let mut best_plan = vec![];

        for (label, action) in &self.actions {
            if let Some(next_model) = model.apply(label.clone(), action) {
                let discontent_delta = current_score - next_model.calculate_discontentment();
                let efficiency = discontent_delta / action.duration.max(1) as f32;

                // Decide whether to use raw discontent or efficiency
                let use_efficiency = depth > 2 && efficiency > 0.1;
                let metric = if use_efficiency {
                    // Lower is better, so invert efficiency
                    1.0 / (efficiency + 1e-6)
                } else {
                    // Minimizing discontent
                    next_model.calculate_discontentment()
                };

                let mut sub_plan = self.best_hybrid_plan(&next_model, depth - 1, memo);

                // Compare metric to decide best path
                if metric < best_metric
                    || (metric == best_metric && sub_plan.total_time + action.duration < best_time)
                {
                    best_metric = metric;
                    best_time = sub_plan.total_time + action.duration;
                    sub_plan.actions.insert(0, (label.clone(), action.clone()));
                    best_plan = sub_plan.actions.clone();
                }
            }
        }

        let final_discontent = if best_metric != f32::MAX {
            current_score - (1.0 / best_metric)
        } else {
            current_score
        };
        let res = Plan {
            total_discontentment: final_discontent,
            total_time: best_time,
            actions: best_plan,
        };
        memo.insert(key, res.clone());
        res
    }
}

// A helper struct to hold search nodes for A*.
#[derive(Clone)]
struct AStarNode {
    // The cost so far (g-cost) – in this context, the “discontentment” so far.
    cost_so_far: f32,
    // The estimated total cost (f = g + h).
    estimated_total: f32,
    // Time spent for this path.
    time: i32,
    // The current model (state, etc.).
    model: Model,
    // Actions taken to reach this state.
    plan: Vec<(String, Action)>,
}

// We need an ordering so the BinaryHeap picks the smallest estimated_total first.
impl PartialEq for AStarNode {
    fn eq(&self, other: &Self) -> bool {
        self.estimated_total == other.estimated_total
    }
}
impl Eq for AStarNode {}
impl Ord for AStarNode {
    fn cmp(&self, other: &Self) -> Ordering {
        // Flip ordering to make the smallest f-cost the "greatest" priority in the heap
        other
            .estimated_total
            .partial_cmp(&self.estimated_total)
            .unwrap_or(Ordering::Equal)
    }
}
impl PartialOrd for AStarNode {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}