Skip to content

Commit

Permalink
Following petting_zoo registry API (#5557)
Browse files Browse the repository at this point in the history
* 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
maryamhonari authored and miguelalonsojr committed Jan 27, 2022
1 parent a5a275c commit 6871304
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ jobs:
python -m pip install --progress-bar=off -e ./ml-agents -c ${{ matrix.pip_constraints }}
python -m pip install --progress-bar=off -r test_requirements.txt -c ${{ matrix.pip_constraints }}
python -m pip install --progress-bar=off -e ./gym-unity -c ${{ matrix.pip_constraints }}
python -m pip install --progress-bar=off -e ./pettingzoo-unity -c ${{ matrix.pip_constraints }}
python -m pip install --progress-bar=off -e ./ml-agents-plugin-examples -c ${{ matrix.pip_constraints }}
- name: Save python dependencies
run: |
pip freeze > pip_versions-${{ matrix.python-version }}.txt
cat pip_versions-${{ matrix.python-version }}.txt
- name: Run pytest
run: pytest --cov=ml-agents --cov=ml-agents-envs --cov=gym-unity --cov-report html --junitxml=junit/test-results-${{ matrix.python-version }}.xml -p no:warnings -v
run: pytest --cov=ml-agents --cov=ml-agents-envs --cov=gym-unity --cov=pettingzoo-unity --cov-report=html --junitxml=junit/test-results-${{ matrix.python-version }}.xml -p no:warnings -v
- name: Upload pytest test results
uses: actions/upload-artifact@v2
with:
Expand Down
1 change: 0 additions & 1 deletion gym-unity/gym_unity/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class UnityGymException(error.Error):


logger = logging_util.get_logger(__name__)
logging_util.set_log_level(logging_util.INFO)

GymStepResult = Tuple[np.ndarray, float, bool, Dict]

Expand Down
167 changes: 92 additions & 75 deletions pettingzoo-unity/Colab_PettingZoo.ipynb
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Colab-PettingZoo-Unity.ipynb",
"private_outputs": true,
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.11 64-bit ('pettingzoo': conda)",
"metadata": {
"interpreter": {
"hash": "5d8bf1ccad4050d475e9ef008ff2de5ee0b8bb440e3e2ef203ad7e8f5a9e706b"
}
}
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
}
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ML-Agents PettingZoo Wrapper"
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -125,11 +96,11 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installing ml-agents"
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
Expand All @@ -149,36 +120,59 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Run the Environment"
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
"cell_type": "markdown",
"metadata": {
"id": "htb-p1hSNX7D"
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"#@title Select Environment { display-mode: \"form\" }\n",
"env_id = \"StrikersVsGoalie\" #@param ['Basic', '3DBall', '3DBallHard', 'GridWorld', 'Hallway', 'VisualHallway', 'CrawlerDynamicTarget', 'CrawlerStaticTarget', 'Bouncer', 'SoccerTwos', 'PushBlock', 'VisualPushBlock', 'WallJump', 'Tennis', 'Reacher', 'Pyramids', 'VisualPyramids', 'Walker', 'FoodCollector', 'VisualFoodCollector', 'StrikersVsGoalie', 'WormStaticTarget', 'WormDynamicTarget']"
],
"execution_count": null,
"outputs": []
"List of available environments:\n",
"* Basic\n",
"* ThreeDBall\n",
"* ThreeDBallHard\n",
"* GridWorld\n",
"* Hallway\n",
"* VisualHallway\n",
"* CrawlerDynamicTarget\n",
"* CrawlerStaticTarget\n",
"* Bouncer\n",
"* SoccerTwos\n",
"* PushBlock\n",
"* VisualPushBlock\n",
"* WallJump\n",
"* Tennis\n",
"* Reacher\n",
"* Pyramids\n",
"* VisualPyramids\n",
"* Walker\n",
"* FoodCollector\n",
"* VisualFoodCollector\n",
"* StrikersVsGoalie\n",
"* WormStaticTarget\n",
"* WormDynamicTarget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Start Environment with PettingZoo Wrapper"
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YSf-WhxbqtLw"
},
"outputs": [],
"source": [
"# -----------------\n",
"# This code is used to close an env that might not have been closed before\n",
Expand All @@ -189,29 +183,26 @@
"# -----------------\n",
"\n",
"import numpy as np\n",
"from mlagents_envs.registry import default_registry\n",
"from pettingzoo_unity import UnityToPettingZooWrapper\n",
"\n",
"unity_env = default_registry[env_id].make()\n",
"env = UnityToPettingZooWrapper(unity_env)"
],
"execution_count": null,
"outputs": []
"from pettingzoo_unity.envs import StrikersVsGoalie # import unity environment\n",
"env = StrikersVsGoalie.env()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stepping the environment\n",
"\n",
"Example of interacting with the environment in basic RL loop. It follows the same interface as described in [PettingZoo API page](https://www.pettingzoo.ml/api)."
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dhtl0mpeqxYi"
},
"outputs": [],
"source": [
"num_cycles = 10\n",
"\n",
Expand All @@ -225,18 +216,16 @@
" else:\n",
" action = env.action_spaces[agent].sample() # randomly choose an action for example\n",
" env.step(action)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Additional Environment API\n",
"\n",
"All the API described in the `Additional Environment API` section in the [PettingZoo API page](https://www.pettingzoo.ml/api) are all supported. A few examples are shown below."
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -281,22 +270,50 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Close the Environment to free the port it is using"
],
"cell_type": "markdown",
"metadata": {}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "a7KatdThq7OV"
},
"outputs": [],
"source": [
"env.close()"
],
"execution_count": null,
"outputs": []
]
}
]
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Colab-UnityEnvironment-1-Run.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions pettingzoo-unity/pettingzoo_unity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

try:
from .pettingzoo_wrapper import UnityToPettingZooWrapper # noqa
import pettingzoo_unity.envs # noqa
except ImportError:
pass
61 changes: 61 additions & 0 deletions pettingzoo-unity/pettingzoo_unity/envs/__init__.py
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.

0 comments on commit 6871304

Please sign in to comment.