Beispiel #1
0
        /// <summary>
        /// Generic search algorithm
        /// </summary>
        private static SearchResult GenericSearch(StateBase initialState, StateBase goalState, string algName, OpenListComparator comparator, CostFunc cost, IHeuristic heuristic, int? depthLimit = null)
        {
            var openList = new OpenList(comparator);
            var closedList = new ClosedList();
            var cache = new HeuristicCache(goalState, heuristic);
            var nodesGenerated = 0;
            var nodesPrevGenerated = 0;

            // add initial node to open list
            openList.Push(new SearchNode(cost, initialState, null, null, cache.Evaluate));

            while (true)
            {
                // if nothing on open list, fail
                if (openList.Count == 0)
                {
                    return new SearchResult(null, nodesGenerated, nodesPrevGenerated, openList.Count, closedList.Count, algName, heuristic);
                }

                // get next node to expand
                var node = openList.Pop();
                closedList.Push(node);

                // if at goal state, success
                if (node.State.Equals(goalState))
                {
                    return new SearchResult(node, nodesGenerated, nodesPrevGenerated, openList.Count, closedList.Count, algName, heuristic);
                }

                // if at depth limit, don't generate successors
                if (depthLimit != null && node.Depth == depthLimit)
                {
                    continue;
                }

                var daughters = node.Successors(cost, cache.Evaluate);

                foreach (var daughter in daughters)
                {
                    nodesGenerated++;

                    // if this state is already in open list, replace old node with new node if g-hat is better
                    var foundInOpen = openList.Find(daughter.State);
                    if (foundInOpen != null)
                    {
                        nodesPrevGenerated++;
                        if (daughter.Ghat < foundInOpen.Ghat)
                        {
                            openList.Replace(foundInOpen, daughter);
                        }
                    }
                    else
                    {
                        // else if this state is already in closed list, move from closed to open if g-hat is better
                        var foundInClosed = closedList.Find(daughter.State);
                        if (foundInClosed != null)
                        {
                            nodesPrevGenerated++;
                            if (daughter.Ghat < foundInClosed.Ghat)
                            {
                                openList.Push(daughter);
                                closedList.Remove(foundInClosed);
                            }
                        }
                        else
                        {
                            // else didn't find in open or closed, add to open
                            openList.Push(daughter);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Generic search algorithm
        /// </summary>
        private static SearchResult GenericSearch(StateBase initialState, StateBase goalState, string algName, OpenListComparator comparator, CostFunc cost, IHeuristic heuristic, int?depthLimit = null)
        {
            var openList           = new OpenList(comparator);
            var closedList         = new ClosedList();
            var cache              = new HeuristicCache(goalState, heuristic);
            var nodesGenerated     = 0;
            var nodesPrevGenerated = 0;

            // add initial node to open list
            openList.Push(new SearchNode(cost, initialState, null, null, cache.Evaluate));

            while (true)
            {
                // if nothing on open list, fail
                if (openList.Count == 0)
                {
                    return(new SearchResult(null, nodesGenerated, nodesPrevGenerated, openList.Count, closedList.Count, algName, heuristic));
                }

                // get next node to expand
                var node = openList.Pop();
                closedList.Push(node);

                // if at goal state, success
                if (node.State.Equals(goalState))
                {
                    return(new SearchResult(node, nodesGenerated, nodesPrevGenerated, openList.Count, closedList.Count, algName, heuristic));
                }

                // if at depth limit, don't generate successors
                if (depthLimit != null && node.Depth == depthLimit)
                {
                    continue;
                }

                var daughters = node.Successors(cost, cache.Evaluate);

                foreach (var daughter in daughters)
                {
                    nodesGenerated++;

                    // if this state is already in open list, replace old node with new node if g-hat is better
                    var foundInOpen = openList.Find(daughter.State);
                    if (foundInOpen != null)
                    {
                        nodesPrevGenerated++;
                        if (daughter.Ghat < foundInOpen.Ghat)
                        {
                            openList.Replace(foundInOpen, daughter);
                        }
                    }
                    else
                    {
                        // else if this state is already in closed list, move from closed to open if g-hat is better
                        var foundInClosed = closedList.Find(daughter.State);
                        if (foundInClosed != null)
                        {
                            nodesPrevGenerated++;
                            if (daughter.Ghat < foundInClosed.Ghat)
                            {
                                openList.Push(daughter);
                                closedList.Remove(foundInClosed);
                            }
                        }
                        else
                        {
                            // else didn't find in open or closed, add to open
                            openList.Push(daughter);
                        }
                    }
                }
            }
        }