更改SIR mod的恢复率

2024-05-15 16:04:49 发布

您现在位置:Python中文网/ 问答频道 /正文

import networkx as nx
from collections import defaultdict
from collections import Counter

def test_transmission(u, v, p):

    return random.random()<p


def discrete_SIR(G,w,initial_infecteds,beta,Vl,duration):

    if G.has_node(initial_infecteds):
        initial_infecteds=[initial_infecteds]           

    N=G.order()
    #t = [tmin]
    S = [N-len(initial_infecteds)]
    #I = [len(initial_infecteds)]
    R = [0]
    V = [0]

    susceptible = defaultdict(lambda: True)  
    #above line is equivalent to u.susceptible=True for all nodes.

    for u in initial_infecteds:
        susceptible[u] = False

    infecteds = [{}]*duration  #bunch of empty sets  
    infecteds[0] = set(initial_infecteds)

    I = [sum(map(len, infecteds))]  #set I[0] to be the total number of infections

    while I[-1]>0 :
        new_infecteds = set()
        vaccinated= set()

        for u in infecteds:
            for v in G.neighbors(u):
                if len(vaccinated)+V[-1]< (Vl*N)  : #check if vaccination over or not


                    if susceptible[v] and test_transmission(u, v, w): 
                        vaccinated.add(v)
                        susceptible[v] = False
         #               print('transmitting vaccination')

                    elif susceptible[v] and test_transmission(u,v,beta):
                        new_infecteds.add(v)
                        susceptible[v]=False
         #               print('transmitting infection')
                else:

        #            print("BYE")
                    if susceptible[v] and test_transmission(u, v,beta): 
                        new_infecteds.add(v)
                        susceptible[v] = False

               #infector[v] = [u]
        recovering_nodes = infecteds.pop()

        infecteds.insert(0,new_infecteds)      


        infecteds = new_infecteds

        I.append(sum(map(len, infecteds)))

        R.append(R[-1]+I[-1])
        V.append(len(vaccinated)+V[-1])
        S.append(N-V[-1]-I[-1]-R[-1])




    return scipy.array(S),scipy.array(V), scipy.array(I),scipy.array(R)



m=100
w=0.2
#ran=nx.gnp_random_graph(100,0.003)
G=nx.grid_2d_graph(m,m,periodic=True)

initial_infections = [(u,v) for (u,v) in G if u==int(m/2) and v==int(m/2)]



S, V, I, R = discrete_SIR(G,w,initial_infecteds=initial_infections,beta=0.5,Vl=1,duration=8)            

这是SIR模型的代码,但这是恢复率1的代码。我想把这段代码改为包含一个可变的参数恢复率,而不是默认值,在本例中是1。我试着修改代码来包含它。基本代码是SIR模型。你知道吗

我在修改后的SIR模型中添加了Joels post所做的更改。你知道吗

簿记-

    next_time = t[-1]+1
    if next_time <= tmax:
        for i in infecteds:
            for u in i:
                node_history[u][0].append(next_time+duration)
                node_history[u][1].append('R')
        for j in new_infecteds:
            for v in j:
                node_history[v][0].append(next_time)
                node_history[v][1].append('I')     

Tags: 代码intestnodenewforlenif
1条回答
网友
1楼 · 发布于 2024-05-15 16:04:49

infecteds是一个集合列表,这样infecteds[T]就是那些刚刚被感染的,infecteds[T-1]就是那些已经被感染了一个时间步的集合,等等。然后弹出infecteds[0][例如,recovering_nodes = infecteds.pop(0)]并将新感染的节点附加到列表中。你知道吗

然后对于每个时间步,只需遍历infecteds中的所有集合。你知道吗

下面是一些相关的伪代码:

duration = 8
infecteds = [{}]*duration  #bunch of empty sets  
infecteds[0] = {1,2,3}
I = [sum(map(len, infecteds))]  #set I[0] to be the total number of infections

while I[-1] >0:
    new_infecteds = {}
    for infected_set in infecteds:
        for infected_node in infected_set:
            Do some stuff with the node and its neighbors.
            new_infecteds gets some things added to it.
    recovering_nodes = infecteds.pop()

    infecteds.insert(0,new_infecteds)

    for node in recovering_nodes:
        update status and do any bookkeeping.

    I.append(sum(map(len, infecteds)))

小心使用“价格”这个词。更高的速率意味着更快的恢复,因此持续时间更短(持续时间类似于1/rate)。你的评论似乎用“比率”这个词来表示“持续时间”,所以对你来说,较高的“比率”实际上是较长的“持续时间”。这与大多数人理解你的意思相反。你知道吗

相关问题 更多 >