osmgraphing 0.12.0

Playing around with graphs created via parsing OpenStreetMap data
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
use super::paths::Path;
use crate::{
    configs::routing::Config,
    helpers,
    network::{EdgeIdx, Graph, Node, NodeIdx},
};
use std::{cmp::Reverse, collections::BinaryHeap};

/// A bidirectional implementation of Dijkstra's algorithm.
/// This implementation reuses the underlying datastructures to speedup multiple computations.
///
/// This implementation is correct for contracted and non-contracted graphs.
/// However, the performance highly depends on a flag in the config, which has to be provided when computing the best path.
pub struct Dijkstra {
    // general
    is_ch_dijkstra: bool,
    // data-structures for a query
    queue: BinaryHeap<Reverse<CostNode>>,
    costs: [Vec<f64>; 2],
    predecessors: [Vec<Option<EdgeIdx>>; 2],
    is_visited: [Vec<bool>; 2],
    has_found_best_meeting_node: [bool; 2],
}

impl Dijkstra {
    pub fn new() -> Dijkstra {
        Dijkstra {
            is_ch_dijkstra: false,
            queue: BinaryHeap::new(),
            costs: [Vec::new(), Vec::new()],
            predecessors: [Vec::new(), Vec::new()],
            is_visited: [Vec::new(), Vec::new()],
            has_found_best_meeting_node: [false, false],
        }
    }

    fn fwd_idx(&self) -> usize {
        0
    }

    fn bwd_idx(&self) -> usize {
        1
    }

    fn dir_idx(&self, direction: Direction) -> usize {
        match direction {
            Direction::FWD => self.fwd_idx(),
            Direction::BWD => self.bwd_idx(),
        }
    }

    fn opp_dir_idx(&self, direction: Direction) -> usize {
        match direction {
            Direction::FWD => self.bwd_idx(),
            Direction::BWD => self.fwd_idx(),
        }
    }

    /// Resizes existing datastructures storing routing-data, like costs, saving re-allocations.
    fn init_query(&mut self, new_len: usize) {
        // fwd and bwd
        for dir in vec![Direction::FWD, Direction::BWD] {
            let dir = self.dir_idx(dir);
            self.costs[dir].resize(new_len, std::f64::INFINITY);
            self.costs[dir]
                .iter_mut()
                .for_each(|c| *c = std::f64::INFINITY);

            self.predecessors[dir].resize(new_len, None);
            self.predecessors[dir].iter_mut().for_each(|p| *p = None);

            if !self.is_ch_dijkstra {
                self.is_visited[dir].resize(new_len, false);
                self.is_visited[dir].iter_mut().for_each(|v| *v = false);
            }

            self.has_found_best_meeting_node[dir] = false;
        }

        self.queue.clear();
    }

    fn visit(&mut self, costnode: &CostNode) {
        if !self.is_ch_dijkstra {
            self.is_visited[self.dir_idx(costnode.direction)][*costnode.idx] = true
        }
    }

    /// This method is optimized by assuming that the provided CostNode has already been visited.
    fn is_meeting_costnode(&self, costnode: &CostNode) -> bool {
        // Costs are updated when costnodes are enqueued, but costnodes have to be dequeued
        // before they can be considered as visited (for bidir Dijkstra).
        if self.is_ch_dijkstra {
            self.costs[self.opp_dir_idx(costnode.direction)][*costnode.idx] != std::f64::INFINITY
        } else {
            // The CostNode has already been dequeued, which is the reason for this assertion.
            debug_assert!(
                self.is_visited[self.dir_idx(costnode.direction)][*costnode.idx],
                "CostNode should already be visited."
            );
            self.is_visited[self.opp_dir_idx(costnode.direction)][*costnode.idx]
        }
    }

    /// This method returns true, if both queries can't be better.
    fn has_found_best_meeting_node(&self) -> bool {
        self.has_found_best_meeting_node[self.fwd_idx()]
            && self.has_found_best_meeting_node[self.bwd_idx()]
    }

    /// Returns true, if the provided costnode's cost are better than the registered cost for this
    /// node-idx (and for this query-direction).
    fn has_costnode_improved(&self, costnode: &CostNode) -> bool {
        costnode.cost <= self.costs[self.dir_idx(costnode.direction)][*costnode.idx]
    }

