// 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>(); }
// function DEPTH-LIMITED-SEARCH(problem, limit) returns a solution, or // failure/cutoff /** * @param p * @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. If the search was cutoff (i.e. reached * its limit without finding a goal) a List with one * CutOffIndicatorAction.CUT_OFF in it will be returned (Note: this * is a NoOp action). */ public List<Action> search(Problem p) { clearInstrumentation(); // return RECURSIVE-DLS(MAKE-NODE(INITIAL-STATE[problem]), problem, // limit) return recursiveDLS(new Node(p.getInitialState()), p, limit); }
/** * * @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()); }
/** * * @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(); }
// function RECURSIVE-BEST-FIRST-SEARCH(problem) returns a solution, or // failure public List<Action> search(Problem p) { List<Action> actions = new List<Action>(); clearInstrumentation(); // RBFS(problem, MAKE-NODE(INITIAL-STATE[problem]), infinity) Node n = new Node(p.getInitialState()); SearchResult sr = rbfs(p, n, evaluationFunction.f(n), INFINITY, 0); if (sr.getOutcome() == SearchResult.SearchOutcome.SOLUTION_FOUND) { Node s = sr.getSolution(); actions = SearchUtils.actionsFromNodes(s.getPathFromRoot()); setPathCost(s.getPathCost()); } // Empty List can indicate already at Goal // or unable to find valid set of actions return actions; }
// 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; }