-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
282 lines (237 loc) · 8.52 KB
/
simulation.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
from typing import Optional
from models import Node, Connection, Point
import random
from datetime import datetime
colors = [
"red",
"green",
"blue",
"orange",
"purple",
"pink",
"brown",
"gray",
"black",
"cyan",
"magenta",
]
node_markersize = 4
endpoint_markersize = 8
endpoint_linewidth = 5
def create_random_nodes(
number_of_nodes: int,
endpoints: int,
seed: Optional[int] = None,
node_domain: tuple[float, float] = (-20, 20),
endpoint_domain: Optional[tuple[float, float]] = None,
max_connections: int = -1,
) -> tuple[list[Node], list[Connection]]:
"""
Create a list of random nodes.
"""
if node_domain[0] > node_domain[1]:
raise ValueError(
"The first element of node_domain must be less than the second."
)
if endpoint_domain is not None and endpoint_domain[0] > endpoint_domain[1]:
raise ValueError(
"The first element of endpoint_domain must be less than the second."
)
random.seed(seed)
points = [
Point(
random.uniform(*node_domain),
random.uniform(*node_domain),
)
for _ in range(number_of_nodes)
]
nodes, connections = create_nodes(points, endpoints, max_connections)
if endpoint_domain is not None:
for node in filter(lambda n: n.endpoint, nodes):
node.pos = Point(
random.uniform(*endpoint_domain),
random.uniform(*endpoint_domain),
)
for connection in node.connections.values():
connection.update_cost()
return nodes, connections
def create_nodes(
points: list[Point], endpoints: int, max_connections: int = -1
) -> tuple[list[Node], list[Connection]]:
"""
Initialize the nodes and connections between them based on the given points.
The first node is the source and the last node is the sink.
"""
if endpoints > len(points):
raise ValueError(
"The number of endpoints cannot be greater than the number of nodes."
)
nodes = [Node(i, points[i], {}) for i in range(len(points))]
connections = []
for i in range(len(points)):
for j in range(i + 1, len(points)):
connection = Connection((nodes[i], nodes[j]), 0)
connection.update_cost()
connections.append(connection)
nodes[i].connections[j] = connection
nodes[j].connections[i] = connection
for node in nodes[:endpoints]:
node.make_endpoint()
for node in nodes[endpoints:]:
node.top_connections = sorted(
list(node.connections.values()), key=lambda c: c.cost
)[:max_connections]
return nodes, connections
def get_value(node: Node) -> float:
"""
Compute the nodes worth based on the cost from all routes to the connected
endpoints.
"""
connected_nodes = {route.source for route in node.endpoint_routes.values()}
return -sum(
node.connections[connected_node].calculate_cost()
for connected_node in connected_nodes
) * len(connected_nodes)
def find_move_direction(
node: Node, wiggle: float, move_strength: float, max_speed: float
) -> Point:
"""
Find the direction in which the node should move to minimize the cost from
one of the node to the other nodes it is connected to.
"""
# Do not move if the node only knows about one route to an endpoint.
if len(node.endpoint_routes) < 2:
return Point(0, 0)
# The value is the sum of the cost from the source to the sink and the cost
# to the drain.
current_value = get_value(node)
# How does the value change if we move the node a little bit in the x or y
# direction?
node.pos.x += wiggle
new_value = get_value(node)
dx = new_value - current_value
node.pos.x -= wiggle
node.pos.y += wiggle
new_value = get_value(node)
dy = new_value - current_value
node.pos.y -= wiggle
speed_x = dx / wiggle * move_strength
speed_y = dy / wiggle * move_strength
# Limit the speed of the node.
speed_x = min(max_speed, max(-max_speed, speed_x))
speed_y = min(max_speed, max(-max_speed, speed_y))
return Point(speed_x, speed_y)
def simulate(
simulation_steps: int = 1000,
wiggle: float = 0.01,
move_strength: float = 0.01,
number_of_nodes: int = 5,
number_of_endpoints: int = 2,
seed: Optional[int] = None,
transmit_from_endpoints: bool = False,
node_domain: tuple[float, float] = (-20, 20),
endpoint_domain: Optional[tuple[float, float]] = None,
draw_steps: bool = True,
export: bool = False,
max_speed: float = 5,
max_connections: int = -1,
) -> None:
"""
Simulate the network. The nodes will move around to minimize the cost to the
endpoints.
Args:
simulation_steps (int, optional): number of steps to simulate. Defaults
to 1000.
wiggle (float, optional): wiggle factor when calculating the move
direction. Defaults to 0.01.
move_strength (float, optional): distance the node moves each step.
Defaults to 0.01.
number_of_nodes (int, optional): number of moving nodes. Defaults to 5.
number_of_endpoints (int, optional): number of endpoints. Defaults to 2.
seed (int, optional): generate the same random network each time with a
seed. Defaults to 0.
transmit_from_endpoints (bool, optional): choose whether endpoints can
emit signals to nodes. Defaults to False.
"""
from matplotlib import pyplot as plt # type: ignore
from tqdm import trange
nodes, connections = create_random_nodes(
number_of_nodes,
number_of_endpoints,
seed,
node_domain,
endpoint_domain,
max_connections,
)
for _ in trange(simulation_steps, desc="Simulating time steps"):
for node in nodes:
if draw_steps:
plt.plot(
node.pos.x,
node.pos.y,
"o",
color=colors[node.id % len(colors)],
markersize=node_markersize,
)
node.send_routes(transmit_from_endpoints)
directions = {
node.id: find_move_direction(node, wiggle, move_strength, max_speed)
for node in nodes
if not node.endpoint
}
for node in nodes:
if node.endpoint:
continue
direction = directions[node.id]
node.pos.x += direction.x
node.pos.y += direction.y
for connection in connections:
connection.update_cost()
# Plot the connections
min_cost = min(connection.cost for connection in connections)
max_strength = 1 / max(min_cost, 0.001)
for node in filter(lambda n: not n.endpoint or transmit_from_endpoints, nodes):
for source in set(map(lambda r: r.source, node.endpoint_routes.values())):
if source == node.id: # Do not plot the connection to itself.
continue
connection = node.connections[source]
if connection.cost <= 0:
continue
strength = 1 / connection.cost
plt.plot(
[n.pos.x for n in connection.nodes],
[n.pos.y for n in connection.nodes],
color="black",
alpha=min(1, (strength / max_strength) ** 0.3),
)
if not draw_steps:
for node in filter(lambda n: not n.endpoint, nodes):
plt.plot(
node.pos.x,
node.pos.y,
"o",
color=colors[node.id % len(colors)],
markersize=node_markersize,
)
# Plot endpoints
for node in filter(lambda n: n.endpoint, nodes):
plt.plot(
node.pos.x,
node.pos.y,
marker="P",
color=colors[node.id % len(colors)],
markersize=endpoint_markersize,
linewidth=endpoint_linewidth,
)
plt.text(node.pos.x + 1, node.pos.y, f"endpoint {node.id}")
if export:
time = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
# file name is time + all args separated by underscores
filename = (
f"{time}_{simulation_steps=}_{wiggle=}_{move_strength=}"
f"{number_of_nodes=}_{number_of_endpoints=}_{seed=}"
f"{transmit_from_endpoints=}_{node_domain=}_{endpoint_domain=}.png"
)
plt.savefig(filename, bbox_inches="tight", dpi=2000)
else:
plt.show()