forked from Farama-Foundation/ViZDoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recording.py
307 lines (239 loc) · 9.11 KB
/
test_recording.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
#!/usr/bin/env python3
#####################################################################
# This script test recording functionality
#####################################################################
from __future__ import print_function
import vizdoom as vzd
from random import random
from time import sleep
import os
GV = vzd.GameVariable
B = vzd.Button
def setup_test(buttons, variables, visible=True):
game = vzd.DoomGame()
game.set_doom_scenario_path("../../scenarios/basic.wad")
game.set_doom_map("map01")
game.set_episode_start_time(10)
game.set_window_visible(visible)
if visible:
game.set_screen_resolution(vzd.ScreenResolution.RES_640X480)
# Test if everything is alright with variables
game.set_available_game_variables(variables)
assert len(variables) == game.get_available_game_variables_size()
assert variables == game.get_available_game_variables()
# Test if everything is alright with buttons
game.set_available_buttons(buttons)
assert len(buttons) == game.get_available_buttons_size()
assert buttons == game.get_available_buttons()
game.set_mode(vzd.Mode.PLAYER)
return game
def normalize_action(action):
for i, a in enumerate(action):
action[i] = round(float(a), 1)
return action
def normalize_variables(vars):
vars = list(vars)
for i, v in enumerate(vars):
vars[i] = round(float(v), 1)
return vars
def test_mode_and_sleep(buttons, variables, actions, title):
sleep_times = [0, 0.001, 0.1]
for sleep_time in sleep_times:
recording_test(buttons, variables, actions, mode=vzd.Mode.PLAYER,
title=title + " PLAYER mode with sleep = " + str(sleep_time))
#recording_test(buttons, variables, actions, mode=vzd.Mode.SPECTATOR,
# title=title + " SPECTATOR mode with sleep = " + str(sleep_time))
def recording_test(buttons, variables, actions, recording_file="test_recording.lmp", sleep_time=0, mode=vzd.Mode.PLAYER,
verbose=0, verbose_sleep_time=0.1, title="Unnamed test"):
print("Test: {}".format(title))
game = setup_test(buttons, variables, visible=verbose)
# Append all zeros action
actions.append([0.0] * game.get_available_buttons_size())
history_of_actions = []
history_of_variables = []
history_of_rewards = []
game.init()
game.new_episode(recording_file)
for i, action in enumerate(actions):
print(" Playing:", i, "/", len(actions), end = '\r')
normalize_action(action)
history_of_actions.append(action)
assert type(action) == list
state = game.get_state()
variables = normalize_variables(state.game_variables)
history_of_variables.append(variables)
assert type(variables) == list
reward = game.make_action(action)
history_of_rewards.append(reward)
last_action = game.get_last_action()
normalize_action(last_action)
assert type(last_action) == list
if verbose > 1:
print(" State: {}, tic: {}, reward: {}, action: {}, last action: {}, variables: {}".format(
state.number, state.tic, reward, action, last_action, variables))
sleep(verbose_sleep_time)
# Asserts
assert action == last_action
game.close()
state_number = 0
error_count = 0
game.set_mode(mode)
game.init()
game.replay_episode(recording_file)
while not game.is_episode_finished() and state_number < len(history_of_variables):
print(" Replaying:", state_number, "/", len(history_of_variables), end = '\r')
state = game.get_state()
variables = normalize_variables(state.game_variables)
assert type(variables) == list
game.advance_action()
last_action = game.get_last_action()
reward = game.get_last_reward()
normalize_action(last_action)
assert type(last_action) == list
sleep(sleep_time)
if verbose > 1:
print(" State: {}, tic: {}, reward: {}, recorded action: {}, last action: {}, recorded variables: {} variables: {}".format(
state.number, state.tic, reward, history_of_actions[state_number], last_action, history_of_variables[state_number], variables))
sleep(verbose_sleep_time)
# Asserts
error = False
try:
assert history_of_rewards[state_number] == reward
except AssertionError:
error = True
if verbose > 0:
print(" Test failed:")
print(" State: {}, tic: {}, recorded reward: {} != reward: {}".format(
state.number, state.tic, history_of_rewards[state_number], reward))
try:
assert history_of_variables[state_number] == variables
except AssertionError:
error = True
if verbose > 0:
print(" Test failed:")
print(" State: {}, tic: {}, recorded variables: {} != variables: {}".format(
state.number, state.tic, history_of_variables[state_number], variables))
try:
assert history_of_actions[state_number] == last_action
except AssertionError:
error = True
if verbose > 0:
print(" Test failed:")
print(" State: {}, tic: {}, recorded action: {} != last action: {}".format(
state.number, state.tic, history_of_actions[state_number], last_action))
if error:
error_count += 1
state_number += 1
game.close()
os.remove(recording_file)
print(" Total errors:", error_count, "/", state_number)
def random_action(action_len, button_press_prob):
new_action = [0] * action_len
for i in range(action_len):
new_action[i] = 1.0 if random() < button_press_prob else 0.0
return new_action
def random_test():
buttons = [B.MOVE_LEFT,
B.MOVE_RIGHT,
B.MOVE_BACKWARD,
B.MOVE_FORWARD,
B.TURN_RIGHT,
B.TURN_LEFT,
B.CROUCH,
B.JUMP]
variables = [GV.POSITION_X,
GV.POSITION_Y,
GV.POSITION_Z,
GV.VIEW_HEIGHT,
GV.ANGLE,
GV.PITCH,
GV.ROLL]
test_actions1 = []
for _ in range(1000):
test_actions1.append(random_action(len(buttons), 0.5))
test_actions2 = []
for _ in range(1000):
test_actions2.append(random_action(len(buttons), 0.9))
test_actions3 = []
for _ in range(1000):
test_actions3.append(random_action(len(buttons), 0.1))
test_mode_and_sleep(buttons, variables, test_actions1, title="Random test #1")
test_mode_and_sleep(buttons, variables, test_actions2, title="Random test #2")
test_mode_and_sleep(buttons, variables, test_actions3, title="Random test #3")
# https://github.com/mwydmuch/ViZDoom/issues/354
def ankitkv_test():
buttons = [B.MOVE_FORWARD, B.TURN_LEFT_RIGHT_DELTA]
variables = [GV.POSITION_X, GV.ANGLE]
test_actions1 = [
[0.0, 90.0],
[0.0, -90.0],
[1.0, 0.0],
[0.0, 90.0],
[0.0, -90.0],
[0.0, 90.0],
[0.0, -90.0],
[0.0, 90.0],
[0.0, 90.0],
[1.0, 0.0],
[1.0, 0.0],
[1.0, 0.0],
[0.0, -90.0],
[0.0, 90.0],
[1.0, 0.0],
]
test_actions2 = [
[0.0, -90.0],
[0.0, 90.0],
[1.0, 0.0],
[0.0, -90.0],
[0.0, 90.0],
[0.0, -90.0],
[0.0, 90.0],
[0.0, -90.0],
[0.0, -90.0],
[1.0, 0.0],
[1.0, 0.0],
[1.0, 0.0],
[1.0, 0.0],
[1.0, 0.0],
[0.0, 90.0],
[0.0, -90.0],
[1.0, 0.0],
]
test_mode_and_sleep(buttons, variables, test_actions1, title="ankitkv test #1")
test_mode_and_sleep(buttons, variables, test_actions2, title="ankitkv test #2")
def delta_buttons_test():
buttons = [B.MOVE_FORWARD_BACKWARD_DELTA,
B.MOVE_LEFT_RIGHT_DELTA,
B.LOOK_UP_DOWN_DELTA,
B.TURN_LEFT_RIGHT_DELTA]
variables = [GV.POSITION_X,
GV.POSITION_Y,
GV.ANGLE,
GV.PITCH]
test_actions1 = []
for i in range(10):
new_action = [25, 15 if i % 2 == 0 else 0, -3, 10]
test_actions1.append(new_action)
for i in range(10):
new_action = [-15, -10 if i % 2 == 0 else 0, 6, -20]
test_actions1.append(new_action)
test_mode_and_sleep(buttons, variables, test_actions1, title="Delta buttons test #1")
# https://github.com/mwydmuch/ViZDoom/issues/412
def mhe500_test():
buttons = [B.MOVE_LEFT,
B.MOVE_RIGHT,
B.ATTACK]
variables = [GV.POSITION_X,
GV.POSITION_Y,
GV.AMMO1]
test_actions1 = []
for i in range(300):
new_action = [0] * len(buttons) # Do nothing
test_actions1.append(new_action)
test_mode_and_sleep(buttons, variables, test_actions1, title="mhe500 test #1")
if __name__ == "__main__":
random_test()
ankitkv_test()
delta_buttons_test()
mhe500_test()