Пример #1
0
        public override List <Edge> GetShortestRoute(int sourceStopId, int destinationStopId, DateTime when)
        {
            if (!_priorityQueues.ContainsKey(Thread.CurrentThread))
            {
                var storage = new BinaryHeapPriorityQueue();
                _priorityQueues.Add(Thread.CurrentThread, storage);
            }

            var result = new List <Edge>();

            #region Dijkstra
            {
                int iterationCount = 0;

                Console.WriteLine("Looking for route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");

                var  inclompleteNodes = new List <Node>();
                bool traceComplete    = false;

                // Kezdetben csak a forráspont van kész, nem vezet hozzá semmi
                var currentNode = new CompleteNode
                {
                    stopId        = sourceStopId,
                    departureTime = when.TimeOfDay,
                    viaNode       = null,
                    usedEdges     = new Stack <Edge>(),
                    usedRouteIds  = new Stack <int>()
                };

                // Mindenki mást pedig várakozik a kifejtésre
                foreach (var stop in AllStops.Where(s => s.DbId != sourceStopId))
                {
                    inclompleteNodes.Add(new Node
                    {
                        stopId = stop.DbId
                    });
                }

                // Az útvonalkereső hurok
                while ((!traceComplete) && (inclompleteNodes.Count > 0))
                {
                    if ((iterationCount % 25) == 0)
                    {
                        Console.WriteLine("Iteration: " + ((iterationCount / 25) + 1) + " x 25 | with " + inclompleteNodes.Count + " icnodes");
                        iterationCount++;
                    }

                    currentNode = GetNextCompleteNode(currentNode, inclompleteNodes, when);

                    if (currentNode == null)
                    {
                        Console.WriteLine("No route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                        break;
                    }

                    // Áthelyezés a kész csomópontok közé
                    {
                        var removedNode = inclompleteNodes.Single(n => n.stopId == currentNode.stopId);
                        inclompleteNodes.Remove(removedNode);
                    }

                    // Ha megtaláltuk a célt, akkor leállunk
                    if (currentNode.stopId == destinationStopId)
                    {
                        traceComplete = true;
                        Console.WriteLine("Trace complete between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                        Console.WriteLine("Shortest path Found. # of used routes: " + currentNode.usedRouteIds.Count + " | arrival: " + currentNode.departureTime.ToString());

                        result.AddRange(currentNode.usedEdges.Reverse());
                        result.ForEach(
                            e =>
                        {
                            Console.WriteLine(e.GetType().Name + " " + e.ToString());
                        });
                    }
                }
            }
            #endregion

            if (_priorityQueues.ContainsKey(Thread.CurrentThread))
            {
                _priorityQueues.Remove(Thread.CurrentThread);
            }

            return(result);
        }
Пример #2
0
        public override List<Edge> GetShortestRoute(int sourceStopId, int destinationStopId, DateTime when)
        {
            if (!_priorityQueues.ContainsKey(Thread.CurrentThread))
            {
                var storage = new BinaryHeapPriorityQueue();
                _priorityQueues.Add(Thread.CurrentThread, storage);
            }

            var result = new List<Edge>();

            #region Dijkstra
            {
                int iterationCount = 0;

                Console.WriteLine("Looking for route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");

                var inclompleteNodes = new List<Node>();
                bool traceComplete = false;

                // Kezdetben csak a forráspont van kész, nem vezet hozzá semmi
                var currentNode = new CompleteNode
                {
                    stopId = sourceStopId,
                    departureTime = when.TimeOfDay,
                    viaNode = null,
                    usedEdges = new Stack<Edge>(),
                    usedRouteIds = new Stack<int>()
                };

                // Mindenki mást pedig várakozik a kifejtésre
                foreach (var stop in AllStops.Where(s => s.DbId != sourceStopId))
                {
                    inclompleteNodes.Add(new Node
                    {
                        stopId = stop.DbId
                    });
                }

                // Az útvonalkereső hurok
                while ((!traceComplete) && (inclompleteNodes.Count > 0))
                {
                    if ((iterationCount % 25) == 0)
                    {
                        Console.WriteLine("Iteration: " + ((iterationCount / 25) + 1) + " x 25 | with " + inclompleteNodes.Count + " icnodes");
                        iterationCount++;
                    }

                    currentNode = GetNextCompleteNode(currentNode, inclompleteNodes, when);

                    if (currentNode == null)
                    {
                        Console.WriteLine("No route between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                        break;
                    }

                    // Áthelyezés a kész csomópontok közé
                    {
                        var removedNode = inclompleteNodes.Single(n => n.stopId == currentNode.stopId);
                        inclompleteNodes.Remove(removedNode);
                    }

                    // Ha megtaláltuk a célt, akkor leállunk
                    if (currentNode.stopId == destinationStopId)
                    {
                        traceComplete = true;
                        Console.WriteLine("Trace complete between '" + GetStop(sourceStopId).StopName + "' and '" + GetStop(destinationStopId).StopName + "'");
                        Console.WriteLine("Shortest path Found. # of used routes: " + currentNode.usedRouteIds.Count + " | arrival: " + currentNode.departureTime.ToString());

                        result.AddRange(currentNode.usedEdges.Reverse());
                        result.ForEach(
                            e =>
                            {
                                Console.WriteLine(e.GetType().Name + " " + e.ToString());
                            });
                    }
                }
            }
            #endregion

            if (_priorityQueues.ContainsKey(Thread.CurrentThread))
            {
                _priorityQueues.Remove(Thread.CurrentThread);
            }

            return result;
        }