def test_flip_maze_twice(width, height): maze = generate_random_maze(width, height, 0.1) maze_flipped = flip_maze(flip_maze(maze)) assert maze._horizontal_walls == maze_flipped._horizontal_walls assert maze_flipped._vertical_walls == maze._vertical_walls
def test_rotate_maze_clockwise_4times(width, height): maze = generate_random_maze(width, height, 0.1) maze_rotated = rotate_maze_clockwise(maze) maze_rotated = rotate_maze_clockwise(maze_rotated) maze_rotated = rotate_maze_clockwise(maze_rotated) maze_rotated = rotate_maze_clockwise(maze_rotated) assert maze_rotated._horizontal_walls == maze._horizontal_walls assert maze_rotated._vertical_walls == maze._vertical_walls
def test_flip_maze(width, height): maze = generate_random_maze(width, height, 0.1) maze_flipped = flip_maze(maze) assert maze._horizontal_walls == [ walls[::-1] for walls in maze_flipped._horizontal_walls ] assert maze_flipped._vertical_walls == [ walls[::-1] for walls in maze._vertical_walls ]
def test_rotate_observations(width, height): maze = generate_random_maze(width, height, 0.1) agent = DummyAgent() Game(maze, agent, [(width // 2, height // 2)], (0, 0)).play(30) maze_rotated = rotate_maze_clockwise(maze) agent2 = DummyAgent(list(map(data_utils.rotate_direction_id, agent.move_dir_ids))) Game( maze_rotated, agent2, [(height - height // 2 - 1, width // 2)], (width - 1, 0) ).play(30) assert agent2.observations == data_utils.rotate_observations(agent.observations)
def test_rotate_observations_4times(width, height): maze = generate_random_maze(width, height, 0.1) agent = DummyAgent() Game(maze, agent, [(width // 2, height // 2)], (0, 0)).play(30) rotated_dir_ids = agent.move_dir_ids rotated_observations = agent.observations for _ in range(4): rotated_dir_ids = list(map(data_utils.rotate_direction_id, rotated_dir_ids)) rotated_observations = data_utils.rotate_observations(rotated_observations) assert agent.move_dir_ids == rotated_dir_ids assert agent.observations == rotated_observations
def test_flip_observations_twice(width, height): maze = generate_random_maze(width, height, 0.1) agent = DummyAgent() Game(maze, agent, [(width // 2, height // 2)], (0, 0)).play(30) assert agent.move_dir_ids == list( map( data_utils.flip_direction_id, map(data_utils.flip_direction_id, agent.move_dir_ids), ) ) assert agent.observations == data_utils.flip_observations( data_utils.flip_observations(agent.observations) )
def test_rotate_maze_clockwise(width, height): maze = generate_random_maze(width, height, 0.1) maze_rotated = rotate_maze_clockwise(maze) def rotate_2d_list(list2d): transposed = [[] for _ in range(len(list2d[0]))] for row in list2d[::-1]: for i, x in enumerate(row): transposed[i].append(x) return transposed assert maze_rotated._horizontal_walls == rotate_2d_list( maze._vertical_walls) assert maze_rotated._vertical_walls == rotate_2d_list( maze._horizontal_walls)
def main(): cli = argparse.ArgumentParser() cli.add_argument("model_dir") cli.add_argument("--size", type=int) cli.add_argument("--n-agents", type=int) cli.add_argument("--n-games", type=int, default=10000) cli.add_argument("--max-game-len", type=int, default=100) cli.add_argument("--out") opts = cli.parse_args() model_dir = opts.model_dir if opts.out is None: opts.out = os.path.join(model_dir, "test-results.txt") with open(os.path.join(model_dir, "config.yml")) as f: cfg = yaml.safe_load(f) cfg = dacite.from_dict(config_lib.ModelConfig, cfg) if opts.size is None: opts.size = cfg.maze_size if opts.n_agents is None: opts.n_agents = cfg.model.max_agents session = tf.compat.v1.Session() model = agent.AgentModel(cfg.model) model_instance = model.build_layers() agent_fn = model.create_agent_fn(model_instance, session, True) model_instance.load_variables(session, opts.model_dir) results = [] try: for _ in tqdm.tqdm(range(opts.n_games), desc=f"testing: {model_dir}"): maze = maze_lib.generate_random_maze(opts.size, opts.size, 0.1) game = game_lib.Game.generate_random(maze, agent_fn, opts.n_agents) agent_fn.init_before_game(opts.n_agents) replays = game.play(opts.max_game_len or maze.height * maze.width * 2) results.append(list(map(len, replays))) except KeyboardInterrupt: return np.savetxt(opts.out, np.array(results), fmt="%d")
def main(): cli = argparse.ArgumentParser() cli.add_argument("model_dir") cli.add_argument("--size", type=int) cli.add_argument("--n-agents", type=int) opts = cli.parse_args() model_dir = opts.model_dir with open(os.path.join(model_dir, "config.yml")) as f: cfg = yaml.safe_load(f) cfg = dacite.from_dict(config_lib.ModelConfig, cfg) if opts.size is None: opts.size = cfg.maze_size if opts.n_agents is None: opts.n_agents = cfg.model.max_agents session = tf.compat.v1.Session() model = agent.AgentModel(cfg.model) model_instance = model.build_layers() agent_fn = model.create_agent_fn(model_instance, session, True) model_instance.load_variables(session, opts.model_dir) visualizer = Visualizer( cell_size_px=100, sec_per_turn=0.5, move_animation_sec=0.4, autoplay=True, autoend=False, ) try: while True: maze = maze_lib.generate_random_maze(opts.size, opts.size, 0.1) game = game_lib.Game.generate_random(maze, agent_fn, opts.n_agents) agent_fn.init_before_game(opts.n_agents) replays = game.play(maze.height * maze.width * 2) visualizer.run(game, replays) except KeyboardInterrupt: return
def maze_generator_loop(mazes_queue, maze_size, should_stop, initial_step, end_step, n_workers): i = initial_step while not should_stop(): i += 1 if end_step is not None and i > end_step: break maze = generate_random_maze(*maze_size, 0.01) while not should_stop(): try: mazes_queue.put((i, maze), timeout=1) except Full: pass else: break if should_stop(): try: while True: mazes_queue.get_nowait() except Empty: pass for _ in range(n_workers): mazes_queue.put((None, None))