-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.py
424 lines (327 loc) · 14.1 KB
/
utils.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import random
from abc import ABC, abstractmethod
from enum import Enum
import networkx as nx
import numpy as np
class EdgeType(Enum):
UNIFORM = 1
DISCRETE = 2
RANDOM = 3
class RewardSignal(Enum):
DENSE = 1
BLS = 2
SINGLE = 3
CUSTOM_BLS = 4
class ExtraAction(Enum):
PASS = 1
RANDOMISE = 2
NONE = 3
class OptimisationTarget(Enum):
CUT = 1
ENERGY = 2
class SpinBasis(Enum):
SIGNED = 1
BINARY = 2
class Observable(Enum):
# Local observations that differ between nodes.
SPIN_STATE = 1
IMMEDIATE_REWARD_AVAILABLE = 2
TIME_SINCE_FLIP = 3
# Global observations that are the same for all nodes.
EPISODE_TIME = 4
TERMINATION_IMMANENCY = 5
NUMBER_OF_GREEDY_ACTIONS_AVAILABLE = 6
DISTANCE_FROM_BEST_SCORE = 7
DISTANCE_FROM_BEST_STATE = 8
DEFAULT_OBSERVABLES = [Observable.SPIN_STATE,
Observable.IMMEDIATE_REWARD_AVAILABLE,
Observable.TIME_SINCE_FLIP,
Observable.DISTANCE_FROM_BEST_SCORE,
Observable.DISTANCE_FROM_BEST_STATE,
Observable.NUMBER_OF_GREEDY_ACTIONS_AVAILABLE,
Observable.TERMINATION_IMMANENCY]
class GraphGenerator(ABC):
def __init__(self, n_spins, edge_type, biased=False):
self.n_spins = n_spins
self.edge_type = edge_type
self.biased = biased
def pad_matrix(self, matrix):
dim = matrix.shape[0]
m = np.zeros((dim+1,dim+1))
m[:-1,:-1] = matrix
return matrix
def pad_bias(self, bias):
return np.concatenate((bias,[0]))
@abstractmethod
def get(self, with_padding=False):
raise NotImplementedError
###################
# Unbiased graphs #
###################
class RandomGraphGenerator(GraphGenerator):
def __init__(self, n_spins=20, edge_type=EdgeType.DISCRETE, biased=False):
super().__init__(n_spins, edge_type, biased)
if self.edge_type == EdgeType.UNIFORM:
self.get_w = lambda : 1
elif self.edge_type == EdgeType.DISCRETE:
self.get_w = lambda : np.random.choice([+1, -1])
elif self.edge_type == EdgeType.RANDOM:
self.get_w = lambda : np.random.uniform(-1, 1)
else:
raise NotImplementedError()
def get(self, with_padding=False):
g_size = self.n_spins
density = np.random.uniform()
matrix = np.zeros((g_size, g_size))
for i in range(self.n_spins):
for j in range(i):
if np.random.uniform() < density:
w = self.get_w()
matrix[i, j] = w
matrix[j, i] = w
matrix = self.pad_matrix(matrix) if with_padding else matrix
if self.biased:
bias = np.array([self.get_w() if np.random.uniform() < density else 0 for _ in range(self.n_spins)])
bias = self.pad_bias(bias) if with_padding else bias
return matrix, bias
else:
return matrix
m = self.pad_matrix(self.matrix) if with_padding else self.matrix
if self.biased:
b = self.pad_bias(self.bias) if with_padding else self.bias
return m, b
else:
return m
class RandomErdosRenyiGraphGenerator(GraphGenerator):
def __init__(self, n_spins=20, p_connection=[0.1,0], edge_type=EdgeType.DISCRETE):
super().__init__(n_spins, edge_type, False)
if type(p_connection) not in [list,tuple]:
p_connection = [p_connection, 0]
assert len(p_connection)==2, "p_connection must have length 2"
self.p_connection = p_connection
if self.edge_type == EdgeType.UNIFORM:
self.get_connection_mask = lambda : np.ones((self.n_spins,self.n_spins))
elif self.edge_type == EdgeType.DISCRETE:
def get_connection_mask():
mask = 2. * np.random.randint(2, size=(self.n_spins, self.n_spins)) - 1.
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
elif self.edge_type == EdgeType.RANDOM:
def get_connection_mask():
mask = 2.*np.random.rand(self.n_spins,self.n_spins)-1
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
else:
raise NotImplementedError()
def get(self, with_padding=False):
p = np.clip(np.random.normal(*self.p_connection),0,1)
g = nx.erdos_renyi_graph(self.n_spins, p)
adj = np.multiply(nx.to_numpy_array(g), self.get_connection_mask())
# No self-connections (this modifies adj in-place).
np.fill_diagonal(adj, 0)
return self.pad_matrix(adj) if with_padding else adj
class RandomBarabasiAlbertGraphGenerator(GraphGenerator):
def __init__(self, n_spins=20, m_insertion_edges=4, edge_type=EdgeType.DISCRETE):
super().__init__(n_spins, edge_type, False)
self.m_insertion_edges = m_insertion_edges
if self.edge_type == EdgeType.UNIFORM:
self.get_connection_mask = lambda : np.ones((self.n_spins,self.n_spins))
elif self.edge_type == EdgeType.DISCRETE:
def get_connection_mask():
mask = 2. * np.random.randint(2, size=(self.n_spins, self.n_spins)) - 1.
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
elif self.edge_type == EdgeType.RANDOM:
def get_connection_mask():
mask = 2.*np.random.rand(self.n_spins,self.n_spins)-1
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
else:
raise NotImplementedError()
def get(self, with_padding=False):
g = nx.barabasi_albert_graph(self.n_spins, self.m_insertion_edges)
adj = np.multiply(nx.to_numpy_array(g), self.get_connection_mask())
# No self-connections (this modifies adj in-place).
np.fill_diagonal(adj, 0)
return self.pad_matrix(adj) if with_padding else adj
class RandomRegularGraphGenerator(GraphGenerator):
def __init__(self, n_spins=20, d_node=[2,0], edge_type=EdgeType.DISCRETE, biased=False):
super().__init__(n_spins, edge_type, biased)
if type(d_node) not in [list,tuple]:
d_node = [d_node, 0]
assert len(d_node)==2, "k_neighbours must have length 2"
self.d_node = d_node
if self.edge_type == EdgeType.UNIFORM:
self.get_connection_mask = lambda : np.ones((self.n_spins,self.n_spins))
elif self.edge_type == EdgeType.DISCRETE:
def get_connection_mask():
mask = 2. * np.random.randint(2, size=(self.n_spins, self.n_spins)) - 1.
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
elif self.edge_type == EdgeType.RANDOM:
def get_connection_mask():
mask = 2.*np.random.rand(self.n_spins,self.n_spins)-1
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
else:
raise NotImplementedError()
def get(self, with_padding=False):
k = np.clip(int(np.random.normal(*self.d_node)),0,self.n_spins)
g = nx.random_regular_graph(k, self.n_spins)
adj = np.multiply(nx.to_numpy_array(g), self.get_connection_mask())
if not self.biased:
# No self-connections (this modifies adj in-place).
np.fill_diagonal(adj, 0)
return self.pad_matrix(adj) if with_padding else adj
class RandomWattsStrogatzGraphGenerator(GraphGenerator):
def __init__(self, n_spins=20, k_neighbours=[2,0], edge_type=EdgeType.DISCRETE, biased=False):
super().__init__(n_spins, edge_type, biased)
if type(k_neighbours) not in [list,tuple]:
k_neighbours = [k_neighbours, 0]
assert len(k_neighbours)==2, "k_neighbours must have length 2"
self.k_neighbours = k_neighbours
if self.edge_type == EdgeType.UNIFORM:
self.get_connection_mask = lambda: np.ones((self.n_spins, self.n_spins))
elif self.edge_type == EdgeType.DISCRETE:
def get_connection_mask():
mask = 2. * np.random.randint(2, size=(self.n_spins, self.n_spins)) - 1.
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
elif self.edge_type == EdgeType.RANDOM:
def get_connection_mask():
mask = 2. * np.random.rand(self.n_spins, self.n_spins) - 1
mask = np.tril(mask) + np.triu(mask.T, 1)
return mask
self.get_connection_mask = get_connection_mask
else:
raise NotImplementedError()
def get(self, with_padding=False):
k = np.clip(int(np.random.normal(*self.k_neighbours)),0,self.n_spins)
g = nx.watts_strogatz_graph(self.n_spins, k, 0)
adj = np.multiply(nx.to_numpy_array(g), self.get_connection_mask())
if not self.biased:
# No self-connections (this modifies adj in-place).
np.fill_diagonal(adj, 0)
return self.pad_matrix(adj) if with_padding else adj
################
# Known graphs #
################
class SingleGraphGenerator(GraphGenerator):
def __init__(self, matrix, bias=None):
n_spins = matrix.shape[0]
if np.isin(matrix,[0,1]).all():
edge_type=EdgeType.UNIFORM
elif np.isin(matrix,[0,-1,1]).all():
edge_type=EdgeType.DISCRETE
else:
edge_type = EdgeType.RANDOM
super().__init__(n_spins, edge_type, bias is not None)
self.matrix = matrix
self.bias = bias
def get(self, with_padding=False):
m = self.pad_matrix(self.matrix) if with_padding else self.matrix
if self.biased:
b = self.pad_bias(self.bias) if with_padding else self.bias
return m, b
else:
return m
class SetGraphGenerator(GraphGenerator):
def __init__(self, matrices, biases=None, ordered=False):
if len(set([m.shape[0]-1 for m in matrices]))==1:
n_spins = matrices[0].shape[0]
else:
raise NotImplementedError("All graphs in SetGraphGenerator must have the same dimension.")
if all([np.isin(m,[0,1]).all() for m in matrices]):
edge_type=EdgeType.UNIFORM
elif all([np.isin(m,[0,-1,1]).all() for m in matrices]):
edge_type=EdgeType.DISCRETE
else:
edge_type = EdgeType.RANDOM
super().__init__(n_spins, edge_type, biases is not None)
if not self.biased:
self.graphs = matrices
else:
assert len(matrices)==len(biases), "Must pass through the same number of matrices and biases."
assert all([len(b)==self.n_spins+1 for b in biases]), "All biases and must have the same dimension as the matrices."
self.graphs = list(zip(matrices, biases))
self.ordered = ordered
if self.ordered:
self.i = 0
def get(self, with_padding=False):
if self.ordered:
m = self.graphs[self.i]
self.i = (self.i + 1)%len(self.graphs)
else:
m = random.sample(self.graphs, k=1)[0]
return self.pad_matrix(m) if with_padding else m
class PerturbedGraphGenerator(GraphGenerator):
def __init__(self, matrices, perturb_mean=0, perturb_std=0.01, biases=None, ordered=False):
if type(matrices) != list:
matrices = list(matrices)
if biases is not None:
if type(biases) != list:
biases = list(biases)
if len(set([m.shape[0] - 1 for m in matrices])) == 1:
n_spins = matrices[0].shape[0]
else:
raise NotImplementedError("All graphs passed to PerturbedGraphGenerator must have the same dimension.")
super().__init__(n_spins, EdgeType.RANDOM, biases is not None)
self.perturb_mean = perturb_mean
self.perturb_std = perturb_std
if not self.biased:
self.graphs = matrices
else:
raise NotImplementedError("Not implemented PerturbedGraphGenerator for biased graphs yet.")
self.ordered = ordered
if self.ordered:
self.i = 0
def get(self, with_padding=False):
if self.ordered:
m = self.graphs[self.i]
self.i = (self.i + 1)%len(self.graphs)
if self.biased:
m, b = m
else:
if not self.biased:
m = random.sample(self.graphs, k=1)[0]
else:
m, b = random.sample(self.graphs, k=1)[0]
# Sample noise.
noise = np.random.normal(self.perturb_mean, self.perturb_std, size=m.shape)
# Set noise to 0 for non-edges in the adjacency matrix.
np.putmask(noise, m == 0, 0)
# Ensure noise is symettric.
noise = np.tril(noise) + np.triu(noise.T, 1)
m = m + noise
return self.pad_matrix(m) if with_padding else m
class HistoryBuffer():
def __init__(self):
self.buffer = {}
self.current_action_hist = set([])
self.current_action_hist_len = 0
def update(self, action):
new_action_hist = self.current_action_hist.copy()
if action in self.current_action_hist:
new_action_hist.remove(action)
self.current_action_hist_len -= 1
else:
new_action_hist.add(action)
self.current_action_hist_len += 1
try:
list_of_states = self.buffer[self.current_action_hist_len]
if new_action_hist in list_of_states:
self.current_action_hist = new_action_hist
return False
except KeyError:
list_of_states = []
list_of_states.append(new_action_hist)
self.current_action_hist = new_action_hist
self.buffer[self.current_action_hist_len] = list_of_states
return True