routee-compass-core 0.19.2

The core routing algorithms and data structures of the RouteE-Compass energy-aware routing engine
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
use super::{MemoryUnit, TerminationModelError};
use crate::{
    algorithm::search::SearchTree,
    util::duration_extension::{deserialize_duration, DurationExtension},
};
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

/// the termination model for the application should be evaluated at the top of each iteration
/// of a search. if it returns true, an error response should be created for the user using the
/// explain method.
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(tag = "type")]
pub enum TerminationModel {
    /// terminates a query if the runtime exceeds some limit.
    /// only checks at some provided iteration frequency, since the computation is expensive.
    #[serde(rename = "query_runtime")]
    QueryRuntimeLimit {
        #[serde(deserialize_with = "deserialize_duration")]
        limit: Duration,
        frequency: Option<u64>,
    },
    /// terminates if the size of the solution exceeds (greater than) some limit
    #[serde(rename = "solution_size")]
    SolutionSizeLimit { limit: usize },
    /// terminates if the number of iterations exceeds (greater than) some limit
    /// iterations begin at 0, so we add 1 to the iteration to make this comparison
    #[serde(rename = "iterations")]
    IterationsLimit { limit: u64 },
    /// terminates a query if the solution size exceeds some limit.
    /// only checks at some provided iteration frequency, since the computation is expensive.
    #[serde(rename = "ram")]
    MemoryLimit {
        limit: f64,
        unit: MemoryUnit,
        frequency: Option<u64>,
    },
    #[serde(rename = "combined")]
    Combined { models: Vec<TerminationModel> },
}

impl TerminationModel {
    /// Tests if the search should terminate. If it should terminate, return a message
    /// explaining the reason to terminate. If it should not terminate, return None.
    pub fn continue_or_explain(
        &self,
        start_time: &Instant,
        solution: &SearchTree,
        iterations: u64,
    ) -> Option<String> {
        let should_terminate = self.should_terminate(start_time, solution, iterations);
        if should_terminate {
            let explanation = self.explain(start_time, solution, iterations);
            Some(explanation)
        } else {
            None
        }
    }

    /// Tests if the search should terminate. If it should terminate, generate a useful
    /// termination message and return that in the error channel. If it should not terminate,
    /// returns Ok(()).
    pub fn continue_or_error(
        &self,
        start_time: &Instant,
        solution: &SearchTree,
        iterations: u64,
    ) -> Result<(), TerminationModelError> {
        let should_terminate = self.should_terminate(start_time, solution, iterations);
        if should_terminate {
            let explanation = self.explain(start_time, solution, iterations);
            return Err(TerminationModelError::QueryTerminated(explanation));
        }
        Ok(())
    }

    /// predicate to test whether a query should terminate based on application-level configurations.
    /// evaluates before traversing a link.
    pub fn should_terminate(
        &self,
        start_time: &Instant,
        solution: &SearchTree,
        iteration: u64,
    ) -> bool {
        use TerminationModel as T;
        match self {
            T::QueryRuntimeLimit { limit, frequency } => match frequency {
                Some(freq) if !iteration.is_multiple_of(*freq) => false,
                _ => {
                    let dur = Instant::now().duration_since(*start_time);
                    dur > *limit
                }
            },
            T::SolutionSizeLimit { limit } => {
                // if you add one more branch to the tree it would violate this termination criteria
                solution.len() >= *limit
            }
            T::IterationsLimit { limit } => {
                // if you perform one more iteration it would violate this termination criteria
                iteration >= *limit
            }
            T::MemoryLimit {
                limit,
                unit,
                frequency,
            } => match frequency {
                Some(freq) if !iteration.is_multiple_of(*freq) => false,
                _ => {
                    let root_bytes = allocative::size_of_unique(solution) as f64;
                    let node_bytes = solution
                        .nodes()
                        .map(|n| allocative::size_of_unique(n) as f64)
                        .sum::<f64>();
                    let label_bytes = solution
                        .labels()
                        .map(|l| allocative::size_of_unique(&l) as f64)
                        .sum::<f64>();
                    let memory_bytes = root_bytes + node_bytes + label_bytes;
                    let memory = unit.convert(memory_bytes);
                    &memory > limit
                }
            },
            T::Combined { models } => models.iter().fold(false, |acc, m| {
                let inner = m.should_terminate(start_time, solution, iteration);
                acc || inner
            }),
        }
    }

