Exemplo n.º 1
0
        // function LRTA*-AGENT(s') returns an action
        // inputs: s', a percept that identifies the current state

        public override IAction Execute(IPercept psPrimed)
        {
            S sPrimed = ptsFn(psPrimed);

            // if GOAL-TEST(s') then return stop
            if (problem.testGoal(sPrimed))
            {
                a = default(A);
            }
            else
            {
                // if s' is a new state (not in H) then H[s'] <- h(s')
                if (!H.ContainsKey(sPrimed))
                {
                    H.Put(sPrimed, getHeuristicFunction().applyAsDouble(sPrimed));
                }
                // if s is not null
                double min = double.MaxValue;
                if (null != s)
                {
                    // result[s, a] <- s'
                    result.Put(s, a, sPrimed);

                    // H[s] <- min LRTA*-COST(s, b, result[s, b], H)
                    // b (element of) ACTIONS(s)
                    min = double.MaxValue;
                    foreach (A b in problem.getActions(s))
                    {
                        double cost = lrtaCost(s, b, result.Get(s, b));
                        if (cost < min)
                        {
                            min = cost;
                        }
                    }
                    H.Put(s, min);
                }
                // a <- an action b in ACTIONS(s') that minimizes LRTA*-COST(s', b,
                // result[s', b], H)
                // Just in case no actions
                a = default(A);
                foreach (A b in problem.getActions(sPrimed))
                {
                    double cost = lrtaCost(sPrimed, b, result.Get(sPrimed, b));
                    if (cost < min)
                    {
                        min = cost;
                        a   = b;
                    }
                }
            }

            // s <- s'
            s = sPrimed;

            if (a == null)
            {
                // I'm either at the Goal or can't get to it,
                // which in either case I'm finished so just die.
                SetAlive(false);
            }
            // return a
            return(a);
        }
Exemplo n.º 2
0
        // function ONLINE-DFS-AGENT(s') returns an action
        // inputs: s', a percept that identifies the current state

        public override IAction Execute(IPercept psPrimed)
        {
            S sPrimed = ptsFn(psPrimed);

            // if GOAL-TEST(s') then return stop
            if (problem.testGoal(sPrimed))
            {
                a = default(A);
            }
            else
            {
                // if s' is a new state (not in untried) then untried[s'] <-
                // ACTIONS(s')
                if (!untried.ContainsKey(sPrimed))
                {
                    untried.Put(sPrimed, problem.getActions(sPrimed));
                }

                // if s is not null then do
                if (null != s)
                {
                    // Note: If I've already seen the result of this
                    // [s, a] then don't put it back on the unbacktracked
                    // list otherwise you can keep oscillating
                    // between the same states endlessly.
                    if (!(sPrimed.Equals(result.Get(s, a))))
                    {
                        // result[s, a] <- s'
                        result.Put(s, a, sPrimed);

                        // Ensure the unbacktracked always has a list for s'
                        if (!unbacktracked.ContainsKey(sPrimed))
                        {
                            unbacktracked.Put(sPrimed, CollectionFactory.CreateQueue <S>());
                        }

                        // add s to the front of the unbacktracked[s']
                        unbacktracked.Get(sPrimed).Insert(0, s);
                    }
                }
                // if untried[s'] is empty then
                if (untried.Get(sPrimed).IsEmpty())
                {
                    // if unbacktracked[s'] is empty then return stop
                    if (unbacktracked.Get(sPrimed).IsEmpty())
                    {
                        a = default(A);
                    }
                    else
                    {
                        // else a <- an action b such that result[s', b] =
                        // POP(unbacktracked[s'])
                        S popped = unbacktracked.Get(sPrimed).Pop();
                        foreach (Pair <S, A> sa in result.GetKeys())
                        {
                            if (sa.GetFirst().Equals(sPrimed) && result.Get(sa).Equals(popped))
                            {
                                a = sa.getSecond();
                                break;
                            }
                        }
                    }
                }
                else
                {
                    // else a <- POP(untried[s'])
                    a = untried.Get(sPrimed).Pop();
                }
            }

            if (a == null)
            {
                // I'm either at the Goal or can't get to it,
                // which in either case I'm finished so just die.
                SetAlive(false);
            }

            // s <- s'
            s = sPrimed;
            // return a
            return(a);
        }