Skip to content

Commit 6871304

Browse files
maryamhonarimiguelalonsojr
authored andcommitted
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
1 parent a5a275c commit 6871304

File tree

6 files changed

+156
-77
lines changed

6 files changed

+156
-77
lines changed

.github/workflows/pytest.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ jobs:
5555
python -m pip install --progress-bar=off -e ./ml-agents -c ${{ matrix.pip_constraints }}
5656
python -m pip install --progress-bar=off -r test_requirements.txt -c ${{ matrix.pip_constraints }}
5757
python -m pip install --progress-bar=off -e ./gym-unity -c ${{ matrix.pip_constraints }}
58+
python -m pip install --progress-bar=off -e ./pettingzoo-unity -c ${{ matrix.pip_constraints }}
5859
python -m pip install --progress-bar=off -e ./ml-agents-plugin-examples -c ${{ matrix.pip_constraints }}
5960
- name: Save python dependencies
6061
run: |
6162
pip freeze > pip_versions-${{ matrix.python-version }}.txt
6263
cat pip_versions-${{ matrix.python-version }}.txt
6364
- name: Run pytest
64-
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
65+
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
6566
- name: Upload pytest test results
6667
uses: actions/upload-artifact@v2
6768
with:

gym-unity/gym_unity/envs/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class UnityGymException(error.Error):
1919

2020

2121
logger = logging_util.get_logger(__name__)
22-
logging_util.set_log_level(logging_util.INFO)
2322

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

pettingzoo-unity/Colab_PettingZoo.ipynb

