| | 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.EdgeBased; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// An edge-based dijkstra implementation. |
| | 20 | | /// </summary> |
| | 21 | | internal class Dijkstra |
| | 22 | | { |
| 20 | 23 | | private readonly PathTree _tree = new(); |
| 20 | 24 | | private readonly HashSet<(EdgeId edgeId, VertexId vertexId)> _visits = new(); |
| 20 | 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, |
| | 30 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | 31 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null) |
| 0 | 32 | | { |
| 0 | 33 | | var paths = await this.RunAsync(network, (source, null), new[] { (target, (bool?)null) }, getDijkstraWeight, |
| 0 | 34 | | settled, queued); |
| | 35 | |
|
| 0 | 36 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public async Task<(Path? path, double cost)> RunAsync(RoutingNetwork network, |
| | 40 | | (SnapPoint sp, bool? direction) source, |
| | 41 | | (SnapPoint sp, bool? direction) target, |
| | 42 | | DijkstraWeightFunc getDijkstraWeight, |
| | 43 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | 44 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null) |
| 11 | 45 | | { |
| 11 | 46 | | var paths = await this.RunAsync(network, source, new[] { target }, getDijkstraWeight, settled, queued); |
| | 47 | |
|
| 11 | 48 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| 11 | 49 | | } |
| | 50 | |
|
| | 51 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, SnapPoint source, |
| | 52 | | IReadOnlyList<SnapPoint> targets, |
| | 53 | | DijkstraWeightFunc getDijkstraWeight, |
| | 54 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | 55 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null) |
| 7 | 56 | | { |
| 14 | 57 | | return await this.RunAsync(network, (source, null), targets.Select(x => (x, (bool?)null)).ToArray(), |
| 7 | 58 | | getDijkstraWeight, settled, queued); |
| 7 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="network"></param> |
| | 65 | | /// <param name="source"></param> |
| | 66 | | /// <param name="targets"></param> |
| | 67 | | /// <param name="getDijkstraWeight"></param> |
| | 68 | | /// <param name="settled">This Callback is called for every edge for which the minimal cost is known. If this callba |
| | 69 | | /// <param name="queued">This callback is called before an edge is loaded. Should not be used to influence route pla |
| | 70 | | /// <returns></returns> |
| | 71 | | /// <exception cref="Exception"></exception> |
| | 72 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, |
| | 73 | | (SnapPoint sp, bool? direction) source, |
| | 74 | | IReadOnlyList<(SnapPoint sp, bool? direction)> targets, |
| | 75 | | DijkstraWeightFunc getDijkstraWeight, |
| | 76 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | 77 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null) |
| 20 | 78 | | { |
| | 79 | | static double GetWorst((uint pointer, double cost)[] targets) |
| 30 | 80 | | { |
| 30 | 81 | | var worst = 0d; |
| 120 | 82 | | for (var i = 0; i < targets.Length; i++) |
| 42 | 83 | | { |
| 42 | 84 | | if (!(targets[i].cost > worst)) |
| 9 | 85 | | { |
| 9 | 86 | | continue; |
| | 87 | | } |
| | 88 | |
|
| 33 | 89 | | worst = targets[i].cost; |
| 33 | 90 | | if (worst >= double.MaxValue) |
| 12 | 91 | | { |
| 12 | 92 | | break; |
| | 93 | | } |
| 21 | 94 | | } |
| | 95 | |
|
| 30 | 96 | | return worst; |
| 30 | 97 | | } |
| | 98 | |
|
| 20 | 99 | | var enumerator = network.GetEdgeEnumerator(); |
| | 100 | |
|
| 20 | 101 | | _tree.Clear(); |
| 20 | 102 | | _visits.Clear(); |
| 20 | 103 | | _heap.Clear(); |
| | 104 | |
|
| | 105 | | // add sources. |
| 20 | 106 | | var sourceForwardVisit = uint.MaxValue; |
| 20 | 107 | | if (source.Forward()) |
| 20 | 108 | | { |
| | 109 | | // add forward. |
| 20 | 110 | | if (!enumerator.MoveTo(source.sp.EdgeId, true)) |
| 0 | 111 | | { |
| 0 | 112 | | throw new Exception($"Edge in source {source} not found!"); |
| | 113 | | } |
| | 114 | |
|
| 20 | 115 | | var sourceCostForward = |
| 20 | 116 | | getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost; |
| 20 | 117 | | if (sourceCostForward > 0) |
| 20 | 118 | | { |
| | 119 | | // can traverse edge in the forward direction. |
| 20 | 120 | | var sourceOffsetCostForward = sourceCostForward * (1 - source.sp.OffsetFactor()); |
| 20 | 121 | | sourceForwardVisit = |
| 20 | 122 | | _tree.AddVisit(enumerator, uint.MaxValue); |
| 20 | 123 | | _heap.Push(sourceForwardVisit, sourceOffsetCostForward); |
| 20 | 124 | | } |
| 20 | 125 | | } |
| | 126 | |
|
| 20 | 127 | | var sourceBackwardVisit = uint.MaxValue; |
| 20 | 128 | | if (source.Backward()) |
| 15 | 129 | | { |
| | 130 | | // add backward. |
| 15 | 131 | | if (!enumerator.MoveTo(source.sp.EdgeId, false)) |
| 0 | 132 | | { |
| 0 | 133 | | throw new Exception($"Edge in source {source} not found!"); |
| | 134 | | } |
| | 135 | |
|
| 15 | 136 | | var sourceCostBackward = |
| 15 | 137 | | getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost; |
| 15 | 138 | | if (sourceCostBackward > 0) |
| 15 | 139 | | { |
| | 140 | | // can traverse edge in the backward direction. |
| 15 | 141 | | var sourceOffsetCostBackward = sourceCostBackward * source.sp.OffsetFactor(); |
| 15 | 142 | | sourceBackwardVisit = |
| 15 | 143 | | _tree.AddVisit(enumerator, uint.MaxValue); |
| 15 | 144 | | _heap.Push(sourceBackwardVisit, sourceOffsetCostBackward); |
| 15 | 145 | | } |
| 15 | 146 | | } |
| | 147 | |
|
| | 148 | | // add targets. |
| 20 | 149 | | var bestTargets = new (uint pointer, double cost)[targets.Count]; |
| 20 | 150 | | var targetsPerVertex = new Dictionary<VertexId, List<int>>(); |
| 92 | 151 | | for (var t = 0; t < targets.Count; t++) |
| 26 | 152 | | { |
| 26 | 153 | | bestTargets[t] = (uint.MaxValue, double.MaxValue); |
| 26 | 154 | | var target = targets[t]; |
| | 155 | |
|
| 26 | 156 | | if (target.Forward()) |
| 23 | 157 | | { |
| | 158 | | // add forward. |
| 23 | 159 | | if (!enumerator.MoveTo(target.sp.EdgeId, true)) |
| 0 | 160 | | { |
| 0 | 161 | | throw new Exception($"Edge in target {target} not found!"); |
| | 162 | | } |
| | 163 | |
|
| 23 | 164 | | var targetCostForward = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()) |
| 23 | 165 | | .cost; |
| 23 | 166 | | if (targetCostForward > 0) |
| 23 | 167 | | { |
| 23 | 168 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| 17 | 169 | | { |
| 17 | 170 | | targetsAtVertex = new List<int>(); |
| 17 | 171 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| 17 | 172 | | } |
| | 173 | |
|
| 23 | 174 | | targetsAtVertex.Add(t); |
| 23 | 175 | | } |
| 23 | 176 | | } |
| | 177 | |
|
| 26 | 178 | | if (target.Backward()) |
| 24 | 179 | | { |
| | 180 | | // add backward. |
| 24 | 181 | | if (!enumerator.MoveTo(target.sp.EdgeId, false)) |
| 0 | 182 | | { |
| 0 | 183 | | throw new Exception($"Edge in source {source} not found!"); |
| | 184 | | } |
| | 185 | |
|
| 24 | 186 | | var targetCostBackward = |
| 24 | 187 | | getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost; |
| 24 | 188 | | if (targetCostBackward > 0) |
| 24 | 189 | | { |
| 24 | 190 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| 18 | 191 | | { |
| 18 | 192 | | targetsAtVertex = new List<int>(); |
| 18 | 193 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| 18 | 194 | | } |
| | 195 | |
|
| 24 | 196 | | targetsAtVertex.Add(t); |
| 24 | 197 | | } |
| 24 | 198 | | } |
| | 199 | |
|
| | 200 | | // consider paths 'within' a single edge. |
| 26 | 201 | | if (source.sp.EdgeId != target.sp.EdgeId) |
| 8 | 202 | | { |
| 8 | 203 | | continue; |
| | 204 | | } |
| | 205 | |
|
| 18 | 206 | | if (source.sp.Offset == target.sp.Offset) |
| 1 | 207 | | { |
| | 208 | | // source and target are identical. |
| 1 | 209 | | if (sourceForwardVisit != uint.MaxValue && |
| 1 | 210 | | target.Forward()) |
| 0 | 211 | | { |
| 0 | 212 | | bestTargets[t] = (sourceForwardVisit, 0); |
| 0 | 213 | | } |
| 1 | 214 | | else if (sourceBackwardVisit != uint.MaxValue && |
| 1 | 215 | | target.Backward()) |
| 0 | 216 | | { |
| 0 | 217 | | bestTargets[t] = (sourceForwardVisit, 0); |
| 0 | 218 | | } |
| 1 | 219 | | } |
| 17 | 220 | | else if (source.sp.Offset < target.sp.Offset && |
| 17 | 221 | | source.Forward() && target.Forward()) |
| 11 | 222 | | { |
| | 223 | | // the source is earlier in the direction of the edge |
| | 224 | | // and the edge can be traversed in this direction. |
| 11 | 225 | | if (!enumerator.MoveTo(source.sp.EdgeId, true)) |
| 0 | 226 | | { |
| 0 | 227 | | throw new Exception($"Edge in source {source} not found!"); |
| | 228 | | } |
| | 229 | |
|
| 11 | 230 | | var weight = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost * |
| 11 | 231 | | (target.sp.OffsetFactor() - source.sp.OffsetFactor()); |
| 11 | 232 | | bestTargets[t] = (sourceForwardVisit, weight); |
| 11 | 233 | | } |
| 6 | 234 | | else if (source.sp.Offset > target.sp.Offset && |
| 6 | 235 | | source.Backward() && target.Backward()) |
| 3 | 236 | | { |
| | 237 | | // the source is earlier against the direction of the edge |
| | 238 | | // and the edge can be traversed in this direction. |
| 3 | 239 | | if (!enumerator.MoveTo(source.sp.EdgeId, false)) |
| 0 | 240 | | { |
| 0 | 241 | | throw new Exception($"Edge in source {source} not found!"); |
| | 242 | | } |
| | 243 | |
|
| 3 | 244 | | var weight = getDijkstraWeight(enumerator, Enumerable.Empty<(EdgeId edge, byte? turn)>()).cost * |
| 3 | 245 | | (source.sp.OffsetFactor() - target.sp.OffsetFactor()); |
| 3 | 246 | | bestTargets[t] = (sourceBackwardVisit, weight); |
| 3 | 247 | | } |
| 18 | 248 | | } |
| | 249 | |
|
| | 250 | | // update worst target cost. |
| 20 | 251 | | var worstTargetCost = GetWorst(bestTargets); |
| | 252 | |
|
| | 253 | | // keep going until heap is empty. |
| 68 | 254 | | while (_heap.Count > 0) |
| 52 | 255 | | { |
| | 256 | | // dequeue new visit. |
| 52 | 257 | | var currentPointer = _heap.Pop(out var currentCost); |
| 52 | 258 | | var currentVisit = _tree.GetVisit(currentPointer); |
| 52 | 259 | | while (_visits.Contains((currentVisit.edge, currentVisit.vertex))) |
| 0 | 260 | | { |
| | 261 | | // visited before, skip. |
| 0 | 262 | | currentPointer = uint.MaxValue; |
| 0 | 263 | | if (_heap.Count == 0) |
| 0 | 264 | | { |
| 0 | 265 | | break; |
| | 266 | | } |
| | 267 | |
|
| 0 | 268 | | currentPointer = _heap.Pop(out currentCost); |
| 0 | 269 | | currentVisit = _tree.GetVisit(currentPointer); |
| 0 | 270 | | } |
| | 271 | |
|
| 52 | 272 | | if (currentPointer == uint.MaxValue) |
| 0 | 273 | | { |
| 0 | 274 | | break; |
| | 275 | | } |
| | 276 | |
|
| | 277 | | // log visit. |
| 52 | 278 | | if (currentVisit.previousPointer != uint.MaxValue) |
| 17 | 279 | | { |
| 17 | 280 | | _visits.Add((currentVisit.edge, currentVisit.vertex)); |
| 17 | 281 | | } |
| | 282 | |
|
| 52 | 283 | | if (settled != null && await settled((currentVisit.edge, currentVisit.vertex))) |
| 0 | 284 | | { |
| | 285 | | // the best cost to this edge has already been found; current visit can not improve this anymore so we c |
| 0 | 286 | | continue; |
| | 287 | | } |
| | 288 | |
|
| | 289 | | // check if the search needs to stop. |
| 52 | 290 | | if (currentCost > worstTargetCost) |
| 4 | 291 | | { |
| | 292 | | // impossible to improve on cost to any target. |
| 4 | 293 | | break; |
| | 294 | | } |
| | 295 | |
|
| | 296 | | // check neighbours. |
| 48 | 297 | | if (!enumerator.MoveTo(currentVisit.vertex)) |
| 0 | 298 | | { |
| | 299 | | // no edges, move on! |
| 0 | 300 | | continue; |
| | 301 | | } |
| | 302 | |
|
| | 303 | | // check if this is a target. |
| 48 | 304 | | if (!targetsPerVertex.TryGetValue(currentVisit.vertex, out var targetsAtVertex)) |
| 14 | 305 | | { |
| 14 | 306 | | targetsAtVertex = null; |
| 14 | 307 | | } |
| | 308 | |
|
| 116 | 309 | | while (enumerator.MoveNext()) |
| 68 | 310 | | { |
| | 311 | | // filter out if u-turns or visits on the same edge. |
| 68 | 312 | | var neighbourEdge = enumerator.EdgeId; |
| 68 | 313 | | if (neighbourEdge == currentVisit.edge) |
| 48 | 314 | | { |
| 48 | 315 | | continue; |
| | 316 | | } |
| | 317 | |
|
| | 318 | | // gets the cost of the current edge. |
| 20 | 319 | | var (neighbourCost, turnCost) = |
| 20 | 320 | | getDijkstraWeight(enumerator, _tree.GetPreviousEdges(currentPointer)); |
| 20 | 321 | | if (neighbourCost is >= double.MaxValue or <= 0) |
| 0 | 322 | | { |
| 0 | 323 | | continue; |
| | 324 | | } |
| | 325 | |
|
| 20 | 326 | | if (turnCost is >= double.MaxValue or < 0) |
| 1 | 327 | | { |
| 1 | 328 | | continue; |
| | 329 | | } |
| | 330 | |
|
| | 331 | | // if the vertex has targets, check if this edge is a match. |
| 19 | 332 | | var neighbourPointer = uint.MaxValue; |
| 19 | 333 | | if (targetsAtVertex != null) |
| 11 | 334 | | { |
| | 335 | | // only consider targets when found for the 'from' vertex. |
| | 336 | | // and when this in not a u-turn. |
| 61 | 337 | | foreach (var t in targetsAtVertex) |
| 14 | 338 | | { |
| 14 | 339 | | var target = targets[t]; |
| 14 | 340 | | if (target.sp.EdgeId != neighbourEdge) |
| 4 | 341 | | { |
| 4 | 342 | | continue; |
| | 343 | | } |
| | 344 | |
|
| | 345 | | // check directions. |
| 10 | 346 | | if (enumerator.Forward && !target.Forward()) |
| 0 | 347 | | { |
| 0 | 348 | | continue; |
| | 349 | | } |
| | 350 | |
|
| 10 | 351 | | if (!enumerator.Forward && !target.Backward()) |
| 0 | 352 | | { |
| 0 | 353 | | continue; |
| | 354 | | } |
| | 355 | |
|
| | 356 | | // there is a target on this edge, calculate the cost. |
| | 357 | | // calculate the cost from the 'from' vertex to the target. |
| 10 | 358 | | var targetCost = enumerator.Forward |
| 10 | 359 | | ? neighbourCost * target.sp.OffsetFactor() |
| 10 | 360 | | : neighbourCost * (1 - target.sp.OffsetFactor()); |
| | 361 | | // this is the case where the target is on this edge |
| | 362 | | // and there is a path to 'from' before. |
| 10 | 363 | | targetCost += currentCost; |
| | 364 | |
|
| 10 | 365 | | targetCost += turnCost; |
| | 366 | |
|
| | 367 | | // if this is an improvement, use it! |
| 10 | 368 | | var targetBestCost = bestTargets[t].cost; |
| 10 | 369 | | if (!(targetCost < targetBestCost)) |
| 0 | 370 | | { |
| 0 | 371 | | continue; |
| | 372 | | } |
| | 373 | |
|
| | 374 | | // this is an improvement. |
| 10 | 375 | | neighbourPointer = _tree.AddVisit(enumerator, currentPointer); |
| 10 | 376 | | bestTargets[t] = (neighbourPointer, targetCost); |
| | 377 | |
|
| | 378 | | // update worst. |
| 10 | 379 | | worstTargetCost = GetWorst(bestTargets); |
| 10 | 380 | | } |
| 11 | 381 | | } |
| | 382 | |
|
| 19 | 383 | | if (queued != null && |
| 19 | 384 | | await queued((enumerator.EdgeId, enumerator.Head))) |
| 0 | 385 | | { |
| | 386 | | // don't queue this edge if the queued function returns true. |
| 0 | 387 | | continue; |
| | 388 | | } |
| | 389 | |
|
| | 390 | | // add visit if not added yet. |
| 19 | 391 | | if (neighbourPointer == uint.MaxValue) |
| 12 | 392 | | { |
| 12 | 393 | | neighbourPointer = |
| 12 | 394 | | _tree.AddVisit(enumerator, currentPointer); |
| 12 | 395 | | } |
| | 396 | |
|
| | 397 | | // add visit to heap. |
| 19 | 398 | | _heap.Push(neighbourPointer, neighbourCost + currentCost + turnCost); |
| 19 | 399 | | } |
| 48 | 400 | | } |
| | 401 | |
|
| 20 | 402 | | var paths = new (Path? path, double cost)[targets.Count]; |
| 92 | 403 | | for (var p = 0; p < paths.Length; p++) |
| 26 | 404 | | { |
| 26 | 405 | | var bestTarget = bestTargets[p]; |
| 26 | 406 | | if (bestTarget.pointer == uint.MaxValue) |
| 3 | 407 | | { |
| 3 | 408 | | paths[p] = (null, double.MaxValue); |
| 3 | 409 | | continue; |
| | 410 | | } |
| | 411 | |
|
| | 412 | | // build resulting path. |
| 23 | 413 | | var path = new Path(network); |
| 23 | 414 | | var visit = _tree.GetVisit(bestTarget.pointer); |
| | 415 | |
|
| | 416 | | // path is at least two edges. |
| 42 | 417 | | while (true) |
| 42 | 418 | | { |
| 42 | 419 | | if (visit.previousPointer == uint.MaxValue) |
| 23 | 420 | | { |
| 23 | 421 | | enumerator.MoveTo(visit.edge); |
| 23 | 422 | | path.Prepend(visit.edge, visit.forward); |
| 23 | 423 | | break; |
| | 424 | | } |
| | 425 | |
|
| 19 | 426 | | path.Prepend(visit.edge, visit.forward); |
| 19 | 427 | | visit = _tree.GetVisit(visit.previousPointer); |
| 19 | 428 | | } |
| | 429 | |
|
| | 430 | | // add the offsets. |
| 23 | 431 | | var target = targets[p]; |
| 23 | 432 | | path.Offset1 = path.First.direction ? source.sp.Offset : (ushort)(ushort.MaxValue - source.sp.Offset); |
| 23 | 433 | | path.Offset2 = path.Last.direction |
| 23 | 434 | | ? target.sp.Offset |
| 23 | 435 | | : (ushort)(ushort.MaxValue - target.sp.Offset); |
| | 436 | |
|
| 23 | 437 | | paths[p] = (path, bestTarget.cost); |
| 23 | 438 | | } |
| | 439 | |
|
| 20 | 440 | | return paths; |
| 20 | 441 | | } |
| | 442 | |
|
| | 443 | | /// <summary> |
| | 444 | | /// Gets a default dijkstra instance. |
| | 445 | | /// </summary> |
| | 446 | | public static Dijkstra Default |
| | 447 | | { |
| | 448 | | get |
| 20 | 449 | | { |
| 20 | 450 | | return new Dijkstra(); |
| 20 | 451 | | } |
| | 452 | | } |
| | 453 | | } |