Beispiel #1
0
        public Node <E, A, C> Search()
        {
            HashSet <E> explored = new HashSet <E>();
            //! first node in the frontier is the InitialState
            Node <E, A, C> node = new Node <E, A, C>(problem.InitialState);

            Console.Write("Initial node: ");
            Console.WriteLine(node.state.ToString());
            //! if it happens to be the goal state, return it, we're done
            if (problem.GoalTest(node.state))
            {
                return(node);
            }
            //C heur = heurfun(node);
            frontier.Append(/*heur,*/ node);

            while (frontier.Count() > 0 /*&& frontier.Count() < 100*/)
            {
                node = frontier.Pop();

                if (node == null)
                {
                    NPuzzleUtils.NullSearchNodeException ex = new NPuzzleUtils.NullSearchNodeException("Node popper off frontier is null");
                    throw ex;
                }
                int[] s = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

                if (problem.GoalTest(node.state))
                {
                    return(node);
                }
                explored.Add(node.state);
                //Console.WriteLine("Popped from frontier: {0}", node.state.ToString());

                node.Expand(problem).ForEach(
                    delegate(Node <E, A, C> n)
                {
                    // heur = heurfun(n);
                    bool inPriorityQueue = frontier.InPriorityQueue(n);
                    if (!explored.Contains(n.state) && !inPriorityQueue)
                    {
                        //Console.WriteLine("Appending to frontier: {0}", n.state.ToString());
                        frontier.Append(/*heur,*/ n);
                    }
                    else if (inPriorityQueue)
                    {
                        Node <E, A, C> incumbent = frontier.GetIncumbent(n);
                        if (frontier.Heurfun(n).CompareTo(frontier.Heurfun(incumbent)) == 1)
                        {
                            // remove incumbent
                            frontier.RemoveIncumbent(incumbent);
                            // add n
                            frontier.Append(n);
                        }

                        //		    Console.WriteLine("It's in the frontier");
                    }
                }
                    );
            }

            return(default(Node <E, A, C>));
        }