Пример #1
0
        public override direction[] Solve(nPuzzle puzzle)
        {
            //This method uses the fringe as a queue.
            //Therefore, nodes are searched in order of cost, with the lowest cost
            // unexplored node searched next.
            //-----------------------------------------

            //put the start state in the Fringe to get explored.
            addToFrontier(puzzle.StartState);


            List <PuzzleState> newStates = new List <PuzzleState>();

            while (Frontier.Count > 0)
            {
                //get the next item off the fringe
                PuzzleState thisState = popFrontier();

                //is it the goal item?
                if (thisState.Equals(puzzle.GoalState))
                {
                    //We have found a solution! return it!
                    return(thisState.GetPathToState());
                }
                else
                {
                    //This isn't the goal, just explore the node
                    newStates = thisState.explore();

                    for (int i = 0; i < newStates.Count; i++)
                    {
                        //add this state to the fringe, addToFringe() will take care of duplicates
                        //
                        // TODO: is this the correct way to add to frontier as specified in the Assignment:
                        // When all else is equal, nodes should be expanded according to the following order:
                        // the agent should try to move the empty cell UP before attempting LEFT, before
                        // attempting DOWN, before attempting RIGHT, in that order.
                        addToFrontier(newStates[i]);
                    }
                }
            }

            //No solution found and we've run out of nodes to search
            //return null.
            return(null);
        }
        public override direction[] Solve(nPuzzle aPuzzle)
        {
            //keep searching the fringe until it's empty.
            //Items are "popped" from the fringe in order of lowest heuristic value.

            //Add the start state to the fringe
            addToFrontier(aPuzzle.StartState);
            while (Frontier.Count > 0)
            {
                //get the next State
                PuzzleState thisState = popFrontier();

                //is this the goal state?
                if (thisState.Equals(aPuzzle.GoalState))
                {
                    return(thisState.GetPathToState());
                }

                //not the goal state, explore this node
                List <PuzzleState> newStates = thisState.explore();

                for (int i = 0; i < newStates.Count; i++)
                {
                    PuzzleState newChild = newStates[i];
                    //if you can add these new states to the fringe
                    if (addToFrontier(newChild))
                    {
                        //then, work out it's heuristic value
                        newChild.HeuristicValue     = HeuristicValue(newStates[i], aPuzzle.GoalState);
                        newChild.EvaluationFunction = newChild.HeuristicValue;
                    }
                }

                //Sort the fringe by it's Heuristic Value. The PuzzleComparator uses each nodes EvaluationFunction
                // to determine a node's value, based on another. The sort method uses this to sort the Fringe.
                //
                // TODO: is this the correct way to sort the frontier as specified in the Assignment:
                // When all else is equal, nodes should be expanded according to the following order:
                // the agent should try to move the empty cell UP before attempting LEFT, before
                // attempting DOWN, before attempting RIGHT, in that order.
                Frontier.Sort(new PuzzleComparator());
            }

            //no more nodes and no path found?
            return(null);
        }