-
Notifications
You must be signed in to change notification settings - Fork 7
/
rle_env.py
170 lines (137 loc) · 6.27 KB
/
rle_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
import numpy as np
import os
import gym
import fnmatch
from gym import error, spaces
from gym import utils
from gym.utils import seeding
try:
from rle_python_interface import rle_python_interface
except ImportError as e:
raise error.DependencyNotInstalled("{}. (HINT: you can install RLE dependencies by running 'pip install gym[rle]'.)".format(e))
import logging
logger = logging.getLogger(__name__)
def check_button(action_string, action, value, name, first):
if (action & value) > 0:
if first:
action_string += name
first = False
else:
action_string += ' | ' + name
return action_string, first
def get_action_meaning(action):
action_string = ''
first = True
action_string, first = check_button(action_string, action, 0x1, 'B', first)
action_string, first = check_button(action_string, action, 0x2, 'Y', first)
action_string, first = check_button(action_string, action, 0x4, 'SELECT', first)
action_string, first = check_button(action_string, action, 0x8, 'START', first)
action_string, first = check_button(action_string, action, 0x10, 'UP', first)
action_string, first = check_button(action_string, action, 0x20, 'DOWN', first)
action_string, first = check_button(action_string, action, 0x40, 'LEFT', first)
action_string, first = check_button(action_string, action, 0x80, 'RIGHT', first)
action_string, first = check_button(action_string, action, 0x100, 'A', first)
action_string, first = check_button(action_string, action, 0x200, 'X', first)
action_string, first = check_button(action_string, action, 0x400, 'L', first)
action_string, first = check_button(action_string, action, 0x800, 'R', first)
action_string, first = check_button(action_string, action, 0x1000, 'L2', first)
action_string, first = check_button(action_string, action, 0x2000, 'R2', first)
action_string, first = check_button(action_string, action, 0x4000, 'L3', first)
action_string, first = check_button(action_string, action, 0x8000, 'R3', first)
if action_string == '':
action_string = 'NOOP'
return action_string
def to_ram(rle):
ram_size = rle.getRAMSize()
ram = np.zeros((ram_size),dtype=np.uint8)
rle.getRAM(ram)
return ram
class RleEnv(gym.Env, utils.EzPickle):
metadata = {'render.modes': ['human', 'rgb_array']}
def __init__(self, game='classic_kong', obs_type='ram', frameskip=(2, 5), repeat_action_probability=0.):
"""Frameskip should be either a tuple (indicating a random range to
choose from, with the top value exclude), or an int."""
utils.EzPickle.__init__(self, game, obs_type)
assert obs_type in ('ram', 'image')
self.game_path = self.get_rom_path(game)
self._obs_type = obs_type
self.frameskip = frameskip
self.rle = rle_python_interface.RLEInterface()
self.viewer = None
# Tune (or disable) RLE's action repeat:
# https://github.com/openai/gym/issues/349
assert isinstance(repeat_action_probability, (float, int)), \
"Invalid repeat_action_probability: {!r}".format(repeat_action_probability)
self.rle.setFloat('repeat_action_probability'.encode('utf-8'), repeat_action_probability)
self._seed()
(screen_width, screen_height) = self.rle.getScreenDims()
self._buffer = np.empty((screen_height, screen_width, 4), dtype=np.uint8)
self._action_set = self.rle.getMinimalActionSet()
self.action_space = spaces.Discrete(len(self._action_set))
(screen_width,screen_height) = self.rle.getScreenDims()
ram_size = self.rle.getRAMSize()
if self._obs_type == 'ram':
self.observation_space = spaces.Box(low=np.zeros(ram_size), high=np.zeros(ram_size)+255)
elif self._obs_type == 'image':
self.observation_space = spaces.Box(low=0, high=255, shape=(screen_height, screen_width, 3), dtype=np.uint8)
else:
raise error.Error('Unrecognized observation type: {}'.format(self._obs_type))
def get_rom_path(self, game):
cwd = os.path.dirname(__file__)
roms_path = os.path.join(cwd, 'roms')
for file in os.listdir(roms_path):
if fnmatch.fnmatch(file, game + '*'):
return os.path.join(roms_path, file)
def _seed(self, seed=None):
self.np_random, seed1 = seeding.np_random(seed)
# Derive a random seed. This gets passed as a uint, but gets
# checked as an int elsewhere, so we need to keep it below
# 2**31.
seed2 = seeding.hash_seed(seed1 + 1) % 2**31
# Empirically, we need to seed before loading the ROM.
self.rle.setInt(b'random_seed', seed2)
self.rle.loadROM(self.game_path, 'snes')
return [seed1, seed2]
def _step(self, a):
reward = 0.0
action = self._action_set[a]
if isinstance(self.frameskip, int):
num_steps = self.frameskip
else:
num_steps = self.np_random.randint(self.frameskip[0], self.frameskip[1])
for _ in range(num_steps):
reward += self.rle.act(action)
ob = self._get_obs()
return ob, reward, self.rle.game_over(), {"rle.lives": self.rle.lives()}
def _get_image(self):
self.rle.getScreenRGB(self._buffer)
return self._buffer[:, :, [0, 1, 2]]
def _get_ram(self):
return to_ram(self.rle)
@property
def _n_actions(self):
return len(self._action_set)
def _get_obs(self):
if self._obs_type == 'ram':
return self._get_ram()
if self._obs_type == 'image':
return self._get_image()
def _reset(self):
self.rle.reset_game()
return self._get_obs()
def _render(self, mode='human', close=False):
if close:
if self.viewer is not None:
self.viewer.close()
self.viewer = None
return
img = self._get_image()
if mode == 'rgb_array':
return img
elif mode == 'human':
from gym.envs.classic_control import rendering
if self.viewer is None:
self.viewer = rendering.SimpleImageViewer()
self.viewer.imshow(img)
def get_action_meanings(self):
return [get_action_meaning(i) for i in self._action_set]