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 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 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);
        }
        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);
        }
Example #7
0
        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));
            }
            }
        }