-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdike_model_simulation.py
99 lines (66 loc) · 3.38 KB
/
dike_model_simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import (unicode_literals, print_function, absolute_import,
division)
from ema_workbench import (Model, MultiprocessingEvaluator, Policy,
Scenario)
from ema_workbench.em_framework.evaluators import perform_experiments
from ema_workbench.em_framework.samplers import sample_uncertainties
from ema_workbench.util import ema_logging
import time
from problem_formulation import get_model_for_problem_formulation
from ema_workbench import save_results
from ema_workbench import (Model, MultiprocessingEvaluator, Policy, Scenario)
#choose problem formulation number, between 0-5
#each problem formulation has its own list of outcomes
if __name__ == '__main__':
ema_logging.log_to_stderr(ema_logging.INFO)
dike_model, planning_steps = get_model_for_problem_formulation(5)
# Build a user-defined scenario and policy:
reference_values = {'Bmax': 175, 'Brate': 1.5, 'pfail': 0.5,
'ID flood wave shape': 4, 'planning steps': 2}
reference_values.update({'discount rate {}'.format(n): 3.5 for n in planning_steps})
scen1 = {}
for key in dike_model.uncertainties:
name_split = key.name.split('_')
if len(name_split) == 1:
scen1.update({key.name: reference_values[key.name]})
else:
scen1.update({key.name: reference_values[name_split[1]]})
ref_scenario = Scenario('reference', **scen1)
# no dike increase, no warning, none of the rfr
zero_policy = {'DaysToThreat': 0}
zero_policy.update({'DikeIncrease {}'.format(n): 0 for n in planning_steps})
zero_policy.update({'RfR {}'.format(n): 0 for n in planning_steps})
pol0 = {}
for key in dike_model.levers:
s1, s2 = key.name.split('_')
pol0.update({key.name: zero_policy[s2]})
policy0 = Policy('Policy 0', **pol0)
# Call random scenarios or policies:
# n_scenarios = 5
# scenarios = sample_uncertainties(dike_model, 50)
# n_policies = 10
# single run
# start = time.time()
# dike_model.run_model(ref_scenario, policy0)
# end = time.time()
# print(end - start)
# results = dike_model.outcomes_output
# series run, important to first run only with the sequential evaluator
# experiments, outcomes = perform_experiments(dike_model, ref_scenario, 5)
# start = time.time()
# with MultiprocessingEvaluator(dike_model) as evaluator:
# results = evaluator.perform_experiments(scenarios=10, policies=policy0)
#
# end = time.time()
# print(end - start)
# start = time.time()
# multiprocessing random policies
with MultiprocessingEvaluator(dike_model) as evaluator:
results = evaluator.perform_experiments(scenarios=10, policies=5)
# end = time.time()
# print(end - start)
# multiprocessing sobol sampling for sensitivity analysis
# with MultiprocessingEvaluator(dike_model) as evaluator:
# results = evaluator.perform_experiments(scenarios=10, policies=policy0,
# uncertainty_sampling='sobol')
# save_results(results, r'./nopolicy.tar.gz') #create tar file to save results