    /// Returns the cost of a path, so cost(src->v) + cost(v->dst)
    fn total_cost(&self, costnode: &CostNode) -> f64 {
        self.costs[self.fwd_idx()][*costnode.idx] + self.costs[self.bwd_idx()][*costnode.idx]
    }

    /// None means no path exists, whereas an empty path is a path from a node to itself.
    pub fn compute_best_path(
        &mut self,
        src: &Node,
        dst: &Node,
        graph: &Graph,
        cfg: &Config,
    ) -> Option<Path> {
        if cfg.dim() <= 0 {
            panic!("Best path should be computed, but no metric is specified.");
        }

        self.is_ch_dijkstra = cfg.is_ch_dijkstra();

        //----------------------------------------------------------------------------------------//
        // initialization-stuff

        let nodes = graph.nodes();
        let xwd_edges = {
            debug_assert_eq!(
                0,
                self.dir_idx(Direction::FWD),
                "Direction-Idx of FWD is expected to be 0."
            );
            debug_assert_eq!(
                1,
                self.dir_idx(Direction::BWD),
                "Direction-Idx of BWD is expected to be 1."
            );
            [graph.fwd_edges(), graph.bwd_edges()]
        };
        self.init_query(nodes.count());
        let mut best_meeting: Option<(NodeIdx, f64)> = None;

        //----------------------------------------------------------------------------------------//
        // prepare first iteration(s)

        // push src-node
        self.queue.push(Reverse(CostNode {
            idx: src.idx(),
            cost: 0.0,
            direction: Direction::FWD,
        }));
        // push dst-node
        self.queue.push(Reverse(CostNode {
            idx: dst.idx(),
            cost: 0.0,
            direction: Direction::BWD,
        }));
        // update fwd-stats
        self.costs[self.fwd_idx()][*src.idx()] = 0.0;
        // update bwd-stats
        self.costs[self.bwd_idx()][*dst.idx()] = 0.0;

        //----------------------------------------------------------------------------------------//
        // search for shortest path

        while let Some(Reverse(current)) = self.queue.pop() {
            // For non-contracted graphs, this could be an slight improvement.
            // For contracted graphs, this is the only stop-criterion.
            // This is needed, because the bidirectional Dijkstra processes sub-graphs,
            // which are not equal.
            // This leads to the possibility, that shortest-paths of a sub-graph could be
            // non-optimal for the total graph, even if both sub-queries (forward and backward) have
            // already found a common meeting-node.
            //
            // Paths in sub-graphs have only one direction wrt node-level, namely up for fwd-graph
            // and down for bwd-graph.
            // This leads to weight-inbalanced queries, leading to solutions, which are optimal only
            // for the sub-graphs, not for the whole graph.
            if self.has_found_best_meeting_node() {
                break;
            }

            // distinguish between fwd and bwd
            let dir = self.dir_idx(current.direction);

            // First occurrence has improved, because init-value is infinity.
            // -> Replaces check if current CostNode has already been visited.
            if !self.has_costnode_improved(&current) {
                continue;
            }
            // otherwise, mark CostNode as visitted
            self.visit(&current);

            // if meeting-node is already found
            // -> check if new meeting-node is better
            if let Some((_meeting_node, best_total_cost)) = best_meeting {
                // if cost of single-queue is more expensive than best meeting-node
                // -> This can't be improved anymore
                if current.cost > best_total_cost {
                    self.has_found_best_meeting_node[dir] = true;
                    continue;
                }

                let new_total_cost = self.total_cost(&current);
                if new_total_cost < best_total_cost {
                    best_meeting = Some((current.idx, new_total_cost));
                }
            } else
            // if meeting-node is found for the first time, remember it
            if self.is_meeting_costnode(&current) {
                let new_total_cost = self.total_cost(&current);
                best_meeting = Some((current.idx, new_total_cost));
            }

            // update costs and add predecessors of nodes, which are dst of current's leaving edges
            let leaving_edges = match xwd_edges[dir].starting_from(current.idx) {
                Some(e) => e,
                None => continue,
            };
            for leaving_edge in leaving_edges {
                if self.is_ch_dijkstra
                    && nodes.level(current.idx) > nodes.level(leaving_edge.dst_idx())
                {
                    // break because leaving-edges are sorted by level
                    break;
                }

                let new_cost = current.cost
                    + helpers::dot_product(
                        &cfg.alphas(),
                        &leaving_edge.metrics(&cfg.metric_indices()),
                    );
                if new_cost < self.costs[dir][*leaving_edge.dst_idx()] {
                    self.predecessors[dir][*leaving_edge.dst_idx()] = Some(leaving_edge.idx());
                    self.costs[dir][*leaving_edge.dst_idx()] = new_cost;

                    // if path is found
                    // -> Run until queue is empty
                    //    since the shortest path could have longer hop-distance
                    //    with shorter weight-distance than currently found node.
                    // -> Only for bidirectional Dijkstra, but not incorrect for CH-Dijkstra.
                    //    The CH-Dijkstra has to continue until the global best meeting-node has
                    //    been found (see above).
                    if self.is_ch_dijkstra || best_meeting.is_none() {
                        self.queue.push(Reverse(CostNode {
                            idx: leaving_edge.dst_idx(),
                            cost: new_cost,
                            direction: current.direction,
                        }));
                    }
                }
            }
        }

        //----------------------------------------------------------------------------------------//
        // create path if found

        if let Some((meeting_node_idx, _best_total_cost)) = best_meeting {
            let mut proto_path = Vec::new();

            // iterate backwards over fwd-path
            let mut cur_idx = meeting_node_idx;
            let dir = self.fwd_idx();
            let opp_dir = self.bwd_idx();
            while let Some(incoming_idx) = self.predecessors[dir][*cur_idx] {
                proto_path.push(incoming_idx);

                // get incoming edge, but reversed to get the forward's src-node
                cur_idx = xwd_edges[opp_dir].dst_idx(incoming_idx);
            }

            // take fwd-part in the right order
            proto_path.reverse();

            // iterate backwards over bwd-path
            let mut cur_idx = meeting_node_idx;
            let dir = self.bwd_idx();
            let opp_dir = self.fwd_idx();
            while let Some(leaving_idx) = self.predecessors[dir][*cur_idx] {
                proto_path.push(leaving_idx);

                // get leaving edge, but reversed to get the backward's src-node
                cur_idx = xwd_edges[opp_dir].dst_idx(leaving_idx);
            }

            Some(Path::new(src.idx(), dst.idx(), proto_path))
        } else {
            None
        }
    }
}

