example_vensim_flu.py

  1"""
  2Created on 20 May, 2011
  3
  4This module shows how you can use vensim models directly
  5instead of coding the model in Python. The underlying case
  6is the same as used in fluExample
  7
  8.. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl>
  9                epruyt <e.pruyt (at) tudelft (dot) nl>
 10"""
 11
 12import numpy as np
 13
 14from ema_workbench import (
 15    RealParameter,
 16    TimeSeriesOutcome,
 17    ema_logging,
 18    ScalarOutcome,
 19    perform_experiments,
 20    Policy,
 21    save_results,
 22)
 23from ema_workbench.connectors.vensim import VensimModel
 24
 25if __name__ == "__main__":
 26    ema_logging.log_to_stderr(ema_logging.INFO)
 27
 28    model = VensimModel("fluCase", wd=r"./models/flu", model_file=r"FLUvensimV1basecase.vpm")
 29
 30    # outcomes
 31    model.outcomes = [
 32        TimeSeriesOutcome(
 33            "deceased_population_region_1", variable_name="deceased population region 1"
 34        ),
 35        TimeSeriesOutcome("infected_fraction_R1", variable_name="infected fraction R1"),
 36        ScalarOutcome(
 37            "max_infection_fraction", variable_name="infected fraction R1", function=np.max
 38        ),
 39    ]
 40
 41    # Plain Parametric Uncertainties
 42    model.uncertainties = [
 43        RealParameter(
 44            "additional_seasonal_immune_population_fraction_R1",
 45            0,
 46            0.5,
 47            variable_name="additional seasonal immune population fraction R1",
 48        ),
 49        RealParameter(
 50            "additional_seasonal_immune_population_fraction_R2",
 51            0,
 52            0.5,
 53            variable_name="additional seasonal immune population fraction R2",
 54        ),
 55        RealParameter(
 56            "fatality_ratio_region_1", 0.0001, 0.1, variable_name="fatality ratio region 1"
 57        ),
 58        RealParameter(
 59            "fatality_rate_region_2", 0.0001, 0.1, variable_name="fatality rate region 2"
 60        ),
 61        RealParameter(
 62            "initial_immune_fraction_of_the_population_of_region_1",
 63            0,
 64            0.5,
 65            variable_name="initial immune fraction of the population of region 1",
 66        ),
 67        RealParameter(
 68            "initial_immune_fraction_of_the_population_of_region_2",
 69            0,
 70            0.5,
 71            variable_name="initial immune fraction of the population of region 2",
 72        ),
 73        RealParameter(
 74            "normal_interregional_contact_rate",
 75            0,
 76            0.9,
 77            variable_name="normal interregional contact rate",
 78        ),
 79        RealParameter(
 80            "permanent_immune_population_fraction_R1",
 81            0,
 82            0.5,
 83            variable_name="permanent immune population fraction R1",
 84        ),
 85        RealParameter(
 86            "permanent_immune_population_fraction_R2",
 87            0,
 88            0.5,
 89            variable_name="permanent immune population fraction R2",
 90        ),
 91        RealParameter("recovery_time_region_1", 0.1, 0.75, variable_name="recovery time region 1"),
 92        RealParameter("recovery_time_region_2", 0.1, 0.75, variable_name="recovery time region 2"),
 93        RealParameter(
 94            "susceptible_to_immune_population_delay_time_region_1",
 95            0.5,
 96            2,
 97            variable_name="susceptible to immune population delay time region 1",
 98        ),
 99        RealParameter(
100            "susceptible_to_immune_population_delay_time_region_2",
101            0.5,
102            2,
103            variable_name="susceptible to immune population delay time region 2",
104        ),
105        RealParameter(
106            "root_contact_rate_region_1", 0.01, 5, variable_name="root contact rate region 1"
107        ),
108        RealParameter(
109            "root_contact_ratio_region_2", 0.01, 5, variable_name="root contact ratio region 2"
110        ),
111        RealParameter(
112            "infection_ratio_region_1", 0, 0.15, variable_name="infection ratio region 1"
113        ),
114        RealParameter("infection_rate_region_2", 0, 0.15, variable_name="infection rate region 2"),
115        RealParameter(
116            "normal_contact_rate_region_1", 10, 100, variable_name="normal contact rate region 1"
117        ),
118        RealParameter(
119            "normal_contact_rate_region_2", 10, 200, variable_name="normal contact rate region 2"
120        ),
121    ]
122
123    # add policies
124    policies = [
125        Policy("no policy", model_file=r"FLUvensimV1basecase.vpm"),
126        Policy("static policy", model_file=r"FLUvensimV1static.vpm"),
127        Policy("adaptive policy", model_file=r"FLUvensimV1dynamic.vpm"),
128    ]
129
130    results = perform_experiments(model, 1000, policies=policies)
131    save_results(results, "./data/1000 flu cases with policies.tar.gz")