Ejemplo n.º 1
0
    def add_maze(self, row, col, id=0):
        """Add a maze to the manager. We give the maze an index of
        the total number of mazes in the manager. As long as we don't
        add functionality to delete mazes from the manager, the ids will
        always be unique. Note that the id will always be greater than 0 because
        we add 1 to the length of self.mazes, which is set after the id assignment

        Args:
            row (int): The height of the maze
            col (int): The width of the maze
            id (int):  The optional unique id of the maze.

        Returns
            Maze: The newly created maze
        """

        if id is not 0:
            self.mazes.append(Maze(row, col, id))
        else:
            if len(self.mazes) < 1:
                self.mazes.append(Maze(row, col, 0))
            else:
                self.mazes.append(Maze(row, col, len(self.mazes) + 1))

        return self.mazes[-1]
Ejemplo n.º 2
0
    def add_maze(self,row,col,id=0, show = True, debug=False): # return a optimize graph instead of maze instance
        if id is not 0:
            self.mazes.append(Maze(row, col, id, show, debug))
        else:
            if len(self.mazes) < 1:
                self.mazes.append(Maze(row, col, 0, show, debug))
            else:
                self.mazes.append(Maze(row, col, len(self.mazes) + 1, show, debug))

        graph = Graph(self.mazes[-1],show,debug)
        self.graphs.append(graph)
        return self.graphs[-1]
Ejemplo n.º 3
0
def generate_maze():
    # Used to generate a 5x5 maze for testing, Feel free to modify as needed

    cols = 5
    rows = 5
    cell_size = 1
    return Maze(rows, cols, cell_size)
Ejemplo n.º 4
0
    def test_ctor(self):
        """Make sure that the constructor values are getting properly set."""
        cols = 5
        rows = 5
        maze = Maze(rows, cols)

        self.assertEqual(maze.num_cols, cols)
        self.assertEqual(maze.num_rows, rows)
        self.assertEqual(maze.id, 0)
        self.assertEqual(maze.grid_size, rows * cols)

        id = 33
        maze2 = Maze(rows, cols, id)
        self.assertEqual(maze2.num_cols, cols)
        self.assertEqual(maze2.num_rows, rows)
        self.assertEqual(maze2.id, id)
        self.assertEqual(maze2.grid_size, rows * cols)
Ejemplo n.º 5
0
    def test_get_maze_count(self):
        """Tests the get_maze_number function"""
        manager = MazeManager()

        self.assertEqual(manager.get_maze_count(), 0)
        maze1 = Maze(2, 2)
        manager.add_existing_maze(maze1)
        self.assertEqual(manager.get_maze_count(), 1)
Ejemplo n.º 6
0
def corrmaze(c_len):
    grid = np.zeros((3,c_len+2))
    for x in range(1,c_len+1):
        grid[0][x] = 1
        grid[2][x] = 1
    agents = [Agent("[255,0]",(0,0),(0,0),(c_len+1,0)),Agent("[0,255]",(c_len+1,2),(c_len+1,2),(0,2))]
    m = Maze(grid,agents)
    return m
Ejemplo n.º 7
0
 def __init__(self):
     self.width = 1000
     self.height = 800
     self.player = Player(x=15, y=15, width=10, height=10, speed=1)
     self.game_display = None
     self.tile_size = 25
     self.wall_size = 1
     self.maze = Maze(x=20, y=10, tile_size=self.tile_size,
                      wall_size=self.wall_size)  # , file="..\\image\\maze.png")
Ejemplo n.º 8
0
def add_maze(mazes, weights):
    maze = Maze(rows,
                cols,
                board_width / cols,
                board_height / rows,
                Wilson(screen),
                weights=weights,
                static_locations=staticlocations)
    mazes.append(maze)
Ejemplo n.º 9
0
def initalizemazes(numberofmazes: int, mazes: list[Maze]):
    for i in range(numberofmazes):
        weights = (0.25, 0.25, 0.25, 0.25)
        maze = Maze(rows,
                    cols,
                    board_width / cols,
                    board_height / rows,
                    Wilson(screen),
                    weights=weights,
                    static_locations=staticlocations)
        mazes.append(maze)
Ejemplo n.º 10
0
    def test_add_existing(self):
        """Test adding mazes by passing already existing Maze objects in"""
        manager = MazeManager()

        maze1 = Maze(2, 2)
        self.assertEqual(maze1.id, 0)
        manager.add_existing_maze(maze1)

        self.assertEqual(manager.get_mazes().__len__(), 1)
        self.assertEqual(manager.get_maze_count(), 1)
        self.assertIsNotNone(manager.get_maze(maze1.id))
        self.assertEqual(manager.get_maze(maze1.id).id, maze1.id)

        maze2 = Maze(3, 3, 1)
        self.assertEqual(maze2.id, 1)
        manager.add_existing_maze(maze2)

        self.assertEqual(manager.get_mazes().__len__(), 2)
        self.assertEqual(manager.get_maze_count(), 2)
        self.assertIsNotNone(manager.get_maze(maze2.id))
        self.assertEqual(manager.get_maze(maze2.id).id, maze2.id)
