rpid 0.3.1

Rust Programmable Interface for Domain-Independent Dynamic Programming (RPID)
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
use super::SearchNode;
use crate::dp::{Dominance, DpMut};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use std::cmp::Ordering;
use std::collections::hash_map::Entry;
use std::hash::Hash;
use std::ops::Deref;
use std::rc::Rc;

/// Data structure to store search nodes and remove dominated nodes.
pub struct StateRegistry<K, N> {
    map: FxHashMap<K, SmallVec<[Rc<N>; 1]>>,
}

impl<K, I> Default for StateRegistry<K, I> {
    fn default() -> Self {
        Self {
            map: FxHashMap::default(),
        }
    }
}

/// Result of inserting a node into the registry.
pub struct InsertionResult<N> {
    /// The inserted node.
    pub inserted: Option<Rc<N>>,
    /// The nodes that were dominated by the inserted node.
    pub dominated: SmallVec<[Rc<N>; 1]>,
}

impl<N> Default for InsertionResult<N> {
    #[inline]
    fn default() -> Self {
        Self {
            inserted: None,
            dominated: SmallVec::default(),
        }
    }
}

struct RemoveResult<N> {
    dominated: SmallVec<[Rc<N>; 1]>,
    same_state_index: Option<usize>,
}

impl<K, N, D, S, C> StateRegistry<K, N>
where
    K: Hash + Eq,
    N: SearchNode<DpData = D, State = S, CostType = C>,
    D: DpMut<State = S, CostType = C> + Dominance<State = S, Key = K>,
    C: Ord + Copy,
{
    /// Creates a new state registry with the given capacity.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            map: FxHashMap::with_capacity_and_hasher(capacity, Default::default()),
        }
    }

    fn remove_dominated(
        list: &mut SmallVec<[Rc<N>; 1]>,
        dp: &mut D,
        state: &S,
        cost: C,
    ) -> Option<RemoveResult<N>> {
        let mut dominated_indices = SmallVec::<[usize; 1]>::default();
        let mut same_state_index = None;

        for (i, v) in list.iter().enumerate() {
            let other_cost = v.get_cost(dp);
            let other = v.get_state(dp);

            match dp.compare(state, other) {
                Some(Ordering::Less) | Some(Ordering::Equal)
                    if !dp.is_better_cost(cost, other_cost) =>
                {
                    return None;
                }
                Some(Ordering::Equal) => {
                    same_state_index = Some(dominated_indices.len());
                    dominated_indices.push(i);
                }
                Some(Ordering::Greater) if !dp.is_better_cost(other_cost, cost) => {
                    dominated_indices.push(i);
                }
                _ => {}
            }
        }

        let dominated = dominated_indices
            .into_iter()
            .rev()
            .map(|i| list.swap_remove(i))
            .collect::<SmallVec<_>>();
        let same_state_index = same_state_index.map(|i| dominated.len() - i - 1);

        Some(RemoveResult {
            dominated,
            same_state_index,
        })
    }

    /// Inserts a node into the registry if it is not dominated by any other node.
    pub fn insert_if_not_dominated(&mut self, dp: &mut D, mut node: N) -> InsertionResult<N> {
        match self.map.entry(dp.get_key(node.get_state(dp))) {
            Entry::Occupied(entry) => {
                // Update the key of the state by the already stored key to reduce memory usage.
                dp.update_key(node.get_state_mut(dp), entry.key());

                let list = entry.into_mut();
                let result =
                    Self::remove_dominated(list, dp, node.get_state(dp), node.get_cost(dp));

                if result.is_none() {
                    return InsertionResult::default();
                }

                let result = result.unwrap();
                let inserted = Rc::from(node);
                list.push(inserted.clone());

                InsertionResult {
                    inserted: Some(inserted),
                    dominated: result.dominated,
                }
            }
            Entry::Vacant(entry) => {
                let inserted = Rc::new(node);
                entry.insert(SmallVec::from_vec(vec![inserted.clone()]));

                InsertionResult {
                    inserted: Some(inserted),
                    dominated: SmallVec::default(),
                }
            }
        }
    }

    /// Inserts a node created from a state and a cost by a constructor into the registry if it is not dominated by any other node.
    ///
    /// The constructor may use the information of a node that has the same state as the new node.
    /// If the constructor returns `None`, the node is not inserted.
    pub fn insert_with_if_not_dominated(
        &mut self,
        dp: &mut D,
        mut state: S,
        cost: C,
        constructor: impl FnOnce(&mut D, S, C, Option<&N>) -> Option<N>,
    ) -> InsertionResult<N> {
        match self.map.entry(dp.get_key(&state)) {
            Entry::Occupied(entry) => {
                // Update the key of the state by the already stored key to reduce memory usage.
                dp.update_key(&mut state, entry.key());

                let list = entry.into_mut();
                let result = Self::remove_dominated(list, dp, &state, cost);

                if result.is_none() {
                    return InsertionResult::default();
                }

                let result = result.unwrap();
                let same_state_information =
                    result.same_state_index.map(|i| result.dominated[i].deref());
                let node = constructor(dp, state, cost, same_state_information);

                let inserted = if let Some(node) = node {
                    let inserted = Rc::from(node);
                    list.push(inserted.clone());

                    Some(inserted)
                } else {
                    None
                };

                InsertionResult {
                    inserted,
                    dominated: result.dominated,
                }
            }
            Entry::Vacant(entry) => {
                if let Some(node) = constructor(dp, state, cost, None) {
                    let inserted = Rc::new(node);
                    entry.insert(SmallVec::from_vec(vec![inserted.clone()]));

                    InsertionResult {
                        inserted: Some(inserted),
                        dominated: SmallVec::default(),
                    }
                } else {
                    InsertionResult::default()
                }
            }
        }
    }

    pub fn clear(&mut self) {
        self.map.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dp::{Dominance, Dp};
    use crate::solvers::search_algorithms::CostNode;

    struct MockDp;

    impl Dp for MockDp {
        type State = (i32, i32, i32);
        type CostType = i32;
        type Label = usize;

        fn get_target(&self) -> Self::State {
            (0, 0, 0)
        }

        fn get_successors(
            &self,
            _: &Self::State,
        ) -> impl IntoIterator<Item = (Self::State, Self::CostType, Self::Label)> {
            vec![]
        }

        fn get_base_cost(&self, _: &Self::State) -> Option<Self::CostType> {
            None
        }
    }

    impl Dominance for MockDp {
        type State = (i32, i32, i32);
        type Key = i32;

        fn get_key(&self, state: &Self::State) -> Self::Key {
            state.0
        }

        fn compare(&self, a: &Self::State, b: &Self::State) -> Option<Ordering> {
            if a.1 == b.1 && a.2 == b.2 {
                Some(Ordering::Equal)
            } else if a.1 <= b.1 && a.2 <= b.2 {
                Some(Ordering::Greater)
            } else if a.1 >= b.1 && a.2 >= b.2 {
                Some(Ordering::Less)
            } else {
                None
            }
        }
    }

    #[test]
    fn test_insert_if_not_dominated() {
        let mut registry = StateRegistry::default();
        let mut dp = MockDp;

        let state = (7, 7, 7);
        let cost = 7;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 7, 7));
        assert_eq!(node.get_cost(&dp), 7);
        assert!(result.dominated.is_empty());

        // Different key.
        let state = (6, 8, 8);
        let cost = 8;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(6, 8, 8));
        assert_eq!(node.get_cost(&dp), 8);
        assert!(result.dominated.is_empty());

        // Incomparable due to the state.
        let state = (7, 6, 6);
        let cost = 8;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 6, 6));
        assert_eq!(node.get_cost(&dp), 8);
        assert!(result.dominated.is_empty());

        // Incomparable due to the cost.
        let state = (7, 7, 8);
        let cost = 6;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 7, 8));
        assert_eq!(node.get_cost(&dp), 6);
        assert!(result.dominated.is_empty());

        // Dominated by the first node due to the cost.
        let state = (7, 7, 7);
        let cost = 8;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_none());
        assert!(result.dominated.is_empty());

        // Dominated by the first node due to the state.
        let state = (7, 8, 7);
        let cost = 7;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_none());
        assert!(result.dominated.is_empty());

        // Replaces two nodes.
        let state = (7, 7, 7);
        let cost = 6;
        let node = CostNode::create_root(&dp, state, cost);
        let result = registry.insert_if_not_dominated(&mut dp, node);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 7, 7));
        assert_eq!(node.get_cost(&dp), 6);
        assert_eq!(result.dominated.len(), 2);
        let mut dominated = result.dominated;
        dominated.sort_by_key(|n| n.get_cost(&dp));
        assert_eq!(dominated[0].get_state(&dp), &(7, 7, 8));
        assert_eq!(dominated[0].get_cost(&dp), 6);
        assert_eq!(dominated[1].get_state(&dp), &(7, 7, 7));
        assert_eq!(dominated[1].get_cost(&dp), 7);
    }

    #[test]
    fn test_insert_with_if_not_dominated() {
        let mut registry = StateRegistry::default();
        let mut dp = MockDp;
        let constructor =
            |dp: &mut _, state, cost, _: Option<&_>| Some(CostNode::create_root(dp, state, cost));

        let state = (7, 7, 7);
        let cost = 7;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 7, 7));
        assert_eq!(node.get_cost(&dp), 7);
        assert!(result.dominated.is_empty());

        // Different key.
        let state = (6, 8, 8);
        let cost = 8;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(6, 8, 8));
        assert_eq!(node.get_cost(&dp), 8);
        assert!(result.dominated.is_empty());

        // Incomparable due to the state.
        let state = (7, 6, 6);
        let cost = 8;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 6, 6));
        assert_eq!(node.get_cost(&dp), 8);
        assert!(result.dominated.is_empty());

        // Incomparable due to the cost.
        let state = (7, 7, 8);
        let cost = 6;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 7, 8));
        assert_eq!(node.get_cost(&dp), 6);
        assert!(result.dominated.is_empty());

        // Dominated by the first node due to the cost.
        let state = (7, 7, 7);
        let cost = 8;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_none());
        assert!(result.dominated.is_empty());

        // Dominated by the first node due to the state.
        let state = (7, 8, 7);
        let cost = 7;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_none());
        assert!(result.dominated.is_empty());

        // Replaces two nodes.
        let state = (7, 7, 7);
        let cost = 6;
        let result = registry.insert_with_if_not_dominated(&mut dp, state, cost, constructor);
        assert!(result.inserted.is_some());
        let node = result.inserted.unwrap();
        assert_eq!(node.get_state(&dp), &(7, 7, 7));
        assert_eq!(node.get_cost(&dp), 6);
        assert_eq!(result.dominated.len(), 2);
        let mut dominated = result.dominated;
        dominated.sort_by_key(|n| n.get_cost(&dp));
        assert_eq!(dominated[0].get_state(&dp), &(7, 7, 8));
        assert_eq!(dominated[0].get_cost(&dp), 6);
        assert_eq!(dominated[1].get_state(&dp), &(7, 7, 7));
        assert_eq!(dominated[1].get_cost(&dp), 7);
    }
}