-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaddpg_agents.py
173 lines (129 loc) · 7.61 KB
/
maddpg_agents.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
import numpy as np
import random
import copy
from collections import namedtuple, deque
import torch
from ddpg_agent import Agent
from memory import ReplayBuffer
from hyperparameters import *
from utils import encode, decode
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class Maddpg():
"""MADDPG Agent : Interacts with and learns from the environment."""
def __init__(self, state_size, action_size, num_agents, random_seed):
"""Initialize a MADDPG Agent object.
Params
======
state_size (int): dimension of each state
action_size (int): dimension of each action
num_agents (int): number of agents
random_seed (int): random seed
"""
super(Maddpg, self).__init__()
self.state_size = state_size
self.action_size = action_size
self.num_agents = num_agents
self.seed = random.seed(random_seed)
# Instantiate Multiple Agent
self.agents = [ Agent(state_size,action_size, random_seed, num_agents)
for i in range(num_agents) ]
# Instantiate Memory replay Buffer (shared between agents)
self.memory = ReplayBuffer(action_size, BUFFER_SIZE, BATCH_SIZE, random_seed)
def reset(self):
"""Reset all the agents"""
for agent in self.agents:
agent.reset()
def act(self, states, noise):
"""Return action to perform for each agents (per policy)"""
return [ agent.act(state, noise) for agent, state in zip(self.agents, states) ]
def step(self, states, actions, rewards, next_states, dones, num_current_episode):
""" # Save experience in replay memory, and use random sample from buffer to learn"""
#self.memory.add(states, It mainly reuse function from ``actions, rewards, next_states, dones)
self.memory.add(encode(states),
encode(actions),
rewards,
encode(next_states),
dones)
# If enough samples in the replay memory and if it is time to update
if (len(self.memory) > BATCH_SIZE) and (num_current_episode % UPDATE_EVERY_NB_EPISODE ==0) :
# Note: this code only expects 2 agents
assert(len(self.agents)==2)
# Allow to learn several time in a row in the same episode
for i in range(MULTIPLE_LEARN_PER_UPDATE):
# Sample a batch of experience from the replay buffer
experiences = self.memory.sample()
# Update Agent #0
self.maddpg_learn(experiences, own_idx=0, other_idx=1)
# Sample another batch of experience from the replay buffer
experiences = self.memory.sample()
# Update Agent #1
self.maddpg_learn(experiences, own_idx=1, other_idx=0)
def maddpg_learn(self, experiences, own_idx, other_idx, gamma=GAMMA):
"""
Update the policy of the MADDPG "own" agent. The actors have only access to agent own
information, whereas the critics have access to all agents information.
Update policy and value parameters using given batch of experience tuples.
Q_targets = r + γ * critic_target(next_state, actor_target(next_state))
where:
actor_target(states) -> action
critic_target(all_states, all_actions) -> Q-value
Params
======
experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples
own_idx (int) : index of the own agent to update in self.agents
other_idx (int) : index of the other agent to update in self.agents
gamma (float): discount factor
"""
states, actions, rewards, next_states, dones = experiences
# Filter out the agent OWN states, actions and next_states batch
own_states = decode(self.state_size, self.num_agents, own_idx, states)
own_actions = decode(self.action_size, self.num_agents, own_idx, actions)
own_next_states = decode(self.state_size, self.num_agents, own_idx, next_states)
# Filter out the OTHER agent states, actions and next_states batch
other_states = decode(self.state_size, self.num_agents, other_idx, states)
other_actions = decode(self.action_size, self.num_agents, other_idx, actions)
other_next_states = decode(self.state_size, self.num_agents, other_idx, next_states)
# Concatenate both agent information (own agent first, other agent in second position)
all_states=torch.cat((own_states, other_states), dim=1).to(device)
all_actions=torch.cat((own_actions, other_actions), dim=1).to(device)
all_next_states=torch.cat((own_next_states, other_next_states), dim=1).to(device)
agent = self.agents[own_idx]
# ---------------------------- Update Critic ---------------------------- #
# Get predicted next-state actions and Q values from target models
all_next_actions = torch.cat((agent.actor_target(own_states), agent.actor_target(other_states)),
dim =1).to(device)
Q_targets_next = agent.critic_target(all_next_states, all_next_actions)
# Compute Q targets for current states (y_i)
Q_targets = rewards + (gamma * Q_targets_next * (1 - dones))
# Compute critic loss
Q_expected = agent.critic_local(all_states, all_actions)
critic_loss = F.mse_loss(Q_expected, Q_targets)
# Minimize the loss
agent.critic_optimizer.zero_grad()
critic_loss.backward()
if (CLIP_CRITIC_GRADIENT):
torch.nn.utils.clip_grad_norm(agent.critic_local.parameters(), 1)
agent.critic_optimizer.step()
# ---------------------------- Update Actor ---------------------------- #
# Compute actor loss
all_actions_pred = torch.cat((agent.actor_local(own_states), agent.actor_local(other_states).detach()),
dim = 1).to(device)
actor_loss = -agent.critic_local(all_states, all_actions_pred).mean()
# Minimize the loss
agent.actor_optimizer.zero_grad()
actor_loss.backward()
agent.actor_optimizer.step()
# ----------------------- Update Target Networks ----------------------- #
agent.soft_update(agent.critic_local, agent.critic_target, TAU)
agent.soft_update(agent.actor_local, agent.actor_target, TAU)
def checkpoints(self):
"""Save checkpoints for all Agents"""
for idx, agent in enumerate(self.agents):
actor_local_filename = 'model_dir/checkpoint_actor_local_' + str(idx) + '.pth'
critic_local_filename = 'model_dir/checkpoint_critic_local_' + str(idx) + '.pth'
actor_target_filename = 'model_dir/checkpoint_actor_target_' + str(idx) + '.pth'
critic_target_filename = 'model_dir/checkpoint_critic_target_' + str(idx) + '.pth'
torch.save(agent.actor_local.state_dict(), actor_local_filename)
torch.save(agent.critic_local.state_dict(), critic_local_filename)
torch.save(agent.actor_target.state_dict(), actor_target_filename)
torch.save(agent.critic_target.state_dict(), critic_target_filename)