-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent.py
executable file
·384 lines (339 loc) · 13.6 KB
/
concurrent.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
import numpy as np
import networkx as nx
import requests
import environment
import agents
import ilp
def embedd_requests_with_ilp(topo, rqs, degree_constraint):
"""
Given a set of request find the maximum number of requests that fit into
the topology.
Args:
topo (nx.Graph): Topology on which requests should be fit.
ret (list): List of requests.Request objects.
degree_constraint (int): Maximum degree of each node.
Returns:
embedded_requests (list): List of request ids.
"""
_, ret = ilp.evaluate_model(
g=topo,
rqs=rqs,
inventory=0,
actions=0,
degree_constraint=degree_constraint,
timeout=None,
set_initial_requests=True
)
return ret
def evaluate_sequence(args):
num_nodes = args["num_nodes"]
edge_data = args["edge_data"]
seed = args["seed"]
horizon = args["horizon"]
inventory = args["inventory"]
isize = args["inventory_size"]
request_data = args["request_data"]
num_sequences = args["num_sequences"]
max_degree = args["max_degree"] if "max_degree" in args else None
subsample = args["subsample"] if "subsample" in args else None
sensor_threshold = args["sensor_threshold"] if "sensor_threshold" in args else None
agent_type = args["type"]
base_degrees = args["base_degrees"]
with_ilp = args["with_ilp"]
random = np.random.RandomState(seed=seed)
random_shuffler = np.random.RandomState(seed=100)
rqs = []
if type(request_data[0]) == tuple:
for u, v, i in request_data:
rqs.append(requests.Request(u, v, i))
else:
for i, tm in enumerate(request_data):
rqs.append(requests.TrafficMatrixRequest(tm, i))
states = []
average_embedded = 0
counts = []
for _ in range(num_sequences):
topo = nx.Graph()
topo.add_nodes_from(range(num_nodes))
topo.add_edges_from(edge_data)
env = environment.Environment(topo, max_degree, base_degrees)
seed = random.randint(0, 1000000)
agent = agents.AgentFactory.produce(
order=agent_type,
horizon=horizon,
inventory_size=isize,
requests=rqs,
environment=env,
subsample=subsample,
num_sequences=num_sequences,
seed=seed
)
agent.inventory = inventory
agent.sensing_threshold = sensor_threshold
actions = []
for a in agent.random_action_sequence():
a.apply()
actions.append(a)
current_topo = agent.environment.topology.copy()
order = np.arange(len(rqs))
max_count = None
max_sensor = None
if with_ilp:
agent.environment = environment.Environment(
current_topo.copy(),
degree_limit=max_degree,
base_degree=base_degrees
)
result_rqs = embedd_requests_with_ilp(
topo=agent.environment.topology,
rqs=rqs,
degree_constraint=max_degree
)
for mask in result_rqs:
agent.environment.routed_requests[mask] = []
max_count = len(result_rqs)
max_sensor = agent.sense()
else:
if type(rqs[0]) == requests.Request:
for i in range(10):
count = 0
if subsample is None:
random_shuffler.shuffle(order)
sample = order
else:
sample = random_shuffler.choice(
order,
replace=False,
size=subsample
)
agent.environment = environment.Environment(
current_topo.copy(),
degree_limit=max_degree,
base_degree=base_degrees
)
for i, idx in enumerate(sample):
p = agent.environment.add_request(rqs[idx])
if p is not None:
count += 1
if count > max_count or max_count is None:
max_count = count
max_sensor = agent.sense()
else:
save = {}
for i, r in enumerate(rqs):
p = agent.environment.add_request(r)
if p is not None:
save[r.mask] = []
agent.environment.reset_topology()
max_count = len(save)
agent.environment.routed_requests = save
max_sensor = agent.sense()
average_embedded += max_count
counts.append(max_count)
agent.add_new_reading(max_sensor, states)
if len(states) == 0:
states.append(-1)
return [states, float(average_embedded) / float(num_sequences)]
def fit_requests(topo, rqs, action_budget, capacity_budget,
original_degrees, degree_limit):
"""
Finds the maximum number of requests that fit on a given topology. This
function is intended only for the scenario of unit capacity per edge
and unit demand per request.
Args:
topo (nx.topology): Graph to route requests on.
requests (list): List of requests.Requst objects. Requests that should
be routed.
capacity_budget (int): Budget of capacity budget that can be used.
original_degrees (dict): Node to original degree mapping. I.e., the
degree the node had before mutating the network.
degree_limit (int): Maximum number of edges that can be added to
a node.
Returns:
fitted_requests (list): List of requests.Request objects that
fit into graph.
"""
fitted_request_idx = []
not_fitted = []
for request in rqs:
if topo.has_edge(request.source, request.target):
fitted_request_idx.append(request.mask)
topo.edges[request.source, request.target]["allocated_capacity"] += 1
else:
not_fitted.append(request)
if len(not_fitted) == 0:
return fitted_request_idx
capacity = 0
for _, _, d in topo.edges(data=True):
capacity += d["capacity"]
if capacity < capacity_budget:
for i in range(min(capacity_budget - capacity, action_budget)):
action_budget -= 1
r = not_fitted.pop()
topo.add_edge(r.source,
r.target,
static=False,
capacity=1,
allocated_capacity=1
)
fitted_request_idx.append(r.mask)
if len(not_fitted) == 0:
return fitted_request_idx
env = environment.Environment(topo, degree_limit, original_degrees)
if env.routable_topology.number_of_edges() == 0:
return fitted_request_idx # No free edges to route or move --> done
routed_requests = []
not_routed_requests = []
paths = []
for r in not_fitted:
p = env.add_request(r)
if p is None:
not_routed_requests.append(r)
else:
routed_requests.append(r.mask)
paths.append((r.mask, p))
if len(not_routed_requests) == 0:
fitted_request_idx.extend(routed_requests)
return fitted_request_idx
if action_budget < 2:
fitted_request_idx.extend(routed_requests)
return fitted_request_idx
paths.sort(key=lambda x: len(x[1]))
routable_edges = [(u, v) for u, v in env.routable_topology.edges()]
while action_budget >= 2 and (len(routable_edges) > 0 or len(paths) > 0) \
and len(not_routed_requests) > 0:
r = not_routed_requests.pop()
deg_u = nx.degree(topo, weight="capacity")[r.source]
deg_v = nx.degree(topo, weight="capacity")[r.target]
if deg_u == degree_limit:
free_edge_u = None
for i, j, d in topo.edges(nbunch=u, data=True):
if d["capacity"] - d["allocated_capacity"] > 0:
free_edge_u = (i, j)
break
if deg_v == degree_limit:
free_edge_v = None
for i, j, d in topo.edges(nbunch=v, data=True):
if d["capacity"] - d["allocated_capacity"] > 0:
free_edge_v = (i, j)
break
if deg_u == degree_limit and deg_v == degree_limit and action_budget >= 3:
if free_edge_u is None or free_edge_v is None:
continue
else:
topo.remove_edge(*free_edge_u)
topo.remove_edge(*free_edge_v)
topo.add_edge(r.source,
r.target,
capacity=1,
allocated_capacity=1,
static=False
)
capacity_budget += 1
action_budget -= 3
fitted_request_idx.append(r.mask)
elif ((deg_u < degree_limit and deg_v == degree_limit)
or (deg_v < degree_limit and deg_u == degree_limit)) and action_budget >= 2:
if deg_u < degree_limit:
free_edge_tmp = free_edge_v
node = v
else:
free_edge_tmp = free_edge_u
node = u
# There is not unfree edge. Find longest path any incident
# edge takes part in.
if free_edge_tmp is None:
p = None
for i, j in topo.edges(nbunch=node):
for k, tmp in enumerate(paths):
for x, y in zip(tmp[1][:-1], tmp[1][1:]):
if (x == i and y == j) or (x == j and y == i):
if p is None or len(p[1][1]) < len(tmp[1]):
p = (k, tmp)
free_edge_tmp = (i, j)
break
# Remove path from paths and routed requetss
if p is None:
continue
routed_requests.remove(p[0])
tmp = paths[:p[0]]
if len(paths) > p[0] + 1:
tmp.extend(paths[p[0] + 1:])
for k, l in zip(p[1][1][:-1], p[1][1][1:]):
topo.edges[k, l]["allocated_capacity"] = 0
if k != free_edge_tmp[0] and l != free_edge_tmp[1]:
routable_edges.append(k, l)
topo.remove_edge(*free_edge_tmp)
topo.add_edge(
r.source,
r.target,
capacity=1,
allocated_capacity=1,
static=False
)
action_budget -= 2
fitted_request_idx.append(r.mask)
elif deg_u < degree_limit and deg_v < degree_limit:
taken = 0
if capacity_budget == 0:
if len(routable_edges) == 0:
continue
else:
topo.remove_edge(*routable_edges.pop())
taken = 1
topo.add_edge(
r.source,
r.target,
capacity=1,
allocated_capacity=1,
static=False
)
action_budget -= 1 + taken
fitted_request_idx.append(r.mask)
else:
continue
fitted_request_idx.extend(routed_requests)
return fitted_request_idx
if __name__ == "__main__":
g = nx.random_regular_graph(d=3, n=6, seed=1)
reqs = [rqs.Request(u, v, i) for i, (u, v) in enumerate(g.edges())]
for u, v, d in g.edges(data=True):
d["capacity"] = 1
d["allocated_capacity"] = 0
d["static"] = False
f=fit_requests(g.copy(), reqs, 3, g.number_of_edges(), {i: 0 for i in range(6)}, 3)
print f
g.remove_edge(0, 3)
g.remove_edge(0, 4)
g.remove_edge(0, 5)
f=fit_requests(g.copy(), reqs, 3, g.number_of_edges() + 3, {i: 0 for i in range(6)}, 3)
print f
g.remove_edge(1, 2)
g.add_edge(0, 1, capacity=1, allocated_capacity=0, static=False)
g.add_edge(0, 2, capacity=1, allocated_capacity=0, static=False)
f=fit_requests(g.copy(), reqs, 5, g.number_of_edges() + 3, {i: 0 for i in range(6)}, 3)
print f
g.add_edge(3, 4, capacity=1, allocated_capacity=0, static=False)
g.add_edge(5, 0, capacity=1, allocated_capacity=0, static=False)
print dict(nx.degree(g)).values()
f=fit_requests(g.copy(), reqs, 5, g.number_of_edges() + 3, {i: 0 for i in range(6)}, 3)
print f
random = np.random.RandomState(seed=5)
base_degrees = {i: 0 for i in range(30)}
for i in range(100):
g = nx.random_regular_graph(d=3, n=30, seed=i)
tmp = nx.random_regular_graph(d=3, n=30, seed=i+1)
reqs = [rqs.Request(u, v, i) for i, (u, v) in enumerate(tmp.edges())]
for u, v, d in g.edges(data=True):
d["capacity"] = 1
d["allocated_capacity"] = 0
d["static"] = False
x = np.random.choice(
np.arange(g.number_of_edges()),
replace=False,
size=np.random.randint(3, 20)
)
e = [(u, v) for u, v in g.edges()]
for i in x:
g.remove_edge(*e[i])
f=fit_requests(g.copy(), reqs, 10, g.number_of_edges(), base_degrees, 3)