Exemplo n.º 1
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>());
        }
Exemplo n.º 2
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>());
        }
Exemplo n.º 3
0
        /// <summary>
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="localFrontier"></param>
        /// <returns>if goal found, the list of actions to the Goal. If already at the goal
        /// you will receive a IList with a single NoOp IAction in it. If fail to find the Goal,
        /// an empty list will be returned to indicate that the search failed.</returns>
        public virtual IList <IAction> Search(Problem problem, IQueue <Node> localFrontier)
        {
            this.frontier = localFrontier;

            ClearInstrumentation();
            // initialize the frontier using the initial state of the problem
            var root = new Node(problem.InitialState);

            if (this.IsCheckGoalBeforeAddingToFrontier())
            {
                if (SearchUtils.IsGoalState(problem, root))
                {
                    return(SearchUtils.ActionsFromNodes(root.GetPathFromRoot()));
                }
            }
            localFrontier.Push(root);
            this.SetQueueSize(localFrontier.Size());
            while (!localFrontier.IsEmpty() && !CancelableThread.CurrIsCanceled())
            {
                // choose a leaf node and remove it from the frontier
                Node nodeToExpand = this.PopNodeFromFrontier();
                this.SetQueueSize(localFrontier.Size());
                // Only need to check the nodeToExpand if have not already
                // checked before adding to the frontier
                if (!this.IsCheckGoalBeforeAddingToFrontier())
                {
                    // if the node contains a goal state then return the
                    // corresponding solution
                    if (SearchUtils.IsGoalState(problem, nodeToExpand))
                    {
                        this.SetPathCost(nodeToExpand.PathCost);
                        return(SearchUtils.ActionsFromNodes(nodeToExpand.GetPathFromRoot()));
                    }
                }
                // expand the chosen node, adding the resulting nodes to the
                // frontier
                foreach (Node fn in this.GetResultingNodesToAddToFrontier(nodeToExpand, problem))
                {
                    if (this.IsCheckGoalBeforeAddingToFrontier())
                    {
                        if (SearchUtils.IsGoalState(problem, fn))
                        {
                            return(SearchUtils.ActionsFromNodes(fn.GetPathFromRoot()));
                        }
                    }
                    localFrontier.Push(fn);
                }
                this.SetQueueSize(localFrontier.Size());
            }
            // if the frontier is empty then return failure
            return(this.Failure());
        }
Exemplo n.º 4
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);
        }