Ejemplo n.º 11
0
    def test_ctor(self):
        """Make sure that the constructor values are getting properly set."""
        cols = 5
        rows = 5
        cell_size = 1
        maze = Maze(rows, cols, cell_size)

        self.assertEqual(maze.num_cols, cols)
        self.assertEqual(maze.num_rows, rows)
        self.assertEqual(maze.cell_size, cell_size)
        self.assertEqual(maze.grid_size, rows * cols)
        self.assertEqual(maze.height, rows * cell_size)
        self.assertEqual(maze.width, cols * cell_size)
Ejemplo n.º 12
0
    def __init__(self, window_height, window_width, button_height, screen):

        # Window parameters
        self.window_height = window_height
        self.window_width = window_width
        self.button_height = button_height
        self.screen = screen
        self.buttons = None
        self.clock = pg.time.Clock()

        # Maze parameters
        self.maze_size = 10
        self.scale = (self.window_width / self.maze_size)
        self.maze = Maze(self.maze_size, self.scale)
        self.maze_list = [self.maze.grid]
        self.build_maze = copy.deepcopy(self.maze.grid)

        # Initialize searcher, generator, learner objects
        self.generator = Generator()  # Used to generate mazes
        self.searcher = Searcher()  # Used to search mazes
        #self.learner = Learner()        # Used to simulate learning on mazes

        # Paths searched
        self.paths = {}
        self.shown_path = {}
        # Number of cells visited in each search
        self.visits = {"BFS": "-", "A*": "-", "DFS": "-"}
        # Length of the path found from the start to the end
        self.lengths = {"BFS": "-", "A*": "-", "DFS": "-"}

        # Reinforcement Learning parameters
        self.num_actions = 4
        self.num_states = self.maze.size * self.maze.size
        self.step_size = 0.5
        self.epsilon = 0.1
        self.discount = 0.9
        self.num_episodes = 50
        self.num_runs = 5
        self.all_states_visited = {}
        self.all_state_visits = {}
        self.all_reward_sums = {}

        # Colors
        self.path_colors = {
            "BFS": RED,
            "A*": ORANGE,
            "DFS": PINK,
            "Explore_BFS": DARK_GREEN,
            "Explore_A*": DARK_ORANGE,
            "Explore_DFS": PURPLE
        }
Ejemplo n.º 13
0
def execute(args):
    while True:
        # Create Maze object
        maze = Maze(args.source)
        # Create Run object
        run = Run(maze, args.pta, args.ptb)
        try:
            # Execute find path command on Run object to find path
            run.find_path()
            break  # Path was found
        except ValueError:
            # There are no valid paths in this maze
            print('Attempt failed, no valid paths')
    return run
Ejemplo n.º 14
0
def main():
    w = 100
    h = 100

    maze = Maze(w, h)
    maze.generate(Point.random(w, h))
    # maze.print()
    maze.save('Maze.png')

    a_star = AStar('Maze.png')
    path = a_star.solve(Point(1, 1), Point(w * 2 - 1, h * 2 - 1))

    # path.print()
    path.save('Maze.png', 'MazePath.png')
Ejemplo n.º 15
0
def rect(size, n_agents, dens):
    valid = 0
    while not valid:
        grid = np.zeros(size)
        locs = list(product(range(size[0]), range(size[1])))

        blocks = floor(size[0] * size[1] * dens)

        for _ in range(blocks):
            r = randint(0, len(locs) - 1)
            grid[locs[r][1]][locs[r][0]] = 1
            del locs[r]

        tm = Maze(grid, [])

        agents = []
        for i in range(n_agents):
            r = randint(0, len(locs) - 1)
            start = locs[r]
            del locs[r]

            end = start
            if tm.reachable_from_pos(end):
                for _ in range(5000):
                    end = choice(tm.reachable_from_pos(end))
                for _ in range(100):
                    if end in locs:
                        break
                    end = choice(tm.reachable_from_pos(end))
                if end not in locs:
                    continue
                locs.remove(end)

            agents.append(Agent(f"[{i * 347 % 256},{i * 9231 % 256}]", start, start, end))
            valid = 1

    return Maze(grid, agents)
Ejemplo n.º 16
0
    def reset_maze(self):
        """This method resets the conditions of the maze and removes all paths, visits and lengths established, as well as replacing all walls.
        """
        self.maze = Maze(self.maze_size, self.scale)
        self.build_maze = copy.deepcopy(self.maze.grid)

        self.buttons["Click"].status = False

        for key, path in self.paths.items():
            path = None
        for key, path in self.shown_path.items():
            path = None
        for key, visit in self.visits.items():
            visit = "-"
        for key, length in self.lengths.items():
            length = "-"
