forked from ctlab/dqnroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn.py
executable file
·319 lines (259 loc) · 11.5 KB
/
dqn.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
import random
import math
import logging
import torch
import torch.nn as nn
import numpy as np
import networkx as nx
import pandas as pd
import pprint
import os
from typing import List, Tuple, Dict, Union
from ..base import *
from .link_state import *
from ...constants import DQNROUTE_LOGGER
from ...messages import *
from ...memory import *
from ...utils import *
from ...networks import *
logger = logging.getLogger(DQNROUTE_LOGGER)
class SharedBrainStorage:
INSTANCE = None
PROCESSED_NODES = 0
@staticmethod
def load(brain_loader: Callable[[], QNetwork], no_nodes: int) -> QNetwork:
if SharedBrainStorage.INSTANCE is None:
SharedBrainStorage.INSTANCE = brain_loader()
SharedBrainStorage.PROCESSED_NODES += 1
#print(f"Brain initialization: {SharedBrainStorage.PROCESSED_NODES} / {no_nodes} agents")
result = SharedBrainStorage.INSTANCE
if SharedBrainStorage.PROCESSED_NODES == no_nodes:
# all nodes have been processes
# prepare this class for possible reuse
SharedBrainStorage.INSTANCE = None
SharedBrainStorage.PROCESSED_NODES = 0
return result
class DQNRouter(LinkStateRouter, RewardAgent):
"""
A router which implements the DQN-routing algorithm.
"""
def __init__(self, batch_size: int, mem_capacity: int, nodes: List[AgentId],
optimizer='rmsprop', brain=None, random_init=False, max_act_time=None,
additional_inputs=[], softmax_temperature: float = 1.5,
probability_smoothing: float = 0.0, load_filename: str = None,
use_single_neural_network: bool = False, **kwargs):
"""
Parameters added by Igor:
:param softmax_temperature: larger temperature means larger entropy of routing decisions.
:param probability_smoothing (from 0.0 to 1.0): if greater than 0, then routing probabilities will
be separated from zero.
:param load_filename: filename to load the neural network. If None, a new network will be created.
:param use_single_neural_network: all routers will reference the same instance of the neural network.
In particular, this very network will be influeced by training steps in all nodes.
"""
super().__init__(**kwargs)
self.batch_size = batch_size
self.memory = Memory(mem_capacity)
self.additional_inputs = additional_inputs
self.nodes = nodes
self.max_act_time = max_act_time
# changed by Igor: custom temperatures for softmax:
self.min_temp = softmax_temperature
# added by Igor: probability smoothing (0 means no smoothing):
self.probability_smoothing = probability_smoothing
# changed by Igor: brain loading process
def load_brain():
b = brain
if b is None:
b = self._makeBrain(additional_inputs=additional_inputs, **kwargs)
if random_init:
b.init_xavier()
else:
if load_filename is not None:
b.change_label(load_filename)
b.restore()
return b
if use_single_neural_network:
self.brain = SharedBrainStorage.load(load_brain, len(nodes))
else:
self.brain = load_brain()
self.use_single_neural_network = use_single_neural_network
self.optimizer = get_optimizer(optimizer)(self.brain.parameters())
self.loss_func = nn.MSELoss()
def route(self, sender: AgentId, pkg: Package, allowed_nbrs: List[AgentId]) -> Tuple[AgentId, List[Message]]:
if self.max_act_time is not None and self.env.time() > self.max_act_time:
return super().route(sender, pkg, allowed_nbrs)
else:
to, estimate, saved_state = self._act(pkg, allowed_nbrs)
reward = self.registerResentPkg(pkg, estimate, to, saved_state)
return to, [OutMessage(self.id, sender, reward)] if sender[0] != 'world' else []
def handleMsgFrom(self, sender: AgentId, msg: Message) -> List[Message]:
if isinstance(msg, RewardMsg):
action, Q_new, prev_state = self.receiveReward(msg)
self.memory.add((prev_state, action[1], -Q_new))
self._replay()
return []
else:
return super().handleMsgFrom(sender, msg)
def _makeBrain(self, additional_inputs=[], **kwargs):
return QNetwork(len(self.nodes), additional_inputs=additional_inputs, one_out=False, **kwargs)
def _act(self, pkg: Package, allowed_nbrs: List[AgentId]):
state = self._getNNState(pkg, allowed_nbrs)
prediction = self._predict(state)[0]
distr = softmax(prediction, self.min_temp)
estimate = -np.dot(prediction, distr)
to = -1
while ('router', to) not in allowed_nbrs:
to = sample_distr(distr)
return ('router', to), estimate, state
def _predict(self, x):
self.brain.eval()
return self.brain(*map(torch.from_numpy, x)).clone().detach().numpy()
def _train(self, x, y):
self.brain.train()
self.optimizer.zero_grad()
output = self.brain(*map(torch.from_numpy, x))
loss = self.loss_func(output, torch.from_numpy(y))
loss.backward()
self.optimizer.step()
return float(loss)
def _getAddInput(self, tag, *args, **kwargs):
if tag == 'amatrix':
amatrix = nx.convert_matrix.to_numpy_array(
self.network, nodelist=self.nodes, weight=self.edge_weight,
dtype=np.float32)
gstate = np.ravel(amatrix)
return gstate
else:
raise Exception('Unknown additional input: ' + tag)
def _getNNState(self, pkg: Package, nbrs: List[AgentId]):
n = len(self.nodes)
addr = np.array(self.id[1])
dst = np.array(pkg.dst[1])
neighbours = np.array(
list(map(lambda v: v in nbrs, self.nodes)),
dtype=np.float32)
input = [addr, dst, neighbours]
for inp in self.additional_inputs:
tag = inp['tag']
add_inp = self._getAddInput(tag)
if tag == 'amatrix':
add_inp[add_inp > 0] = 1
input.append(add_inp)
return tuple(input)
def _sampleMemStacked(self):
"""
Samples a batch of episodes from memory and stacks
states, actions and values from a batch together.
"""
i_batch = self.memory.sample(self.batch_size)
batch = [b[1] for b in i_batch]
states = stack_batch([l[0] for l in batch])
actions = [l[1] for l in batch]
values = [l[2] for l in batch]
return states, actions, values
def _replay(self):
"""
Fetches a batch of samples from the memory and fits against them.
"""
states, actions, values = self._sampleMemStacked()
preds = self._predict(states)
for i in range(self.batch_size):
a = actions[i]
preds[i][a] = values[i]
self._train(states, preds)
class DQNRouterOO(DQNRouter):
"""
Variant of DQN router which uses Q-network with scalar output.
"""
def _makeBrain(self, additional_inputs=[], **kwargs):
return QNetwork(len(self.nodes), additional_inputs=additional_inputs,
one_out=True, **kwargs)
def _act(self, pkg: Package, allowed_nbrs: List[AgentId]):
state = self._getNNState(pkg, allowed_nbrs)
prediction = self._predict(state).flatten()
distr = softmax(prediction, self.min_temp)
# Igor: probability smoothing
distr = (1 - self.probability_smoothing) * distr + self.probability_smoothing / len(distr)
to_idx = sample_distr(distr)
estimate = -np.dot(prediction, distr)
saved_state = [s[to_idx] for s in state]
to = allowed_nbrs[to_idx]
return to, estimate, saved_state
def _nodeRepr(self, node):
return np.array(node)
def _getAddInput(self, tag, nbr):
return super()._getAddInput(tag)
def _getNNState(self, pkg: Package, nbrs: List[AgentId]):
n = len(self.nodes)
addr = self._nodeRepr(self.id[1])
dst = self._nodeRepr(pkg.dst[1])
get_add_inputs = lambda nbr: [self._getAddInput(inp['tag'], nbr)
for inp in self.additional_inputs]
input = [[addr, dst, self._nodeRepr(v[1])] + get_add_inputs(v) for v in nbrs]
return stack_batch(input)
def _replay(self):
states, _, values = self._sampleMemStacked()
self._train(states, np.expand_dims(np.array(values, dtype=np.float32), axis=0))
class DQNRouterEmb(DQNRouterOO):
"""
Variant of DQNRouter which uses graph embeddings instead of
one-hot label encodings.
"""
def __init__(self, embedding: Union[dict, Embedding], edges_num: int, **kwargs):
# Those are used to only re-learn the embedding when the topology is changed
self.prev_num_nodes = 0
self.prev_num_edges = 0
self.init_edges_num = edges_num
self.network_initialized = False
if type(embedding) == dict:
self.embedding = get_embedding(**embedding)
else:
self.embedding = embedding
super().__init__(**kwargs)
def _makeBrain(self, additional_inputs=[], **kwargs):
# return QNetwork(len(self.nodes), additional_inputs=additional_inputs,
# embedding_dim=self.embedding.dim, one_out=True, **kwargs)
# In order to use DQN without CombinedModel uncomment lines above
return CombinedNetwork(
len(self.nodes), additional_inputs=additional_inputs,
embedding_dim=self.embedding.dim, one_out=True, **kwargs
)
def _nodeRepr(self, node):
return self.embedding.transform(node).astype(np.float32)
def networkStateChanged(self):
num_nodes = len(self.network.nodes)
num_edges = len(self.network.edges)
if not self.network_initialized and num_nodes == len(self.nodes) and num_edges == self.init_edges_num:
self.network_initialized = True
if self.network_initialized and (num_edges != self.prev_num_edges or num_nodes != self.prev_num_nodes):
self.prev_num_nodes = num_nodes
self.prev_num_edges = num_edges
self.embedding.fit(self.network, weight=self.edge_weight)
# self.log(pprint.pformat(self.embedding._X), force=self.id[1] == 0)
class DQNRouterNetwork(NetworkRewardAgent, DQNRouter):
pass
class DQNRouterOONetwork(NetworkRewardAgent, DQNRouterOO):
pass
class DQNRouterEmbNetwork(NetworkRewardAgent, DQNRouterEmb):
pass
class ConveyorAddInputMixin:
"""
Mixin which adds conveyor-specific additional NN inputs support
"""
def _getAddInput(self, tag, nbr=None):
if tag == 'work_status':
return np.array(
list(map(lambda n: self.network.nodes[n].get('works', False), self.nodes)),
dtype=np.float32)
if tag == 'working':
nbr_works = 1 if self.network.nodes[nbr].get('works', False) else 0
return np.array(nbr_works, dtype=np.float32)
else:
return super()._getAddInput(tag, nbr)
class DQNRouterConveyor(LSConveyorMixin, ConveyorRewardAgent, ConveyorAddInputMixin, DQNRouter):
pass
class DQNRouterOOConveyor(LSConveyorMixin, ConveyorRewardAgent, ConveyorAddInputMixin, DQNRouterOO):
pass
class DQNRouterEmbConveyor(LSConveyorMixin, ConveyorRewardAgent, ConveyorAddInputMixin, DQNRouterEmb):
pass