rusty_planner 0.1.2

Artificial intelligence planning & reasoning library for Rust.
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
use std::cmp;
use std::collections;
use std::thread;
use std::time;

use crate::planner;
use crate::util;

use rusty_agent::agent;

struct StateValues {
    g_val: f64,
    h_val: f64,
}

fn process_message<PS: planner::ProblemSpace>(
    ps: &PS,
    s: PS::State,
    para: StateValues,
    goal: PS::State,
    data: &mut collections::HashMap<PS::State, StateValues>,
    open: &mut collections::BinaryHeap<util::HeapEntry<PS::State>>,
    closed: &mut collections::HashMap<PS::State, f64>,
) {
    // TODO: once open has contains method - remove this!
    let mut in_open: bool = false;
    for item in open.iter() {
        if item.state == s {
            in_open = true;
            break;
        }
    }
    if !(in_open && closed.contains_key(&s)) || data[&s].g_val > para.g_val {
        open.push(util::HeapEntry::new_entry(s, (para.g_val, 0.0)));
        let h_val = cmp::max(ps.heuristic(&s, &goal) as i64, para.h_val as i64);
        data.insert(
            s,
            StateValues {
                g_val: para.g_val,
                h_val: h_val as f64,
            },
        );
    }
}

fn expand<A: agent::Agent, PS: planner::ProblemSpace + planner::SharedStates>(
    ps: &PS,
    agent: &A,
    s: PS::State,
    goal: PS::State,
    data: &mut collections::HashMap<PS::State, StateValues>,
    open: &mut collections::BinaryHeap<util::HeapEntry<PS::State>>,
    closed: &mut collections::HashMap<PS::State, f64>,
) -> bool {
    // if we've found the goal --> tell others.
    if s == goal {
        agent.broadcast(&ps.serialize(0, &s, vec![data[&s].g_val, data[&s].h_val]));
        closed.insert(s, data[&s].g_val + ps.heuristic(&s, &goal));
        return true;
    }

    // if we've found a public state --> tell others.
    if ps.is_public(&s)
        && (!closed.contains_key(&s)
            || closed.get(&s).unwrap() > &(data[&s].g_val + ps.heuristic(&s, &goal)))
    {
        agent.broadcast(&ps.serialize(0, &s, vec![data[&s].g_val, data[&s].h_val]))
    }

    // Add to closed list.
    closed.insert(s, data[&s].g_val + ps.heuristic(&s, &goal));

    // check successors.
    for item in ps.succ(&s) {
        let s_dash: PS::State = item.0;
        let g_val: f64 = data[&s].g_val + item.1;
        let h_val: f64 = ps.heuristic(&s_dash, &goal);
        let f_val: f64 = g_val + h_val;
        if data.contains_key(&s_dash) {
            // in case another action already extended this one only keep better path!
            if data[&s_dash].g_val > g_val {
                data.insert(s_dash, StateValues { g_val, h_val });
            }
        } else {
            data.insert(s_dash, StateValues { g_val, h_val });
        }
        let mut old_f_val = -1.0;
        if closed.contains_key(&s_dash) {
            old_f_val = *closed.get(&s_dash).unwrap();
        }
        if !closed.contains_key(&s_dash) || (old_f_val > 0.0 && f_val < old_f_val) {
            open.push(util::HeapEntry::new_entry(s_dash, (f_val, 0.0)))
        }
    }
    false
}

fn traceback<A: agent::Agent, PS: planner::ProblemSpace + planner::SharedStates>(
    agent: &A,
    ps: &PS,
    start: PS::State,
    goal: PS::State,
    closed: collections::HashMap<PS::State, f64>,
) -> Vec<PS::State> {
    let mut res = Vec::new();
    let mut done = false;
    let mut curr: PS::State = goal;

    while !done {
        let preds = ps.pred(&curr);
        if preds.count() == 0 {
            let revd: Vec<String> = agent.retrieve();
            if !revd.is_empty() {
                for msg in revd {
                    let (msg_id, p_state, _) = ps.deserialize(msg);
                    if msg_id == 1 {
                        curr = p_state;
                    }
                }
            } else {
                thread::sleep(time::Duration::from_millis(250));
                continue;
            }
        }

        // find next shortest step to take...
        let mut min_cost = f64::INFINITY;
        let mut next_state = curr;
        for pred in ps.pred(&curr) {
            if closed.contains_key(&pred.0) && (closed[&pred.0] + pred.1) < min_cost {
                min_cost = closed[&pred.0] + pred.1;
                next_state = pred.0;
            }
        }
        res.push(next_state);
        curr = next_state;

        // if we found a public state tell ngbh. sys.
        if ps.is_public(&curr) {
            agent.broadcast(&ps.serialize(1, &curr, vec![]));
            done = true;
        }

        // we are done if we found the start too.
        if curr == start {
            done = true;
        }
    }
    res.reverse(); // traceback -> so let's reverse...
    res
}

