-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
18DDPG.py
155 lines (123 loc) · 4.86 KB
/
18DDPG.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import gym
import math
import random
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
class Actor(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(Actor, self).__init__()
self.linear1 = nn.Linear(input_size, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, output_size)
def forward(self, s):
x = F.relu(self.linear1(s))
x = F.relu(self.linear2(x))
x = torch.tanh(self.linear3(x))
return x
class Critic(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.linear1 = nn.Linear(input_size, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, output_size)
def forward(self, s, a):
x = torch.cat([s, a], 1)
x = F.relu(self.linear1(x))
x = F.relu(self.linear2(x))
x = self.linear3(x)
return x
class Skylark_DDPG():
def __init__(self, env):
self.env = env
self.gamma = 0.99
self.actor_lr = 0.001
self.critic_lr = 0.001
self.tau = 0.02
self.capacity = 10000
self.batch_size = 32
# Varies by environment
s_dim = self.env.observation_space.shape[0]
a_dim = self.env.action_space.shape[0]
self.actor = Actor(s_dim, 256, a_dim)
self.actor_target = Actor(s_dim, 256, a_dim)
self.critic = Critic(s_dim+a_dim, 256, a_dim)
self.critic_target = Critic(s_dim+a_dim, 256, a_dim)
self.actor_optim = optim.Adam(self.actor.parameters(), lr = self.actor_lr)
self.critic_optim = optim.Adam(self.critic.parameters(), lr = self.critic_lr)
self.buffer = []
self.actor_target.load_state_dict(self.actor.state_dict())
self.critic_target.load_state_dict(self.critic.state_dict())
def select_action(self, state):
state = torch.tensor(state, dtype=torch.float).unsqueeze(0)
action = self.actor(state).squeeze(0).detach().numpy()
return action
def put(self, *transition):
if len(self.buffer)== self.capacity:
self.buffer.pop(0)
self.buffer.append(transition)
def learn(self):
if len(self.buffer) < self.batch_size:
return
samples = random.sample(self.buffer, self.batch_size)
s0, a0, r1, s1 = zip(*samples)
s0 = torch.tensor(s0, dtype=torch.float)
a0 = torch.tensor(a0, dtype=torch.float)
r1 = torch.tensor(r1, dtype=torch.float).view(self.batch_size,-1)
s1 = torch.tensor(s1, dtype=torch.float)
def critic_learn():
a1 = self.actor_target(s1).detach()
y_true = r1 + self.gamma * self.critic_target(s1, a1).detach()
y_pred = self.critic(s0, a0)
loss_fn = nn.MSELoss()
loss = loss_fn(y_pred, y_true)
self.critic_optim.zero_grad()
loss.backward()
self.critic_optim.step()
def actor_learn():
loss = -torch.mean( self.critic(s0, self.actor(s0)) )
self.actor_optim.zero_grad()
loss.backward()
self.actor_optim.step()
def soft_update(net_target, net, tau):
for target_param, param in zip(net_target.parameters(), net.parameters()):
target_param.data.copy_(target_param.data * (1.0 - tau) + param.data * tau)
critic_learn()
actor_learn()
soft_update(self.critic_target, self.critic, self.tau)
soft_update(self.actor_target, self.actor, self.tau)
def train(self, num_episodes):
for i in range(1, num_episodes):
s0 = self.env.reset()
episode_reward = 0
for t in range(1, 1000):
# self.env.render()
a0 = self.select_action(s0)
s1, r1, done, _ = self.env.step(a0)
self.put(s0, a0, r1, s1)
episode_reward += r1
s0 = s1
self.learn()
print('Episode {} : {}'.format(i, episode_reward))
if __name__ == "__main__":
use_ray = True
num_episodes = 1000
env = gym.make("Pendulum-v0").env
if use_ray:
import ray
from ray import tune
tune.run(
'DDPG',
config={
'env': "Pendulum-v0",
'num_workers': 1,
# 'env_config': {}
}
)
else:
ddpg_agent = Skylark_DDPG(env)
ddpg_agent.train(num_episodes)