implement bus and ring experiment
This commit is contained in:
parent
10ba0fbd9d
commit
5b67b5b953
@ -161,7 +161,7 @@ def bus_network(n=30, infected_idx=0) -> tuple[Graph, float]:
|
||||
edges = [Edge(nodes[i], nodes[i + 1], 1.0) for i in range(n - 1)]
|
||||
|
||||
network.add_edges(edges)
|
||||
return network, degree_avg(edges)
|
||||
return network, degree_avg(edges), n
|
||||
|
||||
|
||||
def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float, int]:
|
||||
@ -190,7 +190,7 @@ def ring_network(n=30) -> tuple[Graph, float]:
|
||||
edges.append(end_edge)
|
||||
|
||||
network.add_edges(edges)
|
||||
return network, degree_avg(edges)
|
||||
return network, degree_avg(edges), n
|
||||
|
||||
|
||||
def summary(average_degrees: list[float], propagation_speeds: list[float]) -> None:
|
||||
@ -200,6 +200,35 @@ def summary(average_degrees: list[float], propagation_speeds: list[float]) -> No
|
||||
fig.savefig("summary.png")
|
||||
plt.show()
|
||||
|
||||
def bus_experiment():
|
||||
degrees = []
|
||||
speeds = []
|
||||
sizes = [0, 8, 15]
|
||||
for i in sizes:
|
||||
bus, bus_avg_degree, node_count = bus_network(20 + i)
|
||||
do_graph_animation('bus{i}.gif', bus, 90, nx.spiral_layout)
|
||||
speeds.append(bus.rounds_survived / node_count)
|
||||
degrees.append(bus_avg_degree)
|
||||
print(f"\n{node_count} NODE STAR")
|
||||
print(f"average degree = {bus_avg_degree}")
|
||||
print(f"propagation speed = {round(speeds[-1], 2)}")
|
||||
print(f"bus{i} rounds survived = {bus.rounds_survived + 1}")
|
||||
|
||||
summary(degrees, speeds)
|
||||
|
||||
def ring_experiment():
|
||||
degrees = []
|
||||
speeds = []
|
||||
sizes = [0, 8, 15]
|
||||
for i in sizes:
|
||||
ring, ring_avg_degree, node_count = ring_network(20 + i)
|
||||
do_graph_animation('bus{i}.gif', ring, 90, nx.spiral_layout)
|
||||
speeds.append(ring.rounds_survived / node_count)
|
||||
degrees.append(ring_avg_degree)
|
||||
print(f"\n{node_count} NODE STAR")
|
||||
print(f"average degree = {ring_avg_degree}")
|
||||
print(f"propagation speed = {round(speeds[-1], 2)}")
|
||||
print(f"ring{i} rounds survived = {ring.rounds_survived + 1}")
|
||||
|
||||
def star_experiment():
|
||||
degrees = []
|
||||
@ -213,21 +242,13 @@ def star_experiment():
|
||||
print(f"\n{node_count} NODE STAR")
|
||||
print(f"average degree = {star_avg_degree}")
|
||||
print(f"propagation speed = {round(speeds[-1], 2)}")
|
||||
print(f"rounds survived = {star.rounds_survived + 1}")
|
||||
print(f"star{i} rounds survived = {star.rounds_survived + 1}")
|
||||
|
||||
summary(degrees, speeds)
|
||||
|
||||
|
||||
def main():
|
||||
bus, star_avg_degree = bus_network(36)
|
||||
do_graph_animation('bus.gif', bus, 90, nx.spiral_layout)
|
||||
|
||||
ring, star_avg_degree = ring_network(36)
|
||||
do_graph_animation('ring.gif', ring, 90, nx.circular_layout)
|
||||
|
||||
print(f'bus survived {bus.rounds_survived + 1} rounds')
|
||||
print(f'ring survived {ring.rounds_survived + 1} rounds')
|
||||
|
||||
bus_experiment()
|
||||
ring_experiment()
|
||||
star_experiment()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user