forked from ryanprinster/DQN_EWC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
executable file
·68 lines (49 loc) · 1.82 KB
/
environment.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
import gym
import scipy.misc
def _rgb_to_grayscale(image):
"""
Convert an RGB-image into gray-scale using a formula from Wikipedia:
https://en.wikipedia.org/wiki/Grayscale
"""
# Get the separate colour-channels.
r, g, b = image[:, :, 0], image[:, :, 1], image[:, :, 2]
# Convert to gray-scale using the Wikipedia formula.
img_gray = 0.2990 * r + 0.5870 * g + 0.1140 * b
return img_gray
def _pre_process_image(image, size):
"""Pre-process a raw image from the game-environment."""
# Convert image to gray-scale.
img = _rgb_to_grayscale(image)
# Resize to the desired size using SciPy for convenience.
img = scipy.misc.imresize(img, size=size, interp='bicubic')
return img
class Environment:
def __init__(self, params):
self.gym = gym.make(params.game)
self.observation = None
self.display = params.display
self.terminal = False
self.dims = (params.height, params.width)
self.training = True
def actions(self):
return self.gym.action_space.n
def restart(self):
self.observation = self.gym.reset()
self.terminal = False
def act(self, action):
if self.display:
if self.training == False:
self.gym.render()
self.observation, reward, self.terminal, info = self.gym.step(action)
if self.terminal:
#if self.display:
# print "No more lives, restarting"
self.gym.reset()
return reward
def getScreen(self):
return _pre_process_image(self.observation, self.dims)
def isTerminal(self):
return self.terminal
def get_lives(self):
"""Get the number of lives the agent has in the game-environment."""
return self.gym.unwrapped.ale.lives()