///
/// Find a plan for multiple collaborative systems/agents.
///
pub fn solve<A: agent::Agent, PS: planner::ProblemSpace + planner::SharedStates>(
    agent: &A,
    ps: &PS,
    start: PS::State,
    goal: PS::State,
) -> Vec<PS::State> {
    let mut done: bool = false;

    // Open and closed state lists.
    let mut open: collections::BinaryHeap<util::HeapEntry<PS::State>> =
        collections::BinaryHeap::new();
    let mut closed: collections::HashMap<PS::State, f64> = collections::HashMap::new();
    let mut data: collections::HashMap<PS::State, StateValues> = collections::HashMap::new();

    // insert start into closed...
    data.insert(
        start,
        StateValues {
            g_val: 0.0,
            h_val: ps.heuristic(&start, &goal),
        },
    );
    open.push(util::HeapEntry::new_entry(start, (0.0, 0.0)));

    while !done {
        let revd: Vec<String> = agent.retrieve();
        if !revd.is_empty() {
            for msg in revd {
                let s: (u8, PS::State, Vec<f64>) = ps.deserialize(msg);
                if s.1 == goal {
                    done = true;
                    break;
                }
                process_message(
                    ps,
                    s.1,
                    StateValues {
                        g_val: s.2[0],
                        h_val: s.2[1],
                    },
                    goal,
                    &mut data,
                    &mut open,
                    &mut closed,
                );
            }
            if done {
                break;
            }
        }
        if !open.is_empty() {
            let s: util::HeapEntry<PS::State> = open.pop().unwrap();
            done = expand(ps, agent, s.state, goal, &mut data, &mut open, &mut closed);
        }
        thread::sleep(time::Duration::from_millis(250));
    }
    traceback(agent, ps, start, goal, closed)
}

#[cfg(test)]
mod tests {
    use std::collections;
    use std::vec;

    use crate::mad_astar;
    use crate::planner;
    use crate::util;

    use rusty_agent::agent;

    struct SimpleExample {}

    impl planner::ProblemSpace for SimpleExample {
        type State = i32;
        type Iter = vec::IntoIter<(i32, f64)>;

        fn heuristic(&self, _: &Self::State, _: &Self::State) -> f64 {
            0.0
        }

        fn succ(&self, state: &Self::State) -> Self::Iter {
            match *state {
                0 => vec![(1, 0.7), (2, 1.0)].into_iter(),
                1 => vec![(3, 1.0), (2, 0.1)].into_iter(),
                _ => vec![(3, 0.2)].into_iter(),
            }
        }

        fn pred(&self, state: &Self::State) -> Self::Iter {
            match *state {
                3 => vec![(1, 1.0), (2, 0.2)].into_iter(),
                5 => vec![(4, 1.0)].into_iter(),
                6 => vec![(5, 1.0)].into_iter(),
                _ => vec![].into_iter(),
            }
        }
    }

    impl planner::SharedStates for SimpleExample {
        fn is_public(&self, state: &Self::State) -> bool {
            let mut res = false;
            if *state == 2 || *state == 1 || *state == 4 {
                res = true;
            }
            res
        }

        fn serialize(&self, msg_type: u8, state: &Self::State, para: Vec<f64>) -> String {
            let mut string_list: Vec<String> = vec![msg_type.to_string(), state.to_string()];
            for item in para {
                string_list.push(item.to_string());
            }
            string_list.join(";")
        }

        fn deserialize(&self, msg: String) -> (u8, Self::State, Vec<f64>) {
            let split = msg.split(';');
            let data = split.collect::<Vec<&str>>();

            let msg_type = data[0].parse::<u8>().unwrap();
            let state: i32 = data[1].parse::<i32>().unwrap();

            (msg_type, state, vec![])
        }
    }

    struct SimpleAgent {
        msgs: vec::Vec<String>,
    }

    impl agent::Agent for SimpleAgent {
        fn retrieve(&self) -> Vec<String> {
            self.msgs.clone()
        }

        fn broadcast(&self, _: &str) {}
    }

    // Test for success.

    #[test]
    fn test_process_message_for_success() {
        let ps = SimpleExample {};
        let vals = mad_astar::StateValues {
            g_val: 1.0,
            h_val: 1.0,
        };
        let mut data: collections::HashMap<i32, mad_astar::StateValues> =
            collections::HashMap::new();
        let mut open: collections::BinaryHeap<util::HeapEntry<i32>> =
            collections::BinaryHeap::new();
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();

        mad_astar::process_message(&ps, 1, vals, 3, &mut data, &mut open, &mut closed);
    }

    #[test]
    fn test_expand_for_success() {
        let ps = SimpleExample {};
        let agent = SimpleAgent {
            msgs: vec![String::from("foo")],
        };

        let mut data: collections::HashMap<i32, mad_astar::StateValues> =
            collections::HashMap::new();
        let mut open: collections::BinaryHeap<util::HeapEntry<i32>> =
            collections::BinaryHeap::new();
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();

        let s = 1;
        data.insert(
            s,
            mad_astar::StateValues {
                g_val: 1.0,
                h_val: 0.3,
            },
        );
        mad_astar::expand(&ps, &agent, s, 3, &mut data, &mut open, &mut closed);
    }

