-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrain_mario.py
86 lines (62 loc) · 1.87 KB
/
train_mario.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
# -*- coding: utf-8 -*-
import random
import numpy as np
from collections import namedtuple
import gym
import ppaquette_gym_super_mario
from gym import wrappers
import torch
import torch.optim as optim
from deepq.learn import mario_learning
from deepq.model import DQN
from common.atari_wrapper import wrap_mario
from common.schedule import LinearSchedule
SEED = 0
BATCH_SIZE = 32
GAMMA = 0.99
REPLAY_BUFFER_SIZE = 1000000
LEARNING_STARTS = 10000
LEARNING_FREQ = 4
FRAME_HISTORY_LEN = 4
TARGET_UPDATE_FREQ = 3000
LEARNING_RATE = 0.00025
ALPHA = 0.95
EPS = 0.01
def main(env):
### 首先要為隨時間改變的參數設定schedule
# This is a just rough estimate
num_iterations = float(40000000) / 4.0
# define exploration schedule
exploration_schedule = LinearSchedule(1000000, 0.1)
# optimizer
OptimizerSpec = namedtuple("OptimizerSpec", ["constructor", "kwargs"])
optimizer = OptimizerSpec(
constructor=optim.RMSprop,
kwargs=dict(lr=LEARNING_RATE, alpha=ALPHA, eps=EPS),
)
mario_learning(
env=env,
q_func=DQN,
optimizer_spec=optimizer,
exploration=exploration_schedule,
replay_buffer_size=REPLAY_BUFFER_SIZE,
batch_size=BATCH_SIZE,
gamma=GAMMA,
learning_starts=LEARNING_STARTS,
learning_freq=LEARNING_FREQ,
frame_history_len=FRAME_HISTORY_LEN,
target_update_freq=TARGET_UPDATE_FREQ
)
if __name__ == '__main__':
env = gym.make("ppaquette/SuperMarioBros-1-1-v0")
# set global seeds
env.seed(SEED)
torch.manual_seed(SEED)
np.random.seed(SEED)
random.seed(SEED)
# monitor & wrap the game
env = wrap_mario(env)
expt_dir = 'video/mario'
env = wrappers.Monitor(env, expt_dir, force=True, video_callable=lambda count: count % 10 == 0)
# main
main(env)