From f73259f2a17da9fd35d460294ece65769db5b750 Mon Sep 17 00:00:00 2001 From: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:15:29 +0000 Subject: [PATCH] Fix PettingZoo wrappers dropping multi-dimensional action components _process_action built ActionTuple from 1-D arrays for Box, MultiDiscrete and Tuple spaces and then copied element [0] into the agent's row, so the first component was broadcast to every action dimension. Reshape these actions to (1, n), as the Discrete branch already does. --- com.unity.ml-agents/CHANGELOG.md | 1 + .../envs/unity_pettingzoo_base_env.py | 6 ++-- .../tests/test_pettingzoo_wrapper.py | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/com.unity.ml-agents/CHANGELOG.md b/com.unity.ml-agents/CHANGELOG.md index 807d384ad3..c98e17a8b5 100755 --- a/com.unity.ml-agents/CHANGELOG.md +++ b/com.unity.ml-agents/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to #### ml-agents / ml-agents-envs - Fixed PPO and POCA value estimates being overwritten by returns in the training buffer, preventing value loss clipping around the original predictions. (#6281, #6337) +- Fixed the PettingZoo wrappers sending only the first component of a continuous, multi-discrete or hybrid action to every action dimension. ## [4.1.0] - 2026-07-27 ### Major Changes diff --git a/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py b/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py index 36d75b7ba8..0bb4997abd 100644 --- a/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py +++ b/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py @@ -157,13 +157,13 @@ def _process_action(self, current_agent, action): f"Invalid action, got {action} but was expecting action from {self.action_space}" ) if isinstance(current_action_space, spaces.Tuple): - action = ActionTuple(action[0], action[1]) + action = ActionTuple(action[0].reshape(1, -1), action[1].reshape(1, -1)) elif isinstance(current_action_space, spaces.MultiDiscrete): - action = ActionTuple(None, action) + action = ActionTuple(None, action.reshape(1, -1)) elif isinstance(current_action_space, spaces.Discrete): action = ActionTuple(None, np.array(action).reshape(1, 1)) else: - action = ActionTuple(action, None) + action = ActionTuple(action.reshape(1, -1), None) if not (self._terminations[current_agent] or self._truncations[current_agent]): current_behavior = _agent_id_to_behavior(current_agent) diff --git a/ml-agents-envs/tests/test_pettingzoo_wrapper.py b/ml-agents-envs/tests/test_pettingzoo_wrapper.py index 71b55042c0..c805f43472 100644 --- a/ml-agents-envs/tests/test_pettingzoo_wrapper.py +++ b/ml-agents-envs/tests/test_pettingzoo_wrapper.py @@ -84,3 +84,31 @@ def test_reset_seed_reseeds_action_spaces(): second = env.action_space(agent).sample() assert np.array_equal(first, second) + + +@pytest.mark.parametrize( + "action_sizes", [(2, 0), (0, 2), (2, 2)], ids=["continuous", "discrete", "hybrid"] +) +def test_step_forwards_every_action_component(action_sizes): + # Every component of a multi-dimensional action must reach the environment, + # not only the first one. + unity_env = SimpleEnvironment(["test_single"], action_sizes=action_sizes) + env = UnityParallelEnv(unity_env) + env.reset() + agent = env.agents[0] + continuous = np.array([0.1, -0.2], dtype=np.float32) + discrete = np.array([0, 1], dtype=np.int32) + if action_sizes == (2, 0): + action = continuous + elif action_sizes == (0, 2): + action = discrete + else: + action = (continuous, discrete) + + env.step({agent: action}) + + sent = unity_env.action["test_single"] + if action_sizes[0] > 0: + np.testing.assert_array_equal(sent.continuous, [continuous]) + if action_sizes[1] > 0: + np.testing.assert_array_equal(sent.discrete, [discrete])