예제 #1
0
		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;
		}
예제 #2
0
파일: SearchAgent.cs 프로젝트: langeds/aima
		public SearchAgent(Problem p, Search search) 
		{
			actionList = search.search(p);
			actionIterator = actionList.GetEnumerator();
			searchMetrics = search.getMetrics();

		}
예제 #3
0
파일: GraphSearch.cs 프로젝트: langeds/aima
		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));

			}
		}
예제 #4
0
	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
	}
예제 #5
0
		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;
		}
예제 #6
0
파일: QueueSearch.cs 프로젝트: langeds/aima
		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
		}
예제 #7
0
		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;
			}

		}
예제 #8
0
		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
		}
예제 #9
0
		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();
				}

			}

		}
예제 #10
0
 protected abstract IComparer getComparator(Problem p);
예제 #11
0
		protected abstract IComparer getComparator(Problem p);
예제 #12
0
		public ArrayList search(Problem p)  
		{
			return _search.search(p, new PriorityNodeStore(getComparator(p)));
		}
예제 #13
0
파일: Form1.cs 프로젝트: langeds/aima
		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;
			}
		}
예제 #14
0
		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
		}
예제 #15
0
 public ArrayList search(Problem p)
 {
     return(_search.search(p, new PriorityNodeStore(getComparator(p))));
 }
예제 #16
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #17
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #18
0
파일: AStarSearch.cs 프로젝트: langeds/aima
		protected override IComparer getComparator(Problem p) 
		{

			return new NodeComparator(p);
		}
예제 #19
0
		public ArrayList search(Problem p) 
		{

			return _search.search(p, new LIFONodeStore());
		}
예제 #20
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #21
0
 public SearchAgent(Problem p, Search search)
 {
     actionList     = search.search(p);
     actionIterator = actionList.GetEnumerator();
     searchMetrics  = search.getMetrics();
 }
예제 #22
0
		public ArrayList search(Problem p) 
		{
			clearInstrumentation();
			return recursiveDLS(new Node(p.getInitialState()), p, limit);
		}
예제 #23
0
파일: QueueSearch.cs 프로젝트: langeds/aima
		protected abstract void addExpandedNodesToFringe(NodeStore fringe,
			Node node, Problem p);
예제 #24
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #25
0
파일: AStarSearch.cs 프로젝트: langeds/aima
			public NodeComparator(Problem problem) 
			{
				this.problem = problem;
			}
예제 #26
0
파일: Form1.cs 프로젝트: langeds/aima
		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;
			}
		}
예제 #27
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #28
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #29
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}
예제 #30
0
		private int getHeuristic(Node aNode, Problem p) 
		{
			return p.getHeuristicFunction().getHeuristicValue(aNode.getState());
		}
예제 #31
0
파일: TreeSearch.cs 프로젝트: langeds/aima
		protected override void addExpandedNodesToFringe(NodeStore fringe, Node node,
			Problem problem) 
		{
			fringe.add(expandNode(node, problem));
		}
예제 #32
0
파일: Form1.cs 프로젝트: langeds/aima
		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());
		}