internal (DayDistancesDict, DayEdgeDistanceDict) Execute( AdjacencyGraph <TravelItem, Edge <TravelItem> > graph, Dictionary <Edge <TravelItem>, double> edgedDistances, DayTravelDict dayTravel, Func <Edge <TravelItem>, double> retriever) { var dayDistances = new DayDistancesDict(); var dayEdgeDist = new DayEdgeDistanceDict(); foreach (DateTime key in dayTravel.Keys) { var routes = dayTravel[key]; var pairEdges = routes.Zip(routes.Skip(1), (a, b) => new Edge <TravelItem>(a, b)); foreach (var edge in pairEdges) { if (edgedDistances.ContainsKey(edge)) { dayEdgeDist.Add((key, edge), edgedDistances[edge]); } else { Console.WriteLine($"Unable to find edge distance {edge}: querying..."); if (retriever != null) { double distance = retriever(edge); edgedDistances.Add(edge, distance); dayEdgeDist.Add((key, edge), distance); } } } double resDist = pairEdges.Aggregate(0.0, (acc, item) => acc + GetEdgeDistance(edgedDistances, item)); dayDistances.Add(key, resDist); } return(dayDistances, dayEdgeDist); double GetEdgeDistance(Dictionary <Edge <TravelItem>, double> edgedDistances, Edge <TravelItem> edge) { double result; if (edgedDistances.TryGetValue(edge, out result)) { return(result); } Console.WriteLine($"Unable to find edge distance {edge}: querying..."); if (retriever != null) { double distance = retriever(edge); edgedDistances.Add(edge, distance); return(distance); } return(-1.0f); } }
internal DayTravelDict Execute(List <TravelItem> travelItems) { var result = new DayTravelDict(); var groups = travelItems .GroupBy(t => t.Date) .Select(dl => (date: dl.Key, travel: dl.ToList())); foreach (var group in groups) { result.Add(group.date, group.travel); } return(result); }
internal CalculatorResult Execute(AdjacencyGraph <TravelItem, Edge <TravelItem> > graph, Dictionary <Edge <TravelItem>, double> distance, List <TravelItem> travelItems, Func <Edge <TravelItem>, double> distanceReceiver) { if (graph is null) { throw new ArgumentNullException(nameof(graph)); } if (distance is null) { throw new ArgumentNullException(nameof(distance)); } if (travelItems is null) { throw new ArgumentNullException(nameof(travelItems)); } var dayCalc = new DayDistanceCalculator(); var pathCalc = new ShortestPathCalculator(); var dayTravelCalc = new DayTravelCalculator(); DayTravelDict dayTravel = dayTravelCalc.Execute(travelItems); (DayDistancesDict dayDistances, DayEdgeDistanceDict edgeDist) = dayCalc.Execute(graph, distance, dayTravel, distanceReceiver); DayShortestPathDict shortestPaths = pathCalc.Execute(graph, distance, dayTravel); return(new CalculatorResult() { DayDistances = dayDistances, DayShortestPath = shortestPaths, DayTravel = dayTravel, DayEdgeDistance = edgeDist }); }
internal DayShortestPathDict Execute(AdjacencyGraph <TravelItem, Edge <TravelItem> > graph, Dictionary <Edge <TravelItem>, double> distance, DayTravelDict dayTravel) { var result = new DayShortestPathDict(); foreach (var(day, travel) in dayTravel) { double edgeCost(Edge <TravelItem> edge) => distance[edge]; TravelItem root = travel[0]; var tryGetPaths = graph.ShortestPathsDijkstra(edgeCost, root); TravelItem target = travel[^ 1];