public void BasicSearchAllTest() { DPNProblemContext ctx = GenerateProblemContext(); DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(ctx.StartTime, ctx.EndTime, 1); var graph = new BasicTravelHyperNetwork(ctx, adapter); graph.Build(); var list = DepthFirstSearcher.FindAllPaths(graph, new TravelHyperNode() { Time = 0, Station = ctx.Wor.Net.StationCollection.First(), Price = 0 }, new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = ctx.Wor.Net.StationCollection.Last(), Price = 0 }); foreach (var path in list) { Console.WriteLine(string.Join(",", path)); } Assert.AreEqual(30, list.Count); //路径集 TravelPath p = new TravelPath(graph, list[1]); Assert.AreEqual(ctx.Wor.Net.StationCollection.First(), p.StartStation); }
public void BuildLRxTest() { DPNProblemContext ctx = GenerateProblemContext(); DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(ctx.StartTime, ctx.EndTime, 1); var graph = new LRxTravelHyperNetwork(ctx, adapter, ObjectNetworkFactory.Create("", ctx, adapter), new CustomerArrival(), new Dictionary <CustomerArrival, List <TravelPath> >(), new Dictionary <IServiceSegment, decimal>() { { 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> >(), new Dictionary <IEdge <TravelHyperNode>, decimal>()); graph.Build(); /* * In-train:4*3 * Finish:3*3 * Reservation:11*3*(2+1) * Waiting:10*3 * Total:137 */ Assert.AreEqual(138, graph.EdgesCount); }
public static ObjectTravelHyperNetwork Create(string typeName, DPNProblemContext ctx, DiscreteTimeAdapter adapter) { switch (typeName) { case "MinTotalCost": { return(new ObjectTravelHyperNetwork(ctx, adapter)); } case "MinTravelTimeCost": { return(new MinTravelTimeObjectTravelHyperNetwork(ctx, adapter)); } case "MaxRevenue": { return(new MaxRevenueTravelHyperNetwork(ctx, adapter)); } case "MinTotalCost_MaxRevenue_Mix": { decimal cost_weight = ctx.GetParameter <decimal>("MinTotalCost_weight", 0.5m); decimal rev_weight = ctx.GetParameter <decimal>("MaxRevenue_weight", 0.5m); return(new MinTravelTime_MaxRevenue_MixTravelHyperNetwork(ctx, adapter, cost_weight, rev_weight)); } default: { return(new ObjectTravelHyperNetwork(ctx, adapter)); } } }
public static AbsDpnSolver Build(DPNProblemContext ctx, string solverName) { switch (solverName) { case "Subgradient": { return(new DpnSolver(ctx)); } case "CuttingPlane": { return(new DpnSolverV3(ctx)); } case "CuttingPlaneWithTrustRegion": { return(new DpnSolverV4(ctx)); } default: { return(new DpnSolverV4(ctx)); } } }
public void BuildBasicNetTest() { DPNProblemContext ctx = GenerateProblemContext(); DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(ctx.StartTime, ctx.EndTime, 1); var graph = new BasicTravelHyperNetwork(ctx, adapter); graph.Build(); Assert.AreEqual(138, graph.EdgesCount); }
private static DPNProblemContext GenerateProblemContext() { RnsmWorkspace wor = GenerateWorkspace(); DPNProblemContext _ctx = new DPNProblemContext(wor) { TransferThreshold = 3, Vot = 1, ControlInterval = 3, PriceLevelList = new List <decimal> { 0.9m, 1, 1.1m }, BasicPriceDic = new Dictionary <IServiceSegment, decimal>() { { wor.RailwayTimeTable.TrainTrips.First().ServiceSegments.First(), 20m }, { wor.RailwayTimeTable.TrainTrips.First().ServiceSegments.Last(), 20m }, { wor.RailwayTimeTable.TrainTrips.Last().ServiceSegments.First(), 20m } }, Pal = new CustomerArrivalChain() { new CustomerArrival() { ArriveTime = new DateTime(1991, 7, 5, 0, 0, 1), QueueOrder = 1, Customer = new CustomerInfo() { MarSegID = 3 } }, new CustomerArrival() { ArriveTime = new DateTime(1991, 7, 5, 0, 2, 0), QueueOrder = 2, Customer = new CustomerInfo() { MarSegID = 3 } }, }, StartTime = new DateTime(1991, 7, 5, 0, 0, 0), EndTime = new DateTime(1991, 7, 5, 0, 10, 0), SitaDic = new Dictionary <int, decimal>() }; _ctx.SetParameter("BigM", 9999m); _ctx.SetParameter("Resolution", 1); _ctx.SetParameter("WaitingCost", 0.1); return(_ctx); }
public void Case0_MinTravelTime() { DPNProblemContext ctx = GenerateProblemContext0(); ctx.SetParameter("ObjectiveType", "MinTravelTimeCost"); DpnSolverV4 solver = new DpnSolverV4(ctx); solver.Logger = Console.Out; Console.WriteLine($"旅客到达信息:"); foreach (CustomerArrival arr in ctx.Pal) { Console.WriteLine($"旅客{arr.QueueOrder}:{arr.ArriveTime.ToLongTimeString()}(" + $"{ctx.Wor.Mar.ConvertToInttime(arr.ArriveTime)}),市场:{arr.Customer.MarSegID}"); } solver.OnLowerBoundSolutionGenerated = (a) => { foreach (string str in a) { Console.WriteLine(str); } }; solver.OnFeasibleSolutionGenerated = (a, b) => { foreach (string str in a) { Console.WriteLine(str); } foreach (string str in b) { Console.WriteLine(str); } }; solver.OnIterationFinished = (s) => { Console.WriteLine(s); }; Console.WriteLine($"计算开始:"); solver.Work(); Assert.AreEqual(6.5m + 6.5m + 4m + 8.5m, Math.Round(solver.ObjValue, 1)); }
public void Case1_Mix2()//5:5 { DPNProblemContext ctx = GenerateProblemContext1(); DpnSolver solver = new DpnSolver(ctx); ctx.SetParameter("ObjectiveType", "MinTotalCost_MaxRevenue_Mix"); ctx.SetParameter("MinTotalCost_weight", 0.5m); ctx.SetParameter("MaxRevenue_weight", 0.5m); solver.Logger = Console.Out; Console.WriteLine($"旅客到达信息:"); foreach (CustomerArrival arr in ctx.Pal) { Console.WriteLine($"旅客{arr.QueueOrder}:{arr.ArriveTime.ToLongTimeString()}(" + $"{ctx.Wor.Mar.ConvertToInttime(arr.ArriveTime)}),市场:{arr.Customer.MarSegID}"); } solver.OnFeasibleSolutionGenerated = (a, b) => { foreach (string str in a) { Console.WriteLine(str); } foreach (string str in b) { Console.WriteLine(str); } }; solver.OnIterationFinished = (s) => { }; Console.WriteLine($"计算开始:"); solver.Work(); Assert.IsTrue(true); }
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))); }
public void BuildLRwTest() { DPNProblemContext ctx = GenerateProblemContext(); DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(ctx.StartTime, ctx.EndTime, 1); var travelgraph = new BasicTravelHyperNetwork(ctx, adapter); travelgraph.Build(); 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>(); //WARNING: 这里缺少了没有旅客选择的reservation arc 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); Assert.AreEqual(27, graph.EdgesCount); }
private static DPNProblemContext GenerateProblemContext1() { IRnsmWorkspace wor = GenerateWorkspace1(); DPNProblemContext _ctx = new DPNProblemContext(wor) { TransferThreshold = 60, Vot = 0.1m, ControlInterval = 4, PriceLevelList = new List <decimal> { 0.8m, 1, 1.2m }, BasicPriceDic = new Dictionary <IServiceSegment, decimal>() { { wor.RailwayTimeTable.Trains.First().ServiceSegments.First(), 100m }, { wor.RailwayTimeTable.Trains.First().ServiceSegments.Last(), 100m }, { wor.RailwayTimeTable.Trains.Last().ServiceSegments.First(), 100m }, { wor.RailwayTimeTable.Trains.Last().ServiceSegments.Last(), 100m } }, Pal = new CustomerArrivalChain() { new CustomerArrival() { QueueOrder = 1, ArriveTime = new DateTime(1991, 7, 5, 0, 0, 1), Customer = new CustomerInfo() { MarSegID = 2 } }, new CustomerArrival() { QueueOrder = 2, ArriveTime = new DateTime(1991, 7, 5, 0, 1, 0), Customer = new CustomerInfo() { MarSegID = 3 } }, new CustomerArrival() { QueueOrder = 3, ArriveTime = new DateTime(1991, 7, 5, 0, 2, 0), Customer = new CustomerInfo() { MarSegID = 2 } }, new CustomerArrival() { QueueOrder = 4, ArriveTime = new DateTime(1991, 7, 5, 0, 2, 1), Customer = new CustomerInfo() { MarSegID = 3 } }, new CustomerArrival() { QueueOrder = 5, ArriveTime = new DateTime(1991, 7, 5, 0, 2, 2), Customer = new CustomerInfo() { MarSegID = 2 } }, new CustomerArrival() { QueueOrder = 6, ArriveTime = new DateTime(1991, 7, 5, 0, 2, 3), Customer = new CustomerInfo() { MarSegID = 3 } }, new CustomerArrival() { QueueOrder = 7, ArriveTime = new DateTime(1991, 7, 5, 0, 2, 4), Customer = new CustomerInfo() { MarSegID = 2 } }, new CustomerArrival() { QueueOrder = 8, ArriveTime = new DateTime(1991, 7, 5, 0, 2, 5), Customer = new CustomerInfo() { MarSegID = 3 } }, new CustomerArrival() { QueueOrder = 9, ArriveTime = new DateTime(1991, 7, 5, 0, 5, 1), Customer = new CustomerInfo() { MarSegID = 1 } }, new CustomerArrival() { QueueOrder = 10, ArriveTime = new DateTime(1991, 7, 5, 0, 9, 7), Customer = new CustomerInfo() { MarSegID = 3 } }, }, StartTime = new DateTime(1991, 7, 5, 0, 0, 0), EndTime = new DateTime(1991, 7, 5, 0, 10, 0), SitaDic = new Dictionary <int, decimal>() }; foreach (var marketseg in _ctx.Wor.Mar as IEnumerable <IRailwayMarketSegment> ) { _ctx.SitaDic.Add(marketseg.MSID, 50m); } _ctx.SetParameter("TerminalFactor", 0.0001); _ctx.SetParameter("Iteration", 50); _ctx.SetParameter("Resolution", 1); _ctx.SetParameter("WaitingCost", 0.1m); _ctx.SetParameter("InitMultiper", 0.1m); return(_ctx); }