示例#1
0
    def performUCS(self, position, goal):
        to_explore = PriorityQueue()
        explored = set()
        prevNode = dict({position: 0})

        to_explore.push(position, 0)

        while (not(to_explore.is_empty())):
            aux = to_explore.pop()
            node = aux[0]
            prevCost = aux[1]
            to_explore.remove(node)
            if (goal == node):
                return prevNode
            explored.add(node)

            actions = ['N','S','E','W']

            for action in actions:
                child, actionCost = self.step(node, action)
                if not(child == node):
                    cost = prevCost + actionCost
                    if not(child in explored or child in to_explore):
                        to_explore.push(child, cost)
                        prevNode[child] = (node, action)
                    elif child in to_explore and cost < to_explore.getCost(child):
                        to_explore.update(child, cost)
                        prevNode[child] = (node, action)
        return None
示例#2
0
    def performAStar(self, position, goal):
        to_explore = PriorityQueue()
        explored = set()
        prevNode = dict({position: 0})
        costs = dict({position: 0})

        to_explore.push(position, 0 + self.model.h(position, goal))
        stepCounter = 0

        while (not (to_explore.is_empty())):
            node = to_explore.pop()[0]
            to_explore.remove(node)
            if (goal == node):
                return prevNode
            explored.add(node)

            actions = ['N', 'S', 'E', 'W']

            for action in actions:
                child, actionCost = self.step(node, action)
                if not (child == node):
                    prevCost = costs[node] + actionCost
                    cost = prevCost + self.model.h(child, goal)

                    if not (child in explored or child in to_explore):
                        to_explore.push(child, cost)
                        costs[child] = prevCost
                        prevNode[child] = (node, action)
                    elif child in to_explore and cost < to_explore.getCost(
                            child):
                        to_explore.update(child, cost)
                        costs[child] = prevCost
                        prevNode[child] = (node, action)
        return None