Beispiel #1
0
        /// <summary>
        /// Calculate the adjusted walking time for this route, taking congestion into account.
        /// </summary>
        /// <param name="allStudentRoutes">A collection of all student routes today.</param>
        /// <param name="startingTime">The starting time of this route.</param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="AggregateException"></exception>
        public void CalculateAdjustedWalkingTime(StudentRoute[] allStudentRoutes, TimeSpan startingTime)
        {
            double distanceSoFar            = 0;
            double totalAdjustedWalkingTime = 0;

            List <Dictionary <(string entryId, string exitId), int> > allCongestionValues = new List <Dictionary <(string entryId, string exitId), int> >(RouteNodes.Count);

            for (int i = 0; i < RouteNodes.Count - 1; i++)
            {
                // Increment the distance walked so far.
                if (RouteNodes[i].Type == NodeType.Corridor && RouteNodes[i + 1].Type == NodeType.Corridor)
                {
                    distanceSoFar += RouteNodes[i].DistanceInMetersTo(RouteNodes[i + 1]);
                }
                else
                {
                    distanceSoFar += SharedFunctions.NonCorridorDistance;
                }

                // Calculate the time spent getting to this point and get the current time of day from that.
                TimeSpan currentTime = startingTime.Add(TimeSpan.FromSeconds(SharedFunctions.CalculateWalkingTimeNoRounding(distanceSoFar)));

                // Calculate the occupancies at this point.
                Dictionary <(string entryId, string exitId), int> edgeOccupancies = CongestionHelper.CalculateEdgeOccupanciesAtTime(allStudentRoutes, currentTime);

                if (edgeOccupancies.Count > 0)
                {
                    // Add to running count for heatmap congestion calculation later.
                    allCongestionValues.Add(edgeOccupancies);
                }

                // Get the edge object.
                NodeEdge edge = RouteNodes[i].OutgoingEdges.Find(e => e.Node2.NodeId == RouteNodes[i + 1].NodeId);
                if (edge != default)
                {
                    // Increment the actual walking time.
                    totalAdjustedWalkingTime += Pathfinder.CalculateWalkingTimeWithCongestion(edge, edgeOccupancies).timeWithCongestion;
                }
            }

            // Congestion for heatmap display.
            Congestion = new Dictionary <string, float>();
            if (allCongestionValues.Count > 0)
            {
                foreach (Dictionary <(string entryId, string exitId), int> entry in allCongestionValues)
                {
                    foreach (KeyValuePair <(string entryId, string exitId), int> congestedEdge in entry)
                    {
                        string newKey = $"{congestedEdge.Key.entryId},{congestedEdge.Key.exitId}";

                        if (!Congestion.TryAdd(newKey, congestedEdge.Value))
                        {
                            // Already exists, add to it
                            Congestion[newKey] += congestedEdge.Value;
                        }
                    }
                }

                if (Congestion.Count > 0)
                {
                    MaxCongestion = Congestion.Values.Max();
                }
            }

            WalkingTimeSeconds = Convert.ToInt32(Math.Round(totalAdjustedWalkingTime));
        }