-
Notifications
You must be signed in to change notification settings - Fork 1
/
ppo.py
214 lines (164 loc) · 7.42 KB
/
ppo.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import torch
import torch.nn as nn
from torch.distributions import Categorical
from attention_net_own import AttentionNet
device = torch.device("mps") if torch.backends.mps.is_available() else torch.device("cuda" if torch.cuda.is_available() else "cpu")
class RolloutBuffer:
def __init__(self):
self.actions = []
self.states = []
self.logprobs = []
self.rewards = []
self.is_terminals = []
def clear(self):
del self.actions[:]
del self.states[:]
del self.logprobs[:]
del self.rewards[:]
del self.is_terminals[:]
class ActorCritic(nn.Module):
def __init__(self, state_dim, action_dim, attention_net=None):
super(ActorCritic, self).__init__()
# Attention network (optional)
self.attention_net = attention_net
# Actor network for discrete action space
self.actor = nn.Sequential(
nn.Linear(state_dim, 64),
nn.Tanh(),
nn.Linear(64, 64),
nn.Tanh(),
nn.Linear(64, action_dim),
nn.Softmax(dim=-1)
)
# Critic network
self.critic = nn.Sequential(
nn.Linear(state_dim, 64),
nn.Tanh(),
nn.Linear(64, 64),
nn.Tanh(),
nn.Linear(64, 1)
)
def forward(self):
raise NotImplementedError
def act(self, state, constraints=None):
# Apply attention before actor if attention is being used
if self.attention_net and constraints is not None:
state = self.attention_net(state, constraints)
action_probs = self.actor(state)
dist = Categorical(action_probs)
action = dist.sample()
action_logprob = dist.log_prob(action)
return action.detach(), action_logprob.detach()
def evaluate(self, state, action, constraints=None):
# Apply attention before critic and actor if attention is being used
if self.attention_net and constraints is not None:
state = self.attention_net(state, constraints)
action_probs = self.actor(state)
dist = Categorical(action_probs)
action_logprobs = dist.log_prob(action)
dist_entropy = dist.entropy()
state_value = self.critic(state)
return action_logprobs, state_value, dist_entropy
class PPO:
def __init__(self, state_dim, action_dim, lr_actor, lr_critic, gamma, K_epochs, eps_clip, use_attention=False, attention_net=None):
self.gamma = gamma
self.eps_clip = eps_clip
self.K_epochs = K_epochs
self.use_attention = use_attention
self.buffer = RolloutBuffer()
# Use attention if specified
self.policy = ActorCritic(state_dim, action_dim, attention_net=attention_net if use_attention else None).to(device)
self.optimizer = torch.optim.Adam([
{'params': self.policy.actor.parameters(), 'lr': lr_actor},
{'params': self.policy.critic.parameters(), 'lr': lr_critic}
])
self.policy_old = ActorCritic(state_dim, action_dim, attention_net=attention_net if use_attention else None).to(device)
self.policy_old.load_state_dict(self.policy.state_dict())
self.MseLoss = nn.MSELoss()
def select_action(self, state, constraints=None):
with torch.no_grad():
state = torch.FloatTensor(state).to(device)
action, action_logprob = self.policy_old.act(state, constraints) # Pass constraints
self.buffer.states.append(state)
self.buffer.actions.append(action)
self.buffer.logprobs.append(action_logprob)
return action.item()
def update(self):
# Monte Carlo estimate of returns
rewards = []
discounted_reward = 0
for reward, is_terminal in zip(reversed(self.buffer.rewards), reversed(self.buffer.is_terminals)):
if is_terminal:
discounted_reward = 0
discounted_reward = reward + (self.gamma * discounted_reward)
rewards.insert(0, discounted_reward)
# Convert list to tensor and normalize rewards
rewards = torch.tensor(rewards, dtype=torch.float32).to(device)
rewards = (rewards - rewards.mean()) / (rewards.std() + 1e-7)
# Convert lists to tensors
old_states = torch.squeeze(torch.stack(self.buffer.states, dim=0)).detach().to(device)
old_actions = torch.squeeze(torch.stack(self.buffer.actions, dim=0)).detach().to(device)
old_logprobs = torch.squeeze(torch.stack(self.buffer.logprobs, dim=0)).detach().to(device)
# Optimize policy for K epochs
for _ in range(self.K_epochs):
# In case you want to use constraints for the update, pass them here
logprobs, state_values, dist_entropy = self.policy.evaluate(old_states, old_actions)
state_values = torch.squeeze(state_values)
# Find the ratio (pi_theta / pi_theta__old)
ratios = torch.exp(logprobs - old_logprobs.detach())
# Calculate advantages
advantages = rewards - state_values.detach()
# Surrogate loss
surr1 = ratios * advantages
surr2 = torch.clamp(ratios, 1 - self.eps_clip, 1 + self.eps_clip) * advantages
# Final loss of clipped objective PPO
loss = -torch.min(surr1, surr2) + 0.5 * self.MseLoss(state_values, rewards) - 0.01 * dist_entropy
# Take gradient step
self.optimizer.zero_grad()
loss.mean().backward()
self.optimizer.step()
# Copy new weights into old policy
self.policy_old.load_state_dict(self.policy.state_dict())
# Clear buffer
self.buffer.clear()
def save(self, checkpoint_path):
torch.save(self.policy_old.state_dict(), checkpoint_path)
def load(self, checkpoint_path):
self.policy_old.load_state_dict(torch.load(checkpoint_path))
self.policy.load_state_dict(torch.load(checkpoint_path))
# class ActorCritic(nn.Module):
# def __init__(self, state_dim, action_dim):
# super(ActorCritic, self).__init__()
# # print (f"state_dim = {state_dim}; action_dim = {action_dim}")
# # Actor network for discrete action space
# self.actor = nn.Sequential(
# nn.Linear(state_dim, 64),
# nn.Tanh(),
# nn.Linear(64, 64),
# nn.Tanh(),
# nn.Linear(64, action_dim),
# nn.Softmax(dim=-1)
# )
# # Critic network
# self.critic = nn.Sequential(
# nn.Linear(state_dim, 64),
# nn.Tanh(),
# nn.Linear(64, 64),
# nn.Tanh(),
# nn.Linear(64, 1)
# )
# def forward(self):
# raise NotImplementedError
# def act(self, state):
# action_probs = self.actor(state)
# dist = Categorical(action_probs)
# action = dist.sample()
# action_logprob = dist.log_prob(action)
# return action.detach(), action_logprob.detach()
# def evaluate(self, state, action):
# action_probs = self.actor(state)
# dist = Categorical(action_probs)
# action_logprobs = dist.log_prob(action)
# dist_entropy = dist.entropy()
# state_value = self.critic(state)
# return action_logprobs, state_value, dist_entropy