public static IEnumerable <string> GetPricingPathString(DPNProblemContext ctx, DiscreteTimeAdapter adapter, Dictionary <ITrainTrip, Dictionary <IRailwayStation, PricePath> > solution_w) { //Print Head yield return("Station ID, Station Name, Train No, Time, Price Level"); //For each passenger, print information foreach (var pair in solution_w) { foreach (var subpair in pair.Value) { foreach (string str in subpair.Value.Nodepath) { string[] s = str.Split('_'); if (s[0] == "Start" || s[0] == "End") { continue; } else { int sT = Convert.ToInt32(s[1]); yield return(string.Join(",", subpair.Key.RailwayStationID, subpair.Key.StaName, pair.Key.Name, adapter.ConvertToDateTime(sT).ToString(), s[0])); } } } } }
public MinTravelTime_MaxRevenue_MixTravelHyperNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter, decimal cost_weight, decimal rev_weight) : base(ctx, adapter) { _cost_weight = cost_weight; _rev_weight = rev_weight; }
public ControlledLRxTravelHyperNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter, ObjectTravelHyperNetwork objNet, CustomerArrival customer, //旅客 Dictionary <CustomerArrival, List <TravelPath> > PathDict, Dictionary <IServiceSegment, decimal> LM_rho, //拉格朗日乘子 ρ Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> > LM_mu, //拉格朗日乘子 μ Dictionary <IEdge <TravelHyperNode>, decimal> LM_lambda, Dictionary <IEdge <TravelHyperNode>, bool> y) : base(ctx, adapter, objNet, customer, PathDict, LM_rho, LM_mu, LM_lambda) { _y = y; }
public LRxSimNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter, ObjectTravelHyperNetwork objNet, CustomerArrival customer, //旅客 Dictionary <CustomerArrival, List <TravelPath> > PathDict, Dictionary <IServiceSegment, decimal> LM_rho, //拉格朗日乘子 ρ Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> > LM_mu, //拉格朗日乘子 μ Dictionary <IEdge <TravelHyperNode>, decimal> LM_lambda, Dictionary <IEdge <TravelHyperNode>, bool> y) : base(ctx, adapter, y) { _LM_rho = LM_rho; _LM_mu = LM_mu; _LM_lambda = LM_lambda; _u = (PathDict.ContainsKey(customer) ? PathDict[customer].Sum(i => LM_mu[customer][i]) : 0m); _objNet = objNet; }
public ObjectTravelHyperNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter) : base(ctx, adapter) { }
public MaxRevenueTravelHyperNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter) : base(ctx, adapter) { }
private Dictionary <IServiceSegment, int> _segCapDict = new Dictionary <IServiceSegment, int>(); //segment的容量 //Reservation link 是否存在 Dictionary<IServiceSegment, int> serviceDic public SimNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter, Dictionary <IEdge <TravelHyperNode>, bool> y) : base(ctx, adapter, y) { }
/// <summary> /// 输出每一个旅客的路径信息 /// </summary> /// <param name="ctx"></param> /// <param name="adapter"></param> /// <param name="SegDic"></param> /// <param name="solution_x"></param> /// <returns></returns> public static IEnumerable <string> GetTravelPathString(DPNProblemContext ctx, DiscreteTimeAdapter adapter, BasicTravelHyperNetwork net, Dictionary <CustomerArrival, TravelPath> solution_x, Dictionary <CustomerArrival, decimal> PathCost) { //Print Head yield return("Customer ID, Description, Train Name, waiting Time (min), Waiting Time Cost, Travel Time Cost, Ticket Cost, Total Cost"); //For each passenger, print information foreach (var pair in solution_x) { var segments = pair.Value.GetSegments(net); if (segments != null || segments.Count() > 0) { var waitingTimeCost = pair.Value.GetWaitTime() * adapter.Resolution * ctx.WaitingVot; var travelTimeCost = (decimal)(segments.Max(i => i.ArrTime) - segments.Min(i => i.DepTime)).TotalMinutes * ctx.Vot; yield return(string.Join(",", pair.Key.QueueOrder,//旅客编号 ctx.Wor.Mar[pair.Key.Customer.MarSegID].Description, net.GetTrainByReservationLink(pair.Value.ReservationArc).Name, //string.Join(";", pair.Value.GetSegments(net).Select(i => $"{i.DepStation.StaName}->{i.ArrStation.StaName}")),//使用的服务路径 pair.Value.GetWaitTime() * adapter.Resolution, //等待时间 waitingTimeCost, //时间成本 travelTimeCost, segments.Sum(seg => ctx.BasicPriceDic[seg] * pair.Value.Price), //票价成本 Math.Round(PathCost[pair.Key], 2))); //总成本 } else { //TODO:Print the information of a passenger who gives up travelling. } } }
//增加了拉格朗日乘子之后的LR-w网络 public static DirectedWeightedSparseGraph <string> BuildLRwGraph( DPNProblemContext ctx, DiscreteTimeAdapter adapter, ITrainTrip train, IRailwayStation station, Dictionary <CustomerArrival, List <TravelPath> > PathDict, Dictionary <IEdge <TravelHyperNode>, ITrainTrip> linkTrainDict, Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> > LM_mu, //拉格朗日乘子 μ TODO:int 改为path , Dictionary <IEdge <TravelHyperNode>, decimal> LM_lambda) { DirectedWeightedSparseGraph <string> graph = new DirectedWeightedSparseGraph <string>(); //Build price_transfer links decimal[] priceLevelList = ctx.PriceLevelList.ToArray(); int last = 0; int interval = ctx.ControlInterval / adapter.Resolution; if (interval >= adapter.Horizon) { throw new Exception("控制频率应小于预售期"); } for (int level = 0; level < priceLevelList.Count(); level++) { for (int time = 0; time + interval < adapter.Horizon; time += interval) { decimal cost_part1 = ctx.Pal.Sum(c => PathDict[c].Where(path => path.StartStation == station && priceLevelList[level] == path.Price && linkTrainDict[path.ReservationArc] == train && time <= path.ReservationTime && time + interval > path.ReservationTime).Sum(p => LM_mu[c][p])); decimal cost_part2 = LM_lambda.Where(i => i.Key.Source.Station == station && i.Key.Destination.Price == priceLevelList[level] && linkTrainDict[i.Key] == train && time <= i.Key.Source.Time && time + interval > i.Key.Source.Time).Sum(i => i.Value); string selfnode = $"{priceLevelList[level]}_{time}"; //价格不变 string nextnode = $"{priceLevelList[level]}_{time + interval}"; if (!graph.HasVertex(selfnode)) { graph.AddVertex(selfnode); } if (!graph.HasVertex(nextnode)) { graph.AddVertex(nextnode); } if (!graph.AddEdge(selfnode, nextnode, cost_part1 - cost_part2)) { throw new Exception("存在相同的Edge"); } //上升一段 if (level < priceLevelList.Count() - 1) { string ariseNode = $"{priceLevelList[level + 1]}_{time + interval}"; if (!graph.HasVertex(ariseNode)) { graph.AddVertex(ariseNode); } if (!graph.AddEdge(selfnode, ariseNode, cost_part1 - cost_part2 + DpnAlgorithm.ASmallCost)) { throw new Exception("存在相同的Edge"); } } //下降一段 if (level > 0) { string decreaseNode = $"{priceLevelList[level - 1]}_{time + interval}";//价格等级默认是1 if (!graph.HasVertex(decreaseNode)) { graph.AddVertex(decreaseNode); } if (!graph.AddEdge(selfnode, decreaseNode, cost_part1 - cost_part2 - DpnAlgorithm.ASmallCost)) { throw new Exception("存在相同的Edge"); } } last = time + interval; } } //Build dummy nodes and links. string dummystart = $"Start";//价格等级默认是1 if (!graph.HasVertex(dummystart)) { graph.AddVertex(dummystart); } for (int level = 0; level < priceLevelList.Count(); level++) { string selfnode = $"{priceLevelList[level]}_0";//价格等级默认是1 if (!graph.AddEdge(dummystart, selfnode, (level + 1) * ASmallCost)) { throw new Exception("存在相同的Edge"); } } string dummyend = $"End";//价格等级默认是1 if (!graph.HasVertex(dummyend)) { graph.AddVertex(dummyend); } for (int level = 0; level < priceLevelList.Count(); level++) { string selfnode = $"{priceLevelList[level]}_{last}";//价格等级默认是1 if (!graph.AddEdge(selfnode, dummyend, ASmallCost)) { throw new Exception("存在相同的Edge"); } } var min = graph.Edges.Min(e => e.Weight); if (min <= 0) { foreach (var edge in graph.Edges) { edge.Weight += -min + DpnAlgorithm.ASmallCost; } } return(graph); }
public static decimal ASmallCost = 0.000001m;//为不考虑weight的arc增加一个极小的weight //给定w的网络 public static DirectedWeightedSparseGraph <string> BuildSolutionGraph( DPNProblemContext ctx, DiscreteTimeAdapter adapter, Dictionary <WeightedEdge <string>, bool> y) { int transferThreshold = ctx.TransferThreshold; List <decimal> priceLevelList = ctx.PriceLevelList; DirectedWeightedSparseGraph <string> graph = new DirectedWeightedSparseGraph <string>(); //Build In-train links foreach (var train in ctx.Wor.RailwayTimeTable.Trains) { foreach (var price in priceLevelList) { //Build In-train links in Section foreach (var seg in train.ServiceSegments) { string depNode = $"{adapter.Horizon + seg.DepTime.Hour * 60 + seg.DepTime.Minute}" + $"_{seg.DepStation.RailwayStationID}_{price}";//价格等级默认是1 if (!graph.HasVertex(depNode)) { graph.AddVertex(depNode); } string arrNode = $"{adapter.Horizon + seg.ArrTime.Hour * 60 + seg.ArrTime.Minute}" + $"_{seg.ArrStation.RailwayStationID}_{price}";//价格等级默认是1 if (!graph.HasVertex(arrNode)) { graph.AddVertex(arrNode); } graph.AddEdge(depNode, arrNode, (ctx.BasicPriceDic[seg] * price + (decimal)(seg.ArrTime - seg.DepTime).TotalMinutes * ctx.Vot)); }//Build In-train links in stop station foreach (var stop in train.StopStaions.Skip(1).Take(train.StopStaions.Count() - 2)) { string arrNode = $"{adapter.Horizon + stop.ArrTime.Hour * 60 + stop.ArrTime.Minute}" + $"_{stop.Station.RailwayStationID}_{price}"; //价格等级默认是1 string depNode = $"{adapter.Horizon + stop.DepTime.Hour * 60 + stop.DepTime.Minute}" + $"_{stop.Station.RailwayStationID}_{price}"; //价格等级默认是1 graph.AddEdge(arrNode, depNode, ((decimal)(stop.DepTime - stop.ArrTime).TotalMinutes * ctx.Vot)); } } } //Build waiting links foreach (var ms in ctx.Wor.Mar as IEnumerable <IRailwayMarketSegment> ) { for (int i = 0; i < adapter.Horizon; i++) { if (!graph.HasVertex($"{i}_{ms.OriSta.RailwayStationID}_0")) { graph.AddVertex($"{i}_{ms.OriSta.RailwayStationID}_0"); } if (!graph.HasVertex($"{i + 1}_{ms.OriSta.RailwayStationID}_0")) { graph.AddVertex($"{i + 1}_{ms.OriSta.RailwayStationID}_0"); } graph.AddEdge($"{i}_{ms.OriSta.RailwayStationID}_0", $"{i + 1}_{ms.OriSta.RailwayStationID}_0", adapter.Resolution * ctx.WaitingVot); // One minute each link } } //Build Reservation links foreach (var sta in ctx.Wor.Net.StationCollection) { //获取车次出发点 var depSegs = ctx.Wor.RailwayTimeTable.Trains.SelectMany(i => i.ServiceSegments.Where(s => s.DepStation == sta)); foreach (var seg in depSegs) { foreach (var price in priceLevelList) { string depNode = $"{adapter.Horizon + seg.DepTime.Hour * 60 + seg.DepTime.Minute}_{seg.DepStation.RailwayStationID}_{price}"; for (int i = 0; i < adapter.Horizon; i++) { var edge = y.Keys.FirstOrDefault(link => link.Source == $"{i}_{sta.RailwayStationID.ToString()}_0" && link.Destination == depNode); if (edge != null && y[edge]) { graph.AddEdge($"{i}_{sta.RailwayStationID.ToString()}_0", depNode, ASmallCost); // 一次预订花费一分钟 } } } } } //Build finishing links foreach (var price in priceLevelList) { foreach (var sta in ctx.Wor.Net.StationCollection) { var arrSegs = ctx.Wor.RailwayTimeTable.Trains.SelectMany(i => i.ServiceSegments.Where(s => s.ArrStation == sta)); foreach (var seg in arrSegs) { string arrNode = $"{adapter.Horizon + seg.ArrTime.Hour * 60 + seg.ArrTime.Minute}_{seg.ArrStation.RailwayStationID}_{price}";//价格等级 string endNode = $"End_{sta.RailwayStationID.ToString()}_0"; if (!graph.HasVertex(endNode)) { graph.AddVertex(endNode); } graph.AddEdge(arrNode, endNode, ASmallCost); } } } //Build quit links /* * foreach (var ms in ctx.Wor.Mar as IEnumerable<IRailwayMarketSegment>) * { * graph.AddEdge($"{adapter.Horizon}_{ms.OriSta.RailwayStationID}_0", * $"End_{ms.DesSta.RailwayStationID}_0", 999m); * } */ return(graph); }
public ControlledTravelHyperNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter, Dictionary <IEdge <TravelHyperNode>, bool> y) : base(ctx, adapter) { _y = y; }
public override void Work() { //获取求解设置 decimal terminalFactor = _ctx.GetParameter("TerminalFactor", 0.001m); int iteration = _ctx.GetParameter("Iteration", 10); int resolution = _ctx.GetParameter("Resolution", 60); decimal initMultipler = _ctx.GetParameter("InitMultiper", 0.1m); string objectiveType = _ctx.GetParameter("ObjectiveType", ""); // 相对-绝对时间转化器 DiscreteTimeAdapter adapter = new DiscreteTimeAdapter(_ctx.StartTime, _ctx.EndTime, resolution); // 路径集 Dictionary <CustomerArrival, List <TravelPath> > pathDict = new Dictionary <CustomerArrival, List <TravelPath> >(); #region 建立初始网络,搜索可行路径 //目标函数网络 var objgraph = ObjectNetworkFactory.Create(objectiveType, _ctx, adapter); //new ObjectTravelHyperNetwork(_ctx, adapter); objgraph.Build(); //基础网络 var basicGraph = new BasicTravelHyperNetwork(_ctx, adapter); basicGraph.Build(); SubTasks.Clear(); foreach (CustomerArrival customer in _ctx.Pal) { Task ta = factory.StartNew(() => { var ori = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).OriSta; var des = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).DesSta; var paths = DepthFirstSearcher.FindAllPaths(basicGraph, new TravelHyperNode() { Time = adapter.ConvertToDiscreteTime(customer.ArriveTime), Station = ori, Price = 0 }, new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 }); pathDict.Add(customer, new List <TravelPath>()); foreach (var path in paths) { pathDict[customer].Add(new TravelPath(basicGraph, path)); } }); SubTasks.Add(ta); } Task.WaitAll(SubTasks.ToArray()); #endregion #region 构建对偶问题 with CPLEX Cplex model = new Cplex(); model.SetOut(null); INumVar theta = model.NumVar(double.MinValue, double.MaxValue); //θ model.AddMaximize(theta); Dictionary <IServiceSegment, INumVar> dual_rho = _ctx.Wor.RailwayTimeTable.Trains .SelectMany(i => i.ServiceSegments).ToDictionary(i => i, i => model.NumVar(0, double.MaxValue)); Dictionary <CustomerArrival, Dictionary <TravelPath, INumVar> > dual_mu = new Dictionary <CustomerArrival, Dictionary <TravelPath, INumVar> >(); foreach (CustomerArrival customer in _ctx.Pal) { dual_mu.Add(customer, new Dictionary <TravelPath, INumVar>()); foreach (var path in pathDict[customer]) { dual_mu[customer].Add(path, model.NumVar(0, double.MaxValue)); } } Dictionary <IEdge <TravelHyperNode>, INumVar> dual_lambda = new Dictionary <IEdge <TravelHyperNode>, INumVar>(); foreach (CustomerArrival customer in _ctx.Pal) { foreach (var path in pathDict[customer]) { if (!dual_lambda.ContainsKey(path.ReservationArc)) { dual_lambda.Add(path.ReservationArc, model.NumVar(0, double.MaxValue)); } } } #endregion #region 变量与乘子 //决策变量 x Dictionary <CustomerArrival, TravelPath> x = new Dictionary <CustomerArrival, TravelPath>(); foreach (CustomerArrival customer in _ctx.Pal) { x.Add(customer, new TravelPath()); } //决策变量 w Dictionary <ITrainTrip, Dictionary <IRailwayStation, PricePath> > w = new Dictionary <ITrainTrip, Dictionary <IRailwayStation, PricePath> >(); foreach (var train in _ctx.Wor.RailwayTimeTable.Trains) { w.Add(train, new Dictionary <IRailwayStation, PricePath>()); foreach (var sta in _ctx.Wor.Net.StationCollection) { w[train].Add(sta, null); } } //辅助变量 y //记录每条弧在当前w的取值下是否可行(available),值为true = 可行;false = 不可行 //超出了y记录的reservation arc 不会有人走 Dictionary <IEdge <TravelHyperNode>, bool> y = new Dictionary <IEdge <TravelHyperNode>, bool>(); foreach (var p in pathDict.Values.SelectMany(i => i)) { if (p.ReservationArc != null && !y.ContainsKey(p.ReservationArc)) { y.Add(p.ReservationArc, false); } } //拉格朗日乘子 rho Dictionary <IServiceSegment, decimal> LM_rho = _ctx.Wor.RailwayTimeTable.Trains .SelectMany(i => i.ServiceSegments).ToDictionary(i => i, i => initMultipler); //拉格朗日乘子 rho 迭代方向 Dictionary <IServiceSegment, decimal> Grad_rho = _ctx.Wor.RailwayTimeTable.Trains .SelectMany(i => i.ServiceSegments).ToDictionary(i => i, i => initMultipler); //拉格朗日乘子 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, initMultipler); } } //拉格朗日乘子 mu 迭代方向 Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> > Grad_mu = new Dictionary <CustomerArrival, Dictionary <TravelPath, decimal> >(); foreach (CustomerArrival customer in _ctx.Pal) { Grad_mu.Add(customer, new Dictionary <TravelPath, decimal>()); foreach (var path in pathDict[customer]) { Grad_mu[customer].Add(path, initMultipler); } } //拉格朗日乘子 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, initMultipler); } } } //拉格朗日乘子 lambda 迭代方向 Dictionary <IEdge <TravelHyperNode>, decimal> Grad_lambda = new Dictionary <IEdge <TravelHyperNode>, decimal>(); foreach (CustomerArrival customer in _ctx.Pal) { foreach (var path in pathDict[customer]) { if (!Grad_lambda.ContainsKey(path.ReservationArc)) { Grad_lambda.Add(path.ReservationArc, initMultipler); } } } #endregion decimal bigM1 = pathDict.Max(i => i.Value.Max(j => basicGraph.GetPathCost(j))); decimal bigM2 = _ctx.Pal.Count(); decimal bigM = Math.Max(bigM1, bigM2); decimal lowerBound = decimal.MinValue; decimal upperBound = decimal.MaxValue; bool flag = false;//对偶问题有解 PrintIterationInfo($"Iteration Number, Lower Bound, Upper Bound, Best Lower Bound, Best Upper Bound, Total Gap(%) "); for (int iter = 0; iter < iteration; iter++) { Log($"--------------第{iter}轮求解开始--------------"); bool hasFeasibleSolution = true; #region 求解LR问题 SubTasks.Clear(); foreach (CustomerArrival customer in _ctx.Pal)// 求解x { Task ta = factory.StartNew(() => { var graph = new LRxTravelHyperNetwork(_ctx, adapter, objgraph, customer, pathDict, LM_rho, LM_mu, LM_lambda); graph.Build(); var ori = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).OriSta; var des = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).DesSta; DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode>(graph, new TravelHyperNode() { Time = adapter.ConvertToDiscreteTime(customer.ArriveTime), Station = ori, Price = 0 }); //考虑该旅客到达时间 x[customer] = new TravelPath(graph, dijkstra.ShortestPathTo( new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 })); }); SubTasks.Add(ta); } foreach (var train in _ctx.Wor.RailwayTimeTable.Trains) { foreach (IRailwayStation station in _ctx.Wor.Net.StationCollection)// 求解w { Task ta = factory.StartNew(() => { var graph = DpnAlgorithm.BuildLRwGraph(_ctx, adapter, train, station, pathDict, basicGraph.LinkTrainDict, LM_mu, LM_lambda); DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "Start");//考虑该旅客到达时间 var nodepath = dijkstra.ShortestPathTo("End"); if (nodepath == null) { throw new System.Exception("No path found"); } else { w[train][station] = new PricePath(graph, nodepath); } }); SubTasks.Add(ta); } } Task.WaitAll(SubTasks.ToArray()); foreach (var edge in y.Keys.ToArray())//更新y { var sta = edge.Source.Station; var train = basicGraph.GetTrainByReservationLink(edge); y[edge] = w[train][sta].GetWrapPoints(edge.Destination.Price, edge.Source.Time).Any(); } #endregion #region 计算拉格朗日函数值作为下界 decimal templowerBound = 0m; decimal templowerBound_part1 = 0m; decimal templowerBound_part2 = 0m; decimal templowerBound_part3 = 0m; decimal templowerBound_part4 = 0m; Dictionary <CustomerArrival, decimal> lbValueDic = new Dictionary <CustomerArrival, decimal>(); //1 计算在基础网络中的路径cost foreach (CustomerArrival customer in _ctx.Pal) { lbValueDic.Add(customer, objgraph.GetPathCost(x[customer])); templowerBound_part1 += lbValueDic[customer]; } //2计算BRUE项 templowerBound_part2 += _ctx.Pal.Sum(c => pathDict[c].Sum(p => { decimal secondItem = 0m; secondItem += basicGraph.GetPathCost(x[c]) - basicGraph.GetPathCost(p) - _ctx.SitaDic[c.Customer.MarSegID]; secondItem -= (p.ReservationArc != null && y[p.ReservationArc]) ? 0 : bigM; return(secondItem * LM_mu[c][p]); })); //3计算In-train Capacity项 Dictionary <IServiceSegment, int> ServiceDic = _ctx.Wor.RailwayTimeTable .Trains.SelectMany(i => i.ServiceSegments) .ToDictionary(i => i, i => 0);//当前service segment使用情况 foreach (var p in x.Values) { foreach (IServiceSegment seg in p.GetSegments(basicGraph)) { ServiceDic[seg] += 1; } } foreach (var train in _ctx.Wor.RailwayTimeTable.Trains) { foreach (var seg in train.ServiceSegments) { templowerBound_part3 += LM_rho[seg] * (ServiceDic[seg] - train.Carriage.Chairs.Count()); } } //4 计算reservation constraint 项 Dictionary <IEdge <TravelHyperNode>, int> reservationDic = new Dictionary <IEdge <TravelHyperNode>, int>(); foreach (var p in x.Values) { if (reservationDic.ContainsKey(p.ReservationArc)) { reservationDic[p.ReservationArc] += 1; } else { reservationDic.Add(p.ReservationArc, 1); } } foreach (var pair in y.Keys) { //y是所有的reservation 的集合 reservationDic 是已经使用的reservation 集合 var res = reservationDic.Keys.FirstOrDefault(i => i.Source == pair.Source && i.Destination == pair.Destination); templowerBound_part4 += LM_lambda[pair] * ((res != null ? reservationDic[res] : 0) - (y[pair] ? bigM : 0)); } templowerBound = templowerBound_part1 + templowerBound_part2 + templowerBound_part3 + templowerBound_part4; //Log($"Lower Bound = { Math.Round(templowerBound, 2)}," + // $"({ Math.Round(templowerBound_part1, 2) }" + // $"+{ Math.Round(templowerBound_part2, 2)}" + // $"+{ Math.Round(templowerBound_part3, 2)}" + // $"+{ Math.Round(templowerBound_part4, 2)})"); PrintLBSolution(DpnAlgorithm.GetTravelPathString(_ctx, adapter, objgraph, x, lbValueDic)); #endregion #region 更新乘子 if (flag) { decimal LagLowerBound = Convert.ToDecimal(model.GetValue(theta)); Log($"LagLower Bound = { LagLowerBound }"); Log($"Lower Bound = { templowerBound }"); lowerBound = Math.Max(lowerBound, templowerBound); decimal lagGap = LagLowerBound - templowerBound; if (lagGap <= 0)//判断拉格朗日对偶问题是否达到最优 { Log($"求解终止:对偶函数已最优。"); break; } else { /* 更新乘子值 */ foreach (var pair in dual_rho) { LM_rho[pair.Key] = Convert.ToDecimal(model.GetValue(pair.Value)); } foreach (var pair in dual_lambda) { LM_lambda[pair.Key] = Convert.ToDecimal(model.GetValue(pair.Value)); } foreach (CustomerArrival customer in _ctx.Pal) { foreach (var path in pathDict[customer]) { LM_mu[customer][path] = Convert.ToDecimal(model.GetValue(dual_mu[customer][path])); } } } } else /* 如果对偶问题无可行解,通过次梯度的方式更新乘子 */ { //decimal step = 1.618m / (iter + 1); //foreach (CustomerArrival c in _ctx.Pal)//更新mu //{ // foreach (TravelPath p in pathDict[c]) // { // Grad_mu[c][p] = basicGraph.GetPathCost(x[c]) - basicGraph.GetPathCost(p) - _ctx.SitaDic[c.Customer.MarSegID] // - ((p.ReservationArc != null && y[p.ReservationArc]) ? 0 : bigM); // LM_mu[c][p] = Math.Max(0, LM_mu[c][p] + step * Grad_mu[c][p]); // } //} //foreach (var pair in y.Keys) //更新lambda //{ // var res = reservationDic.Keys.FirstOrDefault(i => i.Source == pair.Source && i.Destination == pair.Destination); // Grad_lambda[pair] = ((res != null ? reservationDic[res] : 0) - (y[pair] ? bigM : 0)); // LM_lambda[pair] = Math.Max(0, LM_lambda[pair] + step * Grad_lambda[pair]); //} //foreach (var train in _ctx.Wor.RailwayTimeTable.Trains)//更新rho //{ // foreach (var seg in train.ServiceSegments) // { // Grad_rho[seg] = ServiceDic[seg] - train.Carriage.Chairs.Count(); // LM_rho[seg] = Math.Max(0, LM_rho[seg] + step * Grad_rho[seg]); // } //} } #endregion #region 求解拉格朗日对偶问题 /* 求解 几何乘子 */ // 增加 一个约束 INumExpr exp = model.NumExpr(); //2 计算BRUE项 foreach (var c in _ctx.Pal) { foreach (var p in pathDict[c]) { decimal secondItem = 0m; secondItem += basicGraph.GetPathCost(x[c]) - basicGraph.GetPathCost(p) - _ctx.SitaDic[c.Customer.MarSegID]; secondItem -= (p.ReservationArc != null && y[p.ReservationArc]) ? 0 : bigM; exp = model.Sum(exp, model.Prod(Convert.ToDouble(secondItem), dual_mu[c][p])); } } //3计算In-train Capacity项 (这里直接用了计算下界时候的 Service_dic foreach (var train in _ctx.Wor.RailwayTimeTable.Trains) { foreach (var seg in train.ServiceSegments) { exp = model.Sum(exp, model.Prod(Convert.ToDouble(ServiceDic[seg] - train.Carriage.Chairs.Count()), dual_rho[seg])); } } //4 计算reservation constraint 项 (这里直接用了计算下界时候的 reservationDic foreach (var pair in y.Keys) { var res = reservationDic.Keys.FirstOrDefault(i => i.Source == pair.Source && i.Destination == pair.Destination); exp = model.Sum(exp, model.Prod(Convert.ToDouble(((res != null ? reservationDic[res] : 0) - (y[pair] ? bigM : 0))), dual_lambda[pair])); } model.AddGe(model.Sum(Convert.ToDouble(templowerBound_part1), exp), theta); /* Trust-Region */ //var trExpr = model.Add() flag = model.Solve(); Log($"Is Dual Problem Feasible: { flag }"); #endregion #region 通过一个启发式规则计算上界(按照w模拟到达) var pathcost = lbValueDic.ToDictionary(i => i.Key, i => i.Value); var x_least = x.ToDictionary(i => i.Key, i => i.Value);//当前w下每个旅客的最短路径 var x_upperbound = x.ToDictionary(i => i.Key, i => i.Value); var x_controlled = x.ToDictionary(i => i.Key, i => i.Value); #region 1-构建当前y下的最优x值 SubTasks.Clear(); foreach (CustomerArrival customer in _ctx.Pal)// 求解x { Task ta = factory.StartNew(() => { var controlledLRxgraph = new ControlledLRxTravelHyperNetwork( _ctx, adapter, objgraph, customer, pathDict, LM_rho, LM_mu, LM_lambda, y); controlledLRxgraph.Build(); var ori = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).OriSta; var des = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).DesSta; TravelHyperNode startNode = new TravelHyperNode() { Time = adapter.ConvertToDiscreteTime(customer.ArriveTime), Station = ori, Price = 0 }; TravelHyperNode endNode = new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 }; DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> (controlledLRxgraph, startNode);//考虑该旅客到达时间 if (!dijkstra.HasPathTo(endNode)) { throw new System.Exception("没有路径!"); } else { x_controlled[customer] = new TravelPath(controlledLRxgraph, dijkstra.ShortestPathTo(endNode)); } }); SubTasks.Add(ta); } Task.WaitAll(SubTasks.ToArray()); #endregion # region 2-构建当前y下的出行最小值 var solutiongraph = new ControlledTravelHyperNetwork(_ctx, adapter, y); solutiongraph.Build(); Parallel.ForEach(_ctx.Pal, customer => //foreach (var customer in _ctx.Pal)//求此网络下每个旅客的最短路径 { var ori = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).OriSta; var des = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).DesSta; TravelHyperNode startNode = new TravelHyperNode() { Time = adapter.ConvertToDiscreteTime(customer.ArriveTime), Station = ori, Price = 0 }; TravelHyperNode endNode = new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 }; DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> (solutiongraph, startNode); if (!dijkstra.HasPathTo(endNode)) { throw new System.Exception("没有路径!"); } else { x_least[customer] = new TravelPath(solutiongraph, dijkstra.ShortestPathTo(endNode)); } }); #endregion #region 3-修复可行解 var solutiongraphTemp = new SimNetwork(_ctx, adapter, y);//建立仿真网络 solutiongraphTemp.Build(); foreach (var customer in _ctx.Pal) { x_upperbound[customer] = x_controlled[customer]; TravelPath path = x_controlled[customer]; if (!solutiongraphTemp.IsPathFeasible(path) || solutiongraphTemp.GetPathCost(path) > solutiongraph.GetPathCost(x_least[customer]) + _ctx.SitaDic[customer.Customer.MarSegID])//如果违反了容量约束或者BRUE约束 { var ori = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).OriSta; var des = (_ctx.Wor.Mar[customer.Customer.MarSegID] as IRailwayMarketSegment).DesSta; DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode> dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <TravelHyperNode>, TravelHyperNode>(solutiongraphTemp, new TravelHyperNode() { Time = adapter.ConvertToDiscreteTime(customer.ArriveTime), Station = ori, Price = 0 }); //重新查找路径,如果存在路径 if (dijkstra.HasPathTo(new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 })) { x_upperbound[customer] = new TravelPath(solutiongraphTemp, dijkstra.ShortestPathTo(new TravelHyperNode() { Time = adapter.Horizon + 1440, Station = des, Price = 0 })); if (solutiongraphTemp.GetPathCost(x_upperbound[customer]) <= //满足BRUE约束 solutiongraph.GetPathCost(x_least[customer]) + _ctx.SitaDic[customer.Customer.MarSegID]) { path = x_upperbound[customer]; } else { hasFeasibleSolution = false; break; } } else { hasFeasibleSolution = false; break; } } pathcost[customer] = objgraph.GetPathCost(path); //加载路径 foreach (var seg in path.GetSegments(basicGraph)) { solutiongraphTemp.AddUsage(seg, 1); } } var tempUpperbound = _ctx.Pal.Sum(c => objgraph.GetPathCost(x_upperbound[c])); #endregion //如果有最优解再更新上界 bool hasBetterUpperbound = tempUpperbound < upperBound; if (hasFeasibleSolution) { upperBound = Math.Min(upperBound, tempUpperbound); } Log($"Upper Bound = { Math.Round(tempUpperbound, 2) },找到可行解 : { hasFeasibleSolution.ToString()}"); #endregion #region Terminatation 判定 Log($"Upper Bound = { tempUpperbound }"); decimal absoluteGap = 0; string gapStr = ""; //如果上限是无穷,那么此时gap也是无穷 if (upperBound == decimal.MaxValue || lowerBound == decimal.MinValue) { absoluteGap = decimal.MaxValue; gapStr = $"+∞"; } else { absoluteGap = upperBound - lowerBound; gapStr = $"{ Math.Round(absoluteGap, 2)}"; } if (absoluteGap < terminalFactor && absoluteGap > 0) { Log($"求解终止:Gap以满足终止条件,Gap={ absoluteGap }"); break; } Log($"Total Gap = { gapStr }"); #endregion #region 输出信息 //SendMessage($"#Iteration Number, Lower Bound, Upper Bound, Best Lower Bound, Best Upper Bound, Total Gap(%) "); PrintIterationInfo($"#{iter},{ Math.Round(templowerBound) },{ Math.Round(tempUpperbound) },{ Math.Round(lowerBound)}" + $",{ Math.Round(upperBound) },{ gapStr }"); string ss = "###,"; foreach (var s in LM_rho) { ss += ($"{s.Key.ToString()}:{ s.Value.ToString() },"); } PrintIterationInfo(ss); if (hasFeasibleSolution && hasBetterUpperbound) { ObjValue = pathcost.Sum(i => i.Value); PrintSolution( DpnAlgorithm.GetTravelPathString(_ctx, adapter, solutiongraph, x_upperbound, pathcost), DpnAlgorithm.GetPricingPathString(_ctx, adapter, w)); } #endregion Log($"--------------第{iter}轮求解结束--------------"); }
public BasicTravelHyperNetwork(DPNProblemContext ctx, DiscreteTimeAdapter adapter) : base() { _ctx = ctx; _adapter = adapter; }