-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment.py
executable file
·168 lines (130 loc) · 5.57 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
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
from mss import mss
import time
from PIL import Image
from collections import deque
from win32api import GetSystemMetrics
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from action_space import ActionSpace
# Helper libraries
import numpy as np
import datetime
class Environment:
def __init__(self, space_sleep=0.58, no_action_sleep=0.02):
self.url = 'http://www.trex-game.skipser.com/'
self.window_width = GetSystemMetrics(0) * 0.37
self.window_height = GetSystemMetrics(1) * 0.8
self.sct = mss()
self.bbox = {'top': 350, 'left': 50, 'width': 630, 'height': 80}
self.terminal_bbox = {'top': 360, 'left': 357, 'width': 5, 'height': 5}
self.game_won_array = np.load('game_won.npy')
self.state = None
self.frame_history = deque(maxlen=4)
self.driver = None
self.window_element = None
self.action_space = None
self.actions = None
self.game_won = False
self.space_sleep = space_sleep
self.no_action_sleep = no_action_sleep
def render(self):
opts = Options()
opts.add_argument(f"--width={self.window_width}")
opts.add_argument(f"--height={self.window_height}")
self.driver = webdriver.Firefox(executable_path='/Users/ryano/Downloads/geckodriver-v0.27.0-win64/geckodriver',
options=opts)
self.driver.get(self.url)
self.window_element = self.driver.find_element_by_id("t")
self.action_space = ActionSpace(self.window_element,
space_sleep=self.space_sleep,
no_action_sleep=self.no_action_sleep)
self.actions = self.action_space.actions
def step(self, action):
self.actions[action].act()
self.frame_history.append(self._grab_sct())
next_state = self._update_state()
terminal = self._is_terminal()
if not terminal:
reward = 0.1
else:
reward = -1
return next_state, reward, terminal
def reset(self):
self.game_won = False
time.sleep(1)
self.action_space.space.act()
self.reset_frame_history()
self.state = self._update_state()
time.sleep(2.0)
def reset_frame_history(self):
for i in range(4):
self.frame_history.append(self._grab_sct())
def _update_state(self):
return np.stack(self.frame_history).reshape(1, 20, 157, 4)
def _grab_sct(self):
sct_img = self.sct.grab(self.bbox)
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX").convert('L')
(width, height) = (img.width // 4, img.height // 4)
img = img.resize((width, height))
img_np = np.array(img) / 255.0
screen_shot = np.expand_dims(img_np, axis=0)
self.game_won = (screen_shot == self.game_won_array).all()
return screen_shot
def _is_terminal(self):
sct_img = self.sct.grab(self.terminal_bbox)
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX").convert('L')
terminal_array_match = np.array([[83, 83, 83, 83, 83],
[83, 83, 83, 83, 83],
[83, 83, 83, 83, 83],
[83, 83, 83, 83, 83],
[83, 83, 247, 247, 247]])
return (terminal_array_match == np.array(img)).all()
def init_game(self):
resp = input('enter "y" to start training: ')
if resp == 'y':
self.render()
else:
exit()
def run(self, epoch, agent, batch_size, logger):
self.reset()
epoch_start_time = datetime.datetime.now()
batch_loader = []
q_values = []
for step in range(10000000):
action, q_value = agent.choose_action(self.state)
next_state, reward, terminal = self.step(action)
batch_loader.append([self.state, action, reward, next_state, terminal])
q_values.append(q_value)
self.state = next_state
if self.game_won:
terminal = True
name = self.driver.find_element_by_name('uname')
name.send_keys("deeptrex")
time.sleep(4)
self.driver.find_element_by_xpath('/html/body/div[6]/div/div[1]/div/form/input[5]').click()
if terminal:
agent.batch_store(batch_loader)
if (agent.memory.length > agent.pretraining_steps) or (agent.memory.memory_size == agent.memory.length):
agent.replay(batch_size, epoch_steps=step)
log_data = {
'epoch': epoch,
'epoch_steps': step,
'epoch_tot_rewards': sum([x[2] for x in batch_loader]),
'epoch_time': datetime.datetime.now() - epoch_start_time,
'epoch_avg_q': np.mean(q_values),
'game_won': self.game_won
}
logger.log(agent, log_data, verbose=True)
if (epoch % 40 == 0) and (epoch != 0):
agent.save_memory(agent.save_memory_fp)
break
def demo(self, agent):
agent.epsilon = 0
agent.epsilon_min = 0
self.reset()
for step in range(10000000):
action = agent.choose_action(self.state)
next_state, reward, terminal = self.step(action)
self.state = next_state
if terminal:
break