| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Itinero.Network; |
| | | 5 | | using Itinero.Network.Enumerators.Edges; |
| | | 6 | | using Itinero.Routing.DataStructures; |
| | | 7 | | |
| | | 8 | | namespace Itinero.Routing.Flavours.Dijkstra.Bidirectional; |
| | | 9 | | |
| | | 10 | | internal abstract class DijkstraAlgorithm |
| | | 11 | | { |
| | 28 | 12 | | private readonly PathTree _tree = new(); |
| | 28 | 13 | | private readonly Dictionary<VertexId, (uint p, double cost)> _settled = []; |
| | 28 | 14 | | private readonly BinaryHeap<uint> _heap = new(); |
| | | 15 | | protected readonly RoutingNetworkEdgeEnumerator _enumerator; |
| | | 16 | | protected readonly RoutingNetwork _network; |
| | | 17 | | |
| | 28 | 18 | | protected DijkstraAlgorithm(RoutingNetwork network) |
| | 28 | 19 | | { |
| | 28 | 20 | | _network = network; |
| | | 21 | | |
| | 28 | 22 | | _enumerator = network.GetEdgeEnumerator(); |
| | 28 | 23 | | } |
| | | 24 | | |
| | 34 | 25 | | internal RoutingNetwork RoutingNetwork => _network; |
| | | 26 | | |
| | | 27 | | protected abstract bool OnQueued(uint visit, EdgeId edge, (double cost, double turnCost) edgeCost, VertexId vertex, |
| | | 28 | | |
| | | 29 | | protected abstract bool OnSettled(uint visit, VertexId vertex, double cost); |
| | | 30 | | |
| | | 31 | | protected abstract (double cost, double turnCost) GetCost(RoutingNetworkEdgeEnumerator edgeEnumerator, |
| | | 32 | | IEnumerable<(EdgeId edge, byte? turn)> previousEdges); |
| | | 33 | | |
| | | 34 | | internal bool TryGetVisit(VertexId vertex, out (uint p, double cost) visit) |
| | 70 | 35 | | { |
| | 70 | 36 | | return _settled.TryGetValue(vertex, out visit); |
| | 70 | 37 | | } |
| | | 38 | | |
| | | 39 | | internal void Clear() |
| | 0 | 40 | | { |
| | 0 | 41 | | _heap.Clear(); |
| | 0 | 42 | | _tree.Clear(); |
| | 0 | 43 | | _settled.Clear(); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | internal (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) GetVisit(uint pointer) |
| | 15 | 47 | | { |
| | 15 | 48 | | return _tree.GetVisit(pointer); |
| | 15 | 49 | | } |
| | | 50 | | |
| | | 51 | | internal uint Push(EdgeId edgeId, bool forward, double cost) |
| | 56 | 52 | | { |
| | 56 | 53 | | if (!_enumerator.MoveTo(edgeId, forward)) throw new Exception($"Edge not found!"); |
| | | 54 | | |
| | 56 | 55 | | var v = _tree.AddVisit(_enumerator, uint.MaxValue); |
| | 56 | 56 | | _heap.Push(v, cost); |
| | 56 | 57 | | return v; |
| | 56 | 58 | | } |
| | | 59 | | |
| | | 60 | | internal (uint pointer, (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) visit, double |
| | 78 | 61 | | { |
| | 78 | 62 | | var currentPointer = _heap.Pop(out var currentCost); |
| | 78 | 63 | | var currentVisit = _tree.GetVisit(currentPointer); |
| | 78 | 64 | | while (!_settled.TryAdd(currentVisit.vertex, (currentPointer, currentCost))) |
| | 8 | 65 | | { |
| | 8 | 66 | | currentPointer = uint.MaxValue; |
| | 16 | 67 | | if (_heap.Count == 0) break; |
| | | 68 | | |
| | 0 | 69 | | currentPointer = _heap.Pop(out currentCost); |
| | 0 | 70 | | currentVisit = _tree.GetVisit(currentPointer); |
| | 0 | 71 | | } |
| | | 72 | | |
| | 78 | 73 | | return (currentPointer, currentVisit, currentCost); |
| | 78 | 74 | | } |
| | | 75 | | |
| | | 76 | | internal IEnumerable<(EdgeId edge, byte? turn)> GetPreviousEdges(uint pointer, int maxCount = 16) |
| | 14 | 77 | | { |
| | 14 | 78 | | using var previous = _tree.GetPreviousEdges(pointer).GetEnumerator(); |
| | 36 | 79 | | while (previous.MoveNext()) |
| | 22 | 80 | | { |
| | 22 | 81 | | if (maxCount <= 0) yield break; |
| | 22 | 82 | | maxCount--; |
| | | 83 | | |
| | 22 | 84 | | yield return previous.Current; |
| | 22 | 85 | | } |
| | 14 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal bool CanTurn(VertexId vertex, |
| | | 89 | | IReadOnlyList<(EdgeId edge, byte? turn)> previousEdges, IReadOnlyList<EdgeId> nextEdges) |
| | 7 | 90 | | { |
| | 7 | 91 | | if (nextEdges.Count == 0) throw new Exception("Not a turn"); |
| | 7 | 92 | | if (previousEdges.Count == 0) throw new Exception("Not a turn"); |
| | | 93 | | |
| | | 94 | | // no U-turns |
| | 7 | 95 | | if (nextEdges[0] == previousEdges[0].edge) return false; |
| | | 96 | | |
| | | 97 | | // check neighbours. |
| | 7 | 98 | | var nextChecked = 0; |
| | 7 | 99 | | var edges = previousEdges.ToList(); |
| | 11 | 100 | | while (nextChecked < nextEdges.Count) |
| | 8 | 101 | | { |
| | 8 | 102 | | var nextEdge = nextEdges[nextChecked]; |
| | 8 | 103 | | var edgeFound = false; |
| | | 104 | | |
| | 8 | 105 | | if (!_enumerator.MoveTo(vertex)) return false; |
| | 8 | 106 | | while (_enumerator.MoveNext()) |
| | 8 | 107 | | { |
| | | 108 | | // filter out if U-turns or visits on the same edge. |
| | 8 | 109 | | var neighbourEdge = _enumerator.EdgeId; |
| | 8 | 110 | | if (neighbourEdge != nextEdge) continue; |
| | | 111 | | |
| | | 112 | | // gets the cost of the current edge. |
| | 8 | 113 | | var (_, turnCost) = this.GetCost(_enumerator, edges); |
| | | 114 | | |
| | 12 | 115 | | if (turnCost is >= double.MaxValue or < 0) return false; |
| | | 116 | | |
| | 4 | 117 | | edges.Add((_enumerator.EdgeId, _enumerator.HeadOrder)); |
| | 4 | 118 | | vertex = _enumerator.Head; |
| | 4 | 119 | | edgeFound = true; |
| | 4 | 120 | | break; |
| | | 121 | | } |
| | | 122 | | |
| | 4 | 123 | | if (!edgeFound) return false; |
| | | 124 | | |
| | 4 | 125 | | nextChecked++; |
| | 4 | 126 | | } |
| | | 127 | | |
| | | 128 | | // if all edges are checked, the turn is possible. |
| | 6 | 129 | | if (nextChecked >= nextEdges.Count) return true; |
| | | 130 | | |
| | | 131 | | // target edge not found as neighbour. |
| | 0 | 132 | | return false; |
| | 7 | 133 | | } |
| | | 134 | | |
| | | 135 | | internal bool Step(uint pointer, (VertexId vertex, EdgeId edge, bool forward, byte? head, uint previousPointer) visi |
| | 70 | 136 | | { |
| | | 137 | | // log settled and see if we need to continue. |
| | 70 | 138 | | if (!this.OnSettled(pointer, visit.vertex, cost)) return false; |
| | | 139 | | |
| | | 140 | | // check neighbours. |
| | 70 | 141 | | if (!_enumerator.MoveTo(visit.vertex)) return true; |
| | 164 | 142 | | while (_enumerator.MoveNext()) |
| | 94 | 143 | | { |
| | | 144 | | // filter out if U-turns or visits on the same edge. |
| | 94 | 145 | | var neighbourEdge = _enumerator.EdgeId; |
| | 164 | 146 | | if (neighbourEdge == visit.edge) continue; |
| | | 147 | | |
| | | 148 | | // gets the cost of the current edge. |
| | 24 | 149 | | var (neighbourCost, turnCost) = this.GetCost(_enumerator, _tree.GetPreviousEdges(pointer)); |
| | | 150 | | |
| | | 151 | | // ignore if cost is 0 or infinite. |
| | 24 | 152 | | if (neighbourCost is >= double.MaxValue or <= 0) continue; |
| | 32 | 153 | | if (turnCost is >= double.MaxValue or < 0) continue; |
| | | 154 | | |
| | | 155 | | // check if the vertex has to be queued. |
| | 16 | 156 | | var totalCost = neighbourCost + cost + turnCost; |
| | | 157 | | |
| | | 158 | | // add visit if not added yet. |
| | 16 | 159 | | var neighbourPointer = _tree.AddVisit(_enumerator, pointer); |
| | | 160 | | |
| | | 161 | | // call the on queued method and allow checking for stopping conditions. |
| | 16 | 162 | | if (!this.OnQueued(neighbourPointer, _enumerator.EdgeId, (neighbourCost, turnCost), _enumerator.Head, totalC |
| | | 163 | | |
| | | 164 | | // add visit to heap. |
| | 16 | 165 | | _heap.Push(neighbourPointer, totalCost); |
| | 16 | 166 | | } |
| | | 167 | | |
| | 70 | 168 | | return true; |
| | 70 | 169 | | } |
| | | 170 | | } |