forked from openai/procgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.py
199 lines (172 loc) · 5.84 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import random
from .libenv import CVecEnv
import numpy as np
from .build import build
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
ENV_NAMES = [
"bigfish",
"bossfight",
"caveflyer",
"chaser",
"collector",
"climber",
"coinrun",
"dodgeball",
"fruitbot",
"heist",
"heistpp",
"jumper",
"leaper",
"maze",
"miner",
"ninja",
"plunder",
"starpilot",
]
EXPLORATION_LEVEL_SEEDS = {
"coinrun": 1949448038,
"caveflyer": 1259048185,
"leaper": 1318677581,
"jumper": 1434825276,
"maze": 158988835,
"heist": 876640971,
"climber": 1561126160,
"ninja": 1123500215,
}
# should match DistributionMode in game.h, except for 'exploration' which is handled by Python
DISTRIBUTION_MODE_DICT = {
"easy": 0,
"hard": 1,
"extreme": 2,
"memory": 10,
"exploration": 20,
}
def create_random_seed():
rand_seed = random.SystemRandom().randint(0, 2 ** 31 - 1)
try:
# force MPI processes to definitely choose different random seeds
from mpi4py import MPI
rand_seed = rand_seed - (rand_seed % MPI.COMM_WORLD.size) + MPI.COMM_WORLD.rank
except ModuleNotFoundError:
pass
return rand_seed
class BaseProcgenEnv(CVecEnv):
"""
Base procedurally generated environment
"""
def __init__(
self,
num_envs,
env_name,
options,
debug=False,
rand_seed=None,
num_levels=0,
start_level=0,
use_sequential_levels=False,
debug_mode=0,
resource_root=None,
num_threads=4,
additional_info_spaces = None,
additional_obs_spaces = None,
max_episodes_per_game = None,
):
if resource_root is None:
resource_root = os.path.join(SCRIPT_DIR, "data", "assets") + os.sep
assert os.path.exists(resource_root)
lib_dir = os.path.join(SCRIPT_DIR, "data", "prebuilt")
if os.path.exists(lib_dir):
assert any([os.path.exists(os.path.join(lib_dir, name)) for name in ["libenv.so", "libenv.dylib", "env.dll"]]), "package is installed, but the prebuilt environment library is missing"
assert not debug, "debug has no effect for pre-compiled library"
else:
# only compile if we don't find a pre-built binary
lib_dir = build(debug=debug)
self.combos = self.get_combos()
if rand_seed is None:
rand_seed = create_random_seed()
if max_episodes_per_game is None:
max_episodes_per_game = np.zeros(num_envs,dtype=np.int32)
elif isinstance(max_episodes_per_game,(tuple,list,set,np.ndarray)):
max_episodes_per_game = np.array(max_episodes_per_game,dtype=np.int32).flatten()
else:
max_episodes_per_game = np.repeat(np.array(max_episodes_per_game,dtype=np.int32),num_envs)
assert max_episodes_per_game.size == num_envs
options.update(
{
"env_name": env_name,
"num_levels": num_levels,
"start_level": start_level,
"num_actions": len(self.combos),
"use_sequential_levels": bool(use_sequential_levels),
"debug_mode": debug_mode,
"rand_seed": rand_seed,
"num_threads": num_threads,
# these will only be used the first time an environment is created in a process
"resource_root": resource_root,
"max_episodes_per_game": max_episodes_per_game,
}
)
self.options = options
super().__init__(
lib_dir=lib_dir, num_envs=num_envs, debug=debug, options=options, additional_info_spaces=additional_info_spaces, additional_obs_spaces=additional_obs_spaces
)
def get_combos(self):
return [
("LEFT", "DOWN"),
("LEFT",),
("LEFT", "UP"),
("DOWN",),
(),
("UP",),
("RIGHT", "DOWN"),
("RIGHT",),
("RIGHT", "UP"),
("D",),
("A",),
("W",),
("S",),
("Q",),
("E",),
]
def step_async(self, actions):
# tensorflow may return int64 actions (https://github.com/openai/gym/blob/master/gym/spaces/discrete.py#L13)
# so always cast actions to int32
return super().step_async(actions.astype(np.int32))
class ProcgenEnv(BaseProcgenEnv):
def __init__(
self,
num_envs,
env_name,
center_agent=True,
options=None,
use_generated_assets=False,
paint_vel_info=False,
distribution_mode="hard",
**kwargs,
):
if options is None:
options = {}
else:
options = dict(options)
assert (
distribution_mode in DISTRIBUTION_MODE_DICT
), f'"{distribution_mode}" is not a valid distribution mode.'
if distribution_mode == "exploration":
assert env_name in EXPLORATION_LEVEL_SEEDS, f"{env_name} does not support exploration mode"
distribution_mode = DISTRIBUTION_MODE_DICT["hard"]
assert "num_levels" not in kwargs, "exploration mode overrides num_levels"
kwargs["num_levels"] = 1
assert "start_level" not in kwargs, "exploration mode overrides start_level"
kwargs["start_level"] = EXPLORATION_LEVEL_SEEDS[env_name]
else:
distribution_mode = DISTRIBUTION_MODE_DICT[distribution_mode]
options.update(
{
"center_agent": bool(center_agent),
"use_generated_assets": bool(use_generated_assets),
"paint_vel_info": bool(paint_vel_info),
"distribution_mode": distribution_mode,
}
)
super().__init__(num_envs, env_name, options, **kwargs)