private bool alreadySeen(Node node) { //return closed.contains(node.getState()); //you must compare hashcode values, or they will never //evaluate to true! return closed.Contains(node.getState().GetHashCode()); }
public ArrayList expandNode(Node node, Problem problem) { int heuristic = problem.getHeuristicFunction().getHeuristicValue( node.getState()); //System.out.println("Expanding\n"+node.getState()+"\n"+"heuristic = // "+heuristic+"\n"); ArrayList nodes = new ArrayList(); ArrayList successors = problem.getSuccessorFunction().getSuccessors( node.getState()); for (int i = 0; i < successors.Count; i++) { Successor successor = (Successor) successors[i]; Node aNode = new Node(node, successor.getState()); aNode.setAction(successor.getAction()); double stepCost = problem.getStepCostFunction().calculateStepCost( node.getState(), successor.getState(), successor.getAction()); aNode.setStepCost(stepCost); aNode.addToPathCost(stepCost); nodes.Add(aNode); } metrics.set(NODES_EXPANDED, metrics.getInt(NODES_EXPANDED) + 1); //System.out.println("Nodes expanded = " + // metrics.getInt(NODES_EXPANDED)); return nodes; }
protected override void addExpandedNodesToFringe(NodeStore fringe, Node node, Problem problem) { if (!(alreadySeen(node))) { //it is critical here put the hashcode of the object //into the hashtable!! closed.Add(node.getState().GetHashCode(),""); fringe.add(expandNode(node,problem)); } }
public ArrayList search(Problem p) { clearInstrumentation(); Node current = new Node(p.getInitialState()); Node neighbor = null; while (true) { ArrayList children = expandNode(current, p); neighbor = getHighestValuedNodeFrom(children, p); if ((neighbor == null) || (getValue(neighbor, p) <= getValue(current, p))) { return SearchUtils.actionsFromNodes(current.getPathFromRoot()); } current = neighbor; } }
public ArrayList search(Problem p) { clearInstrumentation(); Node current = new Node(p.getInitialState()); Node next = null; ArrayList ret = new ArrayList(); for (int step = 0; step < 1000; step = step + 1) { double temp = scheduler.getTemp(step); if (temp == 0.0) { String status = "not completed"; if (p.isGoalState(current.getState())) { status = "success"; } //System.out.println(current.getState()); ret = SearchUtils.actionsFromNodes(current.getPathFromRoot()); ret.Add(status + "\nFinal state = \n" + current.getState()); break; } ArrayList children = expandNode(current, p); //expansions++; //System.out.println("step = "+step+" expansions = "+expansions); if (children.Count > 0) { //TODO take care of no possible expansion situation? next = (Node) Util.selectRandomlyFromList(children); int deltaE = getValue(next, p) - getValue(current, p); //System.out.print("deltaE = "+deltaE+"\n"); if ((deltaE > 0.0) || (new Random().NextDouble() > Math.Exp(deltaE / temp))) { current = next; } } } //System.out.println("Number of expansions = "+expansions); return ret;//Total Failure }
private ArrayList recursiveDLS(Node node, Problem problem, int limit) { bool cutOffOccured = false; if (problem.isGoalState(node.getState())) { return SearchUtils.actionsFromNodes(node.getPathFromRoot()); } else if (node.getDepth() == limit) { return createCutOffResult(); } else { ArrayList children = expandNode(node, problem); for (int i = 0; i < children.Count; i++) { Node child = (Node) children[i]; ArrayList result = recursiveDLS(child, problem, limit); if (cutoffResult(result)) { cutOffOccured = true; } else if (!(failure(result))) { return result; } } if (cutOffOccured) { return createCutOffResult(); } else { return new ArrayList(); } } }
protected abstract void addExpandedNodesToFringe(NodeStore fringe, Node node, Problem p);
public void add(Node anItem) { queue.add(anItem); }
public Node(Node parent, Object state) : this(state) { //this(state); this.parent = parent; this.depth = parent.getDepth() + 1; }
private int getValue(Node n, Problem p) { return -1 * getHeuristic(n, p); //assumption greater heuristic value => // HIGHER on hill; 0 == goal state; //SA deals with gardient DESCENT }
private int getHeuristic(Node aNode, Problem p) { return p.getHeuristicFunction().getHeuristicValue(aNode.getState()); }
protected override void addExpandedNodesToFringe(NodeStore fringe, Node node, Problem problem) { fringe.add(expandNode(node, problem)); }