mirror of
https://github.com/wassname/DeepRL.git
synced 2026-08-21 11:09:46 +08:00
94 lines
4.7 KiB
Python
94 lines
4.7 KiB
Python
#######################################################################
|
|
# Copyright (C) 2017 Shangtong Zhang(zhangshangtong.cpp@gmail.com) #
|
|
# Permission given to modify the code as long as you keep this #
|
|
# declaration at the top #
|
|
#######################################################################
|
|
|
|
from ..network import *
|
|
from ..component import *
|
|
from .BaseAgent import *
|
|
|
|
class PPOAgent(BaseAgent):
|
|
def __init__(self, config):
|
|
BaseAgent.__init__(self, config)
|
|
self.config = config
|
|
self.task = config.task_fn()
|
|
self.network = config.network_fn(self.task.state_dim, self.task.action_dim)
|
|
self.opt = config.optimizer_fn(self.network.parameters())
|
|
self.total_steps = 0
|
|
self.episode_rewards = np.zeros(config.num_workers)
|
|
self.last_episode_rewards = np.zeros(config.num_workers)
|
|
self.states = self.task.reset()
|
|
self.states = config.state_normalizer(self.states)
|
|
|
|
def iteration(self):
|
|
config = self.config
|
|
rollout = []
|
|
states = self.states
|
|
for _ in range(config.rollout_length):
|
|
actions, log_probs, _, values = self.network.predict(states)
|
|
next_states, rewards, terminals, _ = self.task.step(actions.cpu().detach().numpy())
|
|
self.episode_rewards += rewards
|
|
rewards = config.reward_normalizer(rewards)
|
|
for i, terminal in enumerate(terminals):
|
|
if terminals[i]:
|
|
self.last_episode_rewards[i] = self.episode_rewards[i]
|
|
self.episode_rewards[i] = 0
|
|
next_states = config.state_normalizer(next_states)
|
|
rollout.append([states, values.detach(), actions.detach(), log_probs.detach(), rewards, 1 - terminals])
|
|
states = next_states
|
|
|
|
self.states = states
|
|
pending_value = self.network.predict(states)[-1]
|
|
rollout.append([states, pending_value, None, None, None, None])
|
|
|
|
processed_rollout = [None] * (len(rollout) - 1)
|
|
advantages = self.network.tensor(np.zeros((config.num_workers, 1)))
|
|
returns = pending_value.detach()
|
|
for i in reversed(range(len(rollout) - 1)):
|
|
states, value, actions, log_probs, rewards, terminals = rollout[i]
|
|
terminals = self.network.tensor(terminals).unsqueeze(1)
|
|
rewards = self.network.tensor(rewards).unsqueeze(1)
|
|
actions = self.network.tensor(actions)
|
|
states = self.network.tensor(states)
|
|
next_value = rollout[i + 1][1]
|
|
returns = rewards + config.discount * terminals * returns
|
|
if not config.use_gae:
|
|
advantages = returns - value.detach()
|
|
else:
|
|
td_error = rewards + config.discount * terminals * next_value.detach() - value.detach()
|
|
advantages = advantages * config.gae_tau * config.discount * terminals + td_error
|
|
processed_rollout[i] = [states, actions, log_probs, returns, advantages]
|
|
|
|
states, actions, log_probs_old, returns, advantages = map(lambda x: torch.cat(x, dim=0), zip(*processed_rollout))
|
|
advantages = (advantages - advantages.mean()) / advantages.std()
|
|
|
|
batcher = Batcher(states.size(0) // config.num_mini_batches, [np.arange(states.size(0))])
|
|
for _ in range(config.optimization_epochs):
|
|
batcher.shuffle()
|
|
while not batcher.end():
|
|
batch_indices = batcher.next_batch()[0]
|
|
batch_indices = self.network.tensor(batch_indices).long()
|
|
sampled_states = states[batch_indices]
|
|
sampled_actions = actions[batch_indices]
|
|
sampled_log_probs_old = log_probs_old[batch_indices]
|
|
sampled_returns = returns[batch_indices]
|
|
sampled_advantages = advantages[batch_indices]
|
|
|
|
_, log_probs, entropy_loss, values = self.network.predict(sampled_states, sampled_actions)
|
|
ratio = (log_probs - sampled_log_probs_old).exp()
|
|
obj = ratio * sampled_advantages
|
|
obj_clipped = ratio.clamp(1.0 - self.config.ppo_ratio_clip,
|
|
1.0 + self.config.ppo_ratio_clip) * sampled_advantages
|
|
policy_loss = -torch.min(obj, obj_clipped).mean(0) - config.entropy_weight * entropy_loss.mean()
|
|
|
|
value_loss = 0.5 * (sampled_returns - values).pow(2).mean()
|
|
|
|
self.opt.zero_grad()
|
|
(policy_loss + value_loss).backward()
|
|
nn.utils.clip_grad_norm_(self.network.parameters(), config.gradient_clip)
|
|
self.opt.step()
|
|
|
|
steps = config.rollout_length * config.num_workers
|
|
self.total_steps += steps
|