Skip to main content

planning_heuristics/
lib.rs

1//! # Planning heuristics
2//!
3//! Goal-distance estimates for classical planning tasks given as [`sas_plus::Task`].
4//!
5//! ## Implemented
6//!
7//! - [`HMax`], [`HAdd`] and [`HFf`]: the delete-relaxation heuristics `h_max`, `h_add` and
8//!   `h_FF` (Bonet & Geffner 2001, Hoffmann & Nebel 2001), computed with a worklist
9//!   (generalized Dijkstra) over [`Relaxation`], which also extracts [`RelaxedPlan`]s and their preferred operators.
10//! - [`HSetAdd`]: the set-additive heuristic `h_sa` (Keyder & Geffner 2008); [`AdditiveHMax`]:
11//!   `h_max` summed over a partition of the operator costs; [`H2`]: `h²` (Haslum & Geffner
12//!   2000), which sees pairs of facts.
13//! - [`RedBlack`]: the red-black heuristic `h_RB` (Domshlak, Hoffmann &
14//!   Katz 2015), with the black variables chosen by a [`Painting`]: greedy, or by the papers'
15//!   strategies ([`Strategy`]) for an arcless or acyclic black causal graph ([`Fragment`]), or
16//!   a few black variables searched as a product ([`Painting::bounded`]); options
17//!   ([`RedBlackOptions`]) for relaxed plan forwarding and red facts following.
18//! - [`Relaxation::landmarks`]: `h^1` fact landmarks with natural orderings
19//!   ([`LandmarkGraph`]), and the heuristics [`LandmarkCount`] (inadmissible) and
20//!   [`UniformLandmarkCost`] (admissible, uniform cost partitioning); with feature `lp`,
21//!   `OptimalLandmarkCost` (admissible, optimal cost partitioning by linear programming).
22//! - With feature `lp`, `OperatorCounting`: the state equation (Bonet 2013) and LM-cut
23//!   landmarks as an operator-counting LP (Pommerening, Röger, Helmert & Bonet 2014).
24//! - [`Relaxation::rhw_landmarks`]: back-chained landmarks (Richter, Helmert & Westphal 2008)
25//!   with disjunctive and domain-transition-graph landmarks, as [`OrderedLandmarks`] with
26//!   natural, greedy-necessary and reasonable orderings ([`OrderingKind`]).
27//! - [`PathHeuristic`]: path-dependent heuristics; [`LandmarkTracker`] tracks the accepted
28//!   landmarks along the search path, as LAMA's landmark count or admissibly with uniform cost
29//!   partitioning; with feature `lp`, `OptimalLandmarkTracker` with optimal cost partitioning.
30//!   The optimal cost partitioning over LM-cut's action landmarks is `OperatorCounting` with
31//!   only the LM-cut constraints (its LP dual).
32//! - [`LmCut`]: the landmark-cut heuristic `h_LM-cut` (Helmert & Domshlak 2009), admissible,
33//!   with its disjunctive [`ActionLandmark`]s.
34//! - [`AbstractionHeuristic`]: goal distances of any [`sas_plus::Abstraction`], such as a
35//!   pattern database over a [`sas_plus::Projection`]. Features `merge-and-shrink` and
36//!   `cost-partition` implement [`GoalDistance`] for those crates' heuristics, which
37//!   [`Adapter`] turns into a [`Heuristic`].
38//! - [`RelevanceScore`]: the relevance score `h_Ξ` (Kim & Sridharan 2024), landmark-like and
39//!   inadmissible, over a partially explored backtracking tree.
40//! - [`GoalIntention`]: goal-intention ranking keys `ol_rp` / `op_rp` (Rosa et al. 2026) from
41//!   sampled relaxed plans, tracked along a path with [`IntentionTracker`].
42//!
43//! ## Example
44//!
45//! ```
46//! use planning_heuristics::{HAdd, HFf, HMax, Heuristic, Relaxation};
47//! use sas_plus::{Effect, Fact, Operator, State, Task, Variable};
48//!
49//! // One binary variable, one operator that sets it, goal `v0 = 1`.
50//! let task = Task {
51//!     variables: vec![Variable::new("v0", 2)],
52//!     operators: vec![Operator {
53//!         name: "set".into(),
54//!         pre: vec![Fact::new(0, 0)],
55//!         eff: vec![Effect::new(Fact::new(0, 1))],
56//!         cost: 3,
57//!     }],
58//!     initial: State(vec![0]),
59//!     goal: vec![Fact::new(0, 1)],
60//!     axioms: vec![],
61//! };
62//! let state = task.initial_state();
63//!
64//! // Each heuristic owns its relaxation ...
65//! assert_eq!(HMax::new(&task).estimate(&state), Some(3));
66//!
67//! // ... or borrows a shared one; one heuristic per thread, the relaxation is `Sync`.
68//! let relaxation = Relaxation::new(&task);
69//! let mut h_add = HAdd::with_relaxation(&relaxation);
70//! let mut h_ff = HFf::with_relaxation(&relaxation);
71//! assert_eq!(h_add.estimate(&state), Some(3));
72//! assert_eq!(h_ff.value(&state), Some(3_u64));
73//! ```
74//!
75//! Repository: <https://github.com/sunsided/planning-heuristics>
76
77#![no_std]
78#![forbid(unsafe_code)]
79
80extern crate alloc;
81
82mod abstraction;
83mod additive_max;
84mod delete_relaxation;
85mod h2;
86mod intention;
87mod landmarks;
88mod lm_cut;
89mod math;
90#[cfg(feature = "lp")]
91mod operator_counting;
92mod path;
93mod red_black;
94mod relaxation;
95mod relevance;
96mod rng;
97mod set_additive;
98
99pub use abstraction::{AbstractionHeuristic, Adapter, GoalDistance};
100pub use additive_max::AdditiveHMax;
101pub use delete_relaxation::{HAdd, HFf, HMax};
102pub use h2::{H2, H2Error};
103pub use intention::{GoalIntention, IntentionKind, IntentionParams, IntentionTracker, Observation};
104pub use landmarks::{
105    AcceptedLandmarks, LandmarkCount, LandmarkGraph, LandmarkOrdering, LandmarkTracker,
106    OrderedLandmarks, OrderingKind, TrackingMode, UniformLandmarkCost,
107};
108#[cfg(feature = "lp")]
109pub use landmarks::{OptimalLandmarkCost, OptimalLandmarkTracker};
110pub use lm_cut::{ActionLandmark, LmCut};
111#[cfg(feature = "lp")]
112pub use operator_counting::{CountingConstraints, OperatorCounting};
113pub use path::{PathHeuristic, Stateless};
114pub use red_black::{
115    CausalLevel, Fragment, MAX_BOUNDED_PRODUCT, Painting, PaintingError, RedBlack, RedBlackOptions,
116    RedBlackPlan, Strategy,
117};
118pub use relaxation::{Combine, Exploration, Relaxation, RelaxedPlan};
119pub use relevance::{RelevanceParams, RelevanceScore};
120pub use set_additive::HSetAdd;
121// The shared trait of the planning crates: `planning_heuristics::Heuristic` and
122// `search_core::Heuristic` are one trait. A doc comment here would be prepended to all three.
123pub use search_core::{Heuristic, HeuristicFn, heuristic_fn};
124
125/// Compile-time check: the shared tables are `Sync`, the per-thread heuristics are `Send`.
126const _: () = {
127    /// Accepts only `Sync` types.
128    const fn sync<T: Sync>() {}
129    /// Accepts only `Send` types.
130    const fn send<T: Send>() {}
131    sync::<Relaxation>();
132    send::<HMax<'static>>();
133    send::<HAdd<'static>>();
134    send::<HFf<'static>>();
135    send::<LandmarkCount<'static>>();
136    send::<UniformLandmarkCost<'static>>();
137    send::<RedBlack<'static>>();
138    send::<LmCut<'static>>();
139    send::<HSetAdd<'static>>();
140    send::<AdditiveHMax>();
141    send::<H2>();
142    send::<LandmarkTracker<'static>>();
143    send::<AbstractionHeuristic<sas_plus::Projection>>();
144};
145
146/// Doctests for the code examples in README.md (not part of the API).
147#[cfg(doctest)]
148#[doc = include_str!("../README.md")]
149mod readme_doctests {}