Skip to content

Commit eb2a4a1

Browse files
Hotfix on Gym and the notebooks (#3496)
* Hotfix on Gym and the notebooks * REMOVING THE PROTO GENERATION TESTS * REMOVING THE PROTO GENERATION TESTS * Update notebooks/getting-started-gym.ipynb Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Trying to enforce nuget version * Update notebooks/getting-started-gym.ipynb Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Removing proto_generation * Update notebooks/getting-started-gym.ipynb Co-Authored-By: Chris Elion <chris.elion@unity3d.com> * Update docstring Co-authored-by: Chris Elion <celion@gmail.com>
1 parent 298f844 commit eb2a4a1

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ workflows:
273273
# Test python 3.7 with the newest supported versions
274274
pip_constraints: test_constraints_max_tf2_version.txt
275275
- markdown_link_check
276-
- protobuf_generation_check
276+
#- protobuf_generation_check
277277
- pre-commit
278278
- deploy:
279279
name: deploy ml-agents-envs

gym-unity/gym_unity/envs/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def reset(self) -> Union[List[np.ndarray], np.ndarray]:
164164
"""Resets the state of the environment and returns an initial observation.
165165
In the case of multi-agent environments, this is a list.
166166
Returns: observation (object/list): the initial observation of the
167-
space.
167+
space.
168168
"""
169169
step_result = self._step(True)
170170
n_agents = step_result.n_agents()
@@ -250,7 +250,7 @@ def _single_step(self, info: BatchedStepResult) -> GymSingleStepResult:
250250
default_observation = self._get_vector_obs(info)[0, :]
251251
else:
252252
raise UnityGymException(
253-
"The Agent does not have vector observations and the environment was not setup"
253+
"The Agent does not have vector observations and the environment was not setup "
254254
+ "to use visual observations."
255255
)
256256

@@ -345,12 +345,12 @@ def seed(self, seed: Any = None) -> None:
345345
def _check_agents(self, n_agents: int) -> None:
346346
if not self._multiagent and n_agents > 1:
347347
raise UnityGymException(
348-
"The environment was launched as a single-agent environment, however"
348+
"The environment was launched as a single-agent environment, however "
349349
"there is more than one agent in the scene."
350350
)
351351
elif self._multiagent and n_agents <= 1:
352352
raise UnityGymException(
353-
"The environment was launched as a mutli-agent environment, however"
353+
"The environment was launched as a mutli-agent environment, however "
354354
"there is only one agent in the scene."
355355
)
356356
if self._n_agents == -1:
@@ -438,7 +438,7 @@ def _step(self, needs_reset: bool = False) -> BatchedStepResult:
438438
while info.n_agents() - sum(info.done) < self._n_agents:
439439
if not info.done.all():
440440
raise UnityGymException(
441-
"The environment does not have the expected amount of agents."
441+
"The environment does not have the expected amount of agents. "
442442
+ "Some agents did not request decisions at the same time."
443443
)
444444
for agent_id, reward in zip(info.agent_id, info.reward):
@@ -543,15 +543,16 @@ def register_new_agent_id(self, agent_id: int) -> float:
543543
def get_id_permutation(self, agent_ids: List[int]) -> List[int]:
544544
"""
545545
Get the permutation from new agent ids to the order that preserves the positions of previous agents.
546-
The result is a list with each integer from 0 to len(agent_ids)-1 appearing exactly once.
546+
The result is a list with each integer from 0 to len(_agent_id_to_gym_index)-1
547+
appearing exactly once.
547548
"""
548549
# Map the new agent ids to the their index
549550
new_agent_ids_to_index = {
550551
agent_id: idx for idx, agent_id in enumerate(agent_ids)
551552
}
552553

553554
# Make the output list. We don't write to it sequentially, so start with dummy values.
554-
new_permutation = [-1] * len(agent_ids)
555+
new_permutation = [-1] * len(self._agent_id_to_gym_index)
555556

556557
# For each agent ID, find the new index of the agent, and write it in the original index.
557558
for agent_id, original_index in self._agent_id_to_gym_index.items():

notebooks/getting-started-gym.ipynb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,33 @@
1515
"source": [
1616
"## Single-Agent Environments\n",
1717
"\n",
18-
"The first five steps show how to use the `UnityEnv` wrapper with single-agent environments. See below step five for how to use with multi-agent environments."
18+
"These steps show how to use the `UnityEnv` wrapper with single-agent environments. See section below for how to use with multi-agent environments."
1919
]
2020
},
2121
{
2222
"cell_type": "markdown",
2323
"metadata": {},
2424
"source": [
25-
"### 1. Load dependencies\n",
25+
"### 1.Set environment parameters\n",
26+
"Be sure to set `env_name` to the name of the Unity environment file you want to launch.\n",
27+
"\n",
28+
"__Note__ : All the example environments have multiple agents by default. You can disable the duplicates in the inspector before building the executable."
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"env_name = \"../envs/GridWorldSingle\" # Name of the Unity environment binary to launch"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"### 2. Load dependencies\n",
2645
"\n",
2746
"The following loads the necessary dependencies and checks the Python version (at runtime). ML-Agents Toolkit (v0.3 onwards) requires Python 3."
2847
]
@@ -53,8 +72,8 @@
5372
"cell_type": "markdown",
5473
"metadata": {},
5574
"source": [
56-
"### 2. Start the environment\n",
57-
"`UnityEnv` launches and begins communication with the environment when instantiated. We will be using the `GridWorld` environment. You will need to create an `envs` directory within the `/python` subfolder of the repository, and build the GridWorld environment to that directory. For more information on building Unity environments, see [here](../docs/Learning-Environment-Executable.md)."
75+
"### 3. Start the environment\n",
76+
"`UnityEnv` launches and begins communication with the environment when instantiated. We will be using the `GridWorld` environment. You will need to create an `envs` directory in the root directory of the repository, and build the GridWorld environment to that directory. For more information on building Unity environments, see [here](https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Learning-Environment-Executable.md)."
5877
]
5978
},
6079
{
@@ -63,7 +82,6 @@
6382
"metadata": {},
6483
"outputs": [],
6584
"source": [
66-
"env_name = \"../envs/GridWorld\" # Name of the Unity environment binary to launch\n",
6785
"env = UnityEnv(env_name, worker_id=0, use_visual=True)\n",
6886
"\n",
6987
"# Examine environment parameters\n",
@@ -74,7 +92,7 @@
7492
"cell_type": "markdown",
7593
"metadata": {},
7694
"source": [
77-
"### 3. Examine the observation and state spaces\n",
95+
"### 4. Examine the observation and state spaces\n",
7896
"We can reset the environment to be provided with an initial observation of the environment."
7997
]
8098
},
@@ -103,7 +121,7 @@
103121
"cell_type": "markdown",
104122
"metadata": {},
105123
"source": [
106-
"### 4. Take random actions in the environment\n",
124+
"### 5. Take random actions in the environment\n",
107125
"Once we restart an environment, we can step the environment forward and provide actions to all of the agents within the environment. Here we simply choose random actions using the `env.action_space.sample()` function.\n",
108126
"\n",
109127
"Once this cell is executed, 10 messages will be printed that detail how much reward will be accumulated for the next 10 episodes. The Unity environment will then pause, waiting for further signals telling it what to do next. Thus, not seeing any animation is expected when running this cell."
@@ -129,7 +147,7 @@
129147
"cell_type": "markdown",
130148
"metadata": {},
131149
"source": [
132-
"### 5. Close the environment when finished\n",
150+
"### 6. Close the environment when finished\n",
133151
"When we are finished using an environment, we can close it with the function below."
134152
]
135153
},

notebooks/getting-started.ipynb

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"source": [
1616
"### 1. Set environment parameters\n",
1717
"\n",
18-
"Be sure to set `env_name` to the name of the Unity environment file you want to launch. Ensure that the environment build is in `../envs`."
18+
"Be sure to set `env_name` to the name of the Unity environment file you want to launch. "
1919
]
2020
},
2121
{
@@ -24,8 +24,7 @@
2424
"metadata": {},
2525
"outputs": [],
2626
"source": [
27-
"env_name = \"../envs/GridWorld\" # Name of the Unity environment binary to launch\n",
28-
"train_mode = True # Whether to run the environment in training or inference mode"
27+
"env_name = \"../envs/GridWorld\" # Name of the Unity environment binary to launch"
2928
]
3029
},
3130
{
@@ -77,7 +76,7 @@
7776
"outputs": [],
7877
"source": [
7978
"engine_configuration_channel = EngineConfigurationChannel()\n",
80-
"env = UnityEnvironment(base_port = UnityEnvironment.DEFAULT_EDITOR_PORT, file_name=env_name, side_channels = [engine_configuration_channel])\n",
79+
"env = UnityEnvironment(file_name=env_name, side_channels = [engine_configuration_channel])\n",
8180
"\n",
8281
"#Reset the environment\n",
8382
"env.reset()\n",
@@ -110,22 +109,19 @@
110109
"# Examine the number of observations per Agent\n",
111110
"print(\"Number of observations : \", len(group_spec.observation_shapes))\n",
112111
"\n",
113-
"# Examine the state space for the first observation for all agents\n",
114-
"print(\"Agent state looks like: \\n{}\".format(step_result.obs[0]))\n",
115-
"\n",
116-
"# Examine the state space for the first observation for the first agent\n",
117-
"print(\"Agent state looks like: \\n{}\".format(step_result.obs[0][0]))\n",
118-
"\n",
119112
"# Is there a visual observation ?\n",
120113
"vis_obs = any([len(shape) == 3 for shape in group_spec.observation_shapes])\n",
121114
"print(\"Is there a visual observation ?\", vis_obs)\n",
122115
"\n",
123116
"# Examine the visual observations\n",
124117
"if vis_obs:\n",
125118
" vis_obs_index = next(i for i,v in enumerate(group_spec.observation_shapes) if len(v) == 3)\n",
126-
" print(\"Agent visual observation look like:\")\n",
119+
" print(\"Agent visual observation looks like:\")\n",
127120
" obs = step_result.obs[vis_obs_index]\n",
128-
" plt.imshow(obs[0,:,:,:])\n"
121+
" plt.imshow(obs[0,:,:,:])\n",
122+
"else:\n",
123+
" # Examine the state space for the first observation for the first agent\n",
124+
" print(\"First Agent observation looks like: \\n{}\".format(step_result.obs[0][0]))"
129125
]
130126
},
131127
{
@@ -181,13 +177,6 @@
181177
"source": [
182178
"env.close()"
183179
]
184-
},
185-
{
186-
"cell_type": "code",
187-
"execution_count": null,
188-
"metadata": {},
189-
"outputs": [],
190-
"source": []
191180
}
192181
],
193182
"metadata": {

0 commit comments

Comments
 (0)