public WayInfo AddTravel(Travel travel) { if (EndTime.TotalMinutes + travel.TravelTime.TotalMinutes > 24 * 60) { throw new TimeElapsedException(); } WayInfo res = new WayInfo(this); res.Travels.Add(travel); return(res); }
private List <WayInfo> GetWayInfos(int startBusStopStopId, int targetBusStopId, TimeSpan travelStartTime) { if (startBusStopStopId == targetBusStopId) { return(null); } if (!graph.ContainsKey(targetBusStopId) || !graph.ContainsKey(startBusStopStopId)) { return(null); } var res = new List <WayInfo>(); var temp = new List <WayInfo>(); var parallelOptions = new ParallelOptions(); parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount * 4; Parallel.ForEach(graph[startBusStopStopId], parallelOptions, (g) => { foreach (var travelinfo in g.Value) { try { var wayInfo = new WayInfo(new WayInfo.Travel() { BusId = travelinfo.BusId, FromId = startBusStopStopId, ToId = g.Key, WaitTime = new TimeSpan(0, 0, 0), TravelTime = travelinfo.Time, Cost = travelinfo.Cost }, GetNextBusStopTime(travelinfo.BusId, startBusStopStopId, travelStartTime)); if (wayInfo.Travels.Where(b => (b.ToId == g.Key ? true : false)).Count() == 1) { if (wayInfo.Travels.Last().ToId == targetBusStopId) { res.Add(new WayInfo(wayInfo)); } else { temp.Add(new WayInfo(wayInfo)); } } } catch (TimeElapsedException) { } } }); parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount; while (temp.Count > 0) { var tempCount = temp.Count; for (var i = 0; i < tempCount; i++) { var fromBusStopId = temp.ElementAt(i).Travels.Last().ToId; Parallel.ForEach(graph[fromBusStopId], parallelOptions, (g) => { Parallel.ForEach(g.Value, parallelOptions, (travelinfo) => { try { var wayInfo = temp.ElementAt(i).AddTravel(new WayInfo.Travel() { BusId = travelinfo.BusId, FromId = fromBusStopId, ToId = g.Key, WaitTime = GetNextBusStopTime(travelinfo.BusId, fromBusStopId, temp.ElementAt(i).EndTime) - temp.ElementAt(i).EndTime, TravelTime = travelinfo.Time, Cost = temp.ElementAt(i).Travels.Last().BusId == travelinfo.BusId ? 0 : travelinfo.Cost }); if (wayInfo.Travels.Where(b => (b.FromId == g.Key ? true : false)).Count() == 0) { if (wayInfo.Travels.Last().ToId == targetBusStopId) { res.Add(new WayInfo(wayInfo)); } else if (wayInfo.Travels .Where(travel => travel.ToId == targetBusStopId ? true : false) .Count() == 0) { temp.Add(new WayInfo(wayInfo)); } } } catch (TimeElapsedException) { } }); }); } temp.RemoveRange(0, tempCount); } return(res.ToList()); }
public WayInfo(WayInfo info) { Travels = new List <Travel>(); Travels.AddRange(info.Travels); StartTime = info.StartTime; }