Skip to content

Commit de53137

Browse files
vincentpierreChris Elion
authored and
Chris Elion
committed
Develop better error message for #3953 (#3963)
* Making the error for wrong number of agents raise consistently * Better error message for inputs of wrong dimensions
1 parent 48261d3 commit de53137

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

gym-unity/gym_unity/envs/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def __init__(
5353
self._env.step()
5454

5555
self.visual_obs = None
56-
self._n_agents = -1
5756

5857
# Save the step result from the last time all Agents requested decisions.
5958
self._previous_decision_step: DecisionSteps = None
@@ -172,6 +171,7 @@ def step(self, action: List[Any]) -> GymStepResult:
172171

173172
self._env.step()
174173
decision_step, terminal_step = self._env.get_steps(self.name)
174+
self._check_agents(max(len(decision_step), len(terminal_step)))
175175
if len(terminal_step) != 0:
176176
# The agent is done
177177
self.game_over = True
@@ -264,10 +264,11 @@ def seed(self, seed: Any = None) -> None:
264264
logger.warning("Could not seed environment %s", self.name)
265265
return
266266

267-
def _check_agents(self, n_agents: int) -> None:
268-
if self._n_agents > 1:
267+
@staticmethod
268+
def _check_agents(n_agents: int) -> None:
269+
if n_agents > 1:
269270
raise UnityGymException(
270-
"There can only be one Agent in the environment but {n_agents} were detected."
271+
f"There can only be one Agent in the environment but {n_agents} were detected."
271272
)
272273

273274
@property
@@ -290,10 +291,6 @@ def action_space(self):
290291
def observation_space(self):
291292
return self._observation_space
292293

293-
@property
294-
def number_agents(self):
295-
return self._n_agents
296-
297294

298295
class ActionFlattener:
299296
"""

ml-agents-envs/mlagents_envs/environment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ def set_actions(self, behavior_name: BehaviorName, action: np.ndarray) -> None:
415415
expected_shape = (len(self._env_state[behavior_name][0]), spec.action_size)
416416
if action.shape != expected_shape:
417417
raise UnityActionException(
418-
"The behavior {0} needs an input of dimension {1} but received input of dimension {2}".format(
419-
behavior_name, expected_shape, action.shape
420-
)
418+
"The behavior {0} needs an input of dimension {1} for "
419+
"(<number of agents>, <action size>) but received input of "
420+
"dimension {2}".format(behavior_name, expected_shape, action.shape)
421421
)
422422
if action.dtype != expected_type:
423423
action = action.astype(expected_type)

0 commit comments

Comments
 (0)