getPathFromRoot() public méthode

public getPathFromRoot ( ) : List
Résultat List
	// function HILL-CLIMBING(problem) returns a state that is a local maximum
	public List<Action> search(Problem p) {
		clearInstrumentation();
		outcome = SearchOutcome.FAILURE;
		lastState = null;
		// current <- MAKE-NODE(problem.INITIAL-STATE)
		Node current = new Node(p.getInitialState());
		Node neighbor = null;
		// loop do
		while (!CancelableThread.currIsCanceled()) {
			List<Node> children = expandNode(current, p);
			// neighbor <- a highest-valued successor of current
			neighbor = getHighestValuedNodeFrom(children, p);

			// if neighbor.VALUE <= current.VALUE then return current.STATE
			if ((neighbor == null) || (getValue(neighbor) <= getValue(current))) {
				if (SearchUtils.isGoalState(p, current)) {
					outcome = SearchOutcome.SOLUTION_FOUND;
				}
				lastState = current.getState();
				return SearchUtils.actionsFromNodes(current.getPathFromRoot());
			}
			// current <- neighbor
			current = neighbor;
		}
		return new List<Action>();
	}
Exemple #2
0
        /**
         *
         * @param problem
         * @param frontier
         * @return if goal found, the list of actions to the Goal. If already at the
         *         goal you will receive a List with a single NoOp Action in it. If
         *         fail to find the Goal, an empty list will be returned to indicate
         *         that the search failed.
         */
        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());
        }
Exemple #3
0
 public void testGetPathFromRoot()
 {
     Node node1 = new Node("state1");
     Node node2 = new Node("state2", node1, null, 1.0);
     Node node3 = new Node("state3", node2, null, 1.0);
     List<Node> path = node3.getPathFromRoot();
     Assert.Equals(node1, path[0]);
     Assert.Equals(node2, path[1]);
     Assert.Equals(node3, path[2]);
 }
Exemple #4
0
        /**
         * 
         * @param problem
         * @param frontier
         * @return if goal found, the list of actions to the Goal. If already at the
         *         goal you will receive a List with a single NoOp Action in it. If
         *         fail to find the Goal, an empty list will be returned to indicate
         *         that the search failed.
         */
        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();
        }
Exemple #5
0
 public static bool isGoalState(Problem p, Node n)
 {
     bool isGoal = false;
     GoalTest gt = p.getGoalTest();
     if (gt.isGoalState(n.getState()))
     {
         if (gt is SolutionChecker)
         {
             isGoal = ((SolutionChecker)gt).isAcceptableSolution(
                     SearchUtils.actionsFromNodes(n.getPathFromRoot()), n
                             .getState());
         }
         else
         {
             isGoal = true;
         }
     }
     return isGoal;
 }
Exemple #6
0
        public static bool isGoalState(Problem p, Node n)
        {
            bool     isGoal = false;
            GoalTest gt     = p.getGoalTest();

            if (gt.isGoalState(n.getState()))
            {
                if (gt is SolutionChecker)
                {
                    isGoal = ((SolutionChecker)gt).isAcceptableSolution(
                        SearchUtils.actionsFromNodes(n.getPathFromRoot()), n
                        .getState());
                }
                else
                {
                    isGoal = true;
                }
            }
            return(isGoal);
        }
	// function SIMULATED-ANNEALING(problem, schedule) returns a solution state
	public List<Action> search(Problem p) {
		clearInstrumentation();
		outcome = SearchOutcome.FAILURE;
		lastState = null;
		// current <- MAKE-NODE(problem.INITIAL-STATE)
		Node current = new Node(p.getInitialState());
		Node next = null;
		List<Action> ret = new List<Action>();
		// for t = 1 to INFINITY do
		int timeStep = 0;
		while (!CancelableThread.currIsCanceled()) {
			// temperature <- schedule(t)
			double temperature = scheduler.getTemp(timeStep);
			timeStep++;
			// if temperature = 0 then return current
			if (temperature == 0.0) {
				if (SearchUtils.isGoalState(p, current)) {
					outcome = SearchOutcome.SOLUTION_FOUND;
				}
				ret = SearchUtils.actionsFromNodes(current.getPathFromRoot());
				lastState = current.getState();
				break;
			}

			List<Node> children = expandNode(current, p);
			if (children.Count > 0) {
				// next <- a randomly selected successor of current
				next = Util.selectRandomlyFromList(children);
				// /\E <- next.VALUE - current.value
				double deltaE = getValue(p, next) - getValue(p, current);

				if (shouldAccept(temperature, deltaE)) {
					current = next;
				}
			}
		}

		return ret;
	}
        //
        // PRIVATE METHODS
        //

        // function RECURSIVE-DLS(node, problem, limit) returns a solution, or
        // failure/cutoff
        private List<Action> recursiveDLS(Node node, Problem problem, int limit)
        {
            // if problem.GOAL-TEST(node.STATE) then return SOLUTION(node)
            if (SearchUtils.isGoalState(problem, node))
            {
                setPathCost(node.getPathCost());
                return SearchUtils.actionsFromNodes(node.getPathFromRoot());
            }
            else if (0 == limit)
            {
                // else if limit = 0 then return cutoff
                return cutoff();
            }
            else
            {
                // else
                // cutoff_occurred? <- false
                bool cutoff_occurred = false;
                // for each action in problem.ACTIONS(node.STATE) do
                foreach (Node child in this.expandNode(node, problem))
                {
                    // child <- CHILD-NODE(problem, node, action)
                    // result <- RECURSIVE-DLS(child, problem, limit - 1)
                    List<Action> result = recursiveDLS(child, problem, limit - 1);
                    // if result = cutoff then cutoff_occurred? <- true
                    if (isCutOff(result))
                    {
                        cutoff_occurred = true;
                    }
                    else if (!isFailure(result))
                    {
                        // else if result != failure then return result
                        return result;
                    }
                }

                // if cutoff_occurred? then return cutoff else return failure
                if (cutoff_occurred)
                {
                    return cutoff();
                }
                else
                {
                    return failure();
                }
            }
        }