Skip to content

Commit

Permalink
Log warning when using multiple vis obs
Browse files Browse the repository at this point in the history
  • Loading branch information
awjuliani committed Sep 25, 2018
1 parent d15d5c6 commit ad72190
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions gym-unity/gym_unity/envs/unity_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
import gym
import numpy as np
from mlagents.envs import UnityEnvironment
from gym import error, spaces, logger
from gym import error, spaces


class UnityGymException(error.Error):
Expand All @@ -11,6 +12,10 @@ class UnityGymException(error.Error):
pass


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("gym_unity")


class UnityEnv(gym.Env):
"""
Provides Gym wrapper for Unity Learning Environments.
Expand Down Expand Up @@ -44,7 +49,11 @@ def __init__(self, environment_filename: str, worker_id=0, use_visual=False, mul
if use_visual and brain.number_visual_observations == 0:
raise UnityGymException("`use_visual` was set to True, however there are no"
" visual observations as part of this environment.")
self.use_visual = brain.number_visual_observations >= 1 and use_visual
self.use_visual = brain.number_visual_observations >= 1 and use_visual

if brain.number_visual_observations > 1:
logger.warning("The environment contains more than one visual observation. "
"Please note that only the first will be provided in the observation.")

if brain.num_stacked_vector_observations != 1:
raise UnityGymException(
Expand Down Expand Up @@ -114,7 +123,8 @@ def step(self, action):
if not isinstance(action, list):
raise UnityGymException("The environment was expecting `action` to be a list.")
if len(action) != self._n_agents:
raise UnityGymException("The environment was expecting a list of {} actions.".format(self._n_agents))
raise UnityGymException(
"The environment was expecting a list of {} actions.".format(self._n_agents))
else:
action = np.array(action)

Expand All @@ -136,17 +146,19 @@ def _single_step(self, info):
else:
default_observation = info.vector_observations[0, :]

return default_observation, info.rewards[0], info.local_done[0], {"text_observation": info.text_observations[0],
"brain_info": info}
return default_observation, info.rewards[0], info.local_done[0], {
"text_observation": info.text_observations[0],
"brain_info": info}

def _multi_step(self, info):
if self.use_visual:
self.visual_obs = info.visual_observations
default_observation = self.visual_obs
else:
default_observation = info.vector_observations
return list(default_observation), info.rewards, info.local_done, {"text_observation": info.text_observations,
"brain_info": info}
return list(default_observation), info.rewards, info.local_done, {
"text_observation": info.text_observations,
"brain_info": info}

def render(self, mode='rgb_array'):
return self.visual_obs
Expand All @@ -170,11 +182,13 @@ def seed(self, seed=None):

def _check_agents(self, n_agents):
if not self._multiagent and n_agents > 1:
raise UnityGymException("The environment was launched as a single-agent environment, however"
"there is more than one agent in the scene.")
raise UnityGymException(
"The environment was launched as a single-agent environment, however"
"there is more than one agent in the scene.")
elif self._multiagent and n_agents <= 1:
raise UnityGymException("The environment was launched as a mutli-agent environment, however"
"there is only one agent in the scene.")
raise UnityGymException(
"The environment was launched as a mutli-agent environment, however"
"there is only one agent in the scene.")
if self._n_agents is None:
self._n_agents = n_agents
logger.info("{} agents within environment.".format(n_agents))
Expand Down

0 comments on commit ad72190

Please sign in to comment.