summary graph and experiment
This commit is contained in:
parent
f2d85a3e7d
commit
f6e749269a
@ -146,12 +146,12 @@ def do_graph_animation(output_file_name: str, in_graph: Graph, frame_count: int,
|
||||
plt.show()
|
||||
|
||||
|
||||
def rank_avg(edges, digits=2):
|
||||
ranks = {}
|
||||
def degree_avg(edges, digits=2):
|
||||
degrees = {}
|
||||
for e in edges:
|
||||
ranks[e.node_a] = ranks.get(e.node_a, 0) + 1
|
||||
ranks[e.node_b] = ranks.get(e.node_b, 0) + 1
|
||||
return round(mean(ranks.values()), digits)
|
||||
degrees[e.node_a] = degrees.get(e.node_a, 0) + 1
|
||||
degrees[e.node_b] = degrees.get(e.node_b, 0) + 1
|
||||
return round(mean(degrees.values()), digits)
|
||||
|
||||
|
||||
def bus_network(n=30, infected_idx=0) -> tuple[Graph, float]:
|
||||
@ -161,12 +161,13 @@ 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, rank_avg(edges)
|
||||
return network, degree_avg(edges)
|
||||
|
||||
|
||||
def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float]:
|
||||
def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float, int]:
|
||||
node_count = cluster_count + cluster_count * starsize + 1
|
||||
nodes = [Node() for _ in range(node_count)]
|
||||
nodes[starsize-1].is_infected = True
|
||||
|
||||
edges = []
|
||||
for x in range(cluster_count):
|
||||
@ -177,7 +178,7 @@ def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float]:
|
||||
network = Graph()
|
||||
network.add_edges(edges)
|
||||
|
||||
return network, rank_avg(edges)
|
||||
return network, degree_avg(edges), node_count
|
||||
|
||||
|
||||
def ring_network(n=30) -> tuple[Graph, float]:
|
||||
@ -189,23 +190,44 @@ def ring_network(n=30) -> tuple[Graph, float]:
|
||||
edges.append(end_edge)
|
||||
|
||||
network.add_edges(edges)
|
||||
return network, rank_avg(edges)
|
||||
return network, degree_avg(edges)
|
||||
|
||||
|
||||
def summary(average_degrees: list[float], propagation_speeds: list[float]) -> None:
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot(average_degrees, propagation_speeds)
|
||||
ax.set(xlabel='Average degree', ylabel='Propagation speed', title='Summary')
|
||||
fig.savefig("summary.png")
|
||||
plt.show()
|
||||
|
||||
|
||||
def star_experiment():
|
||||
degrees = []
|
||||
speeds = []
|
||||
sizes = range(0, 8, 2)
|
||||
for i in sizes:
|
||||
star, star_avg_degree, node_count = star_network(cluster_count=3 + i, starsize=3 + i)
|
||||
do_graph_animation(f'star{i}.gif', star, 120, nx.kamada_kawai_layout)
|
||||
speeds.append(star.rounds_survived / node_count)
|
||||
degrees.append(star_avg_degree)
|
||||
print(f"\n{node_count} NODE STAR")
|
||||
print(f"average degree = {star_avg_degree}")
|
||||
print(f"propagation speed = {round(speeds[-1], 2)}")
|
||||
|
||||
summary(degrees, speeds)
|
||||
|
||||
|
||||
def main():
|
||||
bus, star_avg_rank = bus_network(36)
|
||||
bus, star_avg_degree = bus_network(36)
|
||||
do_graph_animation('bus.gif', bus, 90, nx.spiral_layout)
|
||||
|
||||
ring, star_avg_rank = ring_network(36)
|
||||
ring, star_avg_degree = ring_network(36)
|
||||
do_graph_animation('ring.gif', ring, 90, nx.circular_layout)
|
||||
|
||||
star, star_avg_rank = star_network()
|
||||
list(star.get_nodes())[1].is_infected = True
|
||||
do_graph_animation('star.gif', star, 90, nx.kamada_kawai_layout)
|
||||
|
||||
print(f'bus survived {bus.rounds_survived} rounds')
|
||||
print(f'ring survived {ring.rounds_survived} rounds')
|
||||
print(f'star survived {star.rounds_survived} rounds')
|
||||
|
||||
star_experiment()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user