-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
152 lines (128 loc) · 5.19 KB
/
agent.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
import torch
import numpy as np
from noise import OrnsteinUhlenbeckActionNoise
from actor import ActorNetwork
from critic import CriticNetwork
from memory import ReplayBuffer
class DDPGAgent(torch.nn.Module):
def __init__(
self,
env_name,
input_dims,
n_actions,
tau,
alpha=1e-4,
beta=1e-3,
gamma=0.99,
batch_size=64,
mem_size=int(1e6),
):
super(DDPGAgent, self).__init__()
self.tau = tau
self.alpha = alpha
self.beta = beta
self.gamma = gamma
self.batch_size = batch_size
self.replay_buffer = ReplayBuffer(input_dims, n_actions, buffer_length=mem_size)
self.action_noise = OrnsteinUhlenbeckActionNoise(np.zeros(n_actions.shape[0]))
self.actor = ActorNetwork(
input_dims,
n_actions,
lr=self.alpha,
chkpt_path=f"weights/{env_name}_actor.pt",
)
self.target_actor = ActorNetwork(
input_dims,
n_actions,
lr=self.alpha,
chkpt_path=f"weights/{env_name}_target_actor.pt",
)
self.critic = CriticNetwork(
input_dims,
n_actions,
lr=self.beta,
chkpt_path=f"weights/{env_name}_critic.pt",
)
self.target_critic = CriticNetwork(
input_dims,
n_actions,
lr=self.beta,
chkpt_path=f"weights/{env_name}_target_critic.pt",
)
self.update_network_parameters(tau=1)
def choose_action(self, state):
self.actor.eval()
# state = torch.Tensor(np.array(state), dtype=torch.float).to(self.actor.device)
state = torch.Tensor(np.array(state)).to(self.actor.device)
mu = self.actor(state).to(self.actor.device)
# add noise to deterministic output
mu = mu + torch.tensor(self.action_noise(), dtype=torch.float).to(
self.actor.device
)
self.actor.train()
# return mu.cpu().detach().numpy()[0]
return mu.cpu().detach().numpy()
def store_transition(self, state, action, reward, next_state, done):
self.replay_buffer.store_transition(state, action, reward, next_state, done)
def save_checkpoints(self, epoch, loss):
self.actor.save_checkpoint(epoch, loss)
self.target_actor.save_checkpoint(epoch, loss)
self.critic.save_checkpoint(epoch, loss)
self.target_critic.save_checkpoint(epoch, loss)
def load_checkpoints(self):
self.actor.load_checkpoint()
self.critic.load_checkpoint()
self.target_actor.load_checkpoint()
self.target_critic.load_checkpoint()
def learn(self):
if self.replay_buffer.mem_counter < self.batch_size:
return
states, actions, rewards, next_states, done = self.replay_buffer.sample(
self.batch_size
)
states = torch.Tensor(states).to(self.actor.device)
actions = torch.Tensor(actions).to(self.actor.device)
next_states = torch.Tensor(next_states).to(self.actor.device)
rewards = torch.Tensor(rewards).to(self.actor.device)
done = torch.Tensor(done).to(self.actor.device).to(torch.bool)
target_actions = self.target_actor(next_states)
target_critic_values = self.target_critic(next_states, target_actions)
critic_values = self.critic(states, actions)
# set target critic value to zero for terminal states
target_critic_values[done] = 0.0 # fix dim issue
target_critic_values = target_critic_values.view(-1)
target = rewards + self.gamma * target_critic_values
target = target.view(self.batch_size, 1) # add batch dim
self.critic.optimizer.zero_grad()
critic_loss = torch.nn.functional.mse_loss(target, critic_values)
critic_loss.backward()
self.critic.optimizer.step()
self.actor.optimizer.zero_grad()
actor_loss = -self.critic(states, self.actor(states))
actor_loss = torch.mean(actor_loss)
actor_loss.backward()
self.actor.optimizer.step()
self.update_network_parameters()
return actor_loss, critic_loss
def update_network_parameters(self, tau=None):
if tau is None:
tau = self.tau
actor_params = dict(self.actor.named_parameters())
target_actor_params = dict(self.target_actor.named_parameters())
for name in actor_params:
actor_params[name] = (
tau * actor_params[name].clone()
+ (1 - tau) * target_actor_params[name].clone()
)
self.target_actor.load_state_dict(actor_params)
critic_params = dict(self.critic.named_parameters())
target_critic_params = dict(self.target_critic.named_parameters())
for name in critic_params:
critic_params[name] = (
tau * critic_params[name].clone()
+ (1 - tau) * target_critic_params[name].clone()
)
self.target_critic.load_state_dict(critic_params)
# To use batch norm instead of layer norm:
# self.target_actor.load_state_dict(actor_params, strict=False)
# self.critic_actor.load_state_dict(critic_params)