Ejemplo n.º 17
0
    def _process_and_validate_config(self):
        if self.config["maze"] is not None:
            maze = Maze(self.config["maze"])
            self.config["height"] = maze.height
            self.config["width"] = maze.width
            self.config["maze"] = maze
            warnings.warn(
                "Maze associated parameters like height and width were overridden because of custom maze!"
            )

        if self.config["vision_type"] not in ["von_neumann", "box"]:
            raise ValueError("Vision type {} not supported".format(
                self.config["vision_type"]))

        if self.config["vision_type"] == "von_neumann" and self.config[
                "vision_radius"] != 1:
            raise ValueError(
                "Vision radius != 1 not supported in case of Von Neumann Neighborhood"
            )
Ejemplo n.º 18
0
def solver(problem):
    from src.CBS import CBS
    from src.agent import Agent
    from src.maze import Maze

    # print(problem)
    n_agents = len(problem.starts)
    agents = [
        Agent(f"[{x * 347 % 256},{x * 9231 % 256}]", tuple(problem.starts[x]),
              tuple(problem.starts[x]), tuple(problem.goals[x]),
              set(tuple(n) for n in problem.waypoints[x]))
        for x in range(n_agents)
    ]
    maze = Maze(problem.grid, agents)

    paths = CBS(maze, False)
    # create_gif("test4.gif", maze, paths, 20)
    print(paths.sum_of_individual_costs())
    # print([[y[0] for y in x.path] for x in paths])
    return [[y[0] for y in x.path] for x in paths]
Ejemplo n.º 19
0
def branching(width, height, output_file):

    # setup
    start, end, nodes, pixel_map = setup(width, height)

    # Make path
    pixel_map = make_basic_path(nodes, start, pixel_map)

    # Make temporary image
    image = make_image(pixel_map, width, height)

    # Solve maze to find solution path and later remove random wall nodes along the path
    maze = Maze(image)
    (path, considered_nodes, visited_nodes) = solve_maze(maze)

    for node in path:
        if node is not start and node is not end:
            random_value = random.randint(0, 10)
            if random_value is 0:
                x_value = node.position[0]
                y_value = node.position[1]
                random_value = random.randint(0, 3)
                if random_value is 0:  # Break left wall
                    pixel_map[(x_value - 1, y_value)] = 1
                elif random_value is 1:  # Break south wall
                    pixel_map[(x_value, y_value + 1)] = 1
                elif random_value is 2:  # Break east wall
                    pixel_map[(x_value + 1, y_value)] = 1
                elif random_value is 3:  # Break north wall
                    pixel_map[(x_value, y_value - 1)] = 1

    for x in range(width):
        pixel_map[(x, 0)] = 0
        pixel_map[(x, height - 1)] = 0
    for y in range(height):
        pixel_map[(0, y)] = 0
        pixel_map[(width - 1, y)] = 0
    pixel_map[start.position] = 1
    pixel_map[end.position] = 1

    make_image(pixel_map, width, height, output_file=output_file)
Ejemplo n.º 20
0
def solver(problem):
    from src.CBS import CBS
    from src.agent import Agent
    from src.maze import Maze
    # if problem.grid[0] == [1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0]:
    #     print(problem.grid)
    #     import pickle
    #     f = open("sub1","wb")
    #     pickle.dump(problem,f)
    #     f.close()

    # print(problem)
    n_agents = len(problem.starts)
    agents = [
        Agent(f"[{x * 347 % 256},{x * 9231 % 256}]", tuple(problem.starts[x]),
              tuple(problem.starts[x]), tuple(problem.goals[x]),
              set(tuple(n) for n in problem.waypoints[x]))
        for x in range(n_agents)
    ]
    maze = Maze(problem.grid, agents)

    paths = CBS(maze, True)
    # print([[y[0] for y in x.path] for x in paths])
    return [[y[0] for y in x.path] for x in paths]
