Skip to content

Commit 56e8ae7

Browse files
KaleabTesseraelliottowerpseudo-rnd-thoughts
authored
Update Melting Pot wrapper to use pypi release (#111)
Co-authored-by: Elliot Tower <etower@farama.org> Co-authored-by: Mark Towers <mark.m.towers@gmail.com>
1 parent abf3122 commit 56e8ae7

9 files changed

Lines changed: 41 additions & 115 deletions

File tree

.github/workflows/optional-test-meltingpot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
# Melting Pot
1414
- run: |
1515
docker build -f bin/meltingpot.Dockerfile \
16-
--build-arg PYTHON_VERSION='3.9' \
16+
--build-arg PYTHON_VERSION='3.10' \
1717
--tag shimmy-meltingpot-docker .
1818
- name: Run meltingpot tests
1919
run: docker run shimmy-meltingpot-docker pytest tests/test_meltingpot.py

bin/meltingpot.Dockerfile

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# A Dockerfile that sets up a full shimmy install with test dependencies
22
# adapted from https://github.com/deepmind/meltingpot/blob/main/.devcontainer/Dockerfile
33

4-
# if PYTHON_VERSION is not specified as a build argument, set it to 3.9.
4+
# if PYTHON_VERSION is not specified as a build argument, set it to 3.10.
55
ARG PYTHON_VERSION
6-
ARG PYTHON_VERSION=${PYTHON_VERSION:-3.9}
6+
ARG PYTHON_VERSION=${PYTHON_VERSION:-3.10}
77
FROM python:$PYTHON_VERSION
88

99
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
@@ -38,40 +38,4 @@ RUN if [ -f "pyproject.toml" ]; then \
3838
mkdir -p bin && mv docker_entrypoint bin/docker_entrypoint; \
3939
fi
4040

41-
# Install Melting Pot dependencies
42-
RUN apt-get update \
43-
&& DEBIAN_FRONTEND=noninteractive apt-get -qq -y install \
44-
build-essential \
45-
curl \
46-
ffmpeg \
47-
git
48-
49-
# Install lab2d (appropriate version for architecture)
50-
RUN if [ "$(uname -m)" != 'x86_64' ]; then \
51-
echo "No Lab2d wheel available for $(uname -m) machines." >&2 \
52-
exit 1; \
53-
elif [ "$(uname -s)" = 'Linux' ]; then \
54-
pip install https://github.com/deepmind/lab2d/releases/download/release_candidate_2022-03-24/dmlab2d-1.0-cp39-cp39-manylinux_2_31_x86_64.whl ;\
55-
else \
56-
pip install https://github.com/deepmind/lab2d/releases/download/release_candidate_2022-03-24/dmlab2d-1.0-cp39-cp39-macosx_10_15_x86_64.whl ;\
57-
fi
58-
59-
# Download Melting Pot assets
60-
RUN mkdir -p /workspaces/meltingpot/meltingpot \
61-
&& curl -SL https://storage.googleapis.com/dm-meltingpot/meltingpot-assets-2.1.0.tar.gz \
62-
| tar -xz --directory=/workspaces/meltingpot/meltingpot
63-
64-
# Clone Melting Pot repository and install dependencies
65-
RUN git clone https://github.com/deepmind/meltingpot.git
66-
RUN cp -r meltingpot/ /workspaces/meltingpot/ && rm -R meltingpot/
67-
68-
# Checkout the last commit with 3.9 support that passed CI (July 17 2023)
69-
# Newer versions and pypi wheels caused issues
70-
RUN cd /workspaces/meltingpot/meltingpot && git checkout ed2e6e79ca49a14a22aa4b6117ac407f39fbef81
71-
72-
RUN pip install -e /workspaces/meltingpot/meltingpot
73-
74-
# Set Python path for meltingpot
75-
ENV PYTHONPATH "${PYTHONPATH}:/workspaces/meltingpot/meltingpot/"
76-
7741
ENTRYPOINT ["/usr/local/shimmy/bin/docker_entrypoint"]

docs/environments/meltingpot.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ Shimmy provides compatibility wrappers to convert all [Melting Pot](https://gith
2525
To install `shimmy` and required dependencies:
2626

2727
```
28-
pip install shimmy[melting-pot]
28+
pip install shimmy[meltingpot]
2929
```
3030

31-
Melting Pot is not distributed via [pypi](https://pypi.org/) and must be installed manually. We provide an [installation script](https://github.com/Farama-Foundation/Shimmy/blob/main/scripts/install_melting_pot.sh) (compatible with macOS and linux). For troubleshooting, refer to the official [installation instructions](https://github.com/deepmind/meltingpot#installation).
31+
We also provide a [Dockerfile](https://github.com/Farama-Foundation/Shimmy/blob/main/bin/meltingpot.Dockerfile) for reproducibility and cross-platform compatibility (see
3232

33-
We also provide a [Dockerfile](https://github.com/Farama-Foundation/Shimmy/blob/main/bin/meltingpot.Dockerfile) for reproducibility and cross-platform compatibility (see [Installation](/content/getting_started/))
33+
[Installation](/content/getting_started/))
3434

3535
```
3636
curl https://raw.githubusercontent.com/Farama-Foundation/Shimmy/main/bin/meltingpot.Dockerfile | docker build -t meltingpot -f - . && docker run -it meltingpot

scripts/install_melting_pot.sh

Lines changed: 0 additions & 44 deletions
This file was deleted.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_version():
4646
],
4747
"dm-lab": ["dm-env>=1.6"],
4848
"openspiel": ["open_spiel>=1.2", "pettingzoo>=1.23"],
49-
"meltingpot": ["pettingzoo>=1.23"],
49+
"meltingpot": ["pettingzoo>=1.23", "dm-meltingpot>=2.2.0; python_version > '3.9'"],
5050
"bsuite": ["bsuite>=0.3.5"],
5151
}
5252
extras["all"] = [

shimmy/dm_control_multiagent_compatibility.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
import functools
5-
from itertools import repeat
65
from typing import TYPE_CHECKING, Any
76

87
import dm_control.composer
@@ -43,27 +42,30 @@ def _unravel_ma_timestep(
4342
observations: dict[AgentID, Any] = dict(zip(agents, list_observations))
4443

4544
# sometimes deepmind decides not to reward people
46-
rewards: dict[AgentID, float] = dict(zip(agents, repeat(0.0)))
45+
rewards: dict[AgentID, float] = {agent: 0.0 for agent in agents}
46+
4747
if timestep.reward:
4848
rewards = dict(zip(agents, timestep.reward))
4949

5050
# expand everything else
51-
terminations: dict[AgentID, bool] = dict(zip(agents, repeat(term)))
52-
truncations: dict[AgentID, bool] = dict(zip(agents, repeat(trunc)))
53-
54-
# duplicate infos
55-
info = {
56-
"timestep.discount": timestep.discount,
57-
"timestep.step_type": timestep.step_type,
51+
terminations: dict[AgentID, bool] = {agent: term for agent in agents}
52+
truncations: dict[AgentID, bool] = {agent: trunc for agent in agents}
53+
54+
# duplicate infos across agents
55+
infos = {
56+
agent: {
57+
"timestep.discount": timestep.discount,
58+
"timestep.step_type": timestep.step_type,
59+
}
60+
for agent in agents
5861
}
59-
info: dict[AgentID, Any] = dict(zip(agents, repeat(info)))
6062

6163
return (
6264
observations,
6365
rewards,
6466
terminations,
6567
truncations,
66-
info,
68+
infos,
6769
)
6870

6971

shimmy/meltingpot_compatibility.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
import functools
11+
from itertools import repeat
1112
from typing import TYPE_CHECKING, Any, Optional
1213

1314
import dm_env
@@ -20,7 +21,8 @@
2021
import shimmy.utils.meltingpot as utils
2122

2223
if TYPE_CHECKING:
23-
import meltingpot.python
24+
import meltingpot
25+
from meltingpot.utils.substrates import substrate
2426

2527

2628
class MeltingPotCompatibilityV0(ParallelEnv, EzPickle):
@@ -46,15 +48,15 @@ class MeltingPotCompatibilityV0(ParallelEnv, EzPickle):
4648

4749
def __init__(
4850
self,
49-
env: meltingpot.python.utils.substrates.substrate.Substrate | None = None,
51+
env: substrate.Substrate | None = None,
5052
substrate_name: str | None = None,
5153
max_cycles: int = MAX_CYCLES,
5254
render_mode: str | None = None,
5355
):
5456
"""Wrapper that converts a Melting Pot environment into a PettingZoo environment.
5557
5658
Args:
57-
env (Optional[meltingpot.python.utils.substrates.substrate.Substrate]): existing Melting Pot environment to wrap
59+
env (Optional[substrate.Substrate]): existing Melting Pot environment to wrap
5860
substrate_name (Optional[str]): name of Melting Pot substrate to load (instead of existing environment)
5961
max_cycles (Optional[int]): maximum number of cycles before truncation
6062
render_mode (Optional[str]): rendering mode
@@ -158,7 +160,7 @@ def reset(
158160
self,
159161
seed: int | None = None,
160162
options: dict | None = None,
161-
) -> tuple[ObsDict, dict[str, Any]]:
163+
) -> tuple[ObsDict, dict[AgentID, Any]]:
162164
"""reset.
163165
164166
Resets the environment.
@@ -176,11 +178,17 @@ def reset(
176178

177179
observations = utils.timestep_to_observations(timestep)
178180

179-
return observations, {
180-
"step-type": timestep.step_type,
181-
"discount": timestep.discount,
181+
# duplicate infos across agents
182+
infos = {
183+
agent: {
184+
"timestep.discount": timestep.discount,
185+
"timestep.step_type": timestep.step_type,
186+
}
187+
for agent in self.agents
182188
}
183189

190+
return observations, infos
191+
184192
def step(
185193
self, actions: ActionDict
186194
) -> tuple[

shimmy/utils/meltingpot.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,22 @@ def load_meltingpot(substrate_name: str):
1818
substrate_name: str
1919
2020
Returns:
21-
env: meltingpot.python.utils.substrates.substrate.Substrate
21+
env: meltingpot.utils.substrates.substrate.Substrate
2222
"""
23-
import meltingpot.python
23+
import meltingpot
2424
from ml_collections import config_dict
2525

2626
# Create env config
2727
substrate_name = substrate_name
28-
player_roles = meltingpot.python.substrate.get_config(
29-
substrate_name
30-
).default_player_roles
28+
player_roles = meltingpot.substrate.get_config(substrate_name).default_player_roles
3129
env_config = {
3230
"substrate": substrate_name,
3331
"roles": player_roles,
3432
}
3533

3634
# Build substrate from pickle
3735
env_config = config_dict.ConfigDict(env_config)
38-
env = meltingpot.python.substrate.build(
39-
env_config["substrate"], roles=env_config["roles"]
40-
)
36+
env = meltingpot.substrate.build(env_config["substrate"], roles=env_config["roles"])
4137
return env
4238

4339

tests/test_meltingpot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
pytest.importorskip("meltingpot")
1212

13-
import meltingpot.python
14-
from meltingpot.python.configs.substrates import SUBSTRATES
13+
import meltingpot
14+
from meltingpot.substrate import SUBSTRATES
1515

1616
from shimmy.meltingpot_compatibility import MeltingPotCompatibilityV0
1717
from shimmy.utils.meltingpot import load_meltingpot

0 commit comments

Comments
 (0)