miniplan 0.1.1

A PDDL planner library built around the pddl crate, with grounding and search utilities
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
use crate::search::HValue;
use crate::search::Heuristic;
use crate::task::{FactId, OpId, State, Task};

/// The h^add heuristic (Bonet & Geffner, 2001).
///
/// Sums the relaxed costs of all goal facts, assuming subgoals are independent.
/// Not admissible (can overestimate), but often more informed than h^max.
///
/// # Examples
///
/// ```
/// use miniplan::heuristic::HAdd;
/// use miniplan::search::Heuristic;
///
/// assert_eq!(HAdd.name(), "hadd");
/// ```
pub struct HAdd;

/// The h^max heuristic (Bonet & Geffner, 2001).
///
/// Takes the maximum relaxed cost among all goal facts.
/// Admissible but often weak (underestimates significantly).
///
/// # Examples
///
/// ```
/// use miniplan::heuristic::HMax;
/// use miniplan::search::Heuristic;
///
/// assert_eq!(HMax.name(), "hmax");
/// ```
pub struct HMax;

/// The FF heuristic (Hoffmann & Nebel, 2001).
///
/// Extracts a relaxed plan from the RPG and sums its operator costs.
/// Not admissible but highly informative in practice.
///
/// # Examples
///
/// ```
/// use miniplan::heuristic::HFF;
/// use miniplan::search::Heuristic;
///
/// assert_eq!(HFF.name(), "hff");
/// ```
pub struct HFF;

impl Heuristic for HAdd {
    fn name(&self) -> &str {
        "hadd"
    }

    fn estimate(&self, task: &Task, state: &State) -> HValue {
        let rpg = build_rpg(task, state);
        let mut cost = 0.0;
        for bit in task.goal_pos.0.ones() {
            cost += rpg.fact_cost[bit];
        }
        HValue(cost)
    }
}

impl Heuristic for HMax {
    fn name(&self) -> &str {
        "hmax"
    }

    fn estimate(&self, task: &Task, state: &State) -> HValue {
        let rpg = build_rpg(task, state);
        let mut max_cost: f64 = 0.0;
        for bit in task.goal_pos.0.ones() {
            max_cost = max_cost.max(rpg.fact_cost[bit]);
        }
        HValue(max_cost)
    }
}

impl Heuristic for HFF {
    fn name(&self) -> &str {
        "hff"
    }

    fn estimate(&self, task: &Task, state: &State) -> HValue {
        let rpg = build_rpg(task, state);
        let cost = extract_relaxed_plan_cost(task, &rpg);
        HValue(cost)
    }

    fn preferred_ops(&self, _task: &Task, _state: &State) -> &[OpId] {
        static EMPTY: [OpId; 0] = [];
        &EMPTY
    }
}

#[allow(dead_code)]
struct RpgLevel {
    facts: Vec<FactId>,
    applicable_ops: Vec<usize>,
}

#[allow(dead_code)]
struct RpgResult {
    fact_cost: Vec<f64>,
    achiever: Vec<Option<usize>>,
    levels: Vec<RpgLevel>,
}

