예제 #1
0
        /**
         * Returns a node corresponding to a local maximum or empty if the search was
         * cancelled by the user.
         *
         * @param p the search problem
         * @return a node or empty
         */
        // function HILL-CLIMBING(problem) returns a state that is a local maximum
        public Node <S, A> findNode(IProblem <S, A> p)
        {
            clearMetrics();
            outcome = SearchOutcome.FAILURE;
            // current <- MAKE-NODE(problem.INITIAL-STATE)
            Node <S, A> current = nodeExpander.createRootNode(p.getInitialState());
            Node <S, A> neighbor;

            // loop do
            while (!currIsCancelled)
            {
                lastState = current.getState();
                metrics.set(METRIC_NODE_VALUE, getValue(current));
                ICollection <Node <S, A> > children = nodeExpander.expand(current, p);
                // neighbor <- a highest-valued successor of current
                neighbor = getHighestValuedNodeFrom(children);

                // if neighbor.VALUE <= current.VALUE then return current.STATE
                if (neighbor == null || getValue(neighbor) <= getValue(current))
                {
                    if (p.testSolution(current))
                    {
                        outcome = SearchOutcome.SOLUTION_FOUND;
                    }
                    return(current);
                }
                // current <- neighbor
                current = neighbor;
            }
            return(null);
        }
예제 #2
0
        // function HILL-CLIMBING(problem) returns a state that is a local maximum
        public IList <IAction> Search(Problem p)
        {
            ClearInstrumentation();
            outcome   = SearchOutcome.Failure;
            lastState = null;
            // current <- MAKE-NODE(problem.INITIAL-STATE)
            Node current  = new Node(p.InitialState);
            Node neighbor = null;

            // loop do
            while (!CancelableThread.CurrIsCanceled())
            {
                IList <Node> children = ExpandNode(current, p);
                // neighbor <- a highest-valued successor of current
                neighbor = this.GetHighestValuedNodeFrom(children, p);

                // if neighbor.VALUE <= current.VALUE then return current.STATE
                if ((neighbor == null) || (this.GetValue(neighbor) <= this.GetValue(current)))
                {
                    if (SearchUtils.IsGoalState(p, current))
                    {
                        outcome = SearchOutcome.SolutionFound;
                    }
                    lastState = current.State;
                    return(SearchUtils.ActionsFromNodes(current.GetPathFromRoot()));
                }
                // current <- neighbor
                current = neighbor;
            }
            return(new List <IAction>());
        }
예제 #3
0
	// function HILL-CLIMBING(problem) returns a state that is a local maximum
	public List<Action> search(Problem p) {
		clearInstrumentation();
		outcome = SearchOutcome.FAILURE;
		lastState = null;
		// current <- MAKE-NODE(problem.INITIAL-STATE)
		Node current = new Node(p.getInitialState());
		Node neighbor = null;
		// loop do
		while (!CancelableThread.currIsCanceled()) {
			List<Node> children = expandNode(current, p);
			// neighbor <- a highest-valued successor of current
			neighbor = getHighestValuedNodeFrom(children, p);

			// if neighbor.VALUE <= current.VALUE then return current.STATE
			if ((neighbor == null) || (getValue(neighbor) <= getValue(current))) {
				if (SearchUtils.isGoalState(p, current)) {
					outcome = SearchOutcome.SOLUTION_FOUND;
				}
				lastState = current.getState();
				return SearchUtils.actionsFromNodes(current.getPathFromRoot());
			}
			// current <- neighbor
			current = neighbor;
		}
		return new List<Action>();
	}
예제 #4
0
        // function HILL-CLIMBING(problem) returns a state that is a local maximum
        public List <Action> search(Problem p)
        {
            clearInstrumentation();
            outcome   = SearchOutcome.FAILURE;
            lastState = null;
            // current <- MAKE-NODE(problem.INITIAL-STATE)
            Node current  = new Node(p.getInitialState());
            Node neighbor = null;

            // loop do
            while (!CancelableThread.currIsCanceled())
            {
                List <Node> children = expandNode(current, p);
                // neighbor <- a highest-valued successor of current
                neighbor = getHighestValuedNodeFrom(children, p);

                // if neighbor.VALUE <= current.VALUE then return current.STATE
                if ((neighbor == null) || (getValue(neighbor) <= getValue(current)))
                {
                    if (SearchUtils.isGoalState(p, current))
                    {
                        outcome = SearchOutcome.SOLUTION_FOUND;
                    }
                    lastState = current.getState();
                    return(SearchUtils.actionsFromNodes(current.getPathFromRoot()));
                }
                // current <- neighbor
                current = neighbor;
            }
            return(new List <Action>());
        }
 public SearchResult(Node solution, double fCostLimit)
 {
     if (null == solution)
     {
         this.outcome = SearchOutcome.Failure;
     }
     else
     {
         this.outcome  = SearchOutcome.SolutionFound;
         this.solution = solution;
     }
     this.fCostLimit = fCostLimit;
 }
 public SearchResult(Node solution, Double fCostLimit)
 {
     if (null == solution)
     {
         this.outcome = SearchOutcome.FAILURE;
     }
     else
     {
         this.outcome  = SearchOutcome.SOLUTION_FOUND;
         this.solution = solution;
     }
     this.fCostLimit = fCostLimit;
 }
