-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_play_a2c.py
executable file
·43 lines (35 loc) · 1.3 KB
/
03_play_a2c.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
#!/usr/bin/env python3
import argparse
import gym
import pybullet_envs
from lib import model
import numpy as np
import torch
ENV_ID = "MinitaurBulletEnv-v0"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model", required=True, help="Model file to load")
parser.add_argument("-e", "--env", default=ENV_ID, help="Environment name to use, default=" + ENV_ID)
parser.add_argument("-r", "--record", help="If specified, sets the recording dir, default=Disabled")
args = parser.parse_args()
spec = gym.envs.registry.spec(args.env)
spec._kwargs['render'] = False
env = gym.make(args.env)
if args.record:
env = gym.wrappers.Monitor(env, args.record)
net = model.ModelA2C(env.observation_space.shape[0], env.action_space.shape[0])
net.load_state_dict(torch.load(args.model))
obs = env.reset()
total_reward = 0.0
total_steps = 0
while True:
obs_v = torch.FloatTensor([obs])
mu_v, var_v, val_v = net(obs_v)
action = mu_v.squeeze(dim=0).data.numpy()
action = np.clip(action, -1, 1)
obs, reward, done, _ = env.step(action)
total_reward += reward
total_steps += 1
if done:
break
print("In %d steps we got %.3f reward" % (total_steps, total_reward))