예제 #1
0
        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")));
        }
예제 #2
0
        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);
        }