Example #1
0
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
Example #2
0
def init_environment(env_path, docker_target_name, no_graphics, worker_id,
                     seed):
    if env_path is not None:
        # Strip out executable extensions if passed
        env_path = (env_path.strip().replace('.app', '').replace(
            '.exe', '').replace('.x86_64', '').replace('.x86', ''))
    docker_training = docker_target_name is not None

    return UnityEnvironment(n_arenas=1,
                            file_name=env_path,
                            worker_id=worker_id,
                            seed=seed,
                            docker_training=docker_training,
                            play=True)
Example #3
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()
Example #4
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()
Example #5
0
resolution = 84
n_channels = 3
dim_actions = 2
sample_size_per_task = 30

# set up the testing env
np.random.seed(seed)
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,
                       resolution=resolution)

# The Agent to test
agent = Agent()

# visualization
plt.ion()
fig, ax = plt.subplots(ncols=1, nrows=1)
image = ax.imshow(np.zeros(
    (resolution, resolution, 3)))  # visual input for the agent
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)
Example #6
0
 def __init__(self, *args, **kwargs):
     '''
     Check UnityEnvironment parameters
     '''
     self._env = UnityEnvironment(*args, **kwargs)
     self._arenas_configurations = None
Example #7
0
from animalai.envs.environment import UnityEnvironment
from animalai.envs.arena_config import ArenaConfig
import time
import random

worker_id = random.randint(0, 100)

env = UnityEnvironment(n_arenas=1,
                       file_name='../env_modified/AnimalAILinux',
                       worker_id=worker_id,
                       seed=0,
                       docker_training=False,
                       inference=True)

arena_config_in = ArenaConfig('configs/empty.yaml')
env.reset(arenas_configurations=arena_config_in)
total_distance = 0
# step_time_length = 0.0595
try:
    while True:
        step = 0
        direction = input()
        if direction == "w":
            action = [1, 0]
        if direction == "s":
            action = [2, 0]
        if direction == "d":
            action = [0, 1]
        if direction == "a":
            action = [0, 2]
        while step < 1.0: