예제 #1
0
def multi_astar(filename):
    maze, start, goals = file_read(filename)
    graph = state_representation(maze)
    mouse = Mouse(start)

    came_from = {start: (None, copy.deepcopy(goals))}
    goals_obtained = {start: (None, [])}

    open_list = [(start, 0, gs_heuristic(start, came_from[start][1]))]
    closed_list = []
    count = 0
    while open_list:
        position = min(open_list, key=lambda t: t[1] + t[2])[0]
        position_cost = min(open_list, key=lambda t: t[1] + t[2])[1]
        count += 1

        if position in came_from[position][1]:
            came_from[position][1].remove(position)
            goals_obtained[position][1].append(position)

            if len(came_from[position][1]) == 0:
                if len(goals_obtained[position][1]) < 10:
                    for i in range(len(maze)):
                        for j in range(len(maze[i])):
                            for k in range(len(goals_obtained[position][1])):
                                if goals_obtained[position][1][k] == (i, j):
                                    maze[i][j] = str(k + 1)
                else:
                    for i in range(len(maze)):
                        for j in range(len(maze[i])):
                            for k in range(len(goals_obtained[position][1])):
                                if goals_obtained[position][1][k] == (i, j):
                                    if k < 10:
                                        maze[i][j] = str(k)
                                    elif k >= 10:
                                        maze[i][j] = chr(k + 87)
                                    elif k >= 36:
                                        maze[i][j] = chr(k + 29)
                for i in maze:
                    print(''.join(i))
                print("Total cost of the path: ", position_cost)
                print("Total number of nodes explored: ", count)

                mouse.position = position
                mouse.goals_obtained = goals_obtained[position][1]
                return mouse

        open_list = [(node, g, h) for (node, g, h) in open_list
                     if node != position]
        closed_list.append((position, came_from[position][1]))

        for i in graph.neighbors(position):

            if (i, came_from[position][1]) in closed_list:
                continue

            for j in open_list:
                new_g = position_cost + 1
                if len(came_from[j[0]][1]) > len(
                        came_from[position][1]) and j[1] < new_g:
                    open_list.remove(j)
                    closed_list.append((j[0], came_from[j[0]][1]))
                    continue
            else:
                g_val = position_cost + 1
                h_val = gs_heuristic(position, came_from[position][1])
                came_from[i] = (position,
                                copy.deepcopy(came_from[position][1]))
                goals_obtained[i] = (position,
                                     copy.deepcopy(
                                         goals_obtained[position][1]))
                open_list.append((i, g_val, h_val))