在一张图上使用Python进行多个gillespie模拟

时间:2019-03-19 18:42:12

标签: python python-3.x simulation

我已经为进化2x2对称游戏编写了gillespie仿真程序。

我的问题是我不确定如何在图形上多次运行游戏?每当我运行它时,它总是会产生一个新图并在其上运行。

除此之外,我还在努力寻找如何进行多次迭代(例如代码的1000次迭代)的方法。我想记录代码从我拥有的代码中获得i = 0或i = N的次数:

# -*- coding: utf-8 -*-
"""


@author: matthew
"""

import random as random 

import math as math

import matplotlib.pyplot as plt



def function(N):

    t = float(0)
    prop_C=0.5
    i = prop_C*N
    S = float(0.01)
    pay_c = ((0.5 * ((i - 1)/(N - 1))) + (0.1 * ((N - i)/(N - 1))))
    pay_d = (i / (N - 1))

    fd = 1 - (S + (S * pay_d))

    fc = 1 - (S + (S * pay_c))

    avg = ((i  / N) * fc) + ((1 - (i / N)) * fd)

    lami = (i / N) * ((N - i) / N) * (fc / avg)

    mui = ((i / N) * (N - (i / N)) * (fd / avg))

    i_list = []

    t_list = []

    X_list = []
    lami_list=[]
    mui_list=[]

    counter = float(0)
    counterlist=[]
    while i <= N:

        if i==0:

            break

        if i ==N:

            break

        r2 = random.uniform(0,1)

        if 0<= r2 <= lami/(lami + mui):

            i=i+1
            i_list.append(i)
            counter = counter +1
            counterlist.append(counter)
            X = (i/N)

            X_list.append(X)

            pay_c = ((0.5 * ((i - 1)/(N - 1))) + (0.1 * ((N - i)/(N - 1))))

            pay_d = (i / (N - 1))

            fd = 1 - S + (S * pay_d)

            fc = 1 - S + (S * pay_c)

            avg = ((i  / N) * fc) + ((1 - (i / N)) * fd)

            r1 = random.uniform(0,1)

            tau = - (1/(lami + mui)) * math.log(r1)
            lami = (i / N) * ((N - i) / N) * (fc / avg)
            mui = (i / N) * ((N - i) / N) * (fd / avg)

            t = t + tau
            t_list.append(t)

            mui_list.append(mui)
            lami_list.append(lami)

        else:

            i=i-1

            i_list.append(i)
            counter = counter +1
            counterlist.append(counter)

            X = (i/N)

            X_list.append(X)

            pay_c = ((0.5 * ((i - 1)/(N - 1))) + (0.1 * ((N - i)/(N - 1))))

            pay_d = (i / (N - 1))

            fd = 1 - S + (S * pay_d)

            fc = 1 - S + (S * pay_c)

            avg = ((i  / N) * fc) + ((1 - (i / N)) * fd)

            r1 = random.uniform(0,1)
            tau = - (1/(lami + mui)) * math.log(r1)
            lami = (i / N) * ((N - i) / N) * (fc / avg)
            mui = (i / N) * ((N - i) / N) * (fd / avg)

            t = t + tau
            t_list.append(t)

            mui_list.append(mui)
            lami_list.append(lami)
        continue

    plt.title("Gillespie algorithm for Hawk Dove Game")

    plt.plot(t_list, X_list, color = 'red')

    plt.show(plt.plot)

    return(X_list, t_list)
function(250)

0 个答案:

没有答案