fn build_rpg(task: &Task, state: &State) -> RpgResult {
    let num_facts = task.num_facts();
    let mut fact_cost = vec![f64::INFINITY; num_facts];
    let mut achiever: Vec<Option<usize>> = vec![None; num_facts];
    let mut levels: Vec<RpgLevel> = Vec::new();

    let mut achieved = fixedbitset::FixedBitSet::with_capacity(num_facts);

    for bit in state.0.ones() {
        fact_cost[bit] = 0.0;
        achieved.set(bit, true);
    }

    let mut changed = true;
    while changed {
        changed = false;
        let mut level_facts = Vec::new();
        let mut level_ops = Vec::new();

        for (op_idx, op) in task.operators.iter().enumerate() {
            let pre_satisfied = op.pre_pos.0.ones().all(|b| achieved.contains(b));
            let neg_satisfied = op.pre_neg.0.ones().all(|b| !achieved.contains(b));

            if pre_satisfied && neg_satisfied {
                let _op_cost = if level_ops.is_empty() {
                    op.pre_pos
                        .0
                        .ones()
                        .map(|b| fact_cost[b])
                        .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                        .unwrap_or(0.0)
                } else {
                    0.0
                };

                let cost = op.cost as f64 + op.pre_pos.0.ones().map(|b| fact_cost[b]).sum::<f64>();

                for bit in op.add.0.ones() {
                    if cost < fact_cost[bit] {
                        fact_cost[bit] = cost;
                        achiever[bit] = Some(op_idx);
                        if !achieved.contains(bit) {
                            achieved.set(bit, true);
                            level_facts.push(FactId(bit));
                            changed = true;
                        }
                    }
                }

                for cond in &op.conditional {
                    let cond_satisfied = cond.cond_pos.0.ones().all(|b| achieved.contains(b))
                        && cond.cond_neg.0.ones().all(|b| !achieved.contains(b));

                    if cond_satisfied {
                        let cond_cost =
                            cost + cond.cond_pos.0.ones().map(|b| fact_cost[b]).sum::<f64>();

                        for bit in cond.add.0.ones() {
                            if cond_cost < fact_cost[bit] {
                                fact_cost[bit] = cond_cost;
                                achiever[bit] = Some(op_idx);
                                if !achieved.contains(bit) {
                                    achieved.set(bit, true);
                                    level_facts.push(FactId(bit));
                                    changed = true;
                                }
                            }
                        }
                    }
                }

                level_ops.push(op_idx);
            }
        }

        if !level_facts.is_empty() || !level_ops.is_empty() {
            levels.push(RpgLevel {
                facts: level_facts,
                applicable_ops: level_ops,
            });
        }

        if levels.len() > num_facts * 2 {
            break;
        }
    }

    RpgResult {
        fact_cost,
        achiever,
        levels,
    }
}

pub(crate) fn rpg_fact_costs(task: &Task, state: &State) -> Vec<f64> {
    build_rpg(task, state).fact_cost
}

fn extract_relaxed_plan_cost(task: &Task, rpg: &RpgResult) -> f64 {
    let mut marked_ops = std::collections::HashSet::new();
    let mut total_cost = 0.0;

    let mut goals_to_achieve: Vec<FactId> = task
        .goal_pos
        .0
        .ones()
        .filter(|&b| rpg.fact_cost[b] < f64::INFINITY)
        .map(FactId)
        .collect();

    let mut visited = std::collections::HashSet::new();

    while let Some(fact) = goals_to_achieve.pop() {
        if visited.contains(&fact) {
            continue;
        }
        visited.insert(fact);

        if let Some(Some(op_idx)) = rpg.achiever.get(fact.0).copied()
            && !marked_ops.contains(&op_idx)
        {
            marked_ops.insert(op_idx);
            if let Some(op) = task.operators.get(op_idx) {
                total_cost += op.cost as f64;
                for pre_bit in op.pre_pos.0.ones() {
                    if !visited.contains(&FactId(pre_bit)) {
                        goals_to_achieve.push(FactId(pre_bit));
                    }
                }
            }
        }
    }

    total_cost
}