    #[test]
    fn test_traceback_for_success() {
        let ps = SimpleExample {};
        let agent = SimpleAgent { msgs: vec![] };
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();

        closed.insert(0, 1.0);
        closed.insert(1, 1.0);
        closed.insert(2, 1.0);

        mad_astar::traceback(&agent, &ps, 0, 3, closed);
    }

    #[test]
    fn test_solve_for_success() {
        let ps = SimpleExample {};
        let agent = SimpleAgent { msgs: vec![] };
        mad_astar::solve(&agent, &ps, 0, 3);
    }

    // Test for failure.

    // Test for sanity.

    #[test]
    fn test_process_message_for_sanity() {
        let ps = SimpleExample {};
        let vals = mad_astar::StateValues {
            g_val: 1.0,
            h_val: 1.0,
        };
        let mut data: collections::HashMap<i32, mad_astar::StateValues> =
            collections::HashMap::new();
        let mut open: collections::BinaryHeap<util::HeapEntry<i32>> =
            collections::BinaryHeap::new();
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();

        // not previously seen this state --> should update g and h val.
        let s = 1;
        mad_astar::process_message(&ps, s, vals, 3, &mut data, &mut open, &mut closed);
        assert_eq!(data[&s].g_val, 1.0);
        assert_eq!(data[&s].h_val, 1.0);

        // now state is in closed & open --> should not change anything.
        closed.insert(s, 1.0);
        let vals_1 = mad_astar::StateValues {
            g_val: 1.0,
            h_val: 1.0,
        };
        mad_astar::process_message(&ps, s, vals_1, 3, &mut data, &mut open, &mut closed);
        assert_eq!(data[&s].g_val, 1.0);
        assert_eq!(data[&s].h_val, 1.0);

        // newly received state has a better g_val --> should update g_val
        let vals = mad_astar::StateValues {
            g_val: 0.5,
            h_val: 1.0,
        };
        mad_astar::process_message(&ps, s, vals, 3, &mut data, &mut open, &mut closed);
        assert_eq!(data[&s].g_val, 0.5);
        assert_eq!(data[&s].h_val, 1.0);

        // newly received state has a worse g_val --> should NOT update g_val
        let vals = mad_astar::StateValues {
            g_val: 2.0,
            h_val: 1.0,
        };
        mad_astar::process_message(&ps, s, vals, 3, &mut data, &mut open, &mut closed);
        assert_eq!(data[&s].g_val, 0.5);
        assert_eq!(data[&s].h_val, 1.0);
    }

    #[test]
    fn test_expand_for_sanity() {
        let ps = SimpleExample {};
        let agent = SimpleAgent {
            msgs: vec![String::from("foo")],
        };

        let mut data: collections::HashMap<i32, mad_astar::StateValues> =
            collections::HashMap::new();
        let mut open: collections::BinaryHeap<util::HeapEntry<i32>> =
            collections::BinaryHeap::new();
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();

        // expand start state.
        data.insert(
            0,
            mad_astar::StateValues {
                g_val: 0.0,
                h_val: 0.0,
            },
        );
        let res = mad_astar::expand(&ps, &agent, 0, 3, &mut data, &mut open, &mut closed);
        assert_eq!(res, false);

        // found a public state.
        let res = mad_astar::expand(&ps, &agent, 1, 3, &mut data, &mut open, &mut closed);
        assert_eq!(res, false);
        assert_eq!(data[&1].g_val, 0.7);

        // found a better path.
        mad_astar::expand(&ps, &agent, 2, 3, &mut data, &mut open, &mut closed);
        assert_eq!(data[&2].g_val as i64, 0.8 as i64);

        // already expanded...
        mad_astar::expand(&ps, &agent, 1, 3, &mut data, &mut open, &mut closed);
        assert_eq!(data[&2].g_val as i64, 0.8 as i64);

        // found the goal.
        let res = mad_astar::expand(&ps, &agent, 3, 3, &mut data, &mut open, &mut closed);
        assert_eq!(res, true);
        assert_eq!(closed.contains_key(&3), true);
    }

    #[test]
    fn test_traceback_for_sanity() {
        let ps = SimpleExample {};
        let agent = SimpleAgent { msgs: vec![] };
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();

        closed.insert(4, 0.0);
        closed.insert(5, 1.0);
        closed.insert(6, 2.0);

        // simple traceback: start to finish!
        let res = mad_astar::traceback(&agent, &ps, 4, 6, closed);
        assert_eq!(res, vec![4, 5]);

        // goal state unknown - trigger by other agent
        let agent = SimpleAgent {
            msgs: vec!["1;6".to_string()],
        };
        let mut closed: collections::HashMap<i32, f64> = collections::HashMap::new();
        closed.insert(4, 0.0);
        closed.insert(5, 1.0);
        closed.insert(6, 2.0);
        let res = mad_astar::traceback(&agent, &ps, 4, 7, closed);
        assert_eq!(res, vec![4, 5]);
    }

    #[test]
    fn test_solve_for_sanity() {
        let ps = SimpleExample {};
        let agent = SimpleAgent { msgs: vec![] };

        // simple example.
        let res = mad_astar::solve(&agent, &ps, 0, 3);
        assert_eq!(res, vec![2]);
    }
}