forked from cogment/cogment-verse
-
Notifications
You must be signed in to change notification settings - Fork 2
/
zoo_env.py
137 lines (113 loc) · 4.78 KB
/
zoo_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Copyright 2021 AI Redefined Inc. <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
import gym
import numpy as np
from cogment_verse_environment.base import GymObservation
from cogment_verse_environment.env_spec import EnvSpec
from cogment_verse_environment.gym_env import GymEnv
def legal_moves_from_mask(action_mask):
return [i for i, action in enumerate(action_mask) if action]
class PettingZooEnv(GymEnv):
"""
Class for loading gym built-in environments.
"""
def __init__(self, *, env_name, flatten=True, **kwargs):
"""
Args:
env_name: Name of the environment (NOTE: make sure it is available at gym.envs.registry.all())
"""
self._flatten = flatten
self._cumulative_rewards = None
self._rewards = None
super().__init__(env_name=env_name, **kwargs)
self.num_players = len(self._env.action_spaces)
def create_env(self, env_name, **_kwargs):
"""Function used to create the environment. Subclasses can override this method
if they are using a gym style environment that needs special logic.
"""
module_name = "pettingzoo.classic." + env_name
env_module = importlib.import_module(module_name)
self._env = env_module.env()
def create_env_spec(self, env_name, **_kwargs):
"""Function used to create the specification. Subclasses can override this method
if they are using a gym style ebservation, reward, done, self._turn, infonvironment that needs special logic.
"""
action_space = self._env.action_spaces["player_0"]
observation_space = self._env.observation_spaces["player_0"]["observation"]
if isinstance(observation_space, gym.spaces.Tuple):
obs_spaces = self._env.observation_space.spaces
else:
obs_spaces = [observation_space]
if isinstance(action_space, gym.spaces.Tuple):
act_spaces = action_space.spaces
else:
act_spaces = [action_space]
act_dim = []
for act_space in act_spaces:
if isinstance(act_space, gym.spaces.Discrete):
act_dim.append(act_space.n)
else:
act_dim.append(act_space.shape)
return EnvSpec(
env_name=env_name,
obs_dim=[space.shape for space in obs_spaces],
act_dim=act_dim,
act_shape=[space.shape for space in act_spaces],
)
def _prepare_obs(self, obs):
if self._flatten:
return obs.reshape(-1)
return obs
def reset(self):
self._env.reset()
self._rewards = np.full(self.num_players, 0.0)
self._cumulative_rewards = np.full(self.num_players, 0.0)
self._turn = 0
obs, _, done, info = self._env.last()
return GymObservation(
observation=self._prepare_obs(obs["observation"]),
current_player=self._turn,
legal_moves_as_int=legal_moves_from_mask(obs["action_mask"]),
rewards=self._rewards,
done=done,
info=info,
)
def step(self, action=None):
if isinstance(action, np.ndarray):
action = action.tolist()
self._env.step(action)
obs, _, done, info = self._env.last()
# 'last' method only returns reward for the actor that performed the action,
# so for example we can lose the -1 reward for the losing player
# NB: PettingZoo returns _cumulative rewards_,
# so we have to compute the per-step reward
cumulative_rewards = np.array([self._env.rewards[agent] for agent in self._env.agents])
self._rewards = cumulative_rewards - self._cumulative_rewards
self._cumulative_rewards = cumulative_rewards
self._turn = (self._turn + 1) % self.num_players
return GymObservation(
observation=self._prepare_obs(obs["observation"]),
current_player=self._turn,
legal_moves_as_int=legal_moves_from_mask(obs["action_mask"]),
rewards=self._rewards,
done=done,
info=info,
)
def render(self, mode="rgb_array"):
return self._env.render(mode=mode)
def seed(self, seed=None):
self._env.seed(seed=seed)
def close(self):
self._env.close()