-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc.py
executable file
·416 lines (334 loc) · 12.6 KB
/
rtc.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import os
import pty
import pyte
import random
import re
import signal
import select
import string
import sys
import time
import threading
from collections import deque
from gym import spaces
from threading import Thread
#from pathos.multiprocessing import Pool
# from multiprocessing import Pool
# Staty - analiza deskryptora, wysokie value stanu =>
# ktora czesc stanu odpowiada za to, saliency
executable = "/home/rahid/Programming/Repos/rogue5.2/rogue"
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 24
CLIPPED_SCREEN_WIDTH = 80
SQUARE_SCREEN = False
ONE_HOT = True
def weird(env, action, idx):
return env.step(action, idx)
class MultiWrapper():
GAME_ACTIONS = [# " ", # skip
"k", # go_up
"j", # go_down
"h", # go_left
"l", # go_right
# ">", # descend
# "s", # search
# "." search
]
ACTIONS = len(GAME_ACTIONS)
NAME = "Rogue"
CHAR_BINS = ["@", # Player char
string.ascii_letters, # Monsters
"-|", # Walls
"+", # Doors
".#", # Floors/corridors
"*?!%[]", # Items
" ", # Empty space
""] # Other
def __init__(self, num_envs=15):
self.action_space = spaces.Discrete(len(self.GAME_ACTIONS))
self.observation_space = spaces.Box(0, 1,
[SCREEN_HEIGHT, SCREEN_WIDTH, len(self.CHAR_BINS)])
self.num_envs = num_envs
self.envs = [Wrapper() for _ in range(self.num_envs)]
self.step_n = 0
def reset(self, restart=True):
return np.array([env.reset(restart) for env in self.envs])
def step(self, actions):
threads = []
results = [None] * self.num_envs
def step_fn(env, action, results, idx):
results[idx] = env.step(action)
for idx, (env, action) in enumerate(zip(self.envs, actions)):
t = threading.Thread(target=step_fn, args=(env, action,
results, idx))
t.start()
threads += [t]
for t in threads:
t.join()
multi_obs, multi_rewards, multi_dones, _ = zip(*results)
# results = []
# for env, action in zip(self.envs, actions):
# results += [env.step(action)]
# multi_obs, multi_rewards, multi_dones, _ = zip(*results)
if not self.step_n % 10:
self.envs[0].render()
self.step_n += 1
return (np.array(multi_obs),
np.array(multi_rewards),
np.array(multi_dones),
None)
def close():
pass
class Wrapper():
GAME_ACTIONS = [# " ", # skip
"k", # go_up
"j", # go_down
"h", # go_left
"l", # go_right
# ">", # descend
# "s", # search
# "." search
]
ACTIONS = len(GAME_ACTIONS)
NAME = "Rogue"
CHAR_BINS = ["@", # Player char
string.ascii_letters, # Monsters
"-|", # Walls
"+", # Doors
".#", # Floors/corridors
"*?!%[]", # Items
" ", # Empty space
""] # Other
def __init__(self):
self.state = []
self.state_history = deque([], 10)
self.scores = [0]
self.action_history = deque(["EPOCH START"], 100)
self.tick = 0
self.last_death = 0
self.reset(restart=False)
self.action_space = spaces.Discrete(len(self.GAME_ACTIONS))
self.observation_space = spaces.Box(0, 1,
[SCREEN_HEIGHT, SCREEN_WIDTH, len(self.CHAR_BINS)])
self.num_envs = 1
def _get_random_input(self):
return random.choice(self.GAME_ACTIONS)
def _dump_screen(self):
for idx, line in enumerate(self.screen.display, 1):
print("{0:2d} {1} |".format(idx, line))
def score_for_logs(self):
return self.scores[-2]
def _scrap_screen(self):
info_line = self.state[-1]
if "Gold" not in info_line: # there's probably a message there
self._push_action("\r\r\n") # push return to skip
self._push_action(" ") # more
self._push_action(" ") # more
info_line = self.screen.display[-1]
time.sleep(0.2)
self._dump_screen()
print(info_line)
heuristics = {}
heuristics["dungeon_level"] = int(
re.search("Level:\s+(\d+)", info_line).group(1))
heuristics["gold"] = int(re.search("Gold:\s+(\d+)", info_line).group(1))
hp_captures = re.search("Hp:\s+(\d+)\((\d+)\)", info_line)
heuristics["hp"] = int(hp_captures.group(1))
heuristics["max_hp"] = int(hp_captures.group(2))
exp_captures = re.search("Exp:\s+(\d+)/(\d+)", info_line)
heuristics["exp_level"] = int(exp_captures.group(1))
heuristics["exp_points"] = int(exp_captures.group(2))
if len(self.state_history) >= 2:
new_explored = sum(not c.isspace()
for line in self.state[1:-1] for c in line)
old_explored = sum(not c.isspace()
for line in self.state_history[-2][1:-1] for c in line)
heuristics["explored"] = new_explored - old_explored
else:
heuristics["explored"] = 0
if len(self.state_history) >= 10:
old_pos = self._get_player_pos(self.state_history[0])
new_pos = self._get_player_pos()
distance = abs(old_pos[0] - new_pos[0]) + abs(old_pos[1] - new_pos[1])
heuristics["pos_diff"] = (distance - 4) * 0.2
else:
heuristics["pos_diff"] = 0
heuristics["constant"] = -0.05
return heuristics
def _calculate_reward(self):
try:
heuristics = self._scrap_screen()
except AttributeError:
return self.scores[-1]
return heuristics["explored"] + heuristics["pos_diff"] + heuristics["constant"]
# + (heuristics["dungeon_level"] - 1) * 1000)
def _push_action(self, input_):
for input_char in input_:
os.write(self.master_fd, bytearray(input_char, "ascii"))
time.sleep(0.001)
def _get_player_pos(self, state=None):
if state is None:
state = self.state
for y, line in enumerate(state):
x = line.find("@")
if x != -1:
return x, y
def char_to_vec(self, char):
if ord(char) == 0:
return [0] * len(self.CHAR_BINS)
for idx, char_bin in enumerate(self.CHAR_BINS):
pos = char_bin.find(char)
if pos != -1:
bin_index = idx
break
else:
bin_index = len(self.CHAR_BINS) - 1
pos = 0
one_hot = [0] * len(self.CHAR_BINS)
one_hot[bin_index] = 1
return one_hot
def get_clipped_edges(self):
if not SQUARE_SCREEN:
return 0, SCREEN_WIDTH
player_pos = self._get_player_pos()
player_left_edge = player_pos[0] - CLIPPED_SCREEN_WIDTH // 2
max_left_edge = SCREEN_WIDTH - CLIPPED_SCREEN_WIDTH
min_left_edge = 0
left_edge = sorted([min_left_edge, player_left_edge, max_left_edge])[1]
right_edge = left_edge + CLIPPED_SCREEN_WIDTH
return left_edge, right_edge
def print_char_frame(self):
left_edge, right_edge = self.get_clipped_edges()
for line in self.state:
print(line[left_edge:left_edge + CLIPPED_SCREEN_WIDTH])
def get_frame(self):
left_edge, right_edge = self.get_clipped_edges()
processed_state = []
for line in self.state:
process_screen = self.char_to_vec if ONE_HOT else ord
processed_state += [[process_screen(c) for c
in line[left_edge:right_edge]]]
padding = (CLIPPED_SCREEN_WIDTH - SCREEN_HEIGHT) if SQUARE_SCREEN else 0
for _ in range(padding):
null_element = self.process_screen('\0')
processed_state += [[null_element] * CLIPPED_SCREEN_WIDTH]
return np.array(processed_state)
def run_in_loop(self):
tick = 0
time.sleep(0.2)
while True:
read_list, _wlist, _xlist = select.select(
[self.master_fd], [], [], 0)
if not read_list:
# No more output, let's process results
terminal = self._is_terminal()
self._push_action(" ")
if terminal:
self._start_game(restart=True)
tick += 1
self.state = self.screen.display
self.scores += [self._calculate_reward()]
self.render()
game_input = self.GAME_ACTIONS[self.action_space.sample()]
self._push_action(game_input)
self.action_history += [game_input]
else:
data = os.read(read_list[0], 1024)
self.stream.feed(data.decode("ascii"))
def _is_terminal(self):
screen = "".join(line for line in self.screen.display)
return ("REST" in screen and "IN" in screen and "PEACE" in screen)
def reset(self, restart=True):
if restart:
self._push_action(" ")
self._push_action("\r\n")
self._push_action("^C")
os.kill(self.p_pid, signal.SIGTERM)
os.close(self.master_fd)
os.waitpid(self.p_pid, 0)
p_pid, master_fd = pty.fork()
if p_pid == 0: # Child.
os.execvpe(executable, [executable],
env=dict(TERM="linux", COLUMNS="80", LINES="24"))
os.exit(0)
else:
self.p_pid = p_pid
self.screen = pyte.Screen(80, 24)
self.stream = pyte.Stream(self.screen)
self.master_fd = master_fd
self.state_history.clear()
self.scores += [0]
time.sleep(0.1)
obs = self.get_frame()
return np.zeros([SCREEN_HEIGHT, SCREEN_WIDTH, len(self.CHAR_BINS)])
def render(self):
print("Action input: " + self.action_history[-1])
self._dump_screen()
print("Current reward: {}\tPrev reward: {}\tDiff:{}"
.format(self.scores[-1], self.scores[-2],
self.scores[-1] - self.scores[-2]))
def step(self, action, idx=None):
time.sleep(0.005)
self.tick += 1
if isinstance(action, list) or isinstance(action, np.ndarray):
action = action[0]
game_input = self.GAME_ACTIONS[action]
self._push_action(game_input)
self.action_history += [game_input]
read_list, _wlist, _xlist = select.select(
[self.master_fd], [], [], 0)
while read_list:
data = os.read(read_list[0], 1024)
self.stream.feed(data.decode("ascii"))
read_list, _wlist, _xlist = select.select(
[self.master_fd], [], [], 0)
# No more output, let's process results
# if not self.tick % SCREEN_DUMP_RATE:
# self.render()
self._push_action(" ")
self.state = self.screen.display
self.state_history.append(self.state)
reward = self._calculate_reward()
self.scores += [reward]
obs = self.get_frame()
terminal = self._is_terminal()
if terminal:
self.reset()
info = {}
if idx is not None:
return idx, obs, reward, terminal, info
else:
return obs, reward, terminal, info
def init_game():
p_pid, master_fd = pty.fork()
if p_pid == 0: # Child.
os.execvpe(executable, [executable],
env=dict(TERM="linux", COLUMNS="80", LINES="24"))
else:
wrapper = Wrapper()
wrapper.run_in_loop()
def init_multiple_games(threads_num):
for i in range(threads_num):
p_pid = os.fork()
if p_pid == 0: # Child
init_game()
break
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit("Please pass the path to Rogue executable as an argument")
elif not os.path.isfile(sys.argv[1]):
sys.exit("Wrong path passed, try again")
else:
executable = sys.argv[1]
if len(sys.argv) > 2 and sys.argv[2].isdigit:
threads = int(sys.argv[2])
else:
threads = 1
screen = pyte.Screen(80, 24)
if threads == 1:
init_game()
else:
init_multiple_games(threads)