example_vensim_advanced_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    TimeSeriesOutcome,
16    ScalarOutcome,
17    ema_logging,
18    Policy,
19    MultiprocessingEvaluator,
20    save_results,
21)
22from ema_workbench.connectors.vensim import VensimModel
23from ema_workbench.em_framework.parameters import parameters_from_csv
24
25
26def time_of_max(infected_fraction, time):
27    index = np.where(infected_fraction == np.max(infected_fraction))
28    timing = time[index][0]
29    return timing
30
31
32if __name__ == "__main__":
33    ema_logging.log_to_stderr(ema_logging.INFO)
34
35    model = VensimModel("fluCase", wd="./models/flu", model_file="FLUvensimV1basecase.vpm")
36
37    # outcomes
38    model.outcomes = [
39        TimeSeriesOutcome(
40            "deceased_population_region_1", variable_name="deceased population region 1"
41        ),
42        TimeSeriesOutcome("infected_fraction_R1", variable_name="infected fraction R1"),
43        ScalarOutcome(
44            "max_infection_fraction", variable_name="infected fraction R1", function=np.max
45        ),
46        ScalarOutcome(
47            "time_of_max", variable_name=["infected fraction R1", "TIME"], function=time_of_max
48        ),
49    ]
50
51    # create uncertainties based on csv
52    # FIXME csv is missing
53    model.uncertainties = parameters_from_csv("./models/flu/flu_uncertainties.csv")
54
55    # add policies
56    policies = [
57        Policy("no policy", model_file="FLUvensimV1basecase.vpm"),
58        Policy("static policy", model_file="FLUvensimV1static.vpm"),
59        Policy("adaptive policy", model_file="FLUvensimV1dynamic.vpm"),
60    ]
61
62    with MultiprocessingEvaluator(model, n_processes=-1) as evaluator:
63        results = evaluator.perform_experiments(1000, policies=policies)
64
65    save_results(results, "./data/1000 flu cases with policies.tar.gz")