public List<Connection> FindShortestPath(string startStopName, string targetStopName, List<Connection> actualConnections, List<Connection> remainingConnections, out Stop endStop, out Stack<Stop> shortestPath)
        {
            while (remainingConnections.Count() > 0)
            {
                remainingConnections = remainingConnections
                    .OrderBy(c => c.DepartureStop.MinutesToArrive)
                    .ToList();

                if (remainingConnections.First().DepartureStop.MinutesToArrive == int.MaxValue)
                {
                    break;
                }

                var nextStop = remainingConnections
                    .First()
                    .DepartureStop;

                //var busLine = nextStop.BusLineInShortestPath;

                var arrivalTime = nextStop.ArrivalTime;

                var nextConnections = remainingConnections
                    .Where(c => c.DepartureStop == nextStop)
                    .ToList();

                foreach (var conn in nextConnections)
                {
                    var additionalTime = (conn.DepartureTime.TimeOfDay - arrivalTime.TimeOfDay).TotalMinutes;

                    if (conn.ArrivalStop.MinutesToArrive > (conn.DepartureStop.MinutesToArrive + conn.TimeOfArrival + additionalTime))
                    {
                        conn.ArrivalStop.MinutesToArrive = conn.DepartureStop.MinutesToArrive + conn.TimeOfArrival + additionalTime;
                        conn.ArrivalStop.PreviousStop = conn.DepartureStop;
                        conn.ArrivalStop.BusLineInShortestPath = conn.BusLine;
                        conn.ArrivalStop.ArrivalTime = conn.ArrivalTime;
                    }

                    remainingConnections.Remove(conn);
                }

            }

            endStop = actualConnections
                .FirstOrDefault(c => c.ArrivalStop.Name == targetStopName)
                .ArrivalStop;

            shortestPath = new Stack<Stop>();
            shortestPath.Push(endStop);
            var previousStop = endStop.PreviousStop;

            while (previousStop.Name != startStopName)
            {
                shortestPath.Push(previousStop);
                previousStop = previousStop.PreviousStop;
            }

            return remainingConnections;
        }
Exemplo n.º 2
0
        public List<Connection> CreateConnections()
        {
            var allTimeTableData = ExcelTableReader.ReadData(GlobalConstants.PathToExcelFile);
            var allVehicles = new List<BusLine>();
            var allStops = new List<Stop>();
            var allConnections = new List<Connection>();
            var allTrips = new List<Trip>();

            foreach (var table in allTimeTableData)
            {
                var busLine = new BusLine();
                var vehicleNumber = table.Item1.Replace("'", string.Empty);
                vehicleNumber = vehicleNumber.Remove(vehicleNumber.Length - 1);
                if (allVehicles.FirstOrDefault(v => v.Number == vehicleNumber) == null)
                {
                    busLine = new BusLine() { Number = vehicleNumber };
                    allVehicles.Add(busLine);
                }

                var rows = table.Item2;

                foreach (var stopName in rows.Select(r => r[0]))
                {
                    if (allStops.FirstOrDefault(s => s.Name == stopName.ToString()) == null)
                    {
                        var stop = new Stop() { Name = stopName.ToString() };
                        allStops.Add(stop);
                    }
                }

                for (int i = 1; i < rows.Count(); i++)
                {
                    var previousRow = rows.ToArray()[i - 1];
                    var currentRow = rows.ToArray()[i];

                    var departureStopName = previousRow[0].ToString();
                    var departureStop = allStops.FirstOrDefault(s => s.Name == departureStopName);
                    var arrivalStopName = currentRow[0].ToString();
                    var arrivalStop = allStops.FirstOrDefault(s => s.Name == arrivalStopName);
                    for (int j = 2; j < currentRow.ItemArray.Count(); j++)
                    {
                        var arrivalTime = Convert.ToDateTime(currentRow[j]);
                        var departureTime = Convert.ToDateTime(previousRow[j]);

                        //var arrivalTime = new DateTime();
                        //if (!String.IsNullOrEmpty(currentRow[j].ToString()))
                        //{
                        //    arrivalTime = Convert.ToDateTime(currentRow[j]);
                        //}
                        //else
                        //{
                        //    continue;
                        //}

                        //var nonEmptyRow = i;

                        //while (String.IsNullOrEmpty(rows.ToArray()[nonEmptyRow][j].ToString()))
                        //{
                        //    nonEmptyRow--;
                        //}
                        //var departureTime = Convert.ToDateTime(rows.ToArray()[nonEmptyRow][j]);

                        var connection = new Connection();
                        connection.DepartureStop = departureStop;
                        connection.ArrivalStop = arrivalStop;
                        connection.DepartureTime = departureTime;
                        connection.ArrivalTime = arrivalTime;
                        connection.BusLine = busLine;

                        allConnections.Add(connection);
                    }

                }
            }
            return allConnections;
        }