pub(crate) fn rpg_backward_fact_costs(task: &Task, goal_pos: &State, goal_neg: &State) -> Vec<f64> {
    let num_facts = task.num_facts();
    let mut back_cost = vec![f64::INFINITY; num_facts];

    for b in goal_pos.0.ones() {
        back_cost[b] = 0.0;
    }

    let _ = goal_neg;

    let mut changed = true;
    while changed {
        changed = false;
        for op in &task.operators {
            if op.add.0.is_empty() {
                continue;
            }
            let mut max_add_cost = 0.0f64;
            let mut all_finite = true;
            for b in op.add.0.ones() {
                let c = back_cost[b];
                if c == f64::INFINITY {
                    all_finite = false;
                    break;
                }
                if c > max_add_cost {
                    max_add_cost = c;
                }
            }
            if !all_finite {
                continue;
            }
            let op_total = max_add_cost + op.cost as f64;
            for p in op.pre_pos.0.ones() {
                if op_total < back_cost[p] {
                    back_cost[p] = op_total;
                    changed = true;
                }
            }
        }
    }

    back_cost
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::task::{Fact, FactId, OpId, Operator, Task, TaskMeta, TypeHierarchy};
    use rustc_hash::FxHashMap;

    fn make_test_task(
        num_facts: usize,
        init_bits: &[usize],
        goal_pos_bits: &[usize],
        goal_neg_bits: &[usize],
        operators: Vec<Operator>,
    ) -> Task {
        let mut facts = Vec::new();
        let mut fact_index = FxHashMap::default();
        for i in 0..num_facts {
            let fact = Fact {
                predicate: format!("f{}", i),
                args: vec![],
            };
            let id = FactId(i);
            fact_index.insert(fact.clone(), id);
            facts.push(fact);
        }
        let mut init = State::new(num_facts);
        for &b in init_bits {
            init.0.set(b, true);
        }
        let mut goal_pos = State::new(num_facts);
        for &b in goal_pos_bits {
            goal_pos.0.set(b, true);
        }
        let mut goal_neg = State::new(num_facts);
        for &b in goal_neg_bits {
            goal_neg.0.set(b, true);
        }
        Task {
            facts,
            fact_index,
            operators,
            init,
            goal_pos,
            goal_neg,
            objects: vec![],
            types: TypeHierarchy::new(),
            metadata: TaskMeta {
                domain_name: "test".to_string(),
                problem_name: "test".to_string(),
                requirements: vec![],
            },
        }
    }

    fn make_op(
        id: usize,
        name: &str,
        pre_pos: &[usize],
        pre_neg: &[usize],
        add: &[usize],
        del: &[usize],
        cost: u32,
    ) -> Operator {
        let s = |bits: &[usize], size: usize| -> State {
            let mut state = State::new(size);
            for &b in bits {
                state.0.set(b, true);
            }
            state
        };
        Operator {
            id: OpId(id),
            name: name.to_string(),
            pre_pos: s(pre_pos, 10),
            pre_neg: s(pre_neg, 10),
            add: s(add, 10),
            del: s(del, 10),
            conditional: vec![],
            cost,
        }
    }

    #[test]
    fn test_backward_rpg_goal_facts_cost_zero() {
        let task = make_test_task(
            3,
            &[0],
            &[2],
            &[],
            vec![
                make_op(0, "op0", &[0], &[], &[1], &[], 1),
                make_op(1, "op1", &[1], &[], &[2], &[], 1),
            ],
        );
        let costs = rpg_backward_fact_costs(&task, &task.goal_pos, &task.goal_neg);
        assert_eq!(costs[2], 0.0);
    }

    #[test]
    fn test_backward_rpg_unreachable_fact_stays_infinity() {
        let task = make_test_task(
            4,
            &[0],
            &[3],
            &[],
            vec![
                make_op(0, "op0", &[0], &[], &[1], &[], 1),
                make_op(1, "op1", &[1], &[], &[3], &[], 1),
            ],
        );
        let costs = rpg_backward_fact_costs(&task, &task.goal_pos, &task.goal_neg);
        assert_eq!(costs[2], f64::INFINITY);
    }

    #[test]
    fn test_backward_rpg_two_step_regression() {
        let task = make_test_task(
            4,
            &[0],
            &[3],
            &[],
            vec![
                make_op(0, "op0", &[0], &[], &[1], &[], 1),
                make_op(1, "op1", &[1], &[], &[3], &[], 1),
            ],
        );
        let costs = rpg_backward_fact_costs(&task, &task.goal_pos, &task.goal_neg);
        assert!(costs[1] >= 1.0);
        assert!(costs[0] >= 2.0);
    }
}