rusty_planner/
planner.rs

1use std::hash;
2
3// TODO: check usage of &str + lifetime vs String.
4// TODO: check if we can have a generic PDDL parser --> ProblemSpace.
5
6///
7/// Public trait which - once implemented - describes the problem space to solve.
8///
9pub trait ProblemSpace {
10    /// Defines the type of your state.
11    type State: Copy + Eq + hash::Hash;
12    /// Iterator for containing states and cost/utility to transition to it.
13    type Iter: Iterator<Item = (Self::State, f64)>; // action_id & cost.
14
15    /// Heuristic function to calculate the "distance" between two states.
16    fn heuristic(&self, _: &Self::State, _: &Self::State) -> f64;
17    /// Given a state calculates the successor states.
18    fn succ(&self, _: &Self::State) -> Self::Iter;
19    /// Given a state calculates the predecessor states.
20    fn pred(&self, _: &Self::State) -> Self::Iter;
21}
22
23///
24/// Trait for lifelong planning algorithms.
25///
26pub trait Lifelong: ProblemSpace {
27    /// Called when obstacle was detected, or start state transitioned.
28    fn update(&mut self, _: &Self::State) {}
29}
30
31///
32/// Trait to enable anytime algorithms to signal back results.
33///
34pub trait Anytime: ProblemSpace {
35    /// Callback to signal - for example - a partial result.
36    fn callback(&mut self, _: &Self::State);
37}
38
39///
40/// Trait for multi-agent/system planning algorithms.
41///
42pub trait SharedStates: ProblemSpace {
43    /// Determine if a given state is a public state.
44    fn is_public(&self, _: &Self::State) -> bool;
45    /// Serialize a state into a string.
46    /// * `msg_type` - Type of the message.
47    /// * `state` - The state to serialize.
48    /// * `para` - Vector for parameters such as g, rhs and h values.
49    fn serialize(&self, msg_type: u8, _: &Self::State, _: Vec<f64>) -> String;
50    /// Serialize a string into a state.
51    fn deserialize(&self, _: String) -> (u8, Self::State, Vec<f64>);
52}
53
54#[cfg(test)]
55mod tests {
56    use std::vec;
57
58    use crate::planner::Lifelong;
59    use crate::planner::ProblemSpace;
60    use crate::planner::SharedStates;
61
62    struct Environment {}
63
64    impl ProblemSpace for Environment {
65        type State = usize;
66        type Iter = vec::IntoIter<(Self::State, f64)>;
67        fn heuristic(&self, _: &Self::State, _: &Self::State) -> f64 {
68            0.0
69        }
70        fn succ(&self, _: &Self::State) -> Self::Iter {
71            vec![].into_iter()
72        }
73        fn pred(&self, s: &Self::State) -> Self::Iter {
74            self.succ(s)
75        }
76    }
77
78    impl Lifelong for Environment {
79        // use default...
80    }
81
82    impl SharedStates for Environment {
83        fn is_public(&self, _: &Self::State) -> bool {
84            false
85        }
86        fn serialize(&self, _: u8, _: &Self::State, _: Vec<f64>) -> String {
87            String::from("0")
88        }
89        fn deserialize(&self, _: String) -> (u8, Self::State, Vec<f64>) {
90            (0, 1, vec![])
91        }
92    }
93
94    // Test for success.
95
96    #[test]
97    fn test_update_for_success() {
98        let mut env = Environment {};
99        env.update(&1);
100    }
101
102    #[test]
103    fn test_traits_for_success() {
104        let mut env = Environment {};
105        env.update(&1);
106        env.heuristic(&0, &1);
107        env.succ(&0);
108        env.pred(&0);
109        env.is_public(&0);
110        env.serialize(0, &0, vec![]);
111        env.deserialize(String::from("0"));
112    }
113}