Ejemplo n.º 21
0
    def handle_event(self, event_key):
        """This method handles processing the event key generated when the user clicked on the GUI. Selecting the appropriate button functionality associated to the event key generated.

        Arguments:
            event_key {string} -- String associated to the button that was pressed in the GUI.
        """

        # When the user clicks on a plus or minus button on the GUI
        if event_key == "stepPlus"     or event_key == "discountPlus"  or event_key == "epsilonPlus" or \
           event_key == "plusEpisode"  or event_key == "plusRun"       or event_key == "Plus" or \
           event_key == "stepMinus"    or event_key == "discountMinus" or event_key == "epsilonMinus" or \
           event_key == "minusEpisode" or event_key == "minusRun"      or event_key == "Minus" \
           and self.buttons[event_key].pressed == 1:

            self.buttons[event_key].color = LIGHT_BLUE
            self.draw_window()
            if event_key == "stepPlus" and self.step_size < 1.0:
                self.step_size += 0.01
            elif event_key == "stepMinus" and self.step_size > 0.01:
                self.step_size -= 0.01
            elif event_key == "discountPlus" and self.discount < 1.0:
                self.discount += 0.01
            elif event_key == "discountMinus" and self.discount > 0.01:
                self.discount -= 0.01
            elif event_key == "epsilonPlus" and self.epsilon < 1.0:
                self.epsilon += 0.01
            elif event_key == "epsilonMinus" and self.epsilon > 0.01:
                self.epsilon -= 0.01
            elif event_key == "plusEpisode" and self.num_episodes < 200:
                self.num_episodes += 5
            elif event_key == "minusEpisode" and self.num_episodes > 5:
                self.num_episodes -= 5
            elif event_key == "plusRun" and self.num_runs < 50:
                self.num_runs += 1
            elif event_key == "minusRun" and self.num_runs > 1:
                self.num_runs -= 1
            elif event_key == "Plus" and self.maze_size < 60:
                self.maze_size += 2
                self.scale = self.window_width / self.maze_size
                self.maze = Maze(self.maze_size, self.scale)
                self.build_maze = copy.deepcopy(self.maze.grid)
            elif event_key == "Minus" and self.maze_size > 2:
                self.maze_size -= 2
                self.scale = self.window_width / self.maze_size
                self.maze = Maze(self.maze_size, self.scale)
                self.build_maze = copy.deepcopy(self.maze.grid)
            self.buttons[event_key].color = GREY
            self.buttons[event_key].pressed = 0

        # When the user clicks the Reset button, the maze will be reset to the original conditions
        if self.buttons["Reset"].pressed == 1:
            self.maze = Maze(self.maze_size, self.scale)
            self.build_maze = copy.deepcopy(self.maze.grid)

            self.reset_maze()
            # Reset the status of each button in the GUI
            for key, button in self.buttons.items():
                if key != "Reset":
                    button.pressed = 0
                    button.color = GREY
                    button.status = False
                else:
                    button.pressed = 0

        # When the user clicks a maze generating button
        if event_key == "genDFS" or event_key == "Prims" or event_key == "Recursive":
            if self.buttons[event_key].pressed == 1:
                self.buttons[event_key].color = LIGHT_BLUE

                # Clear the maze walls to show the recursive process
                if event_key == "Recursive":
                    self.clear_maze()
                # Send the current grid to the generator
                self.generator.get_grid(self.maze.grid)

                # Pass the event key to the generator and run the selected option
                try:
                    maze_list = self.generator.run_generator(event_key)
                    self.maze.grid = copy.deepcopy(self.generator.temp_maze)
                except Exception as e:
                    print(e)

                # Update the start and end cells for the updated maze created
                self.maze.update_start_end()
                # If the maze is recursive it requires having walls set and removed
                if self.buttons["Build"].status and event_key == "Recursive":
                    for temp_current_cell, temp_next_cell, status in maze_list:
                        current_cell = self.build_maze[temp_current_cell.x][
                            temp_current_cell.y]
                        next_cell = self.build_maze[temp_next_cell.x][
                            temp_next_cell.y]
                        if status == "wall":
                            current_cell.set_wall(next_cell)
                            next_cell.set_wall(current_cell)
                        else:
                            current_cell.set_path(next_cell)
                            next_cell.set_path(current_cell)
                        self.draw_window()
                # DFS and Prims only require having walls removed
                elif self.buttons["Build"].status:
                    self.iterate_build_maze(maze_list)
                # Set the event button to an on state
                self.buttons[event_key].pressed = 2
            # When the user clicks the button to turn it off
            elif self.buttons[event_key].pressed == 3:
                self.buttons[event_key].color = GREY
                self.buttons[event_key].pressed = 0
                self.reset_maze()
        # When the user clicks the "S" or "E" buttons, to select a new start point or end point
        if event_key == "Start" or event_key == "End" and self.buttons[
                event_key].pressed == 1:
            self.buttons[event_key].color = LIGHT_BLUE
            running = True
            while running:
                self.draw_window()
                event = pg.event.poll()
                if event.type == pg.QUIT:
                    sys.exit()
                elif event.type == pg.MOUSEBUTTONUP:

                    for row in self.maze.grid:
                        for cell in row:
                            if cell.select(event.pos):
                                if event_key == "Start":
                                    self.maze.change_start(cell)
                                elif event_key == "End":
                                    self.maze.change_end(cell)
                                running = False
            self.buttons[event_key].color = GREY
            self.buttons[event_key].pressed = 0

        # When the user selects the option to show maze exploration, the cells visited in the search process
        if self.buttons["Explore"].pressed == 1:
            self.buttons["Explore"].color = LIGHT_BLUE
        else:
            self.buttons["Explore"].color = GREY
            self.buttons["Explore"].pressed = 0

        # When the "Click" button is not pressed
        if self.buttons["Click"].pressed == 0:
            # If the "Build" button is pressed to illuminate
            if self.buttons["Build"].pressed == 1:
                self.buttons["Build"].color = LIGHT_BLUE
                self.buttons["Build"].status = True
            # If the "Build" button is pressed again to turn off
            elif self.buttons["Build"].pressed == 2:
                self.buttons["Build"].status = False
                self.buttons["Build"].pressed = 0
                self.buttons["Build"].color = GREY

        # When the "Click" button is pressed
        elif self.buttons["Click"].pressed == 1:
            self.buttons["Click"].color = LIGHT_BLUE
            self.clear_maze()  # Clear the walls from the maze

            # Allow the user to continue to select cells to build a wall around
            while True:
                self.draw_window()
                event = pg.event.poll()
                if event.type == pg.QUIT:
                    sys.exit()
                # When an mousebutton event has been registered
                elif event.type == pg.MOUSEBUTTONUP:
                    # When the click button is pressed
                    if self.buttons["Click"].select(event.pos):
                        break
                    # Check if a cell has been selected
                    for row in self.maze.grid:
                        for cell in row:
                            if cell.select(event.pos):
                                cell.changeBackground()

            # Turn off the "Click" button
            self.buttons["Click"].color = GREY
            self.buttons["Click"].pressed = 0
            self.buttons["Click"].status = True

        # When the user clicks on the Show Run button to show the process of the agent running through the grid
        if self.buttons["Show Run"].pressed == 1 and (
                self.buttons["Q-Learning"].pressed == 2
                or self.buttons["Expected SARSA"].pressed == 2):
            self.buttons["Show Run"].color = LIGHT_BLUE
            self.buttons["Show Run"].status = True

            if self.buttons["Q-Learning"].pressed == 2:
                runs = self.all_states_visited["Q-Learning"]
            else:
                runs = self.all_states_visited["Expected SARSA"]

            # Set the mid point of the runs performed by the agent
            #if len(runs) > 2:
            #    mid = len(runs)//2
            #else:
            #    mid = -1

            count = 0
            # Go through each run and display the first set of episodes, the middle set of episodes and the final set of episodes.
            run = runs[-1]
            #for run in runs:
            for episode in run:
                #if count == 0 or count == len(runs)-1 or count == mid:
                for self.state in episode:
                    self.draw_window()
            count += 1
            self.buttons["Show Run"].pressed = 2
        # Reset the Show Run button
        elif self.buttons["Show Run"].pressed == 3:
            self.buttons["Show Run"].color = GREY
            self.buttons["Show Run"].pressed = 0
            self.buttons["Show Run"].status = False

        # When the user clicks the Heat Map button, show the heat map of the average visits made by the agent on each state.
        if self.buttons["Heat Map"].pressed == 1 and (
                self.buttons["Q-Learning"].pressed == 2
                or self.buttons["Expected SARSA"].pressed == 2):
            self.buttons["Heat Map"].color = LIGHT_BLUE

            if self.buttons["Q-Learning"].pressed == 2:
                average_state_visits = np.array(
                    self.all_state_visits["Q-Learning"]).mean(axis=0)
            else:
                average_state_visits = np.array(
                    self.all_state_visits["Expected SARSA"]).mean(axis=0)

            max_visit = np.max(
                average_state_visits[np.nonzero(average_state_visits)])
            min_visit = np.min(
                average_state_visits[np.nonzero(average_state_visits)])

            visit_range = max_visit - min_visit

            for state, visits in enumerate(average_state_visits):
                state_x, state_y = divmod(state, self.maze_size)
                cell = self.maze.grid[state_x][state_y]
                if cell.background != BLACK and not cell.isStart and not cell.isEnd:
                    if visits != 0:
                        cell.background = (
                            255 - ((visits - min_visit) / visit_range) * 255,
                            255,
                            255 - ((visits - min_visit) / visit_range) * 255)
                    else:
                        cell.background = WHITE

            self.buttons["Heat Map"].pressed = 2

        # Reset the Heat Map button
        elif self.buttons["Heat Map"].pressed == 3:
            self.buttons["Heat Map"].color = GREY
            for row in self.maze.grid:
                for cell in row:
                    if cell.background != BLACK and not cell.isStart and not cell.isEnd:
                        cell.background = WHITE
            self.buttons["Heat Map"].pressed = 0

        # The user cannot select a search algorithm without first generating a maze or grid
        if self.buttons["genDFS"].pressed == 2 or self.buttons["Prims"].pressed == 2 or \
           self.buttons["Recursive"].pressed == 2 or self.buttons["Click"].status == True:
            # When the user clicks on a search algorithm button
            if event_key == "BFS" or event_key == "A*" or event_key == "DFS":
                if self.buttons[event_key].pressed == 1:
                    self.buttons[event_key].color = LIGHT_BLUE
                    # Pass the locations of the start and end points to the searcher
                    self.searcher.get_grid(self.maze.start, self.maze.end)
                    # From the provided event key determine which search method to use
                    if event_key == "BFS":
                        self.searcher.run_BFS()
                    elif event_key == "A*":
                        self.searcher.run_Astar()
                    elif event_key == "DFS":
                        self.searcher.run_DFS()
                    # Acquire the output information found by the search algorithm
                    self.paths[event_key] = self.searcher.paths[event_key]
                    self.visits[event_key] = self.searcher.visits[event_key]
                    self.lengths[event_key] = self.searcher.lengths[event_key]

                    # If the user chose to display the exploration process add the paths visited to be shown
                    if self.buttons["Explore"].pressed == 1:
                        self.buttons[event_key].color = LIGHT_BLUE
                        self.shown_path["Explore_" + event_key] = []

                        # Iterate through each cell as it is visited
                        for current in self.visits[event_key]:
                            self.shown_path["Explore_" +
                                            event_key].append(current)
                            self.draw_window()
                    self.buttons[event_key].pressed = 2
                # When the user turns off the search algorithm reset the information for that algorithm
                elif self.buttons[event_key].pressed == 3:
                    self.buttons[event_key].color = GREY
                    self.buttons[event_key].pressed = 0
                    self.shown_path["Explore_" + event_key] = None
                    self.paths[event_key] = None
                    self.visits[event_key] = "-"
                    self.lengths[event_key] = "-"
            # When the user clicks on a reinforcement learning button
            if event_key == "Q-Learning" or event_key == "Expected SARSA" and self.buttons[
                    event_key].pressed == 1:
                self.buttons[event_key].color = LIGHT_BLUE
                if event_key == "Q-Learning" and self.buttons[
                        "Expected SARSA"].pressed == 2:
                    self.buttons["Expected SARSA"].color = GREY
                    self.buttons["Expected SARSA"].pressed = 0
                    self.all_states_visited["Expected SARSA"] = []
                    self.all_state_visits["Expected SARSA"] = []
                    self.all_reward_sums["Expected SARSA"] = []
                elif event_key == "Expected SARSA" and self.buttons[
                        "Q-Learning"].pressed == 2:
                    self.buttons["Q-Learning"].color = GREY
                    self.buttons["Q-Learning"].pressed = 0
                    self.all_states_visited["Q-Learning"] = []
                    self.all_state_visits["Q-Learning"] = []
                    self.all_reward_sums["Q-Learning"] = []
                self.num_states = self.maze.size * self.maze.size
                env = MazeEnv(self.maze)
                agents = {
                    "Q-Learning": QLearningAgent,
                    "Expected SARSA": ExpectedSarsaAgent
                }

                self.all_states_visited[event_key] = []
                self.all_state_visits[event_key] = []
                self.all_reward_sums[event_key] = []

                # Perform each run of the specified set of runs
                for run in tqdm(range(self.num_runs)):

                    agent_info = {
                        "num_actions": self.num_actions,
                        "num_states": self.num_states,
                        "epsilon": self.epsilon,
                        "step_size": self.step_size,
                        "discount": self.discount,
                        "seed": run
                    }

                    interactor = Interactor(env, agents[event_key], agent_info)

                    reward_sums = []
                    visited_list = []
                    state_visits = np.zeros(self.num_states)

                    # Perform each episode of the specified number of episodes
                    for episode in range(self.num_episodes):
                        state, action = interactor.start()
                        state_visits[state] += 1
                        isTerminal = False

                        states_visited = []

                        state_x, state_y = divmod(state, self.maze_size)

                        states_visited.append((state_x, state_y))

                        step_num = 0
                        # Continue performing steps in the episode until the agent reaches the terminal state.
                        while not isTerminal:
                            reward, state, action, isTerminal = interactor.step(
                            )

                            state_visits[state] += 1

                            state_x, state_y = divmod(state, self.maze_size)

                            states_visited.append((state_x, state_y))
                            step_num += 1

                        visited_list.append(states_visited)
                        reward_sums.append(interactor.get_total_reward())

                    # Append the collected data to the outcome lists to be used in displaying the outcome of the learning simulation
                    self.all_states_visited[event_key].append(visited_list)
                    self.all_state_visits[event_key].append(state_visits)
                    self.all_reward_sums[event_key].append(reward_sums)

                self.buttons[event_key].pressed = 2

            # Reset the reinforcement learning button
            elif event_key == "Q-Learning" or event_key == "Expected SARSA" and self.buttons[
                    event_key].pressed == 3:
                self.buttons[event_key].color = GREY
                self.buttons[event_key].pressed = 0
                self.all_states_visited[event_key] = []
                self.all_state_visits[event_key] = []
                self.all_reward_sums[event_key] = []
