예제 #1
0
파일: env.py 프로젝트: ironbar/orangutan
class EnvWrapper(object):
    '''
    Wrapper around UnityEnvironment that resets each arena if the episode is done

    It will only work correctly if using a single arena on each environment
    '''
    def __init__(self, *args, **kwargs):
        '''
        Check UnityEnvironment parameters
        '''
        self._env = UnityEnvironment(*args, **kwargs)
        self._arenas_configurations = None

    def __getattr__(self, attr):
        if attr in self.__dict__:
            return getattr(self, attr)
        return getattr(self._env, attr)

    def reset(self, arenas_configurations=None, train_mode=True):
        """ Shuffle arenas and reset configuration """
        if arenas_configurations is not None:
            self._arenas_configurations = arenas_configurations
        self._arenas_configurations.shuffle_arenas()
        return self._env.reset(self._arenas_configurations, train_mode)

    def step(self, *args, **kwargs):
        ret = self._env.step(*args, **kwargs)
        if ret['Learner'].local_done[0]:
            new_ret = self.reset()
            ret['Learner'].visual_observations = new_ret['Learner'].visual_observations
        return ret
예제 #2
0
def debug_confirm_arena_config(env_path, arena_config):
    env_path = (env_path.strip().replace('.app', '').replace(
        '.exe', '').replace('.x86_64', '').replace('.x86', ''))

    from animalai.envs.environment import UnityEnvironment

    env = UnityEnvironment(n_arenas=16,
                           file_name=env_path,
                           worker_id=1,
                           seed=0,
                           docker_training=False,
                           play=True)
    env.reset(arenas_configurations=arena_config)

    try:
        while True:
            continue
    except KeyboardInterrupt:
        env.close()
예제 #3
0
def main(args):
    docker_training = docker_target_name is not None

    env = UnityEnvironment(
        n_arenas=args.n_arenas,
        file_name=env_path,
        worker_id=worker_id,
        seed=seed,
        docker_training=docker_training,
        play=False,
        resolution=resolution
    )

    arena_config_in = ArenaConfig('configs/3-Obstacles.yaml')
    env.reset(arenas_configurations=arena_config_in)

    start_time = time.time()
    for i in range(args.frames):
        res = env.step(np.random.randint(0, 3, size=2 * args.n_arenas))

    elapsed_time = time.time() - start_time
    fps = float(args.frames) / elapsed_time
    print("n_arenas={0}, fps={1:.3f}".format(args.n_arenas, fps))
    env.close()
예제 #4
0
line, = ax.plot(
    [], [])  # the direction that the agent want to go given the visual input
sca = ax.scatter([], [], s=5, c="yellow")  # the path to the target(food)

# loop over all the config files above
for arenaConfig in arenaConfigs:
    print(arenaConfig)
    arena_config_in = ArenaConfig(arenaConfig)

    # run multiple tests derived from each config file
    for sample_n in range(sample_size_per_task):
        print("ArenaConfig; {} Sample: {}".format(arenaConfig, sample_n))

        # initialize(reset) the agent and the env
        agent.reset(arena_config_in.arenas[0].t)
        brainInfo = env.reset(arenas_configurations=arena_config_in)

        while True:

            # information given by the env
            obs = brainInfo['Learner'].visual_observations[0][
                0, :, :, :], brainInfo['Learner'].vector_observations[0]
            reward = brainInfo['Learner'].rewards[0]
            done = brainInfo['Learner'].local_done[0]
            info = {"brain_info": brainInfo}

            # let the agent generate an action based on the information
            action = agent.step(obs, reward, done, info)

            # Visualization{visual, direction, path}
            image.set_data(obs[0])
n_arenas = 4

if env_path is not None:
    env_path = (env_path.strip().replace('.app', '').replace(
        '.exe', '').replace('.x86_64', '').replace('.x86', ''))
docker_training = docker_target_name is not None

env = UnityEnvironment(n_arenas=n_arenas,
                       file_name=env_path,
                       worker_id=worker_id,
                       seed=seed,
                       docker_training=docker_training,
                       play=False)

arena_config_in = ArenaConfig('configs/lightsOff.yaml')
env.reset(arenas_configurations=arena_config_in)
fig, axes = plt.subplots(2, 2)
imshows = []
for i in range(2):
    for j in range(2):
        axes[i, j].set_title('Arena ' + str(i * 2 + j))
        axes[i, j].axis('off')
        imshows.append(axes[i, j].imshow(np.zeros((84, 84, 3))))


def initialize_animation():
    for i in range(4):
        imshows[i].set_data(np.zeros((84, 84, 3)))


def run_step_imshow(step):