Exemplo n.º 1
0
        /// <summary>
        /// gets a path from start to goal if possible. If no path is found null is returned.
        /// </summary>
        /// <param name="graph">Graph.</param>
        /// <param name="start">Start.</param>
        /// <param name="goal">Goal.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static List <T> Search <T>(IAstarGraph <T> graph, T start, T goal)
        {
            Dictionary <T, T> cameFrom;
            var foundPath = Search(graph, start, goal, out cameFrom);

            return(foundPath ? RecontructPath(cameFrom, start, goal) : null);
        }
Exemplo n.º 2
0
        public static bool Search <T>(IAstarGraph <T> graph, T start, T goal, out Dictionary <T, T> cameFrom)
        {
            var foundPath = false;

            cameFrom = new Dictionary <T, T> {
                { start, start }
            };

            var costSoFar = new Dictionary <T, int>();
            var frontier  = new List <Tuple <int, T> > {
                new Tuple <int, T>(0, start)
            };

            costSoFar[start] = 0;

            while (frontier.Count > 0)
            {
                var current = frontier[0];
                frontier.RemoveAt(0);

                if (current.Item2.Equals(goal))
                {
                    foundPath = true;
                    break;
                }

                foreach (var next in graph.GetNeighbors(current.Item2))
                {
                    var newCost = costSoFar[current.Item2] + graph.Cost(current.Item2, next);
                    if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
                    {
                        costSoFar[next] = newCost;
                        var priority = newCost + graph.Heuristic(next, goal);
                        frontier.Add(new Tuple <int, T>(priority, next));
                        cameFrom[next] = current.Item2;
                    }
                }

                frontier.Sort(new TupleComparer <T>());
            }

            return(foundPath);
        }
Exemplo n.º 3
0
        public static bool Search <T>(IAstarGraph <T> graph, T start, T goal, out Dictionary <T, T> cameFrom)
        {
            var foundPath = false;

            cameFrom = new Dictionary <T, T>();
            cameFrom.Add(start, start);

            var costSoFar = new Dictionary <T, int>();
            var frontier  = new PriorityQueue <AStarNode <T> >(1000);

            frontier.Enqueue(new AStarNode <T>(start), 0);

            costSoFar[start] = 0;

            while (frontier.Count > 0)
            {
                var current = frontier.Dequeue();

                if (current.Data.Equals(goal))
                {
                    foundPath = true;
                    break;
                }

                foreach (var next in graph.GetNeighbors(current.Data))
                {
                    var newCost = costSoFar[current.Data] + graph.Cost(current.Data, next);
                    if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
                    {
                        costSoFar[next] = newCost;
                        var priority = newCost + graph.Heuristic(next, goal);
                        frontier.Enqueue(new AStarNode <T>(next), priority);
                        cameFrom[next] = current.Data;
                    }
                }
            }

            return(foundPath);
        }
Exemplo n.º 4
0
        /// <summary>
        /// gets a path from start to goal if possible. If no path is found null is returned.
        /// </summary>
        /// <param name="graph">Graph.</param>
        /// <param name="start">Start.</param>
        /// <param name="goal">Goal.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static List <T> Search <T>(IAstarGraph <T> graph, T start, T goal)
        {
            var foundPath = Search(graph, start, goal, out var cameFrom);

            return(foundPath ? PathConstructor.RecontructPath(cameFrom, start, goal) : null);
        }