Skip to content

Commit 18b6796

Browse files
committed
Update colab to match pettingZoo import api
1 parent 6d33c73 commit 18b6796

File tree

2 files changed

+138
-81
lines changed

2 files changed

+138
-81
lines changed

pettingzoo-unity/Colab_PettingZoo.ipynb

Lines changed: 137 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,47 @@
11
{
2-
"nbformat": 4,
3-
"nbformat_minor": 0,
4-
"metadata": {
5-
"colab": {
6-
"name": "Colab-UnityEnvironment-1-Run.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",
48-
"execution_count": null,
49-
"metadata": {},
50-
"outputs": [],
19+
"execution_count": 1,
20+
"metadata": {
21+
"tags": []
22+
},
23+
"outputs": [
24+
{
25+
"data": {
26+
"text/html": [
27+
"\n",
28+
" <progress\n",
29+
" value='100'\n",
30+
" max='100',\n",
31+
" style='width: 100%'\n",
32+
" >\n",
33+
" 100\n",
34+
" </progress>\n",
35+
" "
36+
],
37+
"text/plain": [
38+
"<IPython.core.display.HTML object>"
39+
]
40+
},
41+
"metadata": {},
42+
"output_type": "display_data"
43+
}
44+
],
5145
"source": [
5246
"#@title Install Rendering Dependencies { display-mode: \"form\" }\n",
5347
"#@markdown (You only need to run this code when using Colab's hosted runtime)\n",
@@ -125,17 +119,25 @@
125119
]
126120
},
127121
{
122+
"cell_type": "markdown",
123+
"metadata": {},
128124
"source": [
129125
"### Installing ml-agents"
130-
],
131-
"cell_type": "markdown",
132-
"metadata": {}
126+
]
133127
},
134128
{
135129
"cell_type": "code",
136-
"execution_count": null,
130+
"execution_count": 2,
137131
"metadata": {},
138-
"outputs": [],
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"ml-agents already installed\n"
138+
]
139+
}
140+
],
139141
"source": [
140142
"try:\n",
141143
" import mlagents\n",
@@ -149,36 +151,59 @@
149151
]
150152
},
151153
{
154+
"cell_type": "markdown",
155+
"metadata": {},
152156
"source": [
153157
"## Run the Environment"
154-
],
155-
"cell_type": "markdown",
156-
"metadata": {}
158+
]
157159
},
158160
{
159-
"cell_type": "code",
161+
"cell_type": "markdown",
160162
"metadata": {
161-
"id": "htb-p1hSNX7D"
163+
"jp-MarkdownHeadingCollapsed": true,
164+
"tags": []
162165
},
163166
"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": []
167+
"List of available environments:\n",
168+
"* Basic\n",
169+
"* ThreeDBall\n",
170+
"* ThreeDBallHard\n",
171+
"* GridWorld\n",
172+
"* Hallway\n",
173+
"* VisualHallway\n",
174+
"* CrawlerDynamicTarget\n",
175+
"* CrawlerStaticTarget\n",
176+
"* Bouncer\n",
177+
"* SoccerTwos\n",
178+
"* PushBlock\n",
179+
"* VisualPushBlock\n",
180+
"* WallJump\n",
181+
"* Tennis\n",
182+
"* Reacher\n",
183+
"* Pyramids\n",
184+
"* VisualPyramids\n",
185+
"* Walker\n",
186+
"* FoodCollector\n",
187+
"* VisualFoodCollector\n",
188+
"* StrikersVsGoalie\n",
189+
"* WormStaticTarget\n",
190+
"* WormDynamicTarget"
191+
]
169192
},
170193
{
194+
"cell_type": "markdown",
195+
"metadata": {},
171196
"source": [
172197
"### Start Environment with PettingZoo Wrapper"
173-
],
174-
"cell_type": "markdown",
175-
"metadata": {}
198+
]
176199
},
177200
{
178201
"cell_type": "code",
202+
"execution_count": null,
179203
"metadata": {
180204
"id": "YSf-WhxbqtLw"
181205
},
206+
"outputs": [],
182207
"source": [
183208
"# -----------------\n",
184209
"# This code is used to close an env that might not have been closed before\n",
@@ -189,29 +214,26 @@
189214
"# -----------------\n",
190215
"\n",
191216
"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": []
217+
"from pettingzoo_unity.envs import StrikersVsGoalie # import unity environment\n",
218+
"env = StrikersVsGoalie.env()"
219+
]
200220
},
201221
{
222+
"cell_type": "markdown",
223+
"metadata": {},
202224
"source": [
203225
"### Stepping the environment\n",
204226
"\n",
205227
"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": {}
228+
]
209229
},
210230
{
211231
"cell_type": "code",
232+
"execution_count": null,
212233
"metadata": {
213234
"id": "dhtl0mpeqxYi"
214235
},
236+
"outputs": [],
215237
"source": [
216238
"num_cycles = 10\n",
217239
"\n",
@@ -225,18 +247,16 @@
225247
" else:\n",
226248
" action = env.action_spaces[agent].sample() # randomly choose an action for example\n",
227249
" env.step(action)"
228-
],
229-
"execution_count": null,
230-
"outputs": []
250+
]
231251
},
232252
{
253+
"cell_type": "markdown",
254+
"metadata": {},
233255
"source": [
234256
"### Additional Environment API\n",
235257
"\n",
236258
"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": {}
259+
]
240260
},
241261
{
242262
"cell_type": "code",
@@ -281,22 +301,59 @@
281301
]
282302
},
283303
{
304+
"cell_type": "markdown",
305+
"metadata": {},
284306
"source": [
285307
"### Close the Environment to free the port it is using"
286-
],
287-
"cell_type": "markdown",
288-
"metadata": {}
308+
]
289309
},
290310
{
291311
"cell_type": "code",
312+
"execution_count": null,
292313
"metadata": {
293314
"id": "a7KatdThq7OV"
294315
},
316+
"outputs": [],
295317
"source": [
296318
"env.close()"
297-
],
298-
"execution_count": null,
299-
"outputs": []
319+
]
300320
}
301-
]
321+
],
322+
"metadata": {
323+
"colab": {
324+
"collapsed_sections": [],
325+
"name": "Colab-UnityEnvironment-1-Run.ipynb",
326+
"private_outputs": true,
327+
"provenance": [],
328+
"toc_visible": true
329+
},
330+
"kernelspec": {
331+
"display_name": "Python 3 (ipykernel)",
332+
"language": "python",
333+
"name": "python3"
334+
},
335+
"language_info": {
336+
"codemirror_mode": {
337+
"name": "ipython",
338+
"version": 3
339+
},
340+
"file_extension": ".py",
341+
"mimetype": "text/x-python",
342+
"name": "python",
343+
"nbconvert_exporter": "python",
344+
"pygments_lexer": "ipython3",
345+
"version": "3.7.8"
346+
},
347+
"pycharm": {
348+
"stem_cell": {
349+
"cell_type": "raw",
350+
"metadata": {
351+
"collapsed": false
352+
},
353+
"source": []
354+
}
355+
}
356+
},
357+
"nbformat": 4,
358+
"nbformat_minor": 4
302359
}

pettingzoo-unity/pettingzoo_unity/envs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def env(self, seed: Optional[int] = None, **kwargs):
4242

4343

4444
for env_id in default_registry:
45-
env_id = env_id.replace("3DBall", "ThreeDBall")
45+
env_id = env_id.replace("3", "Three")
4646
if not env_id.isidentifier():
4747
logger.warning(
4848
f"Environment id {env_id} can not be registered since it is"

0 commit comments

Comments
 (0)