-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Following petting_zoo registry API (#5557)
* init petting_zoo registry * cherrypick Custom trainer editor analytics (#5511) * cherrypick "Update dotnet-format to address breaking changes introduced by upstream changes (#5528)" * Update colab to match pettingZoo import api * ToRevert: pull exp-petting-registry branch * Add init file to tests * Install pettingzoo-unity requirements for pytest * update pytest command * Add docstrings and comments * update coverage to pettingzoo folder * unset log level * update env string
- Loading branch information
1 parent
a5a275c
commit 6871304
Showing
6 changed files
with
156 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from mlagents_envs.registry import default_registry | ||
from pettingzoo_unity import UnityToPettingZooWrapper | ||
from typing import Optional | ||
from mlagents_envs.exception import UnityWorkerInUseException | ||
from mlagents_envs.side_channel.environment_parameters_channel import ( | ||
EnvironmentParametersChannel, | ||
) | ||
from mlagents_envs.side_channel.engine_configuration_channel import ( | ||
EngineConfigurationChannel, | ||
) | ||
from mlagents_envs.side_channel.stats_side_channel import StatsSideChannel | ||
from mlagents_envs import logging_util | ||
|
||
logger = logging_util.get_logger(__name__) | ||
|
||
|
||
class PettingZooEnv: | ||
def __init__(self, env_id: str) -> None: | ||
self.env_id = env_id | ||
|
||
def env(self, seed: Optional[int] = None, **kwargs) -> UnityToPettingZooWrapper: | ||
""" | ||
Creates the environment with env_id from unity's default_registry and wraps it in a UnityToPettingZooWrapper | ||
:param seed: The seed for the action spaces of the agents. | ||
:param kwargs: Any argument accepted by `UnityEnvironment`class except file_name | ||
""" | ||
# If not side_channels specified, add the followings | ||
if "side_channels" not in kwargs: | ||
kwargs["side_channels"] = [ | ||
EngineConfigurationChannel(), | ||
EnvironmentParametersChannel(), | ||
StatsSideChannel(), | ||
] | ||
_env = None | ||
# If no base port argument is provided, try ports starting at 6000 until one is free | ||
if "base_port" not in kwargs: | ||
port = 6000 | ||
while _env is None: | ||
try: | ||
kwargs["base_port"] = port | ||
_env = default_registry[self.env_id].make(**kwargs) | ||
except UnityWorkerInUseException: | ||
port += 1 | ||
pass | ||
else: | ||
_env = default_registry[self.env_id].make(**kwargs) | ||
return UnityToPettingZooWrapper(_env, seed) | ||
|
||
|
||
# Register each environment in default_registry as a PettingZooEnv | ||
for key in default_registry: | ||
env_name = key | ||
if key[0].isdigit(): | ||
env_name = key.replace("3", "Three") | ||
if not env_name.isidentifier(): | ||
logger.warning( | ||
f"Environment id {env_name} can not be registered since it is" | ||
f"not a valid identifier name." | ||
) | ||
continue | ||
locals()[env_name] = PettingZooEnv(key) |
Empty file.