Exemple #1
0
    def __getitem__(self, node):
        state_hash = self._get_hash(node)

        if state_hash in self.nodes:
            if node.state.player is None: return (0, self.nodes[state_hash][0])

            found = (None, None)

            for similar in self.nodes[state_hash]:
                if similar.state.player is None: return (0, similar)

                try:
                    grid = node.state.instance.get_grid_from_state(
                        similar.state)
                    path_search = GridSearchInstance(grid,
                                                     similar.state.player)
                    path_search_start = GridSearchState(
                        path_search, *node.state.player)

                    path = search.grid_search(
                        path_search, path_search_start,
                        search.AStarFringe(
                            ManhattanDistanceHeuristic(similar.state.player)))

                    if not found[1] or similar.cost + path.info[
                            "cost"] < found[1].cost:
                        found = (path.info["cost"], similar)
                except search.SolutionNotFoundError:
                    continue

            if found[1]: return found

        raise IndexError()
Exemple #2
0
    def _dist(self, first, second):
        try:
            grid = self.instance.empty_grid
            path_search = GridSearchInstance(grid, second)
            path_search_start = GridSearchState(path_search, *first)

            path = search.grid_search(
                path_search, path_search_start,
                search.AStarFringe(ManhattanDistanceHeuristic(second)))

            return path.info["cost"]
        except search.SolutionNotFoundError:
            return float("inf")
Exemple #3
0
    def generate_neighbors(self, state):
        neighbors = []

        current_grid = self.get_grid_from_state(state)
        available_boxes = self.boxes_to_consider.get(self.last_state, state,
                                                     current_grid)

        for x, y in available_boxes:
            for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                old_player_pos = (x - dx, y - dy)
                new_player_pos = (x, y)
                box_pos = (x + dx, y + dy)

                player_blocked = self._is_blocked(current_grid,
                                                  *old_player_pos)
                box_blocked = self._is_blocked(current_grid, *box_pos)

                if player_blocked or box_blocked:
                    continue

                new_boxes = [(x_j, y_j) if (x_j, y_j) != (x, y) else
                             (x_j + dx, y_j + dy) for x_j, y_j in state.boxes]

                neighbor = GameState(self, new_boxes, new_player_pos)

                cost = 1

                if state.player and state.player != old_player_pos:
                    neighbor_grid = self.get_grid_from_state(state)
                    path_search = GridSearchInstance(neighbor_grid,
                                                     state.player)
                    path_search_start = GridSearchState(
                        path_search, *old_player_pos)

                    try:
                        path = search.grid_search(
                            path_search, path_search_start,
                            search.AStarFringe(
                                ManhattanDistanceHeuristic(state.player)))
                        cost = path.info["cost"] + 1
                    except search.SolutionNotFoundError:
                        continue

                try:
                    neighbor = self.states[neighbor]
                except:
                    self.states[neighbor] = neighbor

                neighbors.append((neighbor, cost))

        return neighbors
Exemple #4
0
    def dist(self, first, second):
        if first.player is None or second.player is None:
            return 0

        try:
            grid = first.instance.get_grid_from_state(first)
            path_search = GridSearchInstance(grid, second.player)
            path_search_start = GridSearchState(path_search, *first.player)

            path = search.grid_search(
                path_search, path_search_start,
                search.AStarFringe(ManhattanDistanceHeuristic(second.player)))

            return path.info["cost"]
        except search.SolutionNotFoundError:
            return float("inf")
Exemple #5
0
def solve_uni(instance, search_strategy):
    solution = search.search(instance, instance.start, search_strategy, True)

    for i in range(0, len(solution)):
        print(solution[i].state, solution[i].cost)

    current_grid = instance.get_grid_from_state(instance.goal)

    path_search = GridSearchInstance(current_grid, solution[-1].state.player)
    path_search_start = GridSearchState(path_search, *instance.goal.player)

    path_to_start = search.grid_search(
        path_search, path_search_start,
        search.AStarFringe(
            ManhattanDistanceHeuristic(solution[-1].state.player)))

    solution.info["cost"] += path_to_start.info["cost"]

    print(instance.goal, solution.info["cost"])

    return solution
Exemple #6
0
    def __eq__(self, other):
        for box in self.boxes:
            if not box in other.boxes:
                return False

        if self.player and other.player:
            current_grid = self.instance.get_grid_from_state(self)

            path_search = GridSearchInstance(current_grid, self.player)
            path_search_start = GridSearchState(path_search, other.player[0],
                                                other.player[1])

            try:
                search.grid_search(
                    path_search, path_search_start,
                    search.AStarFringe(ManhattanDistanceHeuristic(
                        self.player)))
            except search.SolutionNotFoundError:
                return False

        return True
Exemple #7
0
    def add(self, node):
        state_hash = self._get_hash(node)

        if state_hash in self.nodes and node.state.player is not None:
            for similar in self.nodes[state_hash]:
                if similar.state.player is None: return None

                try:
                    grid = node.state.instance.get_grid_from_state(
                        similar.state)
                    path_search = GridSearchInstance(grid,
                                                     similar.state.player)
                    path_search_start = GridSearchState(
                        path_search, *node.state.player)

                    path = search.grid_search(
                        path_search, path_search_start,
                        search.AStarFringe(
                            ManhattanDistanceHeuristic(similar.state.player)))

                except search.SolutionNotFoundError:
                    continue

        return self.nodes[state_hash].append(node)