コード例 #1
0
ファイル: NodeExpander.cs プロジェクト: langeds/aima
		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
ファイル: HillClimbingSearch.cs プロジェクト: langeds/aima
		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
ファイル: HillClimbingSearch.cs プロジェクト: langeds/aima
		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
ファイル: DepthLimitedSearch.cs プロジェクト: langeds/aima
		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
ファイル: PrioritySearch.cs プロジェクト: langeds/aima
		protected abstract IComparer getComparator(Problem p);
コード例 #12
0
ファイル: PrioritySearch.cs プロジェクト: langeds/aima
		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
ファイル: DepthFirstSearch.cs プロジェクト: langeds/aima
		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
ファイル: DepthLimitedSearch.cs プロジェクト: langeds/aima
		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());
		}