import networkx as nx import random import matplotlib.pyplot as plt from matplotlib.colors import Normalize, LinearSegmentedColormap n = 20000000 G = nx.barabasi_albert_graph(n, m) trust = {node: 0 for node in G.nodes} num_mainstream = 500 num_fake = 200 mainstream_nodes = random.sample(list(G.nodes), num_mainstream) fake_nodes = random.sample(list(G.nodes), num_fake) pos = nx.spring_layout(G, k=0.2) # 增大k值使布局更松散 # 更新信任度函数 def update_trust(node, is_mainstream, delta_x): if is_mainstream: trust[node] += delta_x # 主流媒体信任度提升 else: trust[node] -= delta_x * 3.7 # 虚假信息传播强度 trust[node] = max(-1, min(1, trust[node])) # 传播函数,动态调整传播强度并引入成功率 def propagate_info(source_node, is_mainstream, success_rate=0.8): neighbors = list(G.neighbors(source_node)) degree = len(neighbors) delta_x = 0.8 / (degree + 1) # 传播强度与节点度成反比 for neighbor in neighbors: if random.random() < success_rate: # 按照传染成功率传播 update_trust(neighbor, is_mainstream, delta_x) # 蓄意攻击函数,攻击主流媒体节点及其邻居 def attack_targeted_mainstream(num_attack, success_rate=0.75): targeted_nodes = [] for node in mainstream_nodes: neighbors = list(G.neighbors(node)) targeted_nodes.append(node) targeted_nodes.extend(neighbors) targeted_nodes = list(set(targeted_nodes)) if len(targeted_nodes) > num_attack: targeted_nodes = random.sample(targeted_nodes, num_attack) for node in targeted_nodes: propagate_info(node, False, success_rate) return targeted_nodes def repair_network(success_rate=0.4): for node in mainstream_nodes: if random.random() < success_rate: propagate_info(node, True, success_rate) def check_cascade_failure(threshold=0.15, accelerate_threshold=0.25): fake_trust_count = sum(1 for v in trust.values() if v < -0.5) fake_ratio = fake_trust_count / n if fake_ratio > accelerate_threshold: return True, fake_ratio, 0.95 if fake_ratio > threshold: return True, fake_ratio, 0.85 return False, fake_ratio, 0.8 def record_data(round_num, fake_ratio, trust_values): print(f"Round {round_num}: Fake info ratio = {fake_ratio:.2f}") def visualize_trust_custom(ax, round_num): node_colors = [trust[node] for node in G.nodes] norm = Normalize(vmin=-1, vmax=1) node_colors_normalized = [norm(trust[node]) for node in G.nodes] cmap_custom = LinearSegmentedColormap.from_list("custom_heatmap", ["#440154", "#3b0f70", "#8c2981", "#de4968", "#fba238", "#fcffa4"]) nx.draw(G, pos, node_color=node_colors_normalized, node_size=5, cmap=cmap_custom, with_labels=False, ax=ax) ax.set_title(f"Round {round_num}", fontsize=10) ax.axis('off') # 模拟主流媒体与虚假信息的博弈 def simulate_rounds_custom(num_rounds, num_attacks_per_round, initial_success_rate=0.8, collapse_threshold=0.2, accelerate_threshold=0.4): success_rate = initial_success_rate fig, axes = plt.subplots(4, 5, figsize=(14, 15)) axes = axes.flatten() for i in range(num_rounds): attacked_nodes = attack_targeted_mainstream(num_attacks_per_round, success_rate) repair_network() is_cascading, fake_ratio, new_success_rate = check_cascade_failure(collapse_threshold, accelerate_threshold) success_rate = new_success_rate visualize_trust_custom(axes[i], i + 1) record_data(i + 1, fake_ratio, trust) if is_cascading: print(f"Network is cascading in round {i + 1} with fake info ratio {fake_ratio:.2f}. Transmission rate increased to {success_rate}.") if fake_ratio > 0.4: print(f"Network collapsed in round {i + 1} with fake info ratio {fake_ratio:.2f}") break plt.tight_layout() plt.show() simulate_rounds_custom(num_rounds=20, num_attacks_per_round=30, initial_success_rate=0.8, collapse_threshold=0.2, accelerate_threshold=0.4)