예제 #7
0
        // function SIMULATED-ANNEALING(problem, schedule) returns a solution state
        public IList <IAction> Search(Problem p)
        {
            ClearInstrumentation();
            outcome   = SearchOutcome.Failure;
            lastState = null;
            // current <- MAKE-NODE(problem.INITIAL-STATE)
            Node            current = new Node(p.InitialState);
            Node            next    = null;
            IList <IAction> ret     = new List <IAction>();
            // for t = 1 to INFINITY do
            int timeStep = 0;

            while (!CancelableThread.CurrIsCanceled())
            {
                // temperature <- schedule(t)
                double temperature = scheduler.GetTemp(timeStep);
                timeStep++;
                // if temperature = 0 then return current
                if (temperature == 0.0)
                {
                    if (SearchUtils.IsGoalState(p, current))
                    {
                        outcome = SearchOutcome.SolutionFound;
                    }
                    ret       = SearchUtils.ActionsFromNodes(current.GetPathFromRoot());
                    lastState = current.State;
                    break;
                }

                IList <Node> children = ExpandNode(current, p);
                if (children.Count > 0)
                {
                    // next <- a randomly selected successor of current
                    next = Util.SelectRandomlyFromList(children);
                    // /\E <- next.VALUE - current.value
                    double deltaE = this.GetValue(p, next) - this.GetValue(p, current);

                    if (this.ShouldAccept(temperature, deltaE))
                    {
                        current = next;
                    }
                }
            }

            return(ret);
        }
        // function SIMULATED-ANNEALING(problem, schedule) returns a solution state
        public List <Action> search(Problem p)
        {
            clearInstrumentation();
            outcome   = SearchOutcome.FAILURE;
            lastState = null;
            // current <- MAKE-NODE(problem.INITIAL-STATE)
            Node          current = new Node(p.getInitialState());
            Node          next    = null;
            List <Action> ret     = new List <Action>();
            // for t = 1 to INFINITY do
            int timeStep = 0;

            while (!CancelableThread.currIsCanceled())
            {
                // temperature <- schedule(t)
                double temperature = scheduler.getTemp(timeStep);
                timeStep++;
                // if temperature = 0 then return current
                if (temperature == 0.0)
                {
                    if (SearchUtils.isGoalState(p, current))
                    {
                        outcome = SearchOutcome.SOLUTION_FOUND;
                    }
                    ret       = SearchUtils.actionsFromNodes(current.getPathFromRoot());
                    lastState = current.getState();
                    break;
                }

                List <Node> children = expandNode(current, p);
                if (children.Count > 0)
                {
                    // next <- a randomly selected successor of current
                    next = Util.selectRandomlyFromList(children);
                    // /\E <- next.VALUE - current.value
                    double deltaE = getValue(p, next) - getValue(p, current);

                    if (shouldAccept(temperature, deltaE))
                    {
                        current = next;
                    }
                }
            }

            return(ret);
        }
