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; }
public SearchAgent(Problem p, Search search) { actionList = search.search(p); actionIterator = actionList.GetEnumerator(); searchMetrics = search.getMetrics(); }
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) { for (int i = 1; i <= limit; i++) { DepthLimitedSearch dls = new DepthLimitedSearch(i); ArrayList result = dls.search(p); iterationMetrics.set(NODES_EXPANDED, iterationMetrics .getInt(NODES_EXPANDED) + dls.getMetrics().getInt(NODES_EXPANDED)); if (!cutOffResult(result)) { return result; } } return new ArrayList();//failure }
private Node getHighestValuedNodeFrom(ArrayList children, Problem p) { int highestValue = int.MinValue; Node nodeWithHighestValue = null; for (int i = 0; i < children.Count; i++) { Node child = (Node) children[i]; int value = getValue(child, p); if (value > highestValue) { highestValue = value; nodeWithHighestValue = child; } } return nodeWithHighestValue; }
public ArrayList search(Problem problem, NodeStore fringe) { clearInstrumentation(); fringe.add(new Node(problem.getInitialState())); setQueueSize(fringe.size()); while (!(fringe.isEmpty())) { Node node = (Node) fringe.remove(); setQueueSize(fringe.size()); if (problem.isGoalState(node.getState())) { return SearchUtils.actionsFromNodes(node.getPathFromRoot()); } addExpandedNodesToFringe(fringe, node, problem); setQueueSize(fringe.size()); } return new ArrayList();//Empty List indicates Failure }
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 IComparer getComparator(Problem p);
public ArrayList search(Problem p) { return _search.search(p, new PriorityNodeStore(getComparator(p))); }
private void btnJocks_Click(object sender, System.EventArgs e) { this.textBox1.Text = ""; JocksNerdsState initialState = new JocksNerdsState(); try { Problem problem = new Problem(initialState, new JocksNerdsSuccessorFunction(), new JocksNerdsGoalTest()); //Search search = new BreadthFirstSearch(new TreeSearch()); //Search search = new BreadthFirstSearch(new GraphSearch()); //this one never ends because it tries to traverse a tree of effectively infinite depth //Search search = new DepthFirstSearch(new TreeSearch()); //Search search = new DepthFirstSearch(new GraphSearch()); //Search search = new DepthLimitedSearch(12); //Search search = new IterativeDeepeningSearch(); //Search search = new AStarSearch(new GraphSearch()); //Search search = new GreedyBestFirstSearch(new GraphSearch()); //Search search = new HillClimbingSearch(); Search search = new SimulatedAnnealingSearch(); ArrayList solution = search.search(problem); if (solution.Count == 0) { //empty list means failure //System.out.println("\nNo Solution\n"); this.textBox1.Text = System.Environment.NewLine + "No Solution"; } for (int i = 0; i < solution.Count; i++) { this.textBox1.Text += solution[i].ToString() + System.Environment.NewLine; } //Printing metrics Metrics searchMetrics = search.getMetrics(); //ArrayList iter = (ArrayList)searchMetrics.keySet(); ICollection col = searchMetrics.keySet(); IEnumerator iter = col.GetEnumerator(); //iter.GetEnumerator(); //Array r = new Array(); //iter.MoveNext(); //iter. while(iter.MoveNext()) { string key = iter.Current.ToString(); string val = searchMetrics.get(key); this.textBox1.Text += System.Environment.NewLine + key + ": " + val; //iter.MoveNext(); } } catch (Exception ex) { //e.printStackTrace(); this.textBox1.Text += ex.Message; } }
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 }
public ArrayList search(Problem p) { return(_search.search(p, new PriorityNodeStore(getComparator(p)))); }
private void btnAStar_Click(object sender, System.EventArgs e) { this.textBox1.Text = "EightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->" + System.Environment.NewLine; Problem problem = new Problem(random1, new EightPuzzleSuccessorFunction(), new EightPuzzleGoalTest(), new MisplacedTilleHeuristicFunction()); Search search = new AStarSearch(new GraphSearch()); SearchAgent agent = new SearchAgent(problem, search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }
private void btnRecursiveDLS_Click(object sender, System.EventArgs e) { this.textBox1.Text = "NQueensDemo recursive DLS -->" + System.Environment.NewLine; Problem problem = new Problem(new NQueensBoard(8),new NQueensSuccessorFunction(), new NQueensGoalTest()); Search search = new DepthLimitedSearch(8); SearchAgent agent = new SearchAgent(problem, search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }
protected override IComparer getComparator(Problem p) { return new NodeComparator(p); }
public ArrayList search(Problem p) { return _search.search(p, new LIFONodeStore()); }
private void btnGreedyBestFirstMan_Click(object sender, System.EventArgs e) { this.textBox1.Text = "EightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)-->" + System.Environment.NewLine; Problem problem = new Problem(boardWithThreeMoveSolution, new EightPuzzleSuccessorFunction(), new EightPuzzleGoalTest(), new ManhattanHeuristicFunction()); Search search = new GreedyBestFirstSearch(new GraphSearch()); SearchAgent agent = new SearchAgent(problem, search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }
public ArrayList search(Problem p) { clearInstrumentation(); return recursiveDLS(new Node(p.getInitialState()), p, limit); }
protected abstract void addExpandedNodesToFringe(NodeStore fringe, Node node, Problem p);
private void btnHillClimbing_Click(object sender, System.EventArgs e) { this.textBox1.Text = "NQueensDemo HillClimbing -->" + System.Environment.NewLine; Problem problem = new Problem(new NQueensBoard(8),new NQueensSuccessorFunction(), new NQueensGoalTest(),new QueensToBePlacedHeuristic()); Search search = new HillClimbingSearch(); SearchAgent agent = new SearchAgent(problem,search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }
public NodeComparator(Problem problem) { this.problem = problem; }
private void btnJugs_Click(object sender, System.EventArgs e) { this.textBox1.Text = ("Jugs Puzzle -->" + System.Environment.NewLine); Q2State initialState = new Q2State(); try { Problem problem = new Problem(initialState, new Q2SuccessorFunction(), new Q2GoalTest(), new Q2StepCostFunction()); Search search = new BreadthFirstSearch(new TreeSearch()); //Search search = new BreadthFirstSearch(new GraphSearch()); //Search search = new DepthFirstSearch(new TreeSearch()); //Search search = new DepthFirstSearch(new GraphSearch()); //Search search = new DepthLimitedSearch(12); //Search search = new IterativeDeepeningSearch(); //Search search = new AStarSearch(new TreeSearch()); ArrayList solution = search.search(problem); if (solution.Count == 0) { //empty list means failure this.textBox1.Text += (System.Environment.NewLine + "No Solution" +System.Environment.NewLine); } //this.textBox1.Text += (solution + System.Environment.NewLine); for (int i = 0; i < solution.Count; i++) { this.textBox1.Text += solution[i].ToString() + System.Environment.NewLine; } //Printing metrics Metrics searchMetrics = search.getMetrics(); IEnumerator iter = searchMetrics.keySet().GetEnumerator(); while (iter.MoveNext()) { string key = iter.Current.ToString(); string value = searchMetrics.get(key); this.textBox1.Text += (key +": " + value); } } catch (Exception ex) { this.textBox1.Text += ex.Message; } }
private void btnBreadthFirst_Click(object sender, System.EventArgs e) { this.textBox1.Text = "NQueensDemo BFS -->" + System.Environment.NewLine; Problem problem = new Problem(new NQueensBoard(8),new NQueensSuccessorFunction(), new NQueensGoalTest()); Search search = new BreadthFirstSearch(new TreeSearch()); SearchAgent agent2 = new SearchAgent(problem, search); printActions(agent2.getActions()); printInstrumentation(agent2.getInstrumentation()); }
private void btnIDLS_Click(object sender, System.EventArgs e) { this.textBox1.Text = "EightPuzzleDemo Iterative DLS-->" + System.Environment.NewLine; Problem problem = new Problem(random1, new EightPuzzleSuccessorFunction(), new EightPuzzleGoalTest()); Search search = new IterativeDeepeningSearch(); SearchAgent agent = new SearchAgent(problem, search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }
private void btnIterativeDeeping_Click(object sender, System.EventArgs e) { this.textBox1.Text = "NQueensDemo Iterative DS -->" + System.Environment.NewLine; Problem problem = new Problem(new NQueensBoard(8),new NQueensSuccessorFunction(), new NQueensGoalTest()); Search search = new IterativeDeepeningSearch(); SearchAgent agent = new SearchAgent(problem,search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }
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)); }
private void btnSimulatedAnnealing_Click(object sender, System.EventArgs e) { this.textBox1.Text = "EightPuzzleDemo Simulated Annealing Search -->" + System.Environment.NewLine; Problem problem = new Problem(random1, new EightPuzzleSuccessorFunction(), new EightPuzzleGoalTest(), new ManhattanHeuristicFunction()); Search search = new SimulatedAnnealingSearch(); SearchAgent agent = new SearchAgent(problem, search); printActions(agent.getActions()); printInstrumentation(agent.getInstrumentation()); }