+92-75
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
11
{
2-
"nbformat": 4,
3-
"nbformat_minor": 0,
4-
"metadata": {
5-
"colab": {
6-
"name": "Colab-PettingZoo-Unity.ipynb",
7-
"private_outputs": true,
8-
"provenance": [],
9-
"collapsed_sections": [],
10-
"toc_visible": true
11-
},
12-
"kernelspec": {
13-
"name": "python3",
14-
"display_name": "Python 3.7.11 64-bit ('pettingzoo': conda)",
15-
"metadata": {
16-
"interpreter": {
17-
"hash": "5d8bf1ccad4050d475e9ef008ff2de5ee0b8bb440e3e2ef203ad7e8f5a9e706b"
18-
}
19-
}
20-
},
21-
"pycharm": {
22-
"stem_cell": {
23-
"cell_type": "raw",
24-
"source": [],
25-
"metadata": {
26-
"collapsed": false
27-
}
28-
}
29-
}
30-
},
312
"cells": [
323
{
4+
"cell_type": "markdown",
5+
"metadata": {},
336
"source": [
347
"# ML-Agents PettingZoo Wrapper"
35-
],
36-
"cell_type": "markdown",
37-
"metadata": {}
8+
]
389
},
3910
{
11+
"cell_type": "markdown",
12+
"metadata": {},
4013
"source": [
4114
"## Setup"
42-
],
43-
"cell_type": "markdown",
44-
"metadata": {}
15+
]
4516
},
4617
{
4718
"cell_type": "code",
@@ -125,11 +96,11 @@
12596
]
12697
},
12798
{
99+
"cell_type": "markdown",
100+
"metadata": {},
128101
"source": [
129102
"### Installing ml-agents"
130-
],
131-
"cell_type": "markdown",
132-
"metadata": {}
103+
]
133104
},
134105
{
135106
"cell_type": "code",
@@ -149,36 +120,59 @@
149120
]
150121
},
151122
{
123+
"cell_type": "markdown",
124+
"metadata": {},
152125
"source": [
153126
"## Run the Environment"
154-
],
155-
"cell_type": "markdown",
156-
"metadata": {}
127+
]
157128
},
158129
{
159-
"cell_type": "code",
130+
"cell_type": "markdown",
160131
"metadata": {
161-
"id": "htb-p1hSNX7D"
132+
"jp-MarkdownHeadingCollapsed": true,
133+
"tags": []
162134
},
163135
"source": [
164-
"#@title Select Environment { display-mode: \"form\" }\n",
165-
"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']"
166-
],
167-
"execution_count": null,
168-
"outputs": []
136+
"List of available environments:\n",
137+
"* Basic\n",
138+
"* ThreeDBall\n",
139+
"* ThreeDBallHard\n",
140+
"* GridWorld\n",
141+
"* Hallway\n",
142+
"* VisualHallway\n",
143+
"* CrawlerDynamicTarget\n",
144+
"* CrawlerStaticTarget\n",
145+
"* Bouncer\n",
146+
"* SoccerTwos\n",
147+
"* PushBlock\n",
148+
"* VisualPushBlock\n",
149+
"* WallJump\n",
150+
"* Tennis\n",
151+
"* Reacher\n",
152+
"* Pyramids\n",
153+
"* VisualPyramids\n",
154+
"* Walker\n",
155+
"* FoodCollector\n",
156+
"* VisualFoodCollector\n",
157+
"* StrikersVsGoalie\n",
158+
"* WormStaticTarget\n",
159+
"* WormDynamicTarget"
160+
]
169161
},
170162
{
163+
"cell_type": "markdown",
164+
"metadata": {},
171165
"source": [
172166
"### Start Environment with PettingZoo Wrapper"
173-
],
174-
"cell_type": "markdown",
175-
"metadata": {}
167+
]
176168
},
177169
{
178170
"cell_type": "code",
171+
"execution_count": null,
179172
"metadata": {
180173
"id": "YSf-WhxbqtLw"
181174
},
175+
"outputs": [],
182176
"source": [
183177
"# -----------------\n",
184178
"# This code is used to close an env that might not have been closed before\n",
@@ -189,29 +183,26 @@
189183
"# -----------------\n",
190184
"\n",
191185
"import numpy as np\n",
192-
"from mlagents_envs.registry import default_registry\n",
193-
"from pettingzoo_unity import UnityToPettingZooWrapper\n",
194-
"\n",
195-
"unity_env = default_registry[env_id].make()\n",
196-
"env = UnityToPettingZooWrapper(unity_env)"
197-
],
198-
"execution_count": null,
199-
"outputs": []
186+
"from pettingzoo_unity.envs import StrikersVsGoalie # import unity environment\n",
187+
"env = StrikersVsGoalie.env()"
188+
]
200189
},
201190
{
191+
"cell_type": "markdown",
192+
"metadata": {},
202193
"source": [
203194
"### Stepping the environment\n",
204195
"\n",
205196
"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)."
206-
],
207-
"cell_type": "markdown",
208-
"metadata": {}
197+
]
209198
},
210199
{
211200
"cell_type": "code",
201+
"execution_count": null,
212202
"metadata": {
213203
"id": "dhtl0mpeqxYi"
214204
},
205+
"outputs": [],
215206
"source": [
216207
"num_cycles = 10\n",
217208
"\n",
@@ -225,18 +216,16 @@
225216
" else:\n",
226217
" action = env.action_spaces[agent].sample() # randomly choose an action for example\n",
227218
" env.step(action)"
228-
],
229-
"execution_count": null,
230-
"outputs": []
219+
]
231220
},
232221
{
222+
"cell_type": "markdown",
223+
"metadata": {},
233224
"source": [
234225
"### Additional Environment API\n",
235226
"\n",
236227
"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."
237-
],
238-
"cell_type": "markdown",
239-
"metadata": {}
228+
]
240229
},
241230
{
242231
"cell_type": "code",
@@ -281,22 +270,50 @@
281270
]
282271
},
283272
{
273+
"cell_type": "markdown",
274+
"metadata": {},
284275
"source": [
285276
"### Close the Environment to free the port it is using"
286-
],
287-
"cell_type": "markdown",
288-
"metadata": {}
277+
]
289278
},
290279
{
291280
"cell_type": "code",
281+
"execution_count": null,
292282
"metadata": {
293283
"id": "a7KatdThq7OV"
294284
},
285+
"outputs": [],
295286
"source": [
296287
"env.close()"
297-
],
298-
"execution_count": null,
299-
"outputs": []
288+
]
300289
}
301-
]
290+
],
291+
"metadata": {
292+
"colab": {
293+
"collapsed_sections": [],
294+
"name": "Colab-UnityEnvironment-1-Run.ipynb",
295+
"private_outputs": true,
296+
"provenance": [],
297+
"toc_visible": true
298+
},
299+
"kernelspec": {
300+
"display_name": "Python 3 (ipykernel)",
301+
"language": "python",
302+
"name": "python3"
303+
},
304+
"language_info": {
305+
"codemirror_mode": {
306+
"name": "ipython",
307+
"version": 3
308+
},
309+
"file_extension": ".py",
310+
"mimetype": "text/x-python",
311+
"name": "python",
312+
"nbconvert_exporter": "python",
313+
"pygments_lexer": "ipython3",
314+
"version": "3.7.8"
315+
}
316+
},
317+
"nbformat": 4,
318+
"nbformat_minor": 4
302319
}