Ejemplo n.º 22
0
data = {
	"algorithm": "CBS",
	"version": "stable"
}
r = requests.post("https://mapfw.nl/api/benchmarks/4/problems",headers=headers,json=data)
# r = requests.post("http://127.0.0.1:5000/api/benchmarks/6/problems",headers=headers,json=data)

n = r.json()
problem = json.loads(r.json()["problems"][0]["problem"])
at_id = r.json()["attempt"]
# print(problem)

n_agents = len(problem["starts"])
agents = [Agent(f"[{x*347%256},{x*9231%256}]",tuple(problem["starts"][x]),tuple(problem["starts"][x]),tuple(problem["goals"][x]),set(tuple(n) for n in problem["waypoints"][x])) for x in range(n_agents)]
maze = Maze(problem["grid"],agents)


t = time()
paths = CBS(maze)
t = time()-t
create_gif("test3.gif", maze, paths, 20)
ans = {
	"solutions": [
		{
			"problem": r.json()["problems"][0]["id"],
			"time": round(t*1000),
			"solution": [[y[0] for y in x.path] for x in paths]
		}
	]
}
Ejemplo n.º 23
0
if __name__ == "__main__":

    # The easiest way to use the library is through the Manager class. It acts as the glue between
    # The visualization, solver, and maze classes. Mazes inside the manager have unique ids that we use
    # to specify particular mazes.
    manager = MazeManager()

    # We can add mazes to the manager two different ways.
    # The first way, we specify the maze dimensions. The maze that is created gets returned back to you.
    maze = manager.add_maze(10, 10)

    # The second way is by creating a maze, and then adding it to the manager. Doing this will require you to add
    # from src.maze import Maze
    # to your imports. Because the ids need to be unique, the manager will ensure this happens. It may change the
    # id of the maze that was passed in, so we assign it to the return value to make sure we're using the updated maze.
    maze2 = Maze(10, 10)
    maze2 = manager.add_existing_maze(maze2)

    # by default when creating a maze, depth first search is used.
    # to generate maze using binary tree method,
    maze_binTree = Maze(10, 10, algorithm = "bin_tree")
    maze_binTree = manager.add_existing_maze(maze_binTree)

    # We can disable showing any output from the solver by entering quiet mode
    # manager.set_quiet_mode(True)

    # Once we have a maze in the manager, we can tell the manager to solve it with a particular algorithm.
    #manager.solve_maze(maze.id, "BreadthFirst")
    #manager.solve_maze(maze.id, "BiDirectional")
    manager.solve_maze(maze.id, "DepthFirstBacktracker")
