| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Itinero.Network; |
| | 4 | | using Itinero.Network.Enumerators.Edges; |
| | 5 | | using Itinero.Routing.DataStructures; |
| | 6 | |
|
| | 7 | | namespace Itinero.Routing.Flavours.Dijkstra.Bidirectional; |
| | 8 | |
|
| | 9 | | internal abstract class DijkstraAlgorithm |
| | 10 | | { |
| 18 | 11 | | private readonly PathTree _tree = new(); |
| 18 | 12 | | private readonly Dictionary<VertexId, (uint p, double cost)> _settled = []; |
| 18 | 13 | | private readonly BinaryHeap<uint> _heap = new(); |
| | 14 | | protected readonly RoutingNetworkEdgeEnumerator _enumerator; |
| | 15 | | protected readonly RoutingNetwork _network; |
| | 16 | |
|
| 18 | 17 | | protected DijkstraAlgorithm(RoutingNetwork network) |
| 18 | 18 | | { |
| 18 | 19 | | _network = network; |
| | 20 | |
|
| 18 | 21 | | _enumerator = network.GetEdgeEnumerator(); |
| 18 | 22 | | } |
| | 23 | |
|
| 22 | 24 | | internal RoutingNetwork RoutingNetwork => _network; |
| | 25 | |
|
| | 26 | | protected abstract bool OnQueued(uint visit, EdgeId edge, (double cost, double turnCost) edgeCost, VertexId vertex, |
| | 27 | |
|
| | 28 | | protected abstract bool OnSettled(uint visit, VertexId vertex, double cost); |
| | 29 | |
|
| | 30 | | protected abstract (double cost, double turnCost) GetCost(RoutingNetworkEdgeEnumerator edgeEnumerator, |
| | 31 | | IEnumerable<(EdgeId edge, byte? turn)> previousEdges); |
| | 32 | |
|
| | 33 | | internal bool TryGetVisit(VertexId vertex, out (uint p, double cost) visit) |
| 40 | 34 | | { |
| 40 | 35 | | return _settled.TryGetValue(vertex, out visit); |
| 40 | 36 | | } |
| | 37 | |
|
| | 38 | | internal void Clear() |
| 0 | 39 | | { |
| 0 | 40 | | _heap.Clear(); |
| 0 | 41 | | _tree.Clear(); |
| 0 | 42 | | _settled.Clear(); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | internal (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) GetVisit(uint pointer) |
| 4 | 46 | | { |
| 4 | 47 | | return _tree.GetVisit(pointer); |
| 4 | 48 | | } |
| | 49 | |
|
| | 50 | | internal uint Push(EdgeId edgeId, bool forward, double cost) |
| 36 | 51 | | { |
| 36 | 52 | | if (!_enumerator.MoveTo(edgeId, forward)) throw new Exception($"Edge not found!"); |
| | 53 | |
|
| 36 | 54 | | var v = _tree.AddVisit(_enumerator, uint.MaxValue); |
| 36 | 55 | | _heap.Push(v, cost); |
| 36 | 56 | | return v; |
| 36 | 57 | | } |
| | 58 | |
|
| | 59 | | internal (uint pointer, (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) visit, double |
| 40 | 60 | | { |
| 40 | 61 | | var currentPointer = _heap.Pop(out var currentCost); |
| 40 | 62 | | var currentVisit = _tree.GetVisit(currentPointer); |
| 40 | 63 | | while (!_settled.TryAdd(currentVisit.vertex, (currentPointer, currentCost))) |
| 0 | 64 | | { |
| 0 | 65 | | currentPointer = uint.MaxValue; |
| 0 | 66 | | if (_heap.Count == 0) break; |
| | 67 | |
|
| 0 | 68 | | currentPointer = _heap.Pop(out currentCost); |
| 0 | 69 | | currentVisit = _tree.GetVisit(currentPointer); |
| 0 | 70 | | } |
| | 71 | |
|
| 40 | 72 | | return (currentPointer, currentVisit, currentCost); |
| 40 | 73 | | } |
| | 74 | |
|
| | 75 | | internal bool Step(uint pointer, (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) visi |
| 40 | 76 | | { |
| | 77 | | // log settled and see if we need to continue. |
| 40 | 78 | | if (!this.OnSettled(pointer, visit.vertex, cost)) return false; |
| | 79 | |
|
| | 80 | | // check neighbours. |
| 40 | 81 | | if (!_enumerator.MoveTo(visit.vertex)) return true; |
| 84 | 82 | | while (_enumerator.MoveNext()) |
| 44 | 83 | | { |
| | 84 | | // filter out if U-turns or visits on the same edge. |
| 44 | 85 | | var neighbourEdge = _enumerator.EdgeId; |
| 84 | 86 | | if (neighbourEdge == visit.edge) continue; |
| | 87 | |
|
| | 88 | | // gets the cost of the current edge. |
| 4 | 89 | | var (neighbourCost, turnCost) = this.GetCost(_enumerator, _tree.GetPreviousEdges(pointer)); |
| | 90 | |
|
| | 91 | | // ignore if cost is 0 or infinite. |
| 4 | 92 | | if (neighbourCost is >= double.MaxValue or <= 0) continue; |
| 4 | 93 | | if (turnCost is >= double.MaxValue or < 0) continue; |
| | 94 | |
|
| | 95 | | // check if the vertex has to be queued. |
| 4 | 96 | | var totalCost = neighbourCost + cost + turnCost; |
| | 97 | |
|
| | 98 | | // add visit if not added yet. |
| 4 | 99 | | var neighbourPointer = _tree.AddVisit(_enumerator, pointer); |
| | 100 | |
|
| | 101 | | // call the on queued method and allow checking for stopping conditions. |
| 4 | 102 | | if (!this.OnQueued(neighbourPointer, _enumerator.EdgeId, (neighbourCost, turnCost), _enumerator.Head, totalC |
| | 103 | |
|
| | 104 | | // add visit to heap. |
| 4 | 105 | | _heap.Push(neighbourPointer, totalCost); |
| 4 | 106 | | } |
| | 107 | |
|
| 40 | 108 | | return true; |
| 40 | 109 | | } |
| | 110 | | } |