-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.py
96 lines (83 loc) · 2.59 KB
/
record.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
#!/usr/bin/env python3
"""
Record game.
Authors:
LICENCE:
"""
import os
from pathlib import Path
from time import sleep
import gym
import numpy as np
from gym.wrappers.monitoring.video_recorder import VideoRecorder
import games
class Record:
"""Record a game from the environment."""
def __init__(
self,
game: games.Game_type,
record: bool = False,
store_path: Path = None,
) -> None:
"""Ctor."""
self.game = game
self.env = gym.make(game.name)
self.env.reset()
self.env.render(mode="human")
self.record = record
self.store_path = store_path
if self.store_path is None:
self.store_path = Path("temp/trial")
if not os.path.exists(self.store_path):
os.mkdir(self.store_path)
if self.record:
self.video = VideoRecorder(
self.env,
str(self.store_path / "video.mp4"),
)
self.env.viewer.window.push_handlers(game.keyboard)
self.actions = []
self.states = []
def close(self) -> None:
"""Close game."""
self.env.close()
if self.record:
self.video.close()
def record_game(self) -> None:
"""Record a game for a given env."""
isopen = True
action_state_path = self.store_path / "action_state"
while isopen:
self.env.reset()
total_reward = 0.0
steps = 0
restart = False
while True:
action = self.game.get_action()
state, reward, done, _ = self.env.step(action)
self.actions.append(action)
self.states.append(state)
total_reward += reward
if steps % 200 == 0 or done:
print("\naction {:+0.2f}".format(action))
print(f"step {steps} total_reward {total_reward:+0.2f}")
steps += 1
if self.record:
self.video.capture_frame()
isopen = self.env.render(mode="human")
if done or restart or not isopen:
break
sleep(0.08)
if self.record:
np.savez_compressed(
action_state_path,
actions=np.array(self.actions, dtype=np.uint8),
states=np.array(self.states, dtype=np.uint8),
)
def test() -> None:
"""Test record on Enduro-v4."""
recenv = Record(games.Enduro(), record=True)
recenv.record_game()
recenv.close()
if __name__ == "__main__":
test()