Beispiel #1
0
def main():
    env_name = 'doom_battle'
    env = create_env(env_name, cfg=default_cfg(env=env_name))

    env.reset()
    done = False
    while not done:
        env.render()
        obs, rew, done, info = env.step(env.action_space.sample())

    log.info('Done!')
Beispiel #2
0
    def test_minigrid_env(self):
        env_name = 'MiniGrid-Empty-Random-5x5-v0'
        env = create_env(env_name, cfg=default_cfg(env=env_name))
        log.info('Env action space: %r', env.action_space)
        log.info('Env obs space: %r', env.observation_space)

        env.reset()
        total_rew = 0
        for i in range(1000):
            obs, rew, done, info = env.step(env.action_space.sample())
            total_rew += rew
            if done:
                env.reset()
Beispiel #3
0
def main():
    env_name = 'doom_health_gathering'
    cfg = default_cfg(env=env_name)
    env = make_doom_env_impl(doom_env_by_name(env_name),
                             cfg=cfg,
                             custom_resolution='1280x720')

    # cfg.num_agents = 1
    # cfg.num_bots = 7
    # # cfg.num_humans = 1
    # env = make_doom_multiplayer_env(doom_env_by_name(env_name), cfg=cfg, custom_resolution='1280x720')

    return VizdoomEnv.play_human_mode(env, skip_frames=2, num_actions=15)
Beispiel #4
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--env', type=str, default=None, required=True)
    parser.add_argument('--demo_path', type=str, default=None, required=True)
    args = parser.parse_args()

    spec = doom_env_by_name(args.env)
    cfg = default_cfg(env=args.env)
    if spec.num_agents <= 1:
        env = make_doom_env(args.env, cfg=cfg, custom_resolution='1280x720')
    else:
        env = make_doom_env_impl(
            spec,
            cfg=cfg,
            custom_resolution='1280x720',
            player_id=0,
            num_agents=spec.num_agents,
            max_num_players=spec.num_agents,
            num_bots=spec.num_bots,
        )

    mode = 'replay'
    env.unwrapped.mode = mode
    env.unwrapped.initialize()
    game = env.unwrapped.game

    game.replay_episode(args.demo_path)

    frames_dir = args.demo_path + '_frames'
    if os.path.exists(frames_dir):
        shutil.rmtree(frames_dir)
    os.makedirs(frames_dir)

    frame_id = 0
    while not game.is_episode_finished():
        # Use advance_action instead of make_action.
        game.advance_action()
        img = env.render(mode='rgb_array')

        frame_name = f'{frame_id:05d}.png'
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        if img is not None:
            cv2.imwrite(join(frames_dir, frame_name), img)

        frame_id += 1

        r = game.get_last_reward()
        log.debug('Reward %.3f at frame %d', r, frame_id)

    game.close()
    def test_voxel_env(self):
        env_name = 'voxel_env_Sokoban'
        env = create_env(env_name, cfg=default_cfg(env=env_name))
        log.info('Env action space: %r', env.action_space)
        log.info('Env obs space: %r', env.observation_space)

        env.reset()
        total_rew = 0
        for i in range(1000):
            obs, rew, done, info = env.step(
                [env.action_space.sample() for _ in range(env.num_agents)])
            total_rew += sum(rew)

        log.info('Total rew: %.3f', total_rew)
Beispiel #6
0
        def make_env_func(env_config):
            print('Creating env!!!')
            cfg = default_cfg(env=spec.name)
            cfg.pixel_format = 'HWC'  # tensorflow models expect HWC by default

            if 'res_w' in env_config:
                cfg.res_w = env_config['res_w']
            if 'res_h' in env_config:
                cfg.res_h = env_config['res_h']
            if 'renderer' in env_config:
                cfg.renderer = env_config['renderer']
            if 'dmlab_throughput_benchmark' in env_config:
                cfg.renderer = env_config['dmlab_throughput_benchmark']

            env = make_dmlab_env(spec.name,
                                 env_config=env_config,
                                 cfg=cfg,
                                 **kwargs)
            return env
Beispiel #7
0
    def forward_pass(device_type):
        env_name = 'atari_breakout'
        cfg = default_cfg(algo='APPO', env=env_name)
        cfg.actor_critic_share_weights = True
        cfg.hidden_size = 128
        cfg.use_rnn = True
        cfg.env_framestack = 4

        env = create_env(env_name, cfg=cfg)

        torch.set_num_threads(1)
        torch.backends.cudnn.benchmark = True

        actor_critic = create_actor_critic(cfg, env.observation_space,
                                           env.action_space)
        device = torch.device(device_type)
        actor_critic.to(device)

        timing = Timing()
        with timing.timeit('all'):
            batch = 128
            with timing.add_time('input'):
                # better avoid hardcoding here...
                observations = dict(
                    obs=torch.rand([batch, 4, 84, 84]).to(device))
                rnn_states = torch.rand([batch,
                                         get_hidden_size(cfg)]).to(device)

            n = 200
            for i in range(n):
                with timing.add_time('forward'):
                    output = actor_critic(observations, rnn_states)

                log.debug('Progress %d/%d', i, n)

        log.debug('Timing: %s', timing)
Beispiel #8
0
        def make_env_func(env_config):
            print('Creating env!!!')
            cfg = default_cfg(env=spec.name)
            cfg.pixel_format = 'HWC'  # tensorflow models expect HWC by default

            if 'skip_frames' in env_config:
                cfg.env_frameskip = env_config['skip_frames']
            if 'res_w' in env_config:
                cfg.res_w = env_config['res_w']
            if 'res_h' in env_config:
                cfg.res_h = env_config['res_h']
            if 'wide_aspect_ratio' in env_config:
                cfg.wide_aspect_ratio = env_config['wide_aspect_ratio']

            env = make_doom_env(spec.name,
                                env_config=env_config,
                                cfg=cfg,
                                **kwargs)

            # we lock the global mutex here, otherwise Doom instances may crash on first reset when too many of them are reset simultaneously
            lock = FileLock(DOOM_LOCK_PATH)
            attempt = 0
            while True:
                attempt += 1
                try:
                    with lock.acquire(timeout=10):
                        print('Env created, resetting...')
                        env.reset()
                        print('Env reset completed! Config:', env_config)
                        break
                except Timeout:
                    print(
                        'Another instance of this application currently holds the lock, attempt:',
                        attempt)

            return env
Beispiel #9
0
def default_doom_cfg():
    return default_cfg(env='doom_env')
Beispiel #10
0
 def make_env(env_config):
     from sample_factory.envs.dmlab.dmlab_env import make_dmlab_env
     return make_dmlab_env('dmlab_nonmatch',
                           cfg=default_cfg(env='dmlab_nonmatch'),
                           env_config=None)
Beispiel #11
0
 def make_env(env_config):
     from sample_factory.envs.atari.atari_utils import make_atari_env
     return make_atari_env('atari_breakout',
                           cfg=default_cfg(env='atari_breakout'))