pettingzoo-unity/pettingzoo_unity/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
try:
88
from .pettingzoo_wrapper import UnityToPettingZooWrapper # noqa
9+
import pettingzoo_unity.envs # noqa
910
except ImportError:
1011
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from mlagents_envs.registry import default_registry
2+
from pettingzoo_unity import UnityToPettingZooWrapper
3+
from typing import Optional
4+
from mlagents_envs.exception import UnityWorkerInUseException
5+
from mlagents_envs.side_channel.environment_parameters_channel import (
6+
EnvironmentParametersChannel,
7+
)
8+
from mlagents_envs.side_channel.engine_configuration_channel import (
9+
EngineConfigurationChannel,
10+
)
11+
from mlagents_envs.side_channel.stats_side_channel import StatsSideChannel
12+
from mlagents_envs import logging_util
13+
14+
logger = logging_util.get_logger(__name__)
15+
16+
17+
class PettingZooEnv:
18+
def __init__(self, env_id: str) -> None:
19+
self.env_id = env_id
20+
21+
def env(self, seed: Optional[int] = None, **kwargs) -> UnityToPettingZooWrapper:
22+
"""
23+
Creates the environment with env_id from unity's default_registry and wraps it in a UnityToPettingZooWrapper
24+
:param seed: The seed for the action spaces of the agents.
25+
:param kwargs: Any argument accepted by `UnityEnvironment`class except file_name
26+
"""
27+
# If not side_channels specified, add the followings
28+
if "side_channels" not in kwargs:
29+
kwargs["side_channels"] = [
30+
EngineConfigurationChannel(),
31+
EnvironmentParametersChannel(),
32+
StatsSideChannel(),
33+
]
34+
_env = None
35+
# If no base port argument is provided, try ports starting at 6000 until one is free
36+
if "base_port" not in kwargs:
37+
port = 6000
38+
while _env is None:
39+
try:
40+
kwargs["base_port"] = port
41+
_env = default_registry[self.env_id].make(**kwargs)
42+
except UnityWorkerInUseException:
43+
port += 1
44+
pass
45+
else:
46+
_env = default_registry[self.env_id].make(**kwargs)
47+
return UnityToPettingZooWrapper(_env, seed)
48+
49+
50+
# Register each environment in default_registry as a PettingZooEnv
51+
for key in default_registry:
52+
env_name = key
53+
if key[0].isdigit():
54+
env_name = key.replace("3", "Three")
55+
if not env_name.isidentifier():
56+
logger.warning(
57+
f"Environment id {env_name} can not be registered since it is"
58+
f"not a valid identifier name."
59+
)
60+
continue
61+
locals()[env_name] = PettingZooEnv(key)

pettingzoo-unity/pettingzoo_unity/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)