forked from yfeng997/MadMario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay.py
68 lines (46 loc) · 1.57 KB
/
replay.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 random, datetime
from pathlib import Path
import gym
import gym_super_mario_bros
from gym.wrappers import FrameStack, GrayScaleObservation, TransformObservation
from nes_py.wrappers import JoypadSpace
from metrics import MetricLogger
from agent import Mario
from wrappers import ResizeObservation, SkipFrame
env = gym_super_mario_bros.make('SuperMarioBros-1-1-v0')
env = JoypadSpace(
env,
[['right'],
['right', 'A']]
)
env = SkipFrame(env, skip=4)
env = GrayScaleObservation(env, keep_dim=False)
env = ResizeObservation(env, shape=84)
env = TransformObservation(env, f=lambda x: x / 255.)
env = FrameStack(env, num_stack=4)
env.reset()
save_dir = Path('checkpoints') / datetime.datetime.now().strftime('%Y-%m-%dT%H-%M-%S')
save_dir.mkdir(parents=True)
checkpoint = Path('checkpoints/2020-10-21T18-25-27/mario.chkpt')
mario = Mario(state_dim=(4, 84, 84), action_dim=env.action_space.n, save_dir=save_dir, checkpoint=checkpoint)
mario.exploration_rate = mario.exploration_rate_min
logger = MetricLogger(save_dir)
episodes = 100
for e in range(episodes):
state = env.reset()
while True:
env.render()
action = mario.act(state)
next_state, reward, done, info = env.step(action)
mario.cache(state, next_state, action, reward, done)
logger.log_step(reward, None, None)
state = next_state
if done or info['flag_get']:
break
logger.log_episode()
if e % 20 == 0:
logger.record(
episode=e,
epsilon=mario.exploration_rate,
step=mario.curr_step
)