    /// this method will a string explaining why a model terminated. if the
    /// conditions do not merit termination, then the result will be None.
    pub fn explain(&self, start_time: &Instant, solution: &SearchTree, iterations: u64) -> String {
        use TerminationModel as T;
        // must test if this particular [`TerminationModel`] variant instance was the cause of
        // termination, in the case of [`TerminationModel::Combined`].
        let caused_termination = self.should_terminate(start_time, solution, iterations);
        match self {
            T::Combined { models } => {
                let combined_explanations: String = models
                    .iter()
                    .map(|m| m.explain(start_time, solution, iterations))
                    .filter(|m| !m.is_empty())
                    .collect::<Vec<_>>()
                    .join(", ");
                if combined_explanations.is_empty() {
                    "".to_string()
                } else {
                    combined_explanations
                }
            }
            T::QueryRuntimeLimit { limit, frequency } => match (caused_termination, frequency) {
                (true, None) => format!("exceeded runtime limit of {}", limit.hhmmss()),
                (true, Some(freq)) => format!(
                    "exceeded runtime limit of {} tested every {freq} iterations",
                    limit.hhmmss()
                ),
                (false, _) => "".to_string(),
            },
            T::SolutionSizeLimit { limit } => {
                if caused_termination {
                    format!("exceeded solution size limit of {limit}")
                } else {
                    "".to_string()
                }
            }
            T::IterationsLimit { limit } => {
                if caused_termination {
                    format!("exceeded iteration limit of {limit}")
                } else {
                    "".to_string()
                }
            }
            T::MemoryLimit {
                limit,
                unit,
                frequency,
            } => match (caused_termination, frequency) {
                (true, None) => format!("exceeded memory limit of {limit} {unit}"),
                (true, Some(freq)) => format!(
                    "exceeded memory limit of {limit} {unit} tested every {freq} iterations"
                ),
                (false, _) => "".to_string(),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::{Duration, Instant};

    use crate::{
        algorithm::search::{Direction, EdgeTraversal, SearchTree},
        model::{
            cost::TraversalCost,
            label::Label,
            network::{EdgeId, EdgeListId, VertexId},
            termination::MemoryUnit,
            unit::Cost,
        },
    };

    use super::TerminationModel as T;

    #[test]
    fn test_within_runtime_limit() {
        let within_limit = Duration::from_secs(1);
        let start_time = Instant::now() - within_limit;
        let limit = Duration::from_secs(2);
        let frequency = 10;
        let tree = mock_tree(0);

        let m = T::QueryRuntimeLimit {
            limit,
            frequency: Some(frequency),
        };

        for iteration in 0..(frequency + 1) {
            let result = m.should_terminate(&start_time, &tree, iteration);
            // in all iterations, the result should be false, though for iterations 1-9, that will be due to the sample frequency
            assert!(!result);
        }
    }

    #[test]
    fn test_exceeds_runtime_limit() {
        let exceeds_limit = Duration::from_secs(3);
        let start_time = Instant::now() - exceeds_limit;
        let limit = Duration::from_secs(2);
        let frequency = 10;
        let tree = mock_tree(0);

        let m = T::QueryRuntimeLimit {
            limit,
            frequency: Some(frequency),
        };

        for iteration in 0..(frequency + 1) {
            let result = m.should_terminate(&start_time, &tree, iteration);
            if iteration == 0 {
                // edge case. when iteration == 0, we will run the test, and it should fail, since 10 % 0 == 0 is true.
                // but let's continue testing iterations 1-10 to explore the expected range of behaviors.
                assert!(result);
            } else if iteration != frequency {
                // from iterations 1 to 9, terminate is false because of the frequency argument of 10
                // bypasses the runtime test
                assert!(!result);
            } else {
                // on iteration 10, terminate is true because "exceeds_limit_time" is greater than the limit duration
                assert!(result);
            }
        }
    }

    #[test]
    fn test_iterations_limit() {
        let m = T::IterationsLimit { limit: 5 };
        let i = Instant::now();

        let t4 = mock_tree(4);
        let t5 = mock_tree(5);
        let t6 = mock_tree(6);

        let good = m.should_terminate(&i, &t4, 4);
        let terminate1 = m.should_terminate(&i, &t5, 5);
        let terminate2 = m.should_terminate(&i, &t6, 6);
        assert!(!good);
        assert!(terminate1);
        assert!(terminate2);
    }

    #[test]
    fn test_solution_size_limit() {
        // solution size limit of 5 should accept tree of size 4 + 5, fail on size 6
        let m = T::SolutionSizeLimit { limit: 5 };
        let i = Instant::now();
        let t4 = mock_tree(4);
        let t5 = mock_tree(5);
        let t6 = mock_tree(6);

        let good1 = m.should_terminate(&i, &t4, 4);
        let terminate1 = m.should_terminate(&i, &t5, 5);
        let terminate2 = m.should_terminate(&i, &t6, 6);
        assert!(!good1);
        assert!(terminate1);
        assert!(terminate2);
    }

    #[test]
    fn test_memory_limit() {
        let m = T::MemoryLimit {
            limit: 0.01,
            unit: MemoryUnit::Megabytes,
            frequency: None,
        };
        let i = Instant::now();
        let empty_tree = mock_tree(0);
        let fuller_tree = mock_tree(100); // larger than 0.01MB == 10KB

        let good = m.should_terminate(&i, &empty_tree, 4);
        let terminate = m.should_terminate(&i, &fuller_tree, 5);

        assert!(!good);
        assert!(terminate);
    }

    #[test]
    fn test_combined_3() {
        let exceeds_limit = Duration::from_secs(3);
        let start_time = Instant::now() - exceeds_limit;
        let runtime_limit = Duration::from_secs(2);
        let frequency = 1;
        let iteration_limit = 5;
        let solution_limit = 3;
        let tree = mock_tree(solution_limit + 1);

        let m1 = T::QueryRuntimeLimit {
            limit: runtime_limit,
            frequency: Some(frequency),
        };
        let m2 = T::IterationsLimit {
            limit: iteration_limit,
        };
        let m3 = T::SolutionSizeLimit {
            limit: solution_limit,
        };
        let cm = T::Combined {
            models: vec![m1, m2, m3],
        };
        let terminate = cm.should_terminate(&start_time, &tree, iteration_limit + 1);
        assert!(terminate);
        let msg = cm.explain(&start_time, &tree, iteration_limit + 1);
        let expected = [
            "exceeded runtime limit of 0:00:02.000 tested every 1 iterations",
            "exceeded iteration limit of 5",
            "exceeded solution size limit of 3",
        ]
        .join(", ");
        assert_eq!(msg, expected);
    }

    #[test]
    fn test_combined_2_of_3() {
        let exceeds_limit = Duration::from_secs(3);
        let start_time = Instant::now() - exceeds_limit;
        let runtime_limit = Duration::from_secs(2);
        let frequency = 1;
        let iteration_limit = 5;
        let solution_limit = 3;
        let tree = mock_tree(solution_limit - 1);

        let m1 = T::QueryRuntimeLimit {
            limit: runtime_limit,
            frequency: Some(frequency),
        };
        let m2 = T::IterationsLimit {
            limit: iteration_limit,
        };
        let m3 = T::SolutionSizeLimit {
            limit: solution_limit,
        };
        let cm = T::Combined {
            models: vec![m1, m2, m3],
        };
        let terminate = cm.should_terminate(&start_time, &tree, iteration_limit + 1);
        assert!(terminate);
        let msg = cm.explain(&start_time, &tree, iteration_limit + 1);
        let expected = [
            "exceeded runtime limit of 0:00:02.000 tested every 1 iterations",
            "exceeded iteration limit of 5",
        ]
        .join(", ");
        assert_eq!(msg, expected);
    }

    fn mock_tree(size: usize) -> SearchTree {
        let mut tree = SearchTree::new_stateful(Direction::Forward);
        if size == 0 {
            return tree;
        }
        // when creating the tree, it will create a root node, so len() will be mock_tree's size + 1
        for idx in 0..(size - 1) {
            let cost = TraversalCost {
                objective_cost: Cost::MIN_COST,
                edge_cost: Cost::MIN_COST,
                #[cfg(feature = "detailed_costs")]
                cost_component: std::collections::HashMap::new(),
            };
            let edge_traversal = EdgeTraversal {
                edge_list_id: EdgeListId(0),
                edge_id: EdgeId(idx),
                cost,
                result_state: vec![],
            };
            tree.insert_trajectory(
                Label::Vertex(VertexId(idx)),
                edge_traversal,
                Label::Vertex(VertexId(idx + 1)),
            )
            .expect("test invariant failed")
        }
        tree
    }

    #[test]
    fn test_deserialize_runtime_limit_from_string() {
        // Test deserialization from hh:mm:ss string format
        let json_str = r#"{"type": "query_runtime", "limit": "01:30:45", "frequency": 10}"#;
        let result: Result<T, _> = serde_json::from_str(json_str);
        if let Err(e) = &result {
            println!("Error: {:?}", e);
        }
        assert!(result.is_ok());

        if let Ok(T::QueryRuntimeLimit { limit, frequency }) = result {
            assert_eq!(limit, Duration::from_secs(3600 + 30 * 60 + 45)); // 1:30:45 = 5445 seconds
            assert_eq!(frequency, Some(10));
        } else {
            panic!("Expected QueryRuntimeLimit variant");
        }
    }

    #[test]
    fn test_deserialize_runtime_limit_from_seconds() {
        // Test deserialization from numeric seconds format (backward compatibility)
        let json_str = r#"{"type": "query_runtime", "limit": 5445, "frequency": 10}"#;
        let result: Result<T, _> = serde_json::from_str(json_str);
        if let Err(e) = &result {
            println!("Error: {:?}", e);
        }
        assert!(result.is_ok());

        if let Ok(T::QueryRuntimeLimit { limit, frequency }) = result {
            assert_eq!(limit, Duration::from_secs(5445));
            assert_eq!(frequency, Some(10));
        } else {
            panic!("Expected QueryRuntimeLimit variant");
        }
    }
}