public void ShortestPathTo_FindShortestPath_WhenThereIsPossibleMultiplePaths() { var graph = new DirectedWeightedSparseGraph <string>(); graph.AddVertex("a"); graph.AddVertex("b"); graph.AddVertex("c"); graph.AddVertex("d"); graph.AddEdge("a", "b", 1); graph.AddEdge("b", "c", 1); graph.AddEdge("c", "a", 1); graph.AddEdge("c", "d", 1); graph.AddEdge("b", "d", 1); var dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "a"); var result = dijkstra.ShortestPathTo("d"); Assert.NotNull(result); Assert.Equal(3, result.Count()); Assert.Contains("a", result); Assert.Contains("b", result); Assert.Contains("d", result); Assert.Equal(2, dijkstra.DistanceTo("d")); }
public void DistanceTo_ReturnInfinity_WhenVertexIsNotAchievable() { var graph = new DirectedWeightedSparseGraph <string>(); graph.AddVertex("a"); graph.AddVertex("b"); graph.AddVertex("c"); graph.AddEdge("a", "b", 1); var dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "a"); Assert.Equal(long.MaxValue, dijkstra.DistanceTo("c")); }
public void DistanceTo_Throw_WhenVertexIsNotInGraph() { var graph = new DirectedWeightedSparseGraph <string>(); graph.AddVertex("a"); graph.AddVertex("b"); graph.AddVertex("c"); graph.AddEdge("a", "b", 1); var dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "a"); Assert.Throws <ArgumentException>(() => dijkstra.DistanceTo("z")); }
public void ShortestPathTo_FindShortestPath_WhenEdgeHaveDifferentWeight() { var vertices = new[] { "r", "s", "t", "x", "y", "z" }; var graph = new DirectedWeightedSparseGraph <string>(); graph.AddVertices(vertices); graph.AddEdge("r", "s", 7); graph.AddEdge("r", "t", 6); graph.AddEdge("s", "t", 5); graph.AddEdge("s", "x", 9); graph.AddEdge("t", "x", 10); graph.AddEdge("t", "y", 7); graph.AddEdge("t", "z", 5); graph.AddEdge("x", "y", 2); graph.AddEdge("x", "z", 4); graph.AddEdge("y", "z", 1); var dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s"); var shortestToZ = dijkstra.ShortestPathTo("z"); Assert.NotNull(shortestToZ); Assert.Equal(3, shortestToZ.Count()); Assert.Contains("s", shortestToZ); Assert.Contains("t", shortestToZ); Assert.Contains("z", shortestToZ); Assert.Equal(10, dijkstra.DistanceTo("z")); var shortestToY = dijkstra.ShortestPathTo("y"); Assert.NotNull(shortestToY); Assert.Equal(3, shortestToY.Count()); Assert.Contains("s", shortestToY); Assert.Contains("x", shortestToY); Assert.Contains("y", shortestToY); Assert.Equal(11, dijkstra.DistanceTo("y")); }
public void LRwPathSearchTest() { DPNProblemContext ctx = GenerateProblemContext(); DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(ctx.StartTime, ctx.EndTime, 1); var travelgraph = new BasicTravelHyperNetwork(ctx, adapter); travelgraph.Build(); /* Train01 Station A*/ var train = ctx.Wor.RailwayTimeTable.Trains.First(); var station = ctx.Wor.Net.StationCollection.First(); //路径集 Dictionary <CustomerArrival, List <TravelPath> > pathDict = new Dictionary <CustomerArrival, List <TravelPath> >(); foreach (CustomerArrival c in ctx.Pal) { var ori = (ctx.Wor.Mar[c.Customer.MarSegID] as IRailwayMarketSegment).OriSta; var des = (ctx.Wor.Mar[c.Customer.MarSegID] as IRailwayMarketSegment).DesSta; var paths = DepthFirstSearcher.FindAllPaths(travelgraph, new TravelHyperNode() { Time = adapter.ConvertToDiscreteTime(c.ArriveTime), Station = ori, Price = 0 }, new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 }); pathDict.Add(c, new List <TravelPath>()); foreach (var path in paths) { pathDict[c].Add(new TravelPath(travelgraph, path)); } } //拉格朗日乘子 mu Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> > LM_mu = new Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> >(); foreach (CustomerArrival customer in ctx.Pal) { LM_mu.Add(customer, new Dictionary <TravelPath, decimal>()); foreach (var path in pathDict[customer]) { LM_mu[customer].Add(path, 2); } } // 拉格朗日乘子 lambda Dictionary <IEdge <TravelHyperNode>, decimal> LM_lambda = new Dictionary <IEdge <TravelHyperNode>, decimal>(); foreach (CustomerArrival customer in ctx.Pal) { foreach (var path in pathDict[customer]) { if (!LM_lambda.ContainsKey(path.ReservationArc)) { LM_lambda.Add(path.ReservationArc, 1); } } } var graph = DpnAlgorithm.BuildLRwGraph(ctx, adapter, train, station, pathDict, travelgraph.LinkTrainDict, LM_mu, LM_lambda); DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "Start"); Assert.IsTrue(dijkstra.HasPathTo("End") == true); var pathToC = string.Empty; var pricepath = dijkstra.ShortestPathTo("End"); PricePath p = new PricePath(graph, pricepath); Assert.IsTrue(p.GetWrapPoints(0.9m, 2).Count() > 0); Assert.IsFalse(p.GetWrapPoints(1.1m, 2).Count() > 0); foreach (var node in pricepath) { pathToC = String.Format("{0}({1}) -> ", pathToC, node); } pathToC = pathToC.TrimEnd(new char[] { ' ', '-', '>' }); Console.WriteLine("Shortest path to Station 'C': " + pathToC + "\r\n"); Assert.AreEqual(23m, Math.Round(dijkstra.DistanceTo("End"))); }
public void LRxPathSearchTest() { DPNProblemContext ctx = GenerateProblemContext(); CustomerArrival customer = new CustomerArrival(); DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(ctx.StartTime, ctx.EndTime, 1); TravelPath p = new TravelPath(); var graph = new LRxTravelHyperNetwork(ctx, adapter, ObjectNetworkFactory.Create("", ctx, adapter), customer, new Dictionary <CustomerArrival, List <TravelPath> >()//path dict { { customer, new List <TravelPath>() { p } } }, new Dictionary <IServiceSegment, decimal>()//rho { { ctx.Wor.RailwayTimeTable.Trains.First().ServiceSegments.First(), 0 }, { ctx.Wor.RailwayTimeTable.Trains.First().ServiceSegments.Last(), 0 }, { ctx.Wor.RailwayTimeTable.Trains.Last().ServiceSegments.First(), 0 } }, new Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> >() { { customer, new Dictionary <TravelPath, decimal>() { { p, 1 } } } },//mu new Dictionary <IEdge <TravelHyperNode>, decimal>()); graph.Build(); DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode>(graph, new TravelHyperNode() { Time = 0, Station = ctx.Wor.Net.StationCollection.First(), Price = 0 }); Assert.IsTrue(dijkstra.HasPathTo(new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = ctx.Wor.Net.StationCollection.Last(), Price = 0 }) == true); var desNode = new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = ctx.Wor.Net.StationCollection.Last(), Price = 0 }; var pathToC = string.Empty; var shortestPath = dijkstra.ShortestPathTo(desNode); foreach (var node in shortestPath) { pathToC = String.Format("{0}({1}) -> ", pathToC, node); } pathToC = pathToC.TrimEnd(new char[] { ' ', '-', '>' }); Console.WriteLine("Shortest path to Station 'C': " + pathToC + "\r\n"); Assert.AreEqual(202m, Math.Round(dijkstra.DistanceTo(desNode))); }