-
Notifications
You must be signed in to change notification settings - Fork 0
/
maddpg.py
165 lines (129 loc) · 7.23 KB
/
maddpg.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
## MADDPG implementation using ddpg_agent.py for Actor-Critic
import torch
import random
from collections import namedtuple, deque
import numpy as np
from ddpg_agent import Agent
BUFFER_SIZE = int(1e5) # replay buffer size
BATCH_SIZE = 256 # minibatch size
UPDATE_FREQ = 1
GAMMA = 0.99 # discount factor
TAU = 1e-3 # for soft update of target parameters
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class MADDPG():
def __init__(self, state_size, action_size, num_agents, random_seed):
self.state_size = state_size
self.action_size = action_size
self.random_seed = random.seed(random_seed)
# creating agents and store them into agents list
self.agents = [Agent(state_size, action_size, num_agents, random_seed) for i in range(num_agents)]
# Replay memory
self.memory = ReplayBuffer(BUFFER_SIZE, BATCH_SIZE, random_seed)
self.step_count = 0
# reset each agent's noise
def reset(self):
for agent in self.agents:
agent.reset()
# state: state array for all agents [agent_no, state_size]
# output: actions of all agents [agent_no, action of an agent]
def act(self, state, i_episode, add_noise=True):
actions = []
for agent_state, agent in zip(state, self.agents):
action = agent.act(agent_state, i_episode, add_noise)
action = np.reshape(action, newshape=(-1))
actions.append(action)
actions = np.stack(actions)
return actions
# store states, actions, etc into ReplayBuffer and trigger training regularly
# state and new_state : state of all agents [agent_no, state of an agent]
# action: action of all agents [agent_no, action of an agent]
# reward: reward of all agents [agent_no]
# dones: dones of all agents [agent_no]
def step(self, i_episode, state, action, reward, next_state, done):
full_state = np.reshape(state, newshape=(-1))
next_full_state = np.reshape(next_state, newshape=(-1))
self.memory.add(state, full_state, action, reward, next_state, next_full_state, done)
self.step_count = ( self.step_count + 1 ) % UPDATE_FREQ
if len(self.memory) > BATCH_SIZE and i_episode > 500:
for l_cnt in range(3):
for agent in self.agents:
experiences = self.memory.sample()
self.learn(experiences, agent, GAMMA)
for agent in self.agents:
agent.soft_update(agent.actor_local, agent.actor_target, TAU)
agent.soft_update(agent.critic_local, agent.critic_target, TAU)
# execute learning on an agent
def learn(self, experiences, agent, GAMMA):
# batch dataset for training
states, full_states, actions, rewards, next_states, next_full_states, dones = experiences
# compute NO-NOISE action using target actor and current state - [batch_size, # of agent, action size]
# this will be used as input on critic local network
actor_target_actions = torch.zeros(actions.shape, dtype=torch.float, device=device)
for agent_idx, agent_i in enumerate(self.agents):
if agent == agent_i:
agent_id = agent_idx
agent_i_current_state = states[:,agent_idx]
actor_target_actions[:,agent_idx,:] = agent_i.actor_target.forward(agent_i_current_state)
actor_target_actions = actor_target_actions.view(BATCH_SIZE, -1)
# print(actor_target_actions)
# qweqw
# agent specific state, action, reward, done
agent_state = states[:,agent_id,:]
agent_action = actions[:,agent_id,:]
agent_reward = rewards[:,agent_id].view(-1,1)
agent_done = dones[:,agent_id].view(-1,1)
# print('---')
# print(agent_state)
# print(agent_action)
# print(agent_reward)
# print(agent_done)
# replace action of the specific agent with actor_local output (NOISE removal)
actor_local_actions = actions.clone()
actor_local_actions[:, agent_id, :] = agent.actor_local.forward(agent_state)
actor_local_actions = actor_local_actions.view(BATCH_SIZE, -1)
# print('actor local actions', actor_local_actions)
# flatt actions
actions = actions.view(BATCH_SIZE, -1)
# print('actions', actions)
# qwe
agent_experience = (full_states, actions, actor_local_actions, actor_target_actions,
agent_state, agent_action, agent_reward, agent_done,
next_states, next_full_states)
agent.learn(agent_experience, GAMMA)
def save(self):
for idx, agent in enumerate(self.agents):
chk_actor_filename = 'checkpoint_agent{}_actor.pth'.format(idx)
chk_critic_filename = 'checkpoint_critic{}_critic.pth'.format(idx)
torch.save(agent.actor_local.state_dict(), chk_actor_filename)
torch.save(agent.critic_local.state_dict(), chk_critic_filename)
class ReplayBuffer(object):
"""Fixed-size buffer to store experience tuples."""
def __init__(self, buffer_size, batch_size, seed):
"""Initialize a ReplayBuffer object.
Params
======
buffer_size (int): maximum size of buffer
batch_size (int): size of each training batch
"""
self.memory = deque(maxlen=buffer_size) # internal memory (deque)
self.batch_size = batch_size
self.experience = namedtuple("Experience", field_names=['state', 'full_state', 'action', 'reward', 'next_state', 'next_full_state','done'])
self.seed = random.seed(seed)
def add(self, state, full_state, action, reward, next_state, next_full_state, done):
"""Add a new experience to memory."""
e = self.experience(state, full_state, action, reward, next_state, next_full_state, done)
self.memory.append(e)
def sample(self):
"""Randomly sample a batch of experiences from memory."""
experiences = random.sample(self.memory, k=self.batch_size)
states = torch.from_numpy(np.array([e.state for e in experiences if e is not None])).float().to(device)
full_states = torch.from_numpy(np.array([e.full_state for e in experiences if e is not None])).float().to(device)
actions = torch.from_numpy(np.array([e.action for e in experiences if e is not None])).float().to(device)
rewards = torch.from_numpy(np.array([e.reward for e in experiences if e is not None])).float().to(device)
next_states = torch.from_numpy(np.array([e.next_state for e in experiences if e is not None])).float().to(device)
next_full_states = torch.from_numpy(np.array([e.next_full_state for e in experiences if e is not None])).float().to(device)
dones = torch.from_numpy(np.array([e.done for e in experiences if e is not None]).astype(np.uint8)).float().to(device)
return (states, full_states, actions, rewards, next_states, next_full_states, dones)
def __len__(self):
"""Return the current size of internal memory."""
return len(self.memory)