Add weighted star experiment
This commit is contained in:
parent
8ee201b319
commit
de0560d538
@ -51,6 +51,9 @@ class Graph:
|
|||||||
nodes.add(edge.node_b)
|
nodes.add(edge.node_b)
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
def get_edges_with_node(self, node: Node):
|
||||||
|
return filter(lambda ed: ed.has_node(node), self.edges)
|
||||||
|
|
||||||
def clear_attacked(self) -> None:
|
def clear_attacked(self) -> None:
|
||||||
for edge in self.edges:
|
for edge in self.edges:
|
||||||
edge.node_a.under_attack = False
|
edge.node_a.under_attack = False
|
||||||
@ -61,7 +64,7 @@ class Graph:
|
|||||||
:param node: Node to search for
|
:param node: Node to search for
|
||||||
:return: An array of tuples (node, weight)
|
:return: An array of tuples (node, weight)
|
||||||
"""
|
"""
|
||||||
edges_with_node = filter(lambda ed: ed.has_node(node), self.edges)
|
edges_with_node = self.get_edges_with_node(node)
|
||||||
nodes = set()
|
nodes = set()
|
||||||
for e in edges_with_node:
|
for e in edges_with_node:
|
||||||
if e.node_a is node:
|
if e.node_a is node:
|
||||||
@ -164,7 +167,7 @@ def bus_network(n=30, infected_idx=0) -> tuple[Graph, float, int]:
|
|||||||
return network, degree_avg(edges), n
|
return network, degree_avg(edges), n
|
||||||
|
|
||||||
|
|
||||||
def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float, int]:
|
def star_network(cluster_count=5, starsize=6, use_weights=False) -> tuple[Graph, float, int]:
|
||||||
node_count = cluster_count + cluster_count * starsize + 1
|
node_count = cluster_count + cluster_count * starsize + 1
|
||||||
nodes = [Node() for _ in range(node_count)]
|
nodes = [Node() for _ in range(node_count)]
|
||||||
nodes[starsize-1].is_infected = True
|
nodes[starsize-1].is_infected = True
|
||||||
@ -172,7 +175,8 @@ def star_network(cluster_count=5, starsize=6) -> tuple[Graph, float, int]:
|
|||||||
edges = []
|
edges = []
|
||||||
for x in range(cluster_count):
|
for x in range(cluster_count):
|
||||||
center_node = x * starsize + x
|
center_node = x * starsize + x
|
||||||
edges += [Edge(nodes[center_node], nodes[i], 1.0) for i in range(center_node + 1, center_node + starsize + 1)]
|
vulnerability = 1.0 if not use_weights else max(1.0, starsize)
|
||||||
|
edges += [Edge(nodes[center_node], nodes[i], vulnerability) for i in range(center_node + 1, center_node + starsize + 1)]
|
||||||
edges.append(Edge(nodes[-1], nodes[center_node], 1.0))
|
edges.append(Edge(nodes[-1], nodes[center_node], 1.0))
|
||||||
|
|
||||||
network = Graph()
|
network = Graph()
|
||||||
@ -201,48 +205,75 @@ def summary(average_degrees: list[float], propagation_speeds: list[float]) -> No
|
|||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
def experiment(network, avg_degree, node_count, iteration):
|
def bus_experiment():
|
||||||
degrees = []
|
degrees = []
|
||||||
speeds = []
|
speeds = []
|
||||||
do_graph_animation(f'bus{iteration}.gif', network, 90, nx.spiral_layout)
|
sizes = [0, 8, 15]
|
||||||
speeds.append(node_count / network.rounds_survived)
|
for i in sizes:
|
||||||
degrees.append(avg_degree)
|
bus, bus_avg_degree, node_count = bus_network(20 + i)
|
||||||
print(f"\n{node_count} NODE BUS")
|
do_graph_animation('bus{i}.gif', bus, 90, nx.spring_layout)
|
||||||
print(f"average degree = {avg_degree}")
|
speeds.append(bus.rounds_survived / node_count)
|
||||||
print(f"propagation speed = {round(speeds[-1], 2)}")
|
degrees.append(bus_avg_degree)
|
||||||
print(f"bus_{iteration} rounds survived = {network.rounds_survived + 1}")
|
print(f"\n{node_count} NODE bus")
|
||||||
|
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.circular_layout)
|
||||||
|
speeds.append(ring.rounds_survived / node_count)
|
||||||
|
degrees.append(ring_avg_degree)
|
||||||
|
print(f"\n{node_count} NODE ring")
|
||||||
|
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 = []
|
||||||
|
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)}")
|
||||||
|
print(f"star{i} rounds survived = {star.rounds_survived + 1}")
|
||||||
|
|
||||||
summary(degrees, speeds)
|
summary(degrees, speeds)
|
||||||
|
|
||||||
|
|
||||||
def bus_experiment():
|
|
||||||
sizes = [0, 8, 15]
|
|
||||||
for i in sizes:
|
|
||||||
bus, bus_avg_degree, node_count = bus_network(20 + i)
|
|
||||||
experiment(bus, bus_avg_degree, node_count, i)
|
|
||||||
|
|
||||||
|
|
||||||
def ring_experiment():
|
|
||||||
sizes = [0, 8, 15]
|
|
||||||
for i in sizes:
|
|
||||||
ring, ring_avg_degree, node_count = ring_network(20 + i)
|
|
||||||
experiment(ring, ring_avg_degree, node_count, i)
|
|
||||||
|
|
||||||
|
|
||||||
def star_experiment():
|
|
||||||
sizes = [0, 8, 15]
|
|
||||||
for i in sizes:
|
|
||||||
star, star_avg_degree, node_count = star_network(cluster_count=3 + i, starsize=3 + i)
|
|
||||||
experiment(star, star_avg_degree, node_count, i)
|
|
||||||
|
|
||||||
def weighted_star_experiment():
|
def weighted_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, use_weights=True)
|
||||||
|
do_graph_animation(f'star_weighted{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)}")
|
||||||
|
print(f"star{i} rounds survived = {star.rounds_survived + 1}")
|
||||||
|
|
||||||
|
summary(degrees, speeds)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
bus_experiment()
|
bus_experiment()
|
||||||
ring_experiment()
|
ring_experiment()
|
||||||
star_experiment()
|
star_experiment()
|
||||||
|
weighted_star_experiment()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user