Ejemplo n.º 24
0
import json
import argparse
from src.maze import Maze


def instanciate_from_string(module_path):
    path_list = module_path.split(".")
    class_name = path_list[-1]
    directory_of_module = ".".join(path_list[:-1])
    module = __import__(directory_of_module, fromlist=[class_name])
    return getattr(module, class_name)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Train agent to navigate a maze specified in the instruction file.')
    parser.add_argument('--instruction', metavar='I', type=str,
                        help='path to instruction file', default="../instructions/prim_maze.json")

    args = parser.parse_args()
    # Load instruction file:
    with open(args.instruction, "r") as file:
        instruction = json.load(file)

    obstacles = instanciate_from_string(instruction["obstacles"]["class"])(**instruction["obstacles"]["kwargs"])
    maze = Maze(obstacles=obstacles)
    learner = instanciate_from_string(instruction["learner"]["class"])(maze, **instruction["learner"]["kwargs"])
    learner.learn(**instruction["learner__learn_kwargs"])
Ejemplo n.º 25
0
 def test_find_neighbors(self):
     maze = Maze(2, 2)
     neighbors = maze.find_neighbours(0, 1)
     self.assertIsNotNone(neighbors)
from __future__ import absolute_import
from matplotlib import pyplot as plt
from src.maze import Maze
from src.maze_viz import plot_maze, animate_maze_generate, plot_maze_solution, animate_maze_solve

