-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdist_ac.py
409 lines (327 loc) · 11.6 KB
/
dist_ac.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
''' Distributed version of the actor-critic algorithm
Loop version.
References:
-----------
`Fully Decentralized Multi-Agent Reinforcement Learning with Networked Agents.`
Zhang, et al. 2018
'''
from operator import itemgetter
from functools import lru_cache
from copy import deepcopy
from operator import itemgetter
from collections import defaultdict
from pathlib import Path
import dill
from environment import Environment
import numpy as np
np.seterr(all='raise')
# Uncoment to run stand alone script.
import sys
sys.path.append(Path.cwd().as_posix())
# Helpful one-liners
def softmax(x):
e_x = np.exp(np.clip(x - np.max(x), a_min=-20, a_max=0))
return e_x / e_x.sum()
class DistributedActorCritic(object):
def __init__(self, env):
# Network
# todo eliminate dependency.
self.env = env
self.n_states = env.n_states
self.n_actions = env.n_actions
self.n_agents = env.n_nodes
self.n_phi = env.n_phi
self.n_varphi = env.n_varphi
self.seed = env.seed
assert env.n_actions == 2
# Parameters
np.random.seed(self.seed)
self.mu = np.zeros(self.n_agents)
self.next_mu = np.zeros(self.n_agents)
self.w = np.ones((self.n_agents, self.n_phi)) * (1/ self.n_phi)
self.theta = np.ones((self.n_agents, self.n_varphi)) * (1/ self.n_varphi)
self.log = defaultdict(list)
self.reset()
@property
def alpha(self): return self._alpha(self.n_steps)
@lru_cache(maxsize=1)
def _alpha(self, n_steps): return np.power((n_steps + 1), -0.65)
@property
def beta(self): return self._beta(self.n_steps)
@lru_cache(maxsize=1)
def _beta(self, n_steps): return np.power((n_steps + 1), -0.85)
def reset(self):
np.random.seed(self.seed)
self.n_steps = 0
def act(self, varphi):
'''Pick actions
Parameters:
-----------
* varphi: [n_actions, n_agents, n_varphi]
Critic features
Returns:
--------
* actions: [n_agents,]
Boolean array with agents actions for time t.
'''
choices = []
for i in range(self.n_agents):
probs = self.policy(varphi, i)
choices.append(int(np.random.choice(self.n_actions, p=probs)))
return choices
def update_mu(self, rewards):
'''Tracks long-term mean reward
Parameters:
-----------
* rewards: float<n_agents>
instantaneous rewards.
'''
self.next_mu = (1 - self.alpha) * self.mu + self.alpha * rewards
def update(self, state, actions, rewards, next_state, next_actions, C):
'''Updates actor and critic parameters
Parameters:
-----------
* state: tuple<np.array<n_phi>, np.array<n_actions, n_agents, n_varphi>>
features representing the state where
state[0]: phi represents the state at time t as seen by the critic.
state[1]: varphi represents the state at time t as seen by the actor.
* actions: np.array<n_agents>
Actions for each agent at time t.
* rewards: np.array<n_agents>
Instantaneous rewards for each of the agents.
* next_state: tuple<np.array<n_phi>, np.array<n_actions, n_agents, n_varphi>>
features representing the state where
next_state[0]: phi represents the state at time t+1 as seen by the critic.
next_state[1]: varphi represents the state at time t+1 as seen by the actor.
* next_actions: tuple(float<>, float<>)
Actions for each agent at time t+1.
'''
# 1. Common knowledge at timestep-t
phi, varphi = self.env.get_features(state, actions)
next_phi, _ = self.env.get_features(next_state, next_actions)
dq = self.grad_q(phi)
alpha = self.alpha
beta = self.beta
mu = self.mu
advantages = []
deltas = []
grad_ws = []
grad_thetas = []
scores = []
ws = [self.w.tolist()]
thetas = [self.theta.tolist()]
wtilde = np.zeros_like(self.w)
# 2. Iterate agents on the network.
for i in range(self.n_agents):
# 2.1 Compute time-difference delta
delta = rewards[i] - mu[i] + \
self.q(next_phi, i) - self.q(phi, i)
# 2.2 Critic step
grad_w = alpha * delta * dq
wtilde[i, :] = self.w[i, :] + grad_w # [n_phi,]
# 3.3 Actor step
adv = self.advantage(phi, varphi, state, actions, i) # [n_varphi,]
ksi = self.grad_log_policy(varphi, actions, i) # [n_varphi,]
grad_theta = (beta * adv * ksi)
self.theta[i, :] += grad_theta # [n_varphi,]
# Log step
grad_ws.append(grad_w.tolist())
grad_thetas.append(grad_theta.tolist())
scores.append(ksi.tolist())
advantages.append(adv)
deltas.append(float(delta))
# Consensus step.
self.w = C @ wtilde
# Log.
ws.append(self.w.tolist())
thetas.append(self.theta.tolist())
self.n_steps += 1
self.mu = self.next_mu
return advantages, deltas, ws, grad_ws, thetas, grad_thetas, scores
def q(self, phi, i):
'''Q-function
Parameters:
-----------
* phi: np.array<n_phi>
critic features
Returns:
--------
* q: float
q-value for agent i
'''
return self.w[i, :] @ phi
def v(self, varphi, state, actions, i):
'''Relative value-function
A version of value-function where the effects of i-agent's
have been averaged.
Parameters:
-----------
* varphi: np.array<n_actions, n_agents, n_varphi>
actor features
* actions: np.array<n_agents>
actions for agents
* i: integer
index of the agent on the interval {0,N-1}
Returns:
--------
* v: float
value-function with averaged i
'''
probabilities = self.policy(varphi, i)
ret = 0
for j, aj in enumerate(range(self.n_actions)):
_actions = [aj if k == i else ak for k, ak in enumerate(actions)]
phi_aj = self.env.get_phi(state, np.array(_actions))
ret += probabilities[j] * self.q(phi_aj, i)
return ret
def grad_q(self, phi):
'''Gradient of the Q-function
Parameters:
----------
* phi: np.array<n_phi>
Critic features
Returns:
-------
* gradient_q: np.array<n_phi>
'''
return phi
def policy(self, varphi, i):
'''Computes gibbs distribution / Boltzman policies
Parameters:
-----------
* varphi: np.array<n_actions, n_agents, n_varphi>
actor features
* i: integer
index of the agent on the interval {0,N-1}
Returns:
-------
* probabilities: np.array<n_actions>
Stochastic policy
'''
# [n_varphi, n_actions]
# [n_varphi] @ [n_varphi, n_actions] --> [n_actions]
x = self.theta[i, :] @ varphi[:, i, :].T
# [n_actions]
z = softmax(x)
return z
def grad_log_policy(self, varphi, actions, i):
'''Computes gibbs distribution / Boltzman policies
Parameters:
-----------
* varphi: np.array<n_actions, n_agents, n_varphi>
actor features
* actions: np.array<n_agents>
actions for agents
* i: integer
index of the agent on the interval {0,N-1}
Returns:
-------
* grad_log_policy: np.array<n_varphi>
Score for policy of agent i.
'''
# [n_actions]
probabilities = self.policy(varphi, i)
return varphi[actions[i], i, :] - probabilities @ varphi[:, i, :]
def advantage(self, phi, varphi, state, actions, i):
'''Advantage agent i and time t
The advantage for agent-i evaluates the `goodness` of
taking action a_i.
Parameters:
-----------
* phi: np.array<n_phi>
Critic features
* varphi: np.array<n_actions, n_agents, n_varphi>
Actor features
* actions: np.array<n_agents>
actions for agents
* i: integer
index of the agent on the interval {0,N-1}
Returns:
-------
* advantege f
Score for policy of agent i.
'''
return self.q(phi, i) - self.v(varphi, state, actions, i)
def get_q(self, phi):
'''Q-function for each agent-i
Parameters:
-----------
* phi: np.array<n_phi>
Critic features
Returns:
-------
* q: np.array<n_agents>
Q value for each agent in the network.
'''
return [self.q(phi, i) for i in range(self.n_agents)]
def get_pi(self, varphi):
'''The policy agent i at time-t and state given by varphi.
Parameters:
-----------
* varphi: np.array<n_actions, n_agents, n_varphi>
Actor features
Returns:
--------
* global policy: list<list<n_actions>>
List of policies for each agent.
'''
return [self.policy(varphi, i).tolist() for i in range(self.n_agents)]
if __name__ == '__main__':
n_states=3
n_actions=2
n_agents=3
n_phi=4
n_varphi=2
env = Environment(
n_states=n_states,
n_actions=n_actions,
n_agents=n_agents,
n_phi=n_phi,
n_varphi=n_varphi
)
# consensus
consensus = build_consensus_matrix(env.adjacency, method='metropolis')
print(consensus)
# consensus should be a doubly stochastic matrix
eigen_vector = np.ones((n_agents, 1), dtype=int)
# left eigen_vector should equal itself.
np.testing.assert_almost_equal(eigen_vector.T @ consensus, eigen_vector.T)
# right eigen_vector should equal itself.
np.testing.assert_almost_equal(consensus @ eigen_vector, eigen_vector)
dac = DistributedActorCritic(env)
first_private = env.get_features()
actions = dac.act(first_private)
print(actions)
phi, varphi = env.get_features(actions)
np.testing.assert_almost_equal(varphi, first_private)
qs = [dac.q(phi, i) for i in range(n_agents)]
print(f'action_value-function {qs}')
grad_q = dac.grad_q(phi)
print(f'grad_q {grad_q}')
np.testing.assert_almost_equal(grad_q, phi)
pis = [dac.policy(varphi, i) for i in range(n_agents)]
print(f'policies {pis}')
vs = [dac.v(varphi, actions, i) for i in range(n_agents)]
print(f'value-function {vs}')
# np.testing.assert_almost_equal(np.stack(qs), np.stack(vs))
advs = [dac.advantage(phi, varphi, actions, i) for i in range(n_agents)]
print(f'advantages {advs}')
next_actions = dac.act(varphi)
print(f'next_actions {next_actions}')
ksis = [dac.grad_log_policy(varphi, actions, i) for i in range(n_agents)]
# print(f'GradientLogPolicy:\t{ksis}')
# params = dac.get_actor()
# print(f'Actor:\t{params}')
# params = dac.get_critic()
# print(f'Critic:\t{params}')
# params = dac.get_probabilities(state)
# print(f'Probabilities:\t{params}')
# params = dac.get_values(state, actions)
# print(f'Values:\t{params}')
# params = dac.get_qs(state, actions)
# print(f'Qs:\t{params}')
# params = dac.get_advantages(state, actions)
# print(f'Advantages:\t{params}')
# N = int(10e5)
# for i in range(N):
# dac.update(state, actions, rewards, next_states)