예제 #9
0
        // function SIMULATED-ANNEALING(problem, schedule) returns a solution state
        public Node <S, A> findNode(IProblem <S, A> p)
        {
            clearMetrics();
            outcome   = SearchOutcome.FAILURE;
            lastState = default(S);
            // current <- MAKE-NODE(problem.INITIAL-STATE)
            Node <S, A> current = nodeExpander.createRootNode(p.getInitialState());
            // for t = 1 to INFINITY do
            int timeStep = 0;

            while (!currIsCancelled)
            {
                // temperature <- schedule(t)
                double temperature = scheduler.getTemp(timeStep);
                timeStep++;
                lastState = current.getState();
                // if temperature = 0 then return current
                if (temperature == 0.0)
                {
                    if (p.testSolution(current))
                    {
                        outcome = SearchOutcome.SOLUTION_FOUND;
                    }
                    return(current);
                }

                updateMetrics(temperature, getValue(current));
                ICollection <Node <S, A> > children = nodeExpander.expand(current, p);
                if (children.Size() > 0)
                {
                    // next <- a randomly selected successor of current
                    Node <S, A> next = Util.selectRandomlyFromList(children);
                    // /\E <- next.VALUE - current.value
                    double deltaE = getValue(next) - getValue(current);

                    if (shouldAccept(temperature, deltaE))
                    {
                        current = next;
                    }
                }
            }
            return(null);
        }
	// function SIMULATED-ANNEALING(problem, schedule) returns a solution state
	public List<Action> search(Problem p) {
		clearInstrumentation();
		outcome = SearchOutcome.FAILURE;
		lastState = null;
		// current <- MAKE-NODE(problem.INITIAL-STATE)
		Node current = new Node(p.getInitialState());
		Node next = null;
		List<Action> ret = new List<Action>();
		// for t = 1 to INFINITY do
		int timeStep = 0;
		while (!CancelableThread.currIsCanceled()) {
			// temperature <- schedule(t)
			double temperature = scheduler.getTemp(timeStep);
			timeStep++;
			// if temperature = 0 then return current
			if (temperature == 0.0) {
				if (SearchUtils.isGoalState(p, current)) {
					outcome = SearchOutcome.SOLUTION_FOUND;
				}
				ret = SearchUtils.actionsFromNodes(current.getPathFromRoot());
				lastState = current.getState();
				break;
			}

			List<Node> children = expandNode(current, p);
			if (children.Count > 0) {
				// next <- a randomly selected successor of current
				next = Util.selectRandomlyFromList(children);
				// /\E <- next.VALUE - current.value
				double deltaE = getValue(p, next) - getValue(p, current);

				if (shouldAccept(temperature, deltaE)) {
					current = next;
				}
			}
		}

		return ret;
	}
 public DirectorySearchResult(SearchOutcome outcome)
 {
     Outcome = outcome;
     Path = "";
 }
 public DirectorySearchResult(SearchOutcome outcome, string path)
 {
     Outcome = outcome;
     Path = path;
 }
        private IList <IAction> RetrieveActions(Problem op, Problem rp, Node originalPath, Node reversePath)
        {
            IList <IAction> actions = new List <IAction>();

            if (null == reversePath)
            {
                // This is the simple case whereby the path has been found
                // from the original problem first
                this.SetPathCost(originalPath.PathCost);
                searchOutcome = SearchOutcome.PathFoundFromOriginalProblem;
                actions       = SearchUtils.ActionsFromNodes(originalPath.GetPathFromRoot());
            }
            else
            {
                var    nodePath      = new List <Node>();
                object originalState = null;
                if (null != originalPath)
                {
                    nodePath.AddRange(originalPath.GetPathFromRoot());
                    originalState = originalPath.State;
                }
                // Only append the reverse path if it is not the
                // GOAL state from the original problem (if you don't
                // you could end up appending a partial reverse path
                // that looks back on its initial state)
                if (!SearchUtils.IsGoalState(op, reversePath))
                {
                    IList <Node> rpath = reversePath.GetPathFromRoot();
                    for (int i = rpath.Count - 1; i >= 0; i--)
                    {
                        // Ensure do not include the node from the reverse path
                        // that is the one that potentially overlaps with the
                        // original path (i.e. if started in goal state or where
                        // they meet in the middle).
                        if (!rpath[i].State.Equals(originalState))
                        {
                            nodePath.Add(rpath[i]);
                        }
                    }
                }

                if (!canTraversePathFromOriginalProblem(op, nodePath, actions))
                {
                    // This is where it is possible to get to the initial state
                    // from the goal state (i.e. reverse path) but not the other way
                    // round, null returned to indicate an invalid path found from
                    // the reverse problem
                    return(null);
                }

                if (null == originalPath)
                {
                    searchOutcome = SearchOutcome.PathFoundFromReverseProblem;
                }
                else
                {
                    // Need to ensure that where the original and reverse paths
                    // overlap, as they can link based on their fringes, that
                    // the reverse path is actually capable of connecting to
                    // the previous node in the original path (if not root).
                    if (this.CanConnectToOriginalFromReverse(rp, originalPath, reversePath))
                    {
                        searchOutcome = SearchOutcome.PathFoundBetweenProblems;
                    }
                    else
                    {
                        searchOutcome = SearchOutcome.PathFoundFromOriginalProblem;
                    }
                }
            }

            return(actions);
        }
 public DirectorySearchResult(SearchOutcome outcome)
 {
     Outcome = outcome;
     Path    = "";
 }
 public DirectorySearchResult(SearchOutcome outcome, string path)
 {
     Outcome = outcome;
     Path    = path;
 }
	public SearchResult(Node solution, Double fCostLimit) {
		if (null == solution) {
			this.outcome = SearchOutcome.FAILURE;
		} else {
			this.outcome = SearchOutcome.SOLUTION_FOUND;
			this.solution = solution;
		}
		this.fCostLimit = fCostLimit;
	}
        //TODO: split this method to multiple methods.
        public IList <IAction> Search(Problem p)
        {
            Debug.Assert(p is IBidirectionalProblem);

            searchOutcome = SearchOutcome.PathNotFound;

            ClearInstrumentation();

            var op = ((IBidirectionalProblem)p).GetOriginalProblem();
            var rp = ((IBidirectionalProblem)p).GetReverseProblem();

            var opFrontier = new CachedStateQueue <Node>();
            var rpFrontier = new CachedStateQueue <Node>();

            var ogs = new GraphSearch();
            var rgs = new GraphSearch();

            // Ensure the instrumentation for these
            // are cleared down as their values
            // are used in calculating the overall
            // bidirectional Metrics.
            ogs.ClearInstrumentation();
            rgs.ClearInstrumentation();

            var opNode = new Node(op.InitialState);
            var rpNode = new Node(rp.InitialState);

            opFrontier.Insert(opNode);
            rpFrontier.Insert(rpNode);

            this.SetQueueSize(opFrontier.Size() + rpFrontier.Size());
            this.SetNodesExpanded(ogs.GetNodesExpanded() + rgs.GetNodesExpanded());

            while (!(opFrontier.IsEmpty() && rpFrontier.IsEmpty()))
            {
                // Determine the nodes to work with and expand their fringes
                // in preparation for testing whether or not the two
                // searches meet or one or other is at the GOAL.
                if (!opFrontier.IsEmpty())
                {
                    opNode = opFrontier.Pop();
                    opFrontier.AddAll(ogs.GetResultingNodesToAddToFrontier(opNode, op));
                }
                else
                {
                    opNode = null;
                }
                if (!rpFrontier.IsEmpty())
                {
                    rpNode = rpFrontier.Pop();
                    rpFrontier.AddAll(rgs.GetResultingNodesToAddToFrontier(rpNode, rp));
                }
                else
                {
                    rpNode = null;
                }

                this.SetQueueSize(opFrontier.Size() + rpFrontier.Size());
                this.SetNodesExpanded(ogs.GetNodesExpanded() + rgs.GetNodesExpanded());

                //
                // First Check if either frontier contains the other's state
                if (null != opNode && null != rpNode)
                {
                    Node popNode = null;
                    Node prpNode = null;
                    if (opFrontier.ContainsNodeBasedOn(rpNode.State))
                    {
                        popNode = opFrontier.GetNodeBasedOn(rpNode.State);
                        prpNode = rpNode;
                    }
                    else if (rpFrontier.ContainsNodeBasedOn(opNode.State))
                    {
                        popNode = opNode;
                        prpNode = rpFrontier.GetNodeBasedOn(opNode.State);
                        // Need to also check whether or not the nodes that
                        // have been taken off the frontier actually represent the
                        // same state, otherwise there are instances whereby
                        // the searches can pass each other by
                    }
                    else if (opNode.State.Equals(rpNode.State))
                    {
                        popNode = opNode;
                        prpNode = rpNode;
                    }
                    if (null != popNode && null != prpNode)
                    {
                        IList <IAction> actions = this.RetrieveActions(op, rp, popNode, prpNode);
                        // It may be the case that it is not in fact possible to
                        // traverse from the original node to the goal node based on
                        // the reverse path (i.e. unidirectional links: e.g.
                        // InitialState(A)<->C<-Goal(B) )
                        if (null != actions)
                        {
                            return(actions);
                        }
                    }
                }

                //
                // Check if the original problem is at the GOAL state
                if (null != opNode && SearchUtils.IsGoalState(op, opNode))
                {
                    // No need to check return value for null here
                    // as an action path discovered from the goal
                    // is guaranteed to exist
                    return(this.RetrieveActions(op, rp, opNode, null));
                }
                //
                // Check if the reverse problem is at the GOAL state
                if (null != rpNode && SearchUtils.IsGoalState(rp, rpNode))
                {
                    IList <IAction> actions = this.RetrieveActions(op, rp, null, rpNode);
                    // It may be the case that it is not in fact possible to
                    // traverse from the original node to the goal node based on
                    // the reverse path (i.e. unidirectional links: e.g.
                    // InitialState(A)<-Goal(B) )
                    if (null != actions)
                    {
                        return(actions);
                    }
                }
            }

            // Empty IList can indicate already at Goal
            // or unable to find valid Set of actions
            return(new List <IAction>());
        }