if __name__ == "__main__":
    maze_generator = Maze(10, 10, 1)
    grid, entry, exit, path_gen = maze_generator.generate_maze((0, 0))
    path_solve = maze_generator.solve_bfs(grid, entry, exit)

    plot_maze(maze_generator, grid)
    plot_maze_solution(maze_generator, grid, path_solve)
    anim_generate = animate_maze_generate(maze_generator, path_gen)
    anim_solve = animate_maze_solve(maze_generator, grid, path_solve)

plt.show()
Ejemplo n.º 27
0
        ]
        self.attempt_id = r.json()["attempt"]

        self.status = {
            "state": "RUNNING",
            "data": {
                "problem_states": [0 for _ in self.problems]
            }
        }


if __name__ == '__main__':
    benchmark = MapfwBenchmarker("13", 4, "test", "api")
    benchmark.load()

    for problem in benchmark:
        print(benchmark.problems)
        n_agents = len(problem.starts)
        agents = [
            Agent(f"[{x * 347 % 256},{x * 9231 % 256}]",
                  tuple(problem.starts[x]), tuple(problem.starts[x]),
                  tuple(problem.goals[x]),
                  set(tuple(n) for n in problem.waypoints[x]))
            for x in range(n_agents)
        ]
        maze = Maze(problem.grid, agents)

        paths = CBS(maze)

        problem.add_solution([[y[0] for y in x.path] for x in paths])
