| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Itinero.Network; |
| | 4 | | using Itinero.Network.Enumerators.Edges; |
| | 5 | |
|
| | 6 | | namespace Itinero.Routing.Costs; |
| | 7 | |
|
| | 8 | | internal class AlternativeRouteCostFunction : ICostFunction |
| | 9 | | { |
| | 10 | | private readonly ICostFunction _originalCostFunction; |
| | 11 | | private readonly Dictionary<EdgeId, int> _moreCostlyEdges; |
| | 12 | | private readonly double _alreadyVisitedCostFactor; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Calculates the cost of the edge as specified by the originalCostFunction. |
| | 16 | | /// If the edge is in moreCostlyEdges, the cost is increased. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="originalCostFunction"></param> |
| | 19 | | /// <param name="moreCostlyEdges"></param> |
| | 20 | | /// <param name="alreadyVisitedCostFactor"></param> |
| 2 | 21 | | public AlternativeRouteCostFunction(ICostFunction originalCostFunction, Dictionary<EdgeId, int> moreCostlyEdges, dou |
| 2 | 22 | | { |
| 2 | 23 | | _originalCostFunction = originalCostFunction; |
| 2 | 24 | | _moreCostlyEdges = moreCostlyEdges; |
| 2 | 25 | | _alreadyVisitedCostFactor = alreadyVisitedCostFactor; |
| 2 | 26 | | } |
| | 27 | |
|
| | 28 | | public (bool canAccess, bool canStop, double cost, double turnCost) Get(IEdgeEnumerator<RoutingNetwork> edgeEnumerat |
| | 29 | | IEnumerable<(EdgeId edgeId, byte? turn)>? previousEdges = null) |
| 2 | 30 | | { |
| 2 | 31 | | previousEdges ??= ArraySegment<(EdgeId edgeId, byte? turn)>.Empty; |
| | 32 | |
|
| 2 | 33 | | if (_moreCostlyEdges.TryGetValue(edgeEnumerator.EdgeId, out var count)) |
| 1 | 34 | | { |
| 1 | 35 | | var alreadyVisitedCost = Math.Pow(_alreadyVisitedCostFactor, count); |
| | 36 | |
|
| 1 | 37 | | var (canAccess, canStop, cost, turnCost) = _originalCostFunction.Get(edgeEnumerator, forward, previousEdges) |
| 1 | 38 | | return (canAccess, canStop, cost * alreadyVisitedCost, turnCost); |
| | 39 | | } |
| | 40 | |
|
| 1 | 41 | | return _originalCostFunction.Get(edgeEnumerator, forward, previousEdges); |
| 2 | 42 | | } |
| | 43 | | } |