Beispiel #1
0
        public SearchResult <T> Search()
        {
            T        state = Problem.InitialState;
            Node <T> node  = new Node <T>(state);

            if (Problem.GoalTest(state))
            {
                return(new SearchResult <T>(node));
            }
            Frontier.Put(node);
            OpenList.Add(state);

            while (!Frontier.IsEmpty)
            {
                node  = Frontier.Take();
                state = node.State;
                ClosedList.Add(state);
                foreach (IAction <T> action in Problem.Actions(state))
                {
                    Node <T> childNode  = node.ChildNode(Problem, action);
                    T        childState = childNode.State;
                    if (!ClosedList.Contains(childState) && !OpenList.Contains(childState))
                    {
                        if (Problem.GoalTest(childState))
                        {
                            return(new SearchResult <T>(childNode));
                        }
                        Frontier.Put(childNode);
                        OpenList.Add(childState);
                    }
                }
            }
            return(new SearchResult <T>(null));
        }
Beispiel #2
0
        public SearchResult <T> Search()
        {
            T state = Problem.InitialState;
            HeuristicNode <T> node = new HeuristicNode <T>(state, 1);

            if (Problem.GoalTest(state))
            {
                return(new SearchResult <T>(node));
            }
            PriorityQueue.Push(node);
            OpenList.Add(state);

            while (!PriorityQueue.IsEmpty)
            {
                node  = PriorityQueue.Pop();
                state = node.State;
                ClosedList.Add(state);
                foreach (IAction <T> action in Problem.Actions(state))
                {
                    HeuristicNode <T> childNode = node.ChildNode(Problem, action, Heuristic);
                    T childState = childNode.State;
                    if (ClosedList.Contains(childState) || OpenList.Contains(childState))
                    {
                        if (PriorityQueue.Contains(childNode) && childNode.HeuristicCost > node.HeuristicCost)
                        {
                            PriorityQueue.Push(childNode);
                            OpenList.Add(childState);
                        }
                    }

                    if (!ClosedList.Contains(childState) && !OpenList.Contains(childState))
                    {
                        if (Problem.GoalTest(childState))
                        {
                            return(new SearchResult <T>(childNode));
                        }
                        PriorityQueue.Push(childNode);
                        OpenList.Add(childState);
                    }
                }
            }
            return(new SearchResult <T>(null));
        }