-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathbuffer.py
83 lines (66 loc) · 3.28 KB
/
buffer.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
import numpy as np
class MultiAgentReplayBuffer:
def __init__(self, max_size, critic_dims, actor_dims,
n_actions, n_agents, batch_size):
self.mem_size = max_size
self.mem_cntr = 0
self.n_agents = n_agents
self.actor_dims = actor_dims
self.batch_size = batch_size
self.n_actions = n_actions
self.state_memory = np.zeros((self.mem_size, critic_dims))
self.new_state_memory = np.zeros((self.mem_size, critic_dims))
self.reward_memory = np.zeros((self.mem_size, n_agents))
self.terminal_memory = np.zeros((self.mem_size, n_agents), dtype=bool)
self.init_actor_memory()
def init_actor_memory(self):
self.actor_state_memory = []
self.actor_new_state_memory = []
self.actor_action_memory = []
for i in range(self.n_agents):
self.actor_state_memory.append(
np.zeros((self.mem_size, self.actor_dims[i])))
self.actor_new_state_memory.append(
np.zeros((self.mem_size, self.actor_dims[i])))
self.actor_action_memory.append(
np.zeros((self.mem_size, self.n_actions)))
def store_transition(self, raw_obs, state, action, reward,
raw_obs_, state_, done):
# this introduces a bug: if we fill up the memory capacity and then
# zero out our actor memory, the critic will still have memories to access
# while the actor will have nothing but zeros to sample. Obviously
# not what we intend.
# In reality, there's no problem with just using the same index
# for both the actor and critic states. I'm not sure why I thought
# this was necessary in the first place. Sorry for the confusion!
#if self.mem_cntr % self.mem_size == 0 and self.mem_cntr > 0:
# self.init_actor_memory()
index = self.mem_cntr % self.mem_size
for agent_idx in range(self.n_agents):
self.actor_state_memory[agent_idx][index] = raw_obs[agent_idx]
self.actor_new_state_memory[agent_idx][index] = raw_obs_[agent_idx]
self.actor_action_memory[agent_idx][index] = action[agent_idx]
self.state_memory[index] = state
self.new_state_memory[index] = state_
self.reward_memory[index] = reward
self.terminal_memory[index] = done
self.mem_cntr += 1
def sample_buffer(self):
max_mem = min(self.mem_cntr, self.mem_size)
batch = np.random.choice(max_mem, self.batch_size, replace=False)
states = self.state_memory[batch]
rewards = self.reward_memory[batch]
states_ = self.new_state_memory[batch]
terminal = self.terminal_memory[batch]
actor_states = []
actor_new_states = []
actions = []
for agent_idx in range(self.n_agents):
actor_states.append(self.actor_state_memory[agent_idx][batch])
actor_new_states.append(self.actor_new_state_memory[agent_idx][batch])
actions.append(self.actor_action_memory[agent_idx][batch])
return actor_states, states, actions, rewards, \
actor_new_states, states_, terminal
def ready(self):
if self.mem_cntr >= self.batch_size:
return True