Exemplo n.º 1
0
        /// <summary>
        /// Estimates traffic delay from the collection of travel times
        /// </summary>
        /// <param name="travelTimes"></param>
        /// <param name="model"></param>
        void EstimateTrafficDelay(IEnumerable<TravelTime> travelTimes, Model model)
        {
            List<TravelTimeDelay> delays = new List<TravelTimeDelay>();
            foreach (var traveltime in travelTimes) {
                double delay = 0;
                if (model.TrafficSignalsDelay.Probability > 0 && traveltime.Stops.Count > 0)
                    delay = traveltime.TotalTravelTime.TotalSeconds - model.FreeFlowTravelTime - traveltime.Stops.Last().Length.TotalSeconds;
                else
                    delay = traveltime.TotalTravelTime.TotalSeconds - model.FreeFlowTravelTime;

                delay = Math.Max(0, delay);
                delays.Add(new TravelTimeDelay() { TravelTime = traveltime, Delay = delay });
            }

            // avg delay as median of delays
            delays.Sort(new Comparison<TravelTimeDelay>((TravelTimeDelay td1, TravelTimeDelay td2) => td1.Delay.CompareTo(td2.Delay)));
            model.AvgDelay = delays[delays.Count / 2].Delay;

            TrafficDelayMap delaysMap = new TrafficDelayMap(Properties.Settings.Default.ModelResolution, model.FreeFlowTravelTime);

            List<List<TravelTimeDelay>> travelTimeClusters = null;
            DBScan<TravelTimeDelay> clusterAnalyzer = new DBScan<TravelTimeDelay>(new DBScan<TravelTimeDelay>.FindNeighbours(FindNeighbours));
            for (int i = _parameters.Length -1; i >= 0; i--) {
                _parametersIndex = i;

                int desiredClusterSize = (int)Math.Max(Properties.Settings.Default.MinimalClusterSize, travelTimes.Count() * Properties.Settings.Default.ClusterSizePercentage / 100.0);
                int clusterSize = Math.Min(travelTimes.Count(), desiredClusterSize);
                travelTimeClusters = clusterAnalyzer.ClusterAnalysis(delays, clusterSize);

                foreach (var cluster in travelTimeClusters) {
                    TrafficDelayInfo delayInfo = new TrafficDelayInfo();
                    if (_parameters[_parametersIndex].Dates == DatesHandling.Any)
                        delayInfo.AppliesTo = DayOfWeek.Any;
                    else if (_parameters[_parametersIndex].Dates == DatesHandling.WeekendWorkdays)
                        delayInfo.AppliesTo = (DayOfWeek.Workday & DayOfWeekHelper.FromDate(cluster[0].TravelTime.TimeStart)) > 0 ? DayOfWeek.Workday : DayOfWeek.Weekend;
                    else
                        delayInfo.AppliesTo = DayOfWeekHelper.FromDate(cluster[0].TravelTime.TimeStart);

                    cluster.Sort(new Comparison<TravelTimeDelay>((TravelTimeDelay td1, TravelTimeDelay td2) => td1.Delay.CompareTo(td2.Delay)));

                    delayInfo.Delay = cluster.Sum(tt => tt.Delay) / cluster.Count;
                    delayInfo.From = cluster.Min(tt => tt.TravelTime.TimeStart.TimeOfDay).Subtract(new TimeSpan(0, _parameters[_parametersIndex].MembersTimeDifference / 2, 0));
                    if (delayInfo.From < new TimeSpan(0))
                        delayInfo.From = new TimeSpan(0);

                    delayInfo.To = cluster.Max(tt => tt.TravelTime.TimeEnd.TimeOfDay).Add(new TimeSpan(0, _parameters[_parametersIndex].MembersTimeDifference / 2, 0));
                    if (delayInfo.To > new TimeSpan(23, 59, 59)) {
                        delayInfo.To = new TimeSpan(23, 59, 59);
                    }
                    delaysMap.AddDelay(delayInfo.From, delayInfo.To, delayInfo.AppliesTo, delayInfo.Delay);
                }

                if (travelTimeClusters.Sum(cluster => cluster.Count) > Properties.Settings.Default.ClusterAnalysisStopPercentage * delays.Count / 100)
                    break;
            }

            model.TrafficDelay.AddRange(delaysMap.GetDelays());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Estimates traffic delay from the collection of travel times
        /// </summary>
        /// <param name="travelTimes"></param>
        /// <param name="model"></param>
        void EstimateTrafficDelay(IEnumerable <TravelTime> travelTimes, Model model)
        {
            List <TravelTimeDelay> delays = new List <TravelTimeDelay>();

            foreach (var traveltime in travelTimes)
            {
                double delay = 0;
                if (model.TrafficSignalsDelay.Probability > 0 && traveltime.Stops.Count > 0)
                {
                    delay = traveltime.TotalTravelTime.TotalSeconds - model.FreeFlowTravelTime - traveltime.Stops.Last().Length.TotalSeconds;
                }
                else
                {
                    delay = traveltime.TotalTravelTime.TotalSeconds - model.FreeFlowTravelTime;
                }

                delay = Math.Max(0, delay);
                delays.Add(new TravelTimeDelay()
                {
                    TravelTime = traveltime, Delay = delay
                });
            }

            // avg delay as median of delays
            delays.Sort(new Comparison <TravelTimeDelay>((TravelTimeDelay td1, TravelTimeDelay td2) => td1.Delay.CompareTo(td2.Delay)));
            model.AvgDelay = delays[delays.Count / 2].Delay;

            TrafficDelayMap delaysMap = new TrafficDelayMap(Properties.Settings.Default.ModelResolution, model.FreeFlowTravelTime);

            List <List <TravelTimeDelay> > travelTimeClusters = null;
            DBScan <TravelTimeDelay>       clusterAnalyzer    = new DBScan <TravelTimeDelay>(new DBScan <TravelTimeDelay> .FindNeighbours(FindNeighbours));

            for (int i = _parameters.Length - 1; i >= 0; i--)
            {
                _parametersIndex = i;

                int desiredClusterSize = (int)Math.Max(Properties.Settings.Default.MinimalClusterSize, travelTimes.Count() * Properties.Settings.Default.ClusterSizePercentage / 100.0);
                int clusterSize        = Math.Min(travelTimes.Count(), desiredClusterSize);
                travelTimeClusters = clusterAnalyzer.ClusterAnalysis(delays, clusterSize);

                foreach (var cluster in travelTimeClusters)
                {
                    TrafficDelayInfo delayInfo = new TrafficDelayInfo();
                    if (_parameters[_parametersIndex].Dates == DatesHandling.Any)
                    {
                        delayInfo.AppliesTo = DayOfWeek.Any;
                    }
                    else if (_parameters[_parametersIndex].Dates == DatesHandling.WeekendWorkdays)
                    {
                        delayInfo.AppliesTo = (DayOfWeek.Workday & DayOfWeekHelper.FromDate(cluster[0].TravelTime.TimeStart)) > 0 ? DayOfWeek.Workday : DayOfWeek.Weekend;
                    }
                    else
                    {
                        delayInfo.AppliesTo = DayOfWeekHelper.FromDate(cluster[0].TravelTime.TimeStart);
                    }

                    cluster.Sort(new Comparison <TravelTimeDelay>((TravelTimeDelay td1, TravelTimeDelay td2) => td1.Delay.CompareTo(td2.Delay)));

                    delayInfo.Delay = cluster.Sum(tt => tt.Delay) / cluster.Count;
                    delayInfo.From  = cluster.Min(tt => tt.TravelTime.TimeStart.TimeOfDay).Subtract(new TimeSpan(0, _parameters[_parametersIndex].MembersTimeDifference / 2, 0));
                    if (delayInfo.From < new TimeSpan(0))
                    {
                        delayInfo.From = new TimeSpan(0);
                    }

                    delayInfo.To = cluster.Max(tt => tt.TravelTime.TimeEnd.TimeOfDay).Add(new TimeSpan(0, _parameters[_parametersIndex].MembersTimeDifference / 2, 0));
                    if (delayInfo.To > new TimeSpan(23, 59, 59))
                    {
                        delayInfo.To = new TimeSpan(23, 59, 59);
                    }
                    delaysMap.AddDelay(delayInfo.From, delayInfo.To, delayInfo.AppliesTo, delayInfo.Delay);
                }

                if (travelTimeClusters.Sum(cluster => cluster.Count) > Properties.Settings.Default.ClusterAnalysisStopPercentage * delays.Count / 100)
                {
                    break;
                }
            }

            model.TrafficDelay.AddRange(delaysMap.GetDelays());
        }