当前位置: 首页>>代码示例>>Python>>正文


Python Simulator.add_event方法代码示例

本文整理汇总了Python中simulator.Simulator.add_event方法的典型用法代码示例。如果您正苦于以下问题:Python Simulator.add_event方法的具体用法?Python Simulator.add_event怎么用?Python Simulator.add_event使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在simulator.Simulator的用法示例。


在下文中一共展示了Simulator.add_event方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: execute_simulation

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import add_event [as 别名]
def execute_simulation(add_rio=False, ol_start=0, rio_length=18,
                       rio_visitors=380e3, n_simulations=5):

    sol_global = []
    sol_rio = []
    sol_moscow = []
    sol_berlin = []
    sol_beijing = []
    sol_sydney = []
    sol_new_york = []
    params = {}
    params['global'] = []
    params['rio'] = []
    params['moscow'] = []
    params['berlin'] = []
    params['beijing'] = []
    params['sydney'] = []
    params['new'] = []
    for j in range(n_simulations):
        print("running simulation {0} / {1}".format(j + 1, n_simulations))
        state = State(regions, routes, verbose=True)
        state.set_outbreak('Rio De Janeiro', 1e3)#'Rio De Janeiro', 1000)
        sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5,
                        verbose=True)

        sol_global.append([])
        sol_rio.append([])
        sol_moscow.append([])
        sol_berlin.append([])
        sol_beijing.append([])
        sol_sydney.append([])
        sol_new_york.append([])
        state_list = []
        for i, state in enumerate(sim.run(iterations=120)):
            state_list.append(state)
            if i == ol_start and add_rio: # start outbreak x days before olympics
                sim.add_event(2560, days=rio_length, total_transfer=rio_visitors)

            sol_global[j].append(state.total_sir().as_tuple(total=True))
            sol_rio[j].append(state.region_sir[2560].as_tuple(total=True))
            sol_moscow[j].append(state.region_sir[4029].as_tuple(total=True))
            sol_berlin[j].append(state.region_sir[351].as_tuple(total=True))
            sol_beijing[j].append(state.region_sir[3364].as_tuple(total=True))
            sol_sydney[j].append(state.region_sir[3361].as_tuple(total=True))
            sol_new_york[j].append(state.region_sir[3797].as_tuple(total=True))

        params['global'].append(sir.ParameterEstimator(
            iter([x.total_sir() for x in state_list]), method='max').beta)
        params['rio'].append(sir.ParameterEstimator(
            iter([x.region_sir[2560] for x in state_list]), method='max').beta)
        params['moscow'].append(sir.ParameterEstimator(
            iter([x.region_sir[4029] for x in state_list]), method='max').beta)
        params['berlin'].append(sir.ParameterEstimator(
            iter([x.region_sir[351] for x in state_list]), method='max').beta)
        params['beijing'].append(sir.ParameterEstimator(
            iter([x.region_sir[3364] for x in state_list]), method='max').beta)
        params['sydney'].append(sir.ParameterEstimator(
            iter([x.region_sir[3361] for x in state_list]), method='max').beta)
        params['new'].append(sir.ParameterEstimator(
            iter([x.region_sir[2560] for x in state_list]), method='max').beta)

    if add_rio:
        fig_name = "rio-{0}-{1}-{2:d}.pdf".format(ol_start, rio_length,
                                                  int(rio_visitors))
    else:
        fig_name = "no_rio.pdf"

    plot_sir([sol_global, sol_rio, sol_new_york, sol_berlin,
              sol_moscow, sol_beijing, sol_sydney],
             ['Global', 'Rio De Janeiro', 'New York', 'Berlin',
              'Moscow', 'Beijing', 'Sydney'], fig_name)

    # estimate means and variance
    global_values = sol_global
    peak_times_global = [np.argmax([x[1] for x in y])
                         for y in global_values]
    peak_amount_global = [y[peak][1]
                          for peak, y in zip(peak_times_global, global_values)]


    peak_times_rio = [np.argmax([x[1] for x in y])
                      for y in  sol_rio]
    peak_times_new_york = [np.argmax([x[1] for x in y])
                           for y in  sol_new_york]
    peak_times_berlin = [np.argmax([x[1] for x in y])
                         for y in  sol_berlin]
    peak_times_moscow = [np.argmax([x[1] for x in y])
                         for y in  sol_moscow]
    peak_times_beijing = [np.argmax([x[1] for x in y])
                          for y in  sol_beijing]
    peak_times_sydney = [np.argmax([x[1] for x in y])
                         for y in  sol_sydney]

    t_deviations = scipy.stats.t.ppf(0.975, len(peak_times_rio)-1)

    # estimate variance with control variates
    with open('control-{0}.csv'.format(add_rio), 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow(['global_amount', 'global_amount_control',
                         'global_peak_time', 'global_peak_time_control',
#.........这里部分代码省略.........
开发者ID:AndreasMadsen,项目名称:course-02443-stochastic-virus-outbreaks,代码行数:103,代码来源:rio_olympics_cities.py

示例2: State

# 需要导入模块: from simulator import Simulator [as 别名]
# 或者: from simulator.Simulator import add_event [as 别名]
import os.path as path
import time

from display import WorldMap
from simulator import State, Simulator
from world import regions, routes

this_dir = path.dirname(path.realpath(__file__))

len_simulation = 67

state = State(regions, routes, verbose=True)
state.set_outbreak('Rio De Janeiro', 1000)

sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True,)
sim.add_event(2560, days=18, total_transfer=380e3)

for i, state in enumerate(sim.run(len_simulation)):
    fig_name = path.join(this_dir, '../../report/plots/gifs/frames/rio-{0}.pdf'.format(i))
    if i == 46:
        base_map = WorldMap(resolution="c")
        base_map.scatter_infections(state, max_infected=0.1, time=i)
        base_map.save_fig(fig_name)
        break


state = State(regions, routes, verbose=True)
state.set_outbreak('Rio De Janeiro', 1000)
sim = Simulator(state, transfer_prob=0.005, beta=2, gamma=0.5, verbose=True)

for i, state in enumerate(sim.run(len_simulation)):
开发者ID:AndreasMadsen,项目名称:course-02443-stochastic-virus-outbreaks,代码行数:33,代码来源:rio_olympics_map_frames.py


注:本文中的simulator.Simulator.add_event方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。