AIMA.Core.Search.Framework.QueueSearch.search C# (CSharp) Method

search() public method

public search ( Problem problem, Queue frontier ) : List
problem Problem
frontier Queue
return List
        public virtual List<Action> search(Problem problem, Queue<Node> frontier)
        {
            this.frontier = frontier;

            clearInstrumentation();
            // initialize the frontier using the initial state of the problem
            Node root = new Node(problem.getInitialState());
            if (isCheckGoalBeforeAddingToFrontier())
            {
                if (SearchUtils.isGoalState(problem, root))
                {
                    return SearchUtils.actionsFromNodes(root.getPathFromRoot());
                }
            }
            frontier.Enqueue(root);
            setQueueSize(frontier.Count);
            while (!(frontier.Count==0))
            {
                // choose a leaf node and remove it from the frontier
                Node nodeToExpand = popNodeFromFrontier();
                setQueueSize(frontier.Count);
                // Only need to check the nodeToExpand if have not already
                // checked before adding to the frontier
                if (!isCheckGoalBeforeAddingToFrontier())
                {
                    // if the node contains a goal state then return the
                    // corresponding solution
                    if (SearchUtils.isGoalState(problem, nodeToExpand))
                    {
                        setPathCost(nodeToExpand.getPathCost());
                        return SearchUtils.actionsFromNodes(nodeToExpand
                                .getPathFromRoot());
                    }
                }
                // expand the chosen node, adding the resulting nodes to the
                // frontier
                foreach (Node fn in getResultingNodesToAddToFrontier(nodeToExpand,
                        problem))
                {
                    if (isCheckGoalBeforeAddingToFrontier())
                    {
                        if (SearchUtils.isGoalState(problem, fn))
                        {
                            setPathCost(fn.getPathCost());
                            return SearchUtils.actionsFromNodes(fn
                                    .getPathFromRoot());
                        }
                    }
                    frontier.Enqueue(fn);
                }
                setQueueSize(frontier.Count);
            }
            // if the frontier is empty then return failure
            return failure();
        }

Usage Example

Example #1
0
 public List <Action> search(Problem p)
 {
     return(search.search(p, new PriorityQueue <Node>(5, getComparator())));
 }