生成一段Python代码,模拟复杂网络中的SIS病毒传播模型。其中,节点有三类,对应的传染率分为0.1,0.2,0.3三个等级,对应的节点的恢复率分为0.1,0.2,0.3三个等级。
时间: 2024-06-10 17:06:30 浏览: 132
以下是生成的Python代码,用于模拟复杂网络中的SIS病毒传播模型:
```
import networkx as nx
import numpy as np
# Generate network graph
G = nx.erdos_renyi_graph(1000, 0.2)
# Assign node attributes
for node in G.nodes():
if node % 3 == 0:
G.nodes[node]['infectious_rate'] = 0.3
G.nodes[node]['recovery_rate'] = 0.3
elif node % 3 == 1:
G.nodes[node]['infectious_rate'] = 0.2
G.nodes[node]['recovery_rate'] = 0.2
else:
G.nodes[node]['infectious_rate'] = 0.1
G.nodes[node]['recovery_rate'] = 0.1
# Initialize infected nodes
infected_nodes = set(np.random.choice(G.nodes(), size=50, replace=False))
# Simulate SIS virus spread
for t in range(100):
# Infect susceptible nodes
for node in G.nodes():
if node not in infected_nodes:
neighbors = list(G.adj[node])
infectious_neighbors = [neighbor for neighbor in neighbors if neighbor in infected_nodes]
infectious_rate = sum([G.nodes[infectious_neighbor]['infectious_rate'] for infectious_neighbor in infectious_neighbors])
if np.random.rand() < infectious_rate:
infected_nodes.add(node)
# Recover infected nodes
for node in infected_nodes.copy():
recovery_rate = G.nodes[node]['recovery_rate']
if np.random.rand() < recovery_rate:
infected_nodes.remove(node)
# Calculate final infection rate
final_infection_rate = len(infected_nodes) / G.number_of_nodes()
print("Final infection rate:", final_infection_rate)
```
请注意,这只是一个示例代码,并不保证正确性和完整性。您可以根据需要对代码进行修改和调整。
阅读全文