2022-06-16 19:57:51 +02:00
|
|
|
import random
|
|
|
|
import networkx as nx
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
from matplotlib import animation
|
|
|
|
|
|
|
|
|
2022-06-16 18:39:16 +02:00
|
|
|
class Node:
|
2022-06-16 19:57:51 +02:00
|
|
|
def __init__(self, is_infected=False):
|
|
|
|
self.id = random.randint(1, 2000000)
|
|
|
|
self.is_infected = is_infected
|
|
|
|
|
|
|
|
def as_tuple(self):
|
|
|
|
return self.id, self.is_infected
|
|
|
|
|
|
|
|
def __repr__(self):
|
2022-06-16 20:58:13 +02:00
|
|
|
return f"id: {self.id}, infected: {self.is_infected}"
|
2022-06-16 18:39:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Edge:
|
|
|
|
def __init__(self, node_a: Node, node_b: Node, weight: float):
|
|
|
|
self.node_a = node_a
|
|
|
|
self.node_b = node_b
|
|
|
|
self.weight = weight
|
|
|
|
|
2022-06-16 19:57:51 +02:00
|
|
|
def as_tuple(self):
|
2022-06-16 20:58:13 +02:00
|
|
|
return self.node_a, self.node_b, {"weight": self.weight}
|
2022-06-16 19:57:51 +02:00
|
|
|
|
2022-06-16 18:39:16 +02:00
|
|
|
|
|
|
|
class Graph:
|
|
|
|
def __init__(self):
|
|
|
|
self.edges = []
|
|
|
|
|
|
|
|
def add_edge(self, edge: Edge):
|
2022-06-16 19:57:51 +02:00
|
|
|
self.edges.append(edge)
|
|
|
|
|
|
|
|
def add_edges(self, edges: [Edge]):
|
|
|
|
[self.edges.append(e) for e in edges]
|
|
|
|
|
2022-06-16 20:14:56 +02:00
|
|
|
def get_nodes(self) -> [Node]:
|
|
|
|
nodes = set()
|
|
|
|
for edge in self.edges:
|
|
|
|
nodes.add(edge.node_a)
|
|
|
|
nodes.add(edge.node_b)
|
|
|
|
return nodes
|
2022-06-16 19:57:51 +02:00
|
|
|
|
2022-06-16 20:14:56 +02:00
|
|
|
|
|
|
|
def update(num, layout, g_repr, ax, our_graph: Graph):
|
2022-06-16 19:57:51 +02:00
|
|
|
"""
|
|
|
|
This function is called every 'step', so if you wish to update the graph, do it here
|
|
|
|
"""
|
|
|
|
ax.clear()
|
2022-06-16 20:14:56 +02:00
|
|
|
|
|
|
|
for n in our_graph.get_nodes():
|
|
|
|
n.is_infected = bool(random.getrandbits(1))
|
|
|
|
|
2022-06-16 20:58:13 +02:00
|
|
|
colors = ["red" if n.is_infected else "blue" for n in g_repr]
|
2022-06-16 20:14:56 +02:00
|
|
|
nx.draw_networkx(g_repr, ax=ax, pos=layout, node_color=colors, with_labels=False)
|
2022-06-16 19:57:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def do_graph_animation(output_file_name: str, in_graph: Graph, frame_count: int):
|
|
|
|
g_repr = nx.Graph()
|
|
|
|
# Convert our graph class into tuples understood by networkx
|
|
|
|
g_repr.add_edges_from([e.as_tuple() for e in in_graph.edges])
|
|
|
|
|
|
|
|
layout = nx.spring_layout(g_repr)
|
|
|
|
fig, ax = plt.subplots()
|
|
|
|
|
2022-06-16 20:58:13 +02:00
|
|
|
anim = animation.FuncAnimation(
|
|
|
|
fig, update, frames=frame_count, fargs=(layout, g_repr, ax, in_graph)
|
|
|
|
)
|
2022-06-16 19:57:51 +02:00
|
|
|
anim.save(output_file_name)
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
2022-06-16 20:44:58 +02:00
|
|
|
def bus_network(n=30) -> Graph:
|
|
|
|
network = Graph()
|
|
|
|
nodes = [Node() for _ in range(n)]
|
2022-06-16 20:58:13 +02:00
|
|
|
edges = [Edge(nodes[i], nodes[i + 1], 1.0) for i in range(n - 1)]
|
|
|
|
|
|
|
|
network.add_edges(edges)
|
|
|
|
return network
|
|
|
|
|
|
|
|
|
|
|
|
def ring_network(n=30) -> Graph:
|
|
|
|
network = Graph()
|
|
|
|
nodes = [Node() for _ in range(n)]
|
|
|
|
edges = [Edge(nodes[i], nodes[i + 1], 1.0) for i in range(n - 1)]
|
|
|
|
end_edge = Edge(nodes[n - 1], nodes[0], 1.0)
|
|
|
|
edges.append(end_edge)
|
2022-06-16 20:44:58 +02:00
|
|
|
|
|
|
|
network.add_edges(edges)
|
|
|
|
return network
|
|
|
|
|
|
|
|
|
2022-06-16 19:57:51 +02:00
|
|
|
def main():
|
|
|
|
network = Graph()
|
|
|
|
nodes = [Node(True), Node(), Node(), Node(True), Node()]
|
|
|
|
|
2022-06-16 20:58:13 +02:00
|
|
|
network.add_edges(
|
|
|
|
[
|
|
|
|
Edge(nodes[1], nodes[0], 0.02),
|
|
|
|
Edge(nodes[1], nodes[2], 0.2),
|
|
|
|
Edge(nodes[2], nodes[0], 0.7),
|
|
|
|
Edge(nodes[3], nodes[2], 0.2),
|
|
|
|
Edge(nodes[3], nodes[1], 0.2),
|
|
|
|
Edge(nodes[4], nodes[3], 0.2),
|
|
|
|
]
|
|
|
|
)
|
2022-06-16 19:57:51 +02:00
|
|
|
|
2022-06-16 20:58:13 +02:00
|
|
|
do_graph_animation("test.gif", network, 5)
|
2022-06-16 19:57:51 +02:00
|
|
|
|
2022-06-16 20:44:58 +02:00
|
|
|
bus = bus_network()
|
2022-06-16 20:58:13 +02:00
|
|
|
do_graph_animation("bus.gif", bus, 5)
|
|
|
|
|
|
|
|
ring = ring_network()
|
|
|
|
do_graph_animation("ring.gif", ring, 5)
|
2022-06-16 20:44:58 +02:00
|
|
|
|
2022-06-16 19:57:51 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|