Ejemplo n.º 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);
        }
Ejemplo n.º 2
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;
		}
Ejemplo n.º 3
0
		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());
		}
Ejemplo n.º 4
0
 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()));
 }
Ejemplo n.º 5
0
 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));
     }
 }
Ejemplo n.º 6
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
		}
Ejemplo n.º 7
0
		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));

			}
		}
Ejemplo n.º 8
0
 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
 }
Ejemplo n.º 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();
				}

			}

		}
Ejemplo n.º 10
0
		private int getHeuristic(Node aNode, Problem p) 
		{
			return p.getHeuristicFunction().getHeuristicValue(aNode.getState());
		}