Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ def _update_action_spaces(self) -> None:
and len(act_spec.discrete_branches) == 0
):
raise error.Error("No actions found")
d_space = None
if act_spec.discrete_size == 1:
d_space = spaces.Discrete(act_spec.discrete_branches[0])
if self._seed is not None:
d_space.seed(self._seed)
if act_spec.continuous_size == 0:
self._action_spaces[behavior_name] = d_space
continue
if act_spec.discrete_size > 0:
elif act_spec.discrete_size > 1:
d_space = spaces.MultiDiscrete(act_spec.discrete_branches)
if self._seed is not None:
d_space.seed(self._seed)
Expand Down Expand Up @@ -154,7 +155,7 @@ def _process_action(self, current_agent, action):
action = self._action_to_np(current_action_space, action)
if not current_action_space.contains(action): # type: ignore
raise error.Error(
f"Invalid action, got {action} but was expecting action from {self.action_space}"
f"Invalid action, got {action} but was expecting action from {current_action_space}"
)
if isinstance(current_action_space, spaces.Tuple):
action = ActionTuple(action[0], action[1])
Expand Down Expand Up @@ -202,7 +203,7 @@ def _step(self):

def _cleanup_agents(self):
for current_agent in self._agents:
if self._terminations[current_agent] or self._truncations[current_agent]:
if (self._terminations.get(current_agent, False) or self._truncations.get(current_agent, False)) and current_agent in self._live_agents:
self._live_agents.remove(current_agent)

@property
Expand Down
23 changes: 23 additions & 0 deletions ml-agents-envs/tests/test_pettingzoo_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,26 @@ def test_reset_seed_reseeds_action_spaces():
second = env.action_space(agent).sample()

assert np.array_equal(first, second)


def test_invalid_action_error_message():
unity_env = SimpleEnvironment(["test_single"])
env = UnityAECEnv(unity_env)
env.reset()
agent = env.possible_agents[0]
with pytest.raises(Exception) as exc_info:
env.step(9999)
assert "bound method" not in str(exc_info.value)
assert "Discrete" in str(exc_info.value) or "Box" in str(exc_info.value) or "MultiDiscrete" in str(exc_info.value)


def test_cleanup_agents_safe_when_already_removed():
unity_env = SimpleEnvironment(["test_single"])
env = UnityParallelEnv(unity_env)
env.reset()
agent = env.possible_agents[0]
env._terminations[agent] = True
env._live_agents.clear() # simulate already removed
# Must not raise ValueError
env._cleanup_agents()
assert len(env._live_agents) == 0