-
Notifications
You must be signed in to change notification settings - Fork 8
/
runner.py
executable file
·67 lines (54 loc) · 2.13 KB
/
runner.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
# -*- coding: utf-8 -*- #
"""*********************************************************************************************"""
# FileName [ runner.py ]
# Synopsis [ main program that runs the training and testing of the DQN agent ]
# Author [ Ting-Wei Liu (Andi611) ]
# Copyright [ Copyleft(c), NTUEE, NTU, Taiwan ]
# Reference [ URL ]
"""*********************************************************************************************"""
###############
# IMPORTATION #
###############
import argparse
from agent_dqn import Agent_DQN
from environment import Environment
seed = 11037
def parse():
parser = argparse.ArgumentParser(description="runner")
parser.add_argument('--env_name', default=None, help='environment name')
parser.add_argument('--train_pg', action='store_true', help='whether train policy gradient')
parser.add_argument('--train_dqn', action='store_true', help='whether train DQN')
parser.add_argument('--test_pg', action='store_true', help='whether test policy gradient')
parser.add_argument('--test_dqn', action='store_true', help='whether test DQN')
parser.add_argument('--video_dir', default=None, help='output video directory')
parser.add_argument('--do_render', action='store_true', help='whether render environment')
args = parser.parse_args()
return args
def run(args):
if args.train_dqn:
env_name = args.env_name or 'BreakoutNoFrameskip-v4'
env = Environment(env_name, args, atari_wrapper=True)
agent = Agent_DQN(env, args)
agent.train()
if args.test_dqn:
env = Environment('BreakoutNoFrameskip-v4', args, atari_wrapper=True, test=True)
agent = Agent_DQN(env, args)
test(agent, env, total_episodes=100)
def test(agent, env, total_episodes=30):
rewards = []
env.seed(seed)
for i in range(total_episodes):
state = env.reset()
done = False
episode_reward = 0.0
# playing one game
while not done:
action = agent.make_action(state, test=True)
state, reward, done, info = env.step(action)
episode_reward += reward
rewards.append(episode_reward)
print('Run %d episodes'%(total_episodes))
print('Mean:', np.mean(rewards))
if __name__ == '__main__':
args = parse()
run(args)