以下是一个基于LT模型的python代码,实现对未激活节点的改动:

import networkx as nx
import random

def lt_model_beta_theta(G, beta, theta):
    """
    Simulates the LT model with beta-theta rule on the given graph G.
    
    Parameters:
        G (networkx.Graph): the graph to simulate on.
        beta (float): the weight of the neighbor's activation rate in the beta-theta rule.
        theta (float): the threshold for activation in the beta-theta rule.
    
    Returns:
        float: the fraction of activated nodes in the graph at the end of the simulation.
    """
    # initialize all nodes as inactive
    for node in G.nodes():
        G.nodes[node]['active'] = False
    
    # randomly select a seed node to activate
    seed = random.choice(list(G.nodes()))
    G.nodes[seed]['active'] = True
    activated = {seed}
    
    # simulate until no more nodes can be activated
    while True:
        newly_activated = set()
        for node in G.nodes():
            if not G.nodes[node]['active']:
                # calculate the activation rate of the node's neighbors
                neighbor_rate = sum([G.nodes[neighbor]['active'] for neighbor in G.neighbors(node)]) / len(G.neighbors(node))
                # calculate the overall activation rate of the graph
                graph_rate = len(activated) / len(G.nodes())
                # apply the beta-theta rule
                if beta * neighbor_rate + (1 - beta) * graph_rate >= theta:
                    G.nodes[node]['active'] = True
                    newly_activated.add(node)
        # stop if no more nodes can be activated
        if len(newly_activated) == 0:
            break
        activated |= newly_activated
    
    return len(activated) / len(G.nodes())

该函数接受一个图对象和两个参数beta和theta。它首先将所有节点初始化为未激活状态,然后随机选择一个种子节点来激活。接下来,它在每次迭代中遍历所有未激活的节点,并计算其邻居的激活率和整个图的激活率,然后应用beta-theta规则来决定该节点是否应该激活。如果有任何节点被激活,则它们被添加到已激活的节点集合中。当没有更多的节点可以被激活时,函数返回激活节点的比例。

为了测试这个函数,我们可以使用以下代码来生成一个小型的随机图,并在不同的beta和theta参数下运行lt_model_beta_theta函数,以查看最终的全网渗透率。

import matplotlib.pyplot as plt

# generate a random graph
G = nx.gnm_random_graph(50, 100)

# run the LT model with different beta and theta values
beta_values = [0.2, 0.5, 0.8]
theta_values = [0.1, 0.3, 0.5]
results = [[lt_model_beta_theta(G, beta, theta) for theta in theta_values] for beta in beta_values]

# plot the results
for i in range(len(beta_values)):
    plt.plot(theta_values, results[i], label=f'beta={beta_values[i]}')
plt.xlabel('theta')
plt.ylabel('global activation rate')
plt.legend()
plt.show()

这将生成一个图,显示不同beta和theta值下的全网渗透率。我们可以看到,当beta较低(例如0.2)时,较高的theta值(例如0.5)可以产生较高的渗透率,而当beta较高(例如0.8)时,较低的theta值(例如0.1)可以产生较高的渗透率


原文地址: https://www.cveoy.top/t/topic/eP9N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录