private double getValue(Node n) { // assumption greater heuristic value => // HIGHER on hill; 0 == goal state; return -1 * hf.h(n.getState()); }
// 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>(); }
public override Node popNodeFromFrontier() { Node toRemove = base.popNodeFromFrontier(); frontierState.Remove(toRemove.getState()); return(toRemove); }
public override bool removeNodeFromFrontier(Node toRemove) { bool removed = base.removeNodeFromFrontier(toRemove); if (removed) { frontierState.Remove(toRemove.getState()); } return removed; }
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; }
public override bool removeNodeFromFrontier(Node toRemove) { bool removed = base.removeNodeFromFrontier(toRemove); if (removed) { frontierState.Remove(toRemove.getState()); } return(removed); }
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); }
public List <Node> expandNode(Node node, Problem problem) { List <Node> childNodes = new List <Node>(); ActionsFunction actionsFunction = problem.getActionsFunction(); ResultFunction resultFunction = problem.getResultFunction(); StepCostFunction stepCostFunction = problem.getStepCostFunction(); foreach (Action action in actionsFunction.actions(node.getState())) { System.Object successorState = resultFunction.result(node.getState(), action); double stepCost = stepCostFunction.c(node.getState(), action, successorState); childNodes.Add(new Node(successorState, node, action, stepCost)); } metrics.set(METRIC_NODES_EXPANDED, metrics .getInt(METRIC_NODES_EXPANDED) + 1); return(childNodes); }
public List<Node> expandNode(Node node, Problem problem) { List<Node> childNodes = new List<Node>(); ActionsFunction actionsFunction = problem.getActionsFunction(); ResultFunction resultFunction = problem.getResultFunction(); StepCostFunction stepCostFunction = problem.getStepCostFunction(); foreach (Action action in actionsFunction.actions(node.getState())) { System.Object successorState = resultFunction.result(node.getState(), action); double stepCost = stepCostFunction.c(node.getState(), action, successorState); childNodes.Add(new Node(successorState, node, action, stepCost)); } metrics.set(METRIC_NODES_EXPANDED, metrics .getInt(METRIC_NODES_EXPANDED) + 1); return childNodes; }
public override List<Node> getResultingNodesToAddToFrontier(Node nodeToExpand, Problem problem) { addToFrontier.Clear(); // add the node to the explored set explored.Add(nodeToExpand.getState()); // expand the chosen node, adding the resulting nodes to the frontier foreach (Node cfn in expandNode(nodeToExpand, problem)) { Node frontierNode = frontierState[cfn.getState()]; bool yesAddToFrontier = false; // only if not in the frontier or explored set if (null == frontierNode && !explored.Contains(cfn.getState())) { yesAddToFrontier = true; } else if (null != frontierNode && null != replaceFrontierNodeAtStateCostFunction && replaceFrontierNodeAtStateCostFunction.Compare(cfn, frontierNode) < 0) { // child.STATE is in frontier with higher cost // replace that frontier node with child yesAddToFrontier = true; // Want to replace the current frontier node with the child // node therefore mark the child to be added and remove the // current fontierNode removeNodeFromFrontier(frontierNode); // Ensure removed from add to frontier as well // as 1 or more may reach the same state at the same time addToFrontier.Remove(frontierNode); } if (yesAddToFrontier) { addToFrontier.Add(cfn); frontierState.Add(cfn.getState(), cfn); } } return addToFrontier; }
public override List <Node> getResultingNodesToAddToFrontier(Node nodeToExpand, Problem problem) { addToFrontier.Clear(); // add the node to the explored set explored.Add(nodeToExpand.getState()); // expand the chosen node, adding the resulting nodes to the frontier foreach (Node cfn in expandNode(nodeToExpand, problem)) { Node frontierNode = frontierState[cfn.getState()]; bool yesAddToFrontier = false; // only if not in the frontier or explored set if (null == frontierNode && !explored.Contains(cfn.getState())) { yesAddToFrontier = true; } else if (null != frontierNode && null != replaceFrontierNodeAtStateCostFunction && replaceFrontierNodeAtStateCostFunction.Compare(cfn, frontierNode) < 0) { // child.STATE is in frontier with higher cost // replace that frontier node with child yesAddToFrontier = true; // Want to replace the current frontier node with the child // node therefore mark the child to be added and remove the // current fontierNode removeNodeFromFrontier(frontierNode); // Ensure removed from add to frontier as well // as 1 or more may reach the same state at the same time addToFrontier.Remove(frontierNode); } if (yesAddToFrontier) { addToFrontier.Add(cfn); frontierState.Add(cfn.getState(), cfn); } } return(addToFrontier); }
// 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 double getValue(Problem p, Node n) { // assumption greater heuristic value => // HIGHER on hill; 0 == goal state; // SA deals with gardient DESCENT return -1 * hf.h(n.getState()); }
public double f(Node n) { // f(n) = h(n) return hf.h(n.getState()); }
public double f(Node n) { // f(n) = g(n) + h(n) return gf.g(n) + hf.h(n.getState()); }