Example #1
0
        protected static internal void DijkstraMinPath(string sourceID, string destinationID)
        {
            try
            {
                var           watch         = Stopwatch.StartNew();
                var           priotityQueue = new Queue <NextAirport>();
                List <string> visited       = new List <string>();
                var           path          = new Dictionary <NextAirport, NextAirport>();
                Airport       source        = AirlineData.GetAirPort(sourceID);
                var           next          = AirlineData.GetNextStation(sourceID);
                next.Weight = 0;//sorce
                priotityQueue.Enqueue(next);
                Graph.Source = next;
                double BestPrice = 0;

                path[next] = null;
                while (priotityQueue.Count > 0)
                {
                    var _flight = priotityQueue.Dequeue();
                    if (_flight.Current.ID == destinationID)
                    {
                        watch.Stop();
                        Graph.Destination = _flight;
                        Graph.Path        = path;
                        Console.WriteLine("Time spended {0}", watch.ElapsedMilliseconds);
                        Console.WriteLine("Count of visited vertices = {0}", visited.Count);
                        ReturnMinPath();

                        return;
                    }
                    visited.Add(_flight.Current.ID);

                    foreach (var neighbour in _flight.adjacementList)
                    {
                        NextAirport _next = AirlineData.GetNextStation(neighbour.DestinationId);;
                        if (!visited.Contains(neighbour.DestinationId) && _next != null)
                        {
                            var best = GetPriceByPath(_flight.Current.ID, neighbour.DestinationId) + _flight.Weight;//maybe best price
                            if (best < _next.Weight)
                            {
                                _next.Weight = best;
                                priotityQueue.Enqueue(_next);
                                path[_next] = _flight;
                                SortQueue(ref priotityQueue);
                            }
                        }
                    }
                }

                Console.ReadKey();
            }
            catch (Exception r)

            {
                Console.WriteLine(r);
            }
        }
Example #2
0
        protected internal static NextAirport GetNextStation(string stationID)
        {
            Airport curr = GetAirPort(stationID);

            if (curr != null)
            {
                NextAirport nextAirport = new NextAirport(curr, curr.AirportName, int.MaxValue, curr.routes);
                return(nextAirport);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        protected static internal List <NextAirport> ReturnMinPath(NextAirport pathNode) // відновлення шляху після алгоритму А*
        {
            List <NextAirport> result = new List <NextAirport>();
            var currentNode           = pathNode;

            Console.WriteLine(currentNode.PathLengthFromStart);
            result.Add(pathNode);
            while (currentNode != null)
            {
                result.Add(currentNode.previous);
                currentNode = currentNode.previous;
            }

            result.Reverse();
            result.RemoveAt(0);
            foreach (var x in result)
            {
                Console.Write($" {x.Current.ID} ->>>");
            }
            Console.WriteLine();
            return(result);
        }
Example #4
0
        protected static internal List <NextAirport> AStarMinPath(string sourceID, string destinationID)
        {
            // отримуємо найкоротший шлях від аеропорта А до аеропорта Б

            NextAirport neighbourNode1;

            var watch     = Stopwatch.StartNew();
            var closedSet = new Queue <NextAirport>();
            var openSet   = new Queue <NextAirport>();

            AStar.Source      = AirlineData.GetNextStation(sourceID);
            AStar.Destination = AirlineData.GetNextStation(destinationID);

            AStar.Source.previous                    = null;
            AStar.Source.PathLengthFromStart         = 0;
            AStar.Source.HeuristicEstimatePathLength = GetPath(AStar.Source.Current, AStar.Destination.Current);

            openSet.Enqueue(AStar.Source);

            // Console.WriteLine($"Start searching path from {AStar.Source.Current.AirportName} to {AStar.Destination.Current.AirportName} ");
            while (openSet.Count > 0)
            {
                var currentAirport = openSet.OrderBy(node => node.EstimateFullPathLength).First();
                currentAirport = openSet.Dequeue();

                if (currentAirport.Current.ID == destinationID) // mccarran
                {
                    Console.WriteLine("Time spended {0}", watch.ElapsedMilliseconds);
                    Console.WriteLine($"There is the way from {AStar.Source.Current.AirportName} to {AStar.Destination.Current.AirportName} ");
                    return(ReturnMinPath(currentAirport));
                }

                closedSet.Enqueue(currentAirport);

                foreach (var neighbourAirport in currentAirport.adjacementList)
                {
                    neighbourNode1 = AirlineData.GetNextStation(neighbourAirport.DestinationId);

                    if (closedSet.Count(node => node.Current.ID == neighbourNode1.Current.ID) > 0)
                    {
                        continue;
                    }

                    double temp_PathLenghtFromStart = currentAirport.PathLengthFromStart + GetPath(currentAirport.Current, neighbourNode1.Current);                                                                                                                                                     // neighbourNode1.PathLengthFromStart = currentAirport.PathLengthFromStart + GetPath(currentAirport.Current, AirlineData.GetAirPort(neighbourNode.ITA)); // виглядає правильніше, працює довше, результат коректний

                    var openNode = openSet.FirstOrDefault(node => node.Current.ID == neighbourNode1.Current.ID);
                    if (openNode == null || temp_PathLenghtFromStart <= neighbourNode1.PathLengthFromStart)
                    {
                        neighbourNode1.previous                    = currentAirport;
                        neighbourNode1.PathLengthFromStart         = temp_PathLenghtFromStart;
                        neighbourNode1.HeuristicEstimatePathLength = GetPath(neighbourNode1.Current, AStar.Destination.Current);
                    }
                    if (openNode == null)
                    {
                        openSet.Enqueue(neighbourNode1);
                    }
                }
            }
            Console.WriteLine($"There is no way from airport with name {AStar.Source.Current.AirportName} to airport {AStar.Destination.Current.AirportName}");
            Console.WriteLine("Time spended {0}", watch.ElapsedMilliseconds);

            return(null);
        }