Exemplo n.º 1
0
class OnlineDFSAgent(LocalSearch):
    def __init__(self, problem, pts_function):
        super().__init__(problem, pts_function)

    def init(self):
        self.alive = True
        self.s = None
        self.a = None

        self.untried = {}
        self.unbacktracked = {}
        self.result = {}

    # function ONLINE-DFS-AGENT(s') returns an action
	# inputs: s', a percept that identifies the current state
    def execute(self, percept):
        s_prime = self.pts_function.get_state(percept)

        # if GOAL-TEST(s') then return stop
        if self._is_goal_state(s_prime):
            self.a = NoOpAction()
        else:
            # if s' is a new state (not in untried) then untried[s'] <- ACTIONS(s')
            if s_prime not in self.untried.keys():
                self.untried[s_prime] = self._actions(s_prime)

            # if s is not null then do
            if self.s != None:
                # 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 s_prime != self.result.get((self.s, self.a)):
                    self.result[(self.s, self.a)] = s_prime

                    lst = self.unbacktracked.setdefault(s_prime, [])
                    lst.insert(0, self.s)

            # if untried[s'] is empty then
            if len(self.untried.get(s_prime)) == 0:
                if len(self.unbacktracked.get(s_prime)) == 0:
                    self.a = NoOpAction()
                else:
                    # else a <- an action b such that result[s', b] = POP(unbacktracked[s'])
                    popped = self.unbacktracked[s_prime].pop(0)
                    for (s, b) in self.result.keys():
                        if s == s_prime and self.result[(s, b)] == popped:
                            self.a = b
                            break
            else:
                # else a <- POP(untried[s'])
                self.a = self.untried[s_prime].pop(0)

        if self.a.is_noop():
            self.alive = False

        # s <- s'
        self.s = s_prime
        # return a
        return self.a
Exemplo n.º 2
0
class OnlineDFSAgent(LocalSearch):
    def __init__(self, problem, pts_function):
        super().__init__(problem, pts_function)

    def init(self):
        self.alive = True
        self.s = None
        self.a = None

        self.untried = {}
        self.unbacktracked = {}
        self.result = {}

    # function ONLINE-DFS-AGENT(s') returns an action
# inputs: s', a percept that identifies the current state

    def execute(self, percept):
        s_prime = self.pts_function.get_state(percept)

        # if GOAL-TEST(s') then return stop
        if self._is_goal_state(s_prime):
            self.a = NoOpAction()
        else:
            # if s' is a new state (not in untried) then untried[s'] <- ACTIONS(s')
            if s_prime not in self.untried.keys():
                self.untried[s_prime] = self._actions(s_prime)

            # if s is not null then do
            if self.s != None:
                # 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 s_prime != self.result.get((self.s, self.a)):
                    self.result[(self.s, self.a)] = s_prime

                    lst = self.unbacktracked.setdefault(s_prime, [])
                    lst.insert(0, self.s)

            # if untried[s'] is empty then
            if len(self.untried.get(s_prime)) == 0:
                if len(self.unbacktracked.get(s_prime)) == 0:
                    self.a = NoOpAction()
                else:
                    # else a <- an action b such that result[s', b] = POP(unbacktracked[s'])
                    popped = self.unbacktracked[s_prime].pop(0)
                    for (s, b) in self.result.keys():
                        if s == s_prime and self.result[(s, b)] == popped:
                            self.a = b
                            break
            else:
                # else a <- POP(untried[s'])
                self.a = self.untried[s_prime].pop(0)

        if self.a.is_noop():
            self.alive = False

        # s <- s'
        self.s = s_prime
        # return a
        return self.a