#[derive(Copy, Clone, Debug)]
enum Direction {
    FWD,
    BWD,
}

#[derive(Clone)]
struct CostNode {
    idx: NodeIdx,
    cost: f64,
    direction: Direction,
}

mod costnode {
    use super::{CostNode, Direction};
    use crate::helpers::{ApproxCmp, ApproxEq};
    use std::{
        cmp::Ordering,
        fmt::{self, Display},
    };

    impl Display for CostNode {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{{ idx: {}, cost: {}, {} }}",
                self.idx, self.cost, self.direction
            )
        }
    }

    impl Ord for CostNode {
        fn cmp(&self, other: &CostNode) -> Ordering {
            self.cost
                .approx_cmp(&other.cost)
                .then_with(|| self.idx.cmp(&other.idx))
                .then_with(|| self.direction.cmp(&other.direction))
        }
    }

    impl PartialOrd for CostNode {
        fn partial_cmp(&self, other: &CostNode) -> Option<Ordering> {
            Some(
                self.cost
                    .approx_partial_cmp(&other.cost)?
                    .then_with(|| self.idx.cmp(&other.idx))
                    .then_with(|| self.direction.cmp(&other.direction)),
            )
        }
    }

    impl Eq for CostNode {}

    impl PartialEq for CostNode {
        fn eq(&self, other: &CostNode) -> bool {
            self.idx == other.idx
                && self.direction == other.direction
                && self.cost.approx_eq(&other.cost)
        }
    }

    impl Display for Direction {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{}",
                match self {
                    Direction::FWD => "forward",
                    Direction::BWD => "backward",
                }
            )
        }
    }

    impl Ord for Direction {
        fn cmp(&self, other: &Direction) -> Ordering {
            let self_value = match self {
                Direction::FWD => 1,
                Direction::BWD => -1,
            };
            let other_value = match other {
                Direction::FWD => 1,
                Direction::BWD => -1,
            };
            self_value.cmp(&other_value)
        }
    }

    impl PartialOrd for Direction {
        fn partial_cmp(&self, other: &Direction) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }

    impl Eq for Direction {}

    impl PartialEq for Direction {
        fn eq(&self, other: &Direction) -> bool {
            self.cmp(other) == Ordering::Equal
        }
    }
}