| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Itinero.Network; |
| | 8 | | using Itinero.Routes.Paths; |
| | 9 | | using Itinero.Routing.DataStructures; |
| | 10 | | using Itinero.Snapping; |
| | 11 | |
|
| | 12 | | [assembly: InternalsVisibleTo("Itinero.Tests")] |
| | 13 | | [assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")] |
| | 14 | | [assembly: InternalsVisibleTo("Itinero.Tests.Functional")] |
| | 15 | |
|
| | 16 | | namespace Itinero.Routing.Flavours.Dijkstra; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// A dijkstra implementation. |
| | 20 | | /// </summary> |
| | 21 | | internal class Dijkstra |
| | 22 | | { |
| 10 | 23 | | private readonly PathTree _tree = new(); |
| 10 | 24 | | private readonly HashSet<VertexId> _visits = new(); |
| 10 | 25 | | private readonly BinaryHeap<uint> _heap = new(); |
| | 26 | |
|
| | 27 | | public async Task<(Path? path, double cost)> RunAsync(RoutingNetwork network, SnapPoint source, |
| | 28 | | SnapPoint target, |
| | 29 | | DijkstraWeightFunc getDijkstraWeight, Func<VertexId, Task<bool>>? settled = null, |
| | 30 | | Func<VertexId, Task<bool>>? queued = null) |
| 8 | 31 | | { |
| 8 | 32 | | var paths = await this.RunAsync(network, source, new[] { target }, getDijkstraWeight, settled, queued); |
| | 33 | |
|
| 8 | 34 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| 8 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Run a one-to-many Dijkstra search |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="network"></param> |
| | 41 | | /// <param name="source"></param> |
| | 42 | | /// <param name="targets"></param> |
| | 43 | | /// <param name="getDijkstraWeight"></param> |
| | 44 | | /// <param name="settled"></param> |
| | 45 | | /// <param name="queued">Queued notifies listeners when a vertex is queued. If this function returns false, the requ |
| | 46 | | /// <returns></returns> |
| | 47 | | /// <exception cref="Exception"></exception> |
| | 48 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, SnapPoint source, |
| | 49 | | IReadOnlyList<SnapPoint> targets, |
| | 50 | | DijkstraWeightFunc getDijkstraWeight, Func<VertexId, Task<bool>>? settled = null, |
| | 51 | | Func<VertexId, Task<bool>>? queued = null) |
| 10 | 52 | | { |
| | 53 | | // Returns the worst cost of all targets, i.e. the cost of the most costly target to reach |
| | 54 | | // Will be Double.MAX_VALUE if at least one target hasn't been reached |
| | 55 | | static double GetWorst((uint pointer, double cost)[] targets) |
| 19 | 56 | | { |
| 19 | 57 | | var worst = 0d; |
| 86 | 58 | | for (var i = 0; i < targets.Length; i++) |
| 31 | 59 | | { |
| 31 | 60 | | if (!(targets[i].cost > worst)) |
| 9 | 61 | | { |
| 9 | 62 | | continue; |
| | 63 | | } |
| | 64 | |
|
| 22 | 65 | | worst = targets[i].cost; |
| 22 | 66 | | if (worst >= double.MaxValue) |
| 7 | 67 | | { |
| 7 | 68 | | break; |
| | 69 | | } |
| 15 | 70 | | } |
| | 71 | |
|
| 19 | 72 | | return worst; |
| 19 | 73 | | } |
| | 74 | |
|
| 10 | 75 | | var enumerator = network.GetEdgeEnumerator(); |
| | 76 | |
|
| 10 | 77 | | _tree.Clear(); |
| 10 | 78 | | _visits.Clear(); |
| 10 | 79 | | _heap.Clear(); |
| | 80 | |
|
| | 81 | | // add sources. |
| | 82 | | // add forward. |
| 10 | 83 | | if (!enumerator.MoveTo(source.EdgeId, true)) |
| 0 | 84 | | { |
| 0 | 85 | | throw new Exception($"Edge in source {source} not found!"); |
| | 86 | | } |
| | 87 | |
|
| 10 | 88 | | var sourceCostForward = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost; |
| 10 | 89 | | var sourceForwardVisit = uint.MaxValue; |
| 10 | 90 | | if (sourceCostForward > 0) |
| 10 | 91 | | { |
| | 92 | | // can traverse edge in the forward direction. |
| 10 | 93 | | var sourceOffsetCostForward = sourceCostForward * (1 - source.OffsetFactor()); |
| 10 | 94 | | sourceForwardVisit = _tree.AddVisit(enumerator, uint.MaxValue); |
| 10 | 95 | | _heap.Push(sourceForwardVisit, sourceOffsetCostForward); |
| 10 | 96 | | } |
| | 97 | |
|
| | 98 | | // add backward. |
| 10 | 99 | | if (!enumerator.MoveTo(source.EdgeId, false)) |
| 0 | 100 | | { |
| 0 | 101 | | throw new Exception($"Edge in source {source} not found!"); |
| | 102 | | } |
| | 103 | |
|
| 10 | 104 | | var sourceCostBackward = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost; |
| 10 | 105 | | var sourceBackwardVisit = uint.MaxValue; |
| 10 | 106 | | if (sourceCostBackward > 0) |
| 10 | 107 | | { |
| | 108 | | // can traverse edge in the backward direction. |
| 10 | 109 | | var sourceOffsetCostBackward = sourceCostBackward * source.OffsetFactor(); |
| 10 | 110 | | sourceBackwardVisit = _tree.AddVisit(enumerator, uint.MaxValue); |
| 10 | 111 | | _heap.Push(sourceBackwardVisit, sourceOffsetCostBackward); |
| 10 | 112 | | } |
| | 113 | |
|
| | 114 | | // add targets. |
| 10 | 115 | | var bestTargets = new (uint pointer, double cost)[targets.Count]; |
| 10 | 116 | | var targetsPerVertex = new Dictionary<VertexId, List<int>>(); |
| 52 | 117 | | for (var t = 0; t < targets.Count; t++) |
| 16 | 118 | | { |
| 16 | 119 | | bestTargets[t] = (uint.MaxValue, double.MaxValue); |
| 16 | 120 | | var target = targets[t]; |
| | 121 | |
|
| | 122 | | // add targets to vertices. |
| 16 | 123 | | if (!enumerator.MoveTo(target.EdgeId, true)) |
| 0 | 124 | | { |
| 0 | 125 | | throw new Exception($"Edge in target {target} not found!"); |
| | 126 | | } |
| | 127 | |
|
| 16 | 128 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| 10 | 129 | | { |
| 10 | 130 | | targetsAtVertex = new List<int>(); |
| 10 | 131 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| 10 | 132 | | } |
| | 133 | |
|
| 16 | 134 | | targetsAtVertex.Add(t); |
| 16 | 135 | | if (!targetsPerVertex.TryGetValue(enumerator.Head, out targetsAtVertex)) |
| 10 | 136 | | { |
| 10 | 137 | | targetsAtVertex = new List<int>(); |
| 10 | 138 | | targetsPerVertex[enumerator.Head] = targetsAtVertex; |
| 10 | 139 | | } |
| | 140 | |
|
| 16 | 141 | | targetsAtVertex.Add(t); |
| | 142 | |
|
| | 143 | | // consider paths 'within' a single edge. |
| 16 | 144 | | if (source.EdgeId == target.EdgeId) |
| 9 | 145 | | { |
| | 146 | | // the source and target are on the same edge. |
| 9 | 147 | | if (source.Offset == target.Offset) |
| 0 | 148 | | { |
| | 149 | | // source and target are identical. |
| 0 | 150 | | bestTargets[t] = (sourceForwardVisit, 0); |
| 0 | 151 | | } |
| 9 | 152 | | else if (source.Offset < target.Offset && |
| 9 | 153 | | sourceForwardVisit != uint.MaxValue) |
| 9 | 154 | | { |
| | 155 | | // the source is earlier in the direction of the edge |
| | 156 | | // and the edge can be traversed in this direction. |
| 9 | 157 | | if (!enumerator.MoveTo(source.EdgeId, true)) |
| 0 | 158 | | { |
| 0 | 159 | | throw new Exception($"Edge in source {source} not found!"); |
| | 160 | | } |
| | 161 | |
|
| 9 | 162 | | var weight = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost * |
| 9 | 163 | | (target.OffsetFactor() - source.OffsetFactor()); |
| 9 | 164 | | bestTargets[t] = (sourceForwardVisit, weight); |
| 9 | 165 | | } |
| 0 | 166 | | else if (sourceBackwardVisit != uint.MaxValue) |
| 0 | 167 | | { |
| | 168 | | // the source is earlier against the direction of the edge |
| | 169 | | // and the edge can be traversed in this direction. |
| 0 | 170 | | if (!enumerator.MoveTo(source.EdgeId, false)) |
| 0 | 171 | | { |
| 0 | 172 | | throw new Exception($"Edge in source {source} not found!"); |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | var weight = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost * |
| 0 | 176 | | (source.OffsetFactor() - target.OffsetFactor()); |
| 0 | 177 | | bestTargets[t] = (sourceBackwardVisit, weight); |
| 0 | 178 | | } |
| 9 | 179 | | } |
| 16 | 180 | | } |
| | 181 | |
|
| | 182 | | // update worst target cost. |
| 10 | 183 | | var worstTargetCost = GetWorst(bestTargets); |
| | 184 | |
|
| | 185 | | // keep going until heap is empty. |
| 32 | 186 | | while (_heap.Count > 0) |
| 31 | 187 | | { |
| | 188 | | // dequeue new visit. |
| 31 | 189 | | var currentPointer = _heap.Pop(out var currentCost); |
| 31 | 190 | | var currentVisit = _tree.GetVisit(currentPointer); |
| 34 | 191 | | while (_visits.Contains(currentVisit.vertex)) |
| 6 | 192 | | { |
| | 193 | | // visited before, skip. |
| 6 | 194 | | currentPointer = uint.MaxValue; |
| 6 | 195 | | if (_heap.Count == 0) |
| 3 | 196 | | { |
| 3 | 197 | | break; |
| | 198 | | } |
| | 199 | |
|
| 3 | 200 | | currentPointer = _heap.Pop(out currentCost); |
| 3 | 201 | | currentVisit = _tree.GetVisit(currentPointer); |
| 3 | 202 | | } |
| | 203 | |
|
| 31 | 204 | | if (currentPointer == uint.MaxValue) |
| 3 | 205 | | { |
| 3 | 206 | | break; |
| | 207 | | } |
| | 208 | |
|
| | 209 | | // log visit. |
| 28 | 210 | | _visits.Add(currentVisit.vertex); |
| | 211 | |
|
| 28 | 212 | | if (settled != null && await settled(currentVisit.vertex)) |
| 0 | 213 | | { |
| | 214 | | // break if requested. |
| 0 | 215 | | break; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | // check if the search needs to stop. |
| 28 | 219 | | if (currentCost >= worstTargetCost) |
| 6 | 220 | | { |
| | 221 | | // impossible to improve on cost to any target. |
| 6 | 222 | | break; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | // check neighbours. |
| 22 | 226 | | if (!enumerator.MoveTo(currentVisit.vertex)) |
| 0 | 227 | | { |
| | 228 | | // no edges, move on! |
| 0 | 229 | | continue; |
| | 230 | | } |
| | 231 | |
|
| | 232 | | // check if this is a target. |
| 22 | 233 | | if (!targetsPerVertex.TryGetValue(currentVisit.vertex, out var targetsAtVertex)) |
| 9 | 234 | | { |
| 9 | 235 | | targetsAtVertex = null; |
| 9 | 236 | | } |
| | 237 | |
|
| 60 | 238 | | while (enumerator.MoveNext()) |
| 38 | 239 | | { |
| | 240 | | // filter out if u-turns or visits on the same edge. |
| 38 | 241 | | var neighbourEdge = enumerator.EdgeId; |
| 38 | 242 | | if (neighbourEdge == currentVisit.edge) |
| 22 | 243 | | { |
| 22 | 244 | | continue; |
| | 245 | | } |
| | 246 | |
|
| | 247 | | // gets the cost of the current edge. |
| 16 | 248 | | var (neighbourCost, turnCost) = |
| 16 | 249 | | getDijkstraWeight(enumerator, _tree.GetPreviousEdges(currentPointer)); |
| 16 | 250 | | if (neighbourCost is >= double.MaxValue or <= 0) |
| 0 | 251 | | { |
| 0 | 252 | | continue; |
| | 253 | | } |
| | 254 | |
|
| 16 | 255 | | if (turnCost is >= double.MaxValue or < 0) |
| 1 | 256 | | { |
| 1 | 257 | | continue; |
| | 258 | | } |
| | 259 | |
|
| | 260 | | // if the vertex has targets, check if this edge is a match. |
| 15 | 261 | | var neighbourPointer = uint.MaxValue; |
| 15 | 262 | | if (targetsAtVertex != null) |
| 10 | 263 | | { |
| | 264 | | // We have found a target! |
| | 265 | |
|
| | 266 | | // only consider targets when found for the 'from' vertex. |
| | 267 | | // and when this in not a u-turn. |
| 56 | 268 | | foreach (var t in targetsAtVertex) |
| 13 | 269 | | { |
| 13 | 270 | | var target = targets[t]; |
| 13 | 271 | | if (target.EdgeId != neighbourEdge) |
| 4 | 272 | | { |
| 4 | 273 | | continue; |
| | 274 | | } |
| | 275 | |
|
| | 276 | | // there is a target on this edge, calculate the cost. |
| | 277 | | // calculate the cost from the 'from' vertex to the target. |
| 9 | 278 | | var targetCost = enumerator.Forward |
| 9 | 279 | | ? neighbourCost * target.OffsetFactor() |
| 9 | 280 | | : neighbourCost * (1 - target.OffsetFactor()); |
| | 281 | | // this is the case where the target is on this edge |
| | 282 | | // and there is a path to 'from' before. |
| 9 | 283 | | targetCost += currentCost; |
| | 284 | |
|
| | 285 | | // add turn cost. |
| 9 | 286 | | targetCost += turnCost; |
| | 287 | |
|
| | 288 | | // if this is an improvement, use it! |
| 9 | 289 | | var targetBestCost = bestTargets[t].cost; |
| 9 | 290 | | if (!(targetCost < targetBestCost)) |
| 0 | 291 | | { |
| 0 | 292 | | continue; |
| | 293 | | } |
| | 294 | |
|
| | 295 | | // this is an improvement. |
| 9 | 296 | | neighbourPointer = _tree.AddVisit(enumerator, currentPointer); |
| 9 | 297 | | bestTargets[t] = (neighbourPointer, targetCost); |
| | 298 | |
|
| | 299 | | // update worst. |
| 9 | 300 | | worstTargetCost = GetWorst(bestTargets); |
| 9 | 301 | | } |
| 10 | 302 | | } |
| | 303 | |
|
| 15 | 304 | | if (queued != null && |
| 15 | 305 | | await queued(enumerator.Head)) |
| 0 | 306 | | { |
| | 307 | | // don't queue this vertex if the queued function returns true. |
| 0 | 308 | | continue; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | // add visit if not added yet. |
| 15 | 312 | | if (neighbourPointer == uint.MaxValue) |
| 9 | 313 | | { |
| 9 | 314 | | neighbourPointer = _tree.AddVisit(enumerator, currentPointer); |
| 9 | 315 | | } |
| | 316 | |
|
| | 317 | | // add visit to heap. |
| 15 | 318 | | _heap.Push(neighbourPointer, neighbourCost + currentCost + turnCost); |
| 15 | 319 | | } |
| 22 | 320 | | } |
| | 321 | |
|
| 10 | 322 | | var paths = new (Path? path, double cost)[targets.Count]; |
| 52 | 323 | | for (var p = 0; p < paths.Length; p++) |
| 16 | 324 | | { |
| 16 | 325 | | var bestTarget = bestTargets[p]; |
| 16 | 326 | | if (bestTarget.pointer == uint.MaxValue) |
| 1 | 327 | | { |
| 1 | 328 | | paths[p] = (null, double.MaxValue); |
| 1 | 329 | | continue; |
| | 330 | | } |
| | 331 | |
|
| | 332 | | // build resulting path. |
| 15 | 333 | | var path = new Path(network); |
| 15 | 334 | | var visit = _tree.GetVisit(bestTarget.pointer); |
| | 335 | |
|
| | 336 | | // path is at least two edges. |
| 31 | 337 | | while (true) |
| 31 | 338 | | { |
| 31 | 339 | | if (visit.previousPointer == uint.MaxValue) |
| 15 | 340 | | { |
| 15 | 341 | | path.Prepend(visit.edge, visit.forward); |
| 15 | 342 | | break; |
| | 343 | | } |
| | 344 | |
|
| 16 | 345 | | path.Prepend(visit.edge, visit.forward); |
| 16 | 346 | | visit = _tree.GetVisit(visit.previousPointer); |
| 16 | 347 | | } |
| | 348 | |
|
| | 349 | | // add the offsets. |
| 15 | 350 | | var target = targets[p]; |
| 15 | 351 | | path.Offset1 = path.First.direction ? source.Offset : (ushort)(ushort.MaxValue - source.Offset); |
| 15 | 352 | | path.Offset2 = path.Last.direction |
| 15 | 353 | | ? target.Offset |
| 15 | 354 | | : (ushort)(ushort.MaxValue - target.Offset); |
| | 355 | |
|
| 15 | 356 | | paths[p] = (path, bestTarget.cost); |
| 15 | 357 | | } |
| | 358 | |
|
| 10 | 359 | | return paths; |
| 10 | 360 | | } |
| | 361 | |
|
| | 362 | | /// <summary> |
| | 363 | | /// Gets a default dijkstra instance. |
| | 364 | | /// </summary> |
| | 365 | | public static Dijkstra Default |
| | 366 | | { |
| | 367 | | get |
| 10 | 368 | | { |
| 10 | 369 | | return new Dijkstra(); |
| 10 | 370 | | } |
| | 371 | | } |
| | 372 | | } |