1
+ # ## Executing a model
2
+ import pandas as pd
3
+ import numpy as np
4
+ from simulation_summary_functions import SimulationSummary
5
+
6
+ def single_run (scenario ,
7
+ model ,
8
+ rc_period = 600 ,
9
+ random_no_set = 1 ,
10
+ return_detailed_logs = False ,
11
+ ):
12
+ '''
13
+ Perform a single run of the model and return the results
14
+
15
+ Parameters:
16
+ -----------
17
+
18
+ scenario: Scenario object
19
+ The scenario/paramaters to run
20
+
21
+ rc_period: int
22
+ The length of the simulation run that collects results
23
+
24
+ random_no_set: int or None, optional (default=DEFAULT_RNG_SET)
25
+ Controls the set of random seeds used by the stochastic parts of the
26
+ model. Set to different ints to get different results. Set to None
27
+ for a random set of seeds.
28
+
29
+ Returns:
30
+ --------
31
+ pandas.DataFrame:
32
+ results from single run.
33
+ '''
34
+ # set random number set - this controls sampling for the run.
35
+ scenario .set_random_no_set (random_no_set )
36
+
37
+ # create an instance of the model
38
+ model = model (scenario )
39
+
40
+ # run the model
41
+ model .run (results_collection_period = rc_period )
42
+
43
+ # run results
44
+ summary = SimulationSummary (model )
45
+
46
+ if return_detailed_logs :
47
+ return {
48
+ 'full_event_log' : pd .DataFrame (model .full_event_log ),
49
+ 'patient_log' : pd .DataFrame (summary .patient_log ),
50
+ 'utilisation_audit' : pd .DataFrame (model .utilisation_audit ),
51
+ 'summary_df' : summary .summary_frame ()
52
+ }
53
+
54
+ summary_df = summary .summary_frame ()
55
+
56
+ return summary_df
57
+
58
+
59
+ def multiple_replications (scenario ,
60
+ model ,
61
+ rc_period = 600 ,
62
+ n_reps = 10 ,
63
+ return_detailed_logs = False ):
64
+ '''
65
+ Perform multiple replications of the model.
66
+
67
+ Params:
68
+ ------
69
+ scenario: Scenario
70
+ Parameters/arguments to configurethe model
71
+
72
+ rc_period: float, optional (default=DEFAULT_RESULTS_COLLECTION_PERIOD)
73
+ results collection period.
74
+ the number of minutes to run the model to collect results
75
+
76
+ n_reps: int, optional (default=DEFAULT_N_REPS)
77
+ Number of independent replications to run.
78
+
79
+ Returns:
80
+ --------
81
+ pandas.DataFrame
82
+ '''
83
+
84
+ if return_detailed_logs :
85
+ results = [{'rep' : rep + 1 ,
86
+ 'results' : single_run (scenario ,
87
+ model ,
88
+ rc_period ,
89
+ random_no_set = (scenario .random_number_set )+ rep ,
90
+ return_detailed_logs = True )}
91
+ for rep in range (n_reps )]
92
+
93
+ # format and return results in a dataframe
94
+
95
+ return results
96
+
97
+
98
+ # If not returning detailed logs, do some additional steps before returning the summary df
99
+ results = [single_run (scenario ,
100
+ model ,
101
+ rc_period ,
102
+ random_no_set = (scenario .random_number_set )+ rep )
103
+ for rep in range (n_reps )]
104
+
105
+ # format and return results in a dataframe
106
+ df_results = pd .concat (results )
107
+ df_results .index = np .arange (1 , len (df_results )+ 1 )
108
+ df_results .index .name = 'rep'
109
+ return df_results
0 commit comments