Пример #1
0
    def a_star(self, start, goal, limit=25):
        # self.logger.debug("A star start: %s, goal: %s, limit: %d", start, goal, limit)
        heuristic = self.ants.distance(start, goal)
        state = a_state(start, goal, heuristic, limit)
        path = []
        count = 0
        while state.open_list:
            # Safety count
            count = count + 1

            # find the lowest f_score node
            current = state.open_list[0]
            for node in state.open_list:
                if current.f_score > node.f_score:
                    current = node

                    # Check if we have reached the goal
            if current.loc == goal or count > state.limit:
                # self.logger.debug("A Star state:\n%s", self.render_state(state))
                while current != None:
                    path.append(current.loc)
                    current = current.parent

                path.reverse()
                # self.logger.debug("Found the goal: %s", path)
                return path

                # Put the current node off the open and on the close list
            state.open_list.remove(current)
            state.closed_list.append(current)

            # Check all directions
            for direction in self.directions:

                # TODO inline destination
                new_loc = self.ants.destination(current.loc, direction)

                # Look for this node in the closed set
                found = False
                for node in state.closed_list:
                    if node.loc == new_loc:
                        found = True
                        break

                for node in state.open_list:
                    if node.loc == new_loc:
                        found = True
                        break

                unpassable = not self.ants.passable(new_loc)
                occupied = False  # new_loc in self.game_state.my_ants
                hill = new_loc in self.game_state.my_hills
                # self.logger.debug("Testing %s, found %s, passable %s", new_loc, found, passable)

                # TODO make passable inline
                # If in the closed list or not passable then move on
                if found or occupied or unpassable or hill:
                    continue

                    # Make a new node
                g_score = current.g_score + 1
                h_score = self.ants.distance(new_loc, goal)
                f_score = g_score + h_score
                new_node = a_node(new_loc, current, g_score, h_score, f_score)
                state.open_list.append(new_node)
                # self.logger.debug("Add location %s", new_loc)

                # self.logger.debug("No goal")
        return path
Пример #2
0
    def hill_push(self, start, limit):
        # self.logger.debug("Hill push: %s, limit: %d", start, limit)
        goal = (0, 0)
        state = a_state(start, goal, 0, limit)
        path = []

        # Keep going until we run out of an open nodes
        count = 0
        while state.open_list:
            count = count + 1

            # find the lowest g_score node
            current = state.open_list[0]
            for node in state.open_list:
                if current.g_score > node.g_score:
                    current = node

                    # Boolean check
            unoccupied = current.loc not in self.game_state.my_ants
            clear_hills = current.loc not in self.game_state.my_hills

            # Check if we an unoccupied, clear hill node
            if unoccupied and clear_hills and count < state.limit:
                while current != None:
                    path.append(current.loc)
                    current = current.parent
                path.reverse()
                # self.logger.debug("Found the empty spot: %s in list %s", new_loc, path)
                return path

                # Put the current node off the open and on the close list
            state.open_list.remove(current)
            state.closed_list.append(current)

            # Check all directions
            for direction in self.directions:

                # TODO inline destination
                new_loc = self.ants.destination(current.loc, direction)

                # Look for this node in the closed set
                found = False
                for node in state.closed_list:
                    if node.loc == new_loc:
                        found = True
                        break

                for node in state.open_list:
                    if node.loc == new_loc:
                        found = True
                        break

                        # TODO make passable inline
                passable = not self.ants.passable(new_loc)

                # If in the closed/open list or not passable then move on
                if found or passable:
                    continue

                    # Boolean quick checks
                unmovable = node.loc not in self.game_state.movable
                occupied = node.loc in self.game_state.my_ants

                # check if the node is occupied by an unmoved ants
                if occupied and unmovable:
                    continue

                    # Make a new node
                g_score = current.g_score + 1
                new_node = a_node(new_loc, current, g_score, 0, 0)
                state.open_list.append(new_node)

                # self.logger.debug("No goal")
        return path