2020-03-18 21:45:41 +08:00
|
|
|
import torch
|
2020-03-21 15:31:31 +08:00
|
|
|
import numpy as np
|
2020-03-18 21:45:41 +08:00
|
|
|
from copy import deepcopy
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
|
|
from tianshou.data import Batch
|
|
|
|
from tianshou.policy import BasePolicy
|
|
|
|
# from tianshou.exploration import OUNoise
|
|
|
|
|
|
|
|
|
|
|
|
class DDPGPolicy(BasePolicy):
|
|
|
|
"""docstring for DDPGPolicy"""
|
|
|
|
|
2020-03-20 19:52:29 +08:00
|
|
|
def __init__(self, actor, actor_optim, critic, critic_optim,
|
|
|
|
tau=0.005, gamma=0.99, exploration_noise=0.1,
|
2020-03-21 15:31:31 +08:00
|
|
|
action_range=None, reward_normalization=True):
|
2020-03-18 21:45:41 +08:00
|
|
|
super().__init__()
|
2020-03-20 19:52:29 +08:00
|
|
|
self.actor, self.actor_old = actor, deepcopy(actor)
|
2020-03-18 21:45:41 +08:00
|
|
|
self.actor_old.eval()
|
|
|
|
self.actor_optim = actor_optim
|
2020-03-23 11:34:52 +08:00
|
|
|
if critic is not None:
|
|
|
|
self.critic, self.critic_old = critic, deepcopy(critic)
|
|
|
|
self.critic_old.eval()
|
|
|
|
self.critic_optim = critic_optim
|
2020-03-18 21:45:41 +08:00
|
|
|
assert 0 < tau <= 1, 'tau should in (0, 1]'
|
|
|
|
self._tau = tau
|
|
|
|
assert 0 < gamma <= 1, 'gamma should in (0, 1]'
|
|
|
|
self._gamma = gamma
|
2020-03-19 17:23:46 +08:00
|
|
|
assert 0 <= exploration_noise, 'noise should not be negative'
|
2020-03-18 21:45:41 +08:00
|
|
|
self._eps = exploration_noise
|
|
|
|
self._range = action_range
|
|
|
|
# self.noise = OUNoise()
|
2020-03-21 15:31:31 +08:00
|
|
|
self._rew_norm = reward_normalization
|
|
|
|
self.__eps = np.finfo(np.float32).eps.item()
|
2020-03-18 21:45:41 +08:00
|
|
|
|
|
|
|
def set_eps(self, eps):
|
|
|
|
self._eps = eps
|
|
|
|
|
|
|
|
def train(self):
|
|
|
|
self.training = True
|
|
|
|
self.actor.train()
|
|
|
|
self.critic.train()
|
|
|
|
|
|
|
|
def eval(self):
|
|
|
|
self.training = False
|
|
|
|
self.actor.eval()
|
|
|
|
self.critic.eval()
|
|
|
|
|
|
|
|
def sync_weight(self):
|
|
|
|
for o, n in zip(self.actor_old.parameters(), self.actor.parameters()):
|
|
|
|
o.data.copy_(o.data * (1 - self._tau) + n.data * self._tau)
|
|
|
|
for o, n in zip(
|
|
|
|
self.critic_old.parameters(), self.critic.parameters()):
|
|
|
|
o.data.copy_(o.data * (1 - self._tau) + n.data * self._tau)
|
|
|
|
|
|
|
|
def __call__(self, batch, state=None,
|
|
|
|
model='actor', input='obs', eps=None):
|
|
|
|
model = getattr(self, model)
|
|
|
|
obs = getattr(batch, input)
|
|
|
|
logits, h = model(obs, state=state, info=batch.info)
|
2020-03-20 19:52:29 +08:00
|
|
|
if eps is None:
|
|
|
|
eps = self._eps
|
2020-03-21 15:31:31 +08:00
|
|
|
# noise = np.random.normal(0, eps, size=logits.shape)
|
|
|
|
# noise = self.noise(logits.shape, eps)
|
2020-03-18 21:45:41 +08:00
|
|
|
# logits += torch.tensor(noise, device=logits.device)
|
2020-03-21 15:31:31 +08:00
|
|
|
logits += torch.randn(size=logits.shape, device=logits.device) * eps
|
2020-03-20 19:52:29 +08:00
|
|
|
if self._range:
|
|
|
|
logits = logits.clamp(self._range[0], self._range[1])
|
2020-03-18 21:45:41 +08:00
|
|
|
return Batch(act=logits, state=h)
|
|
|
|
|
2020-03-20 19:52:29 +08:00
|
|
|
def learn(self, batch, batch_size=None, repeat=1):
|
|
|
|
target_q = self.critic_old(batch.obs_next, self(
|
|
|
|
batch, model='actor_old', input='obs_next', eps=0).act)
|
2020-03-18 21:45:41 +08:00
|
|
|
dev = target_q.device
|
2020-03-21 15:31:31 +08:00
|
|
|
rew = torch.tensor(batch.rew, dtype=torch.float, device=dev)[:, None]
|
|
|
|
if self._rew_norm:
|
|
|
|
rew = (rew - rew.mean()) / (rew.std() + self.__eps)
|
|
|
|
done = torch.tensor(batch.done, dtype=torch.float, device=dev)[:, None]
|
|
|
|
target_q = rew + ((1. - done) * self._gamma * target_q).detach()
|
2020-03-18 21:45:41 +08:00
|
|
|
current_q = self.critic(batch.obs, batch.act)
|
|
|
|
critic_loss = F.mse_loss(current_q, target_q)
|
|
|
|
self.critic_optim.zero_grad()
|
|
|
|
critic_loss.backward()
|
|
|
|
self.critic_optim.step()
|
2020-03-20 19:52:29 +08:00
|
|
|
actor_loss = -self.critic(batch.obs, self(batch, eps=0).act).mean()
|
2020-03-18 21:45:41 +08:00
|
|
|
self.actor_optim.zero_grad()
|
|
|
|
actor_loss.backward()
|
|
|
|
self.actor_optim.step()
|
2020-03-19 17:23:46 +08:00
|
|
|
self.sync_weight()
|
|
|
|
return {
|
|
|
|
'loss/actor': actor_loss.detach().cpu().numpy(),
|
|
|
|
'loss/critic': critic_loss.detach().cpu().numpy(),
|
|
|
|
}
|