-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalfcheetah_pybullet_02_D4PG_play.py
75 lines (59 loc) · 2.32 KB
/
halfcheetah_pybullet_02_D4PG_play.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
#!/usr/bin/env python3
import argparse
import gym
import pybullet_envs
import numpy as np
import torch
import torch.nn as nn
##############################################################################################
# --------------------------------------------------------------------------------------------
# DDPG Actor
# --------------------------------------------------------------------------------------------
class DDPGActor(nn.Module):
def __init__(self, obs_size, act_size):
super(DDPGActor, self).__init__()
self.net = nn.Sequential(
nn.Linear(obs_size, 400),
nn.ReLU(),
nn.Linear(400, 300),
nn.ReLU(),
nn.Linear(300, act_size),
nn.Tanh()
)
def forward(self, x):
return self.net(x)
##############################################################################################
# --------------------------------------------------------------------------------------------
# play
# --------------------------------------------------------------------------------------------
ENV_ID = "HalfCheetahBulletEnv-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)
# ----------
env._max_episode_steps = 1000*2
# ----------
if args.record:
env = gym.wrappers.Monitor(env, args.record)
net = DDPGActor(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 = 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))