Ejemplo n.º 28
0
def main(text_area: Text, input_file: str, output_file: str,
         save_visited_nodes: bool, show_path: bool):
    work_state = int()
    try:
        work_state = 0
        insert_text(text_area, "\nOpening image...")
        image = Image.open(input_file)

        work_state = 1
        insert_text(text_area, "\nAnalyzing image and creating nodes...")
        start_time = time.time()
        maze = Maze(image)
        end_time = time.time()
        total_time = end_time - start_time
        insert_text(text_area, "\nMaze creation time: {}".format(total_time))
        insert_text(text_area, "\nNumber of nodes: {}".format(len(maze.nodes)))

        work_state = 2
        insert_text(text_area, "\nSolving...")
        start_time = time.time()
        (path, considered_nodes, visited_nodes) = solve_maze(maze)
        end_time = time.time()
        total_time = end_time - start_time

        work_state = 3
        if show_path:
            insert_text(text_area, "\n\nPath:")
            for node in path:
                insert_text(text_area, "\n{}".format(node.position))

        insert_text(text_area, "\nSolve time: {}".format(total_time))
        insert_text(text_area,
                    "\n\nVisited nodes: {}".format(len(visited_nodes)))
        insert_text(text_area,
                    "\nConsidered nodes: {}".format(considered_nodes))
        insert_text(
            text_area,
            "\nPath pixel length: {}".format(int(maze.end.distance_to_start)))
        insert_text(text_area, "\nPath node length: {}".format(len(path)))
        insert_text(text_area, "\nDTE Weight: {}".format(Node.weight))

        work_state = 4
        if save_visited_nodes:
            image = paint_nodes(image, visited_nodes)

        work_state = 5
        save_image(text_area, image, path, output_file)

    except AttributeError:
        if work_state == 1:
            insert_text(
                text_area,
                "\nSomething went wrong when loading the maze to memory."
                "\n   Please make sure maze is of the proper format")
        elif input_file == "":
            insert_text(
                text_area,
                "\nPlease specify input image before attempting solve")
        else:
            insert_text(
                text_area,
                "\nAn AttributeError has been raised unexpectedly... please contact support"
                "\n   work_state = " + str(work_state))

    except FileNotFoundError:
        if work_state == 0:
            insert_text(
                text_area, "\nInput image was not found."
                "\n   Please make sure you have specified the right folder and file"
            )
        elif work_state == 5:
            insert_text(
                text_area, "\nOutput folder or file was not found"
                "\n   Please make sure you have specified a valid folder")
        else:
            insert_text(
                text_area,
                "\nA FileNotFoundError was raised unexpectedly... please contact support"
                "\n   work_state = " + str(work_state))

    except IndexError:
        if work_state == 2:
            insert_text(text_area, "\nMaze has no solution")
        else:
            insert_text(
                text_area,
                "\nAn unexpected IndexError was raised... please contact support"
                "\n   work_state = " + str(work_state))
Ejemplo n.º 29
0
from __future__ import absolute_import
from src.maze_manager import MazeManager
from src.maze import Maze


if __name__ == "__main__":

    # create a maze manager to handle all operations
    manager = MazeManager()

    # now create a maze using the binary tree method
    maze_using_btree = Maze(10, 10, algorithm="bin_tree")

    # add this maze to the maze manager
    maze_using_btree = manager.add_existing_maze(maze_using_btree)

    # show the maze
    manager.show_maze(maze_using_btree.id)

    # show how the maze was generated
    manager.show_generation_animation(maze_using_btree.id)
Ejemplo n.º 30
0
def create_maze(algorithm):
    rows, cols = (5,5)
    return Maze(rows, cols, algorithm = algorithm)