Exemplo n.º 3
0
class LRTAStarAgent(LocalSearch):
    def __init__(self, problem, pts_function, heuristic_function):
        super().__init__(problem, pts_function)
        self.heuristic_function = heuristic_function

    def init(self):
        self.alive = True
        self.result = {}
        self.H = {}
        self.s = None
        self.a = None

    # function LRTA*-AGENT(s') returns an action
	# inputs: s', a percept that identifies the current state
    def execute(self, percept):
        s_prime = self.pts_function.get_state(percept)

        # if GOAL-TEST(s') then return stop
        if self._is_goal_state(s_prime):
            self.a = NoOpAction()
        else:
            # if s' is a new state (not in H) then H[s'] <- h(s')
            if s_prime not in self.H.keys():
                self.H[s_prime] = self.heuristic_function.h(s_prime)

            # if s is not null
            if self.s != None:
                # result[s, a] <- s'
                self.result[(self.s, self.a)] = s_prime

                # H[s] <- min LRTA*-COST(s, b, result[s, b], H)
				# b (element of) ACTIONS(s)
                m = min([self._lrta_cost(self.s, b) for b in self._actions(self.s)])
                self.H[self.s] = m

            # a <- an action b in ACTIONS(s') that minimizes LRTA*-COST(s', b,
			# result[s', b], H)
            m = PlusInfinity()
            self.a = NoOpAction()

            for b in self._actions(s_prime):
                cost = self._lrta_cost(s_prime, b)
                if cost < m:
                    m = cost
                    self.a = b

        # s <- s'
        self.s = s_prime

        if self.a.is_noop():
            self.alive = False

        # return a
        return self.a

    # function LRTA*-COST(s, a, s', H) returns a cost estimate
    def _lrta_cost(self, s, action):
        s_prime = self.result.get((s, action))
        # if s' is undefined then return h(s)
        if s_prime == None:
            return self.heuristic_function.h(s)
        # else return c(s, a, s') + H[s']
        return self.problem.step_cost_function.c(s, action, s_prime) + self.H[s_prime]
Exemplo n.º 4
0
class LRTAStarAgent(LocalSearch):
    def __init__(self, problem, pts_function, heuristic_function):
        super().__init__(problem, pts_function)
        self.heuristic_function = heuristic_function

    def init(self):
        self.alive = True
        self.result = {}
        self.H = {}
        self.s = None
        self.a = None

    # function LRTA*-AGENT(s') returns an action
# inputs: s', a percept that identifies the current state

    def execute(self, percept):
        s_prime = self.pts_function.get_state(percept)

        # if GOAL-TEST(s') then return stop
        if self._is_goal_state(s_prime):
            self.a = NoOpAction()
        else:
            # if s' is a new state (not in H) then H[s'] <- h(s')
            if s_prime not in self.H.keys():
                self.H[s_prime] = self.heuristic_function.h(s_prime)

            # if s is not null
            if self.s != None:
                # result[s, a] <- s'
                self.result[(self.s, self.a)] = s_prime

                # H[s] <- min LRTA*-COST(s, b, result[s, b], H)
                # b (element of) ACTIONS(s)
                m = min([
                    self._lrta_cost(self.s, b) for b in self._actions(self.s)
                ])
                self.H[self.s] = m

            # a <- an action b in ACTIONS(s') that minimizes LRTA*-COST(s', b,
# result[s', b], H)
            m = PlusInfinity()
            self.a = NoOpAction()

            for b in self._actions(s_prime):
                cost = self._lrta_cost(s_prime, b)
                if cost < m:
                    m = cost
                    self.a = b

        # s <- s'
        self.s = s_prime

        if self.a.is_noop():
            self.alive = False

        # return a
        return self.a

    # function LRTA*-COST(s, a, s', H) returns a cost estimate
    def _lrta_cost(self, s, action):
        s_prime = self.result.get((s, action))
        # if s' is undefined then return h(s)
        if s_prime == None:
            return self.heuristic_function.h(s)
        # else return c(s, a, s') + H[s']
        return self.problem.step_cost_function.c(s, action,
                                                 s_prime) + self.H[s_prime]