-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
43 lines (36 loc) · 1.49 KB
/
test.py
File metadata and controls
43 lines (36 loc) · 1.49 KB
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
import argparse
import numpy as np
import torch
from env.env_adv import Env
from agents.agents import Agent
parser = argparse.ArgumentParser(description='Test the PPO agent for the CarRacing-v0')
parser.add_argument('--action-repeat', type=int, default=8, metavar='N', help='repeat action in N frames (default: 12)')
parser.add_argument('--img-stack', type=int, default=4, metavar='N', help='stack N image in a state (default: 4)')
parser.add_argument('--seed', type=int, default=0, metavar='N', help='random seed (default: 0)')
parser.add_argument('--render', action='store_true', help='render the environment')
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
torch.manual_seed(args.seed)
if use_cuda:
torch.cuda.manual_seed(args.seed)
if __name__ == "__main__":
agent = Agent(args.img_stack, device)
agent.load_param()
env = Env(args.seed, args.img_stack, args.action_repeat)
training_records = []
running_score = 0
state = env.reset()
for i_ep in range(10):
score = 0
state = env.reset()
for t in range(1000):
action = agent.select_action(state)
state_, reward, done, die = env.step(action * np.array([2., 1., 1.]) + np.array([-1., 0., 0.]))
if args.render:
env.render()
score += reward
state = state_
if done or die:
break
print('Ep {}\tScore: {:.2f}\t'.format(i_ep, score))