-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdes_classes.py
349 lines (269 loc) · 11.5 KB
/
des_classes.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import simpy
import random
import pandas as pd
class g:
# Inter-arrival times
patient_inter = 10
call_inter = 5
# Activity times
mean_reg_time = 2
mean_gp_time = 8
mean_book_test_time = 4
mean_call_time = 4
# Resource numbers
number_of_receptionists = 1
number_of_gps = 2
# Branch probabilities
prob_book_test = 0.25
# Simulation meta parameters
sim_duration =450
number_of_runs = 10
# Class representing patients coming in to the GP surgery
class Patient:
def __init__(self, p_id):
self.id = p_id
self.arrival_time = 0
self.q_time_reg = 0
self.q_time_gp = 0
self.time_with_gp = 0
self.q_time_book_test = 0
self.time_with_receptionist = 0.0
# Class representing callers phoning the GP surgery
class Caller:
def __init__(self, c_id):
self.id = c_id
self.call_time = 0
self.time_with_receptionist = 0.0
self.q_time_call = 0
# Class representing our model of the GP surgery
class Model:
# Constructor
def __init__(self, run_number):
# Set up SimPy environment
self.env = simpy.Environment()
# Set up counters to use as entity IDs
self.patient_counter = 0
self.caller_counter = 0
# Set up lists to store patient objects
self.patient_objects = [] ##NEW
self.caller_objects = [] ##NEW
# Set up resources
self.receptionist = simpy.Resource(
self.env, capacity=g.number_of_receptionists
)
self.gp = simpy.Resource(
self.env, capacity=g.number_of_gps
)
# Set run number from value passed in
self.run_number = run_number
# Set up DataFrame to store patient-level results
self.patient_results_df = pd.DataFrame()
self.patient_results_df["Patient ID"] = [1]
self.patient_results_df["Arrival Time"] = [0.0]
self.patient_results_df["Queue Time Reg"] = [0.0]
self.patient_results_df["Time Seen For Registration"] = [0.0]
self.patient_results_df["Queue Time GP"] = [0.0]
self.patient_results_df["Time Seen By GP"] = [0.0]
self.patient_results_df["Queue Time Book Test"] = [0.0]
self.patient_results_df["Time Test Booking Started"] = [0.0]
self.patient_results_df["Departure Time"] = [0.0]
self.patient_results_df.set_index("Patient ID", inplace=True)
# Set up DataFrame to store caller-level results
self.caller_results_df = pd.DataFrame()
self.caller_results_df["Caller ID"] = [1]
self.caller_results_df["Call Start Time"] = [0.0]
self.caller_results_df["Queue Time Call"] = [0.0]
self.caller_results_df["Call Answered At"] = [0.0]
self.caller_results_df["Call End Time"] = [0.0]
self.caller_results_df.set_index("Caller ID", inplace=True)
# Set up attributes that will store mean queuing times across the run
self.mean_q_time_reg = 0
self.mean_q_time_gp = 0
self.mean_q_time_book_test = 0
self.mean_q_time_call = 0
# Set up attributes used to monitor total resource usage
self.receptionist_utilisation_prop = 0.0
self.gp_utilisation_prop = 0.0
# Generator function that represents the DES generator for patient arrivals
def generator_patient_arrivals(self):
while True:
self.patient_counter += 1
p = Patient(self.patient_counter)
self.patient_objects.append(p) ##NEW
self.env.process(self.attend_gp_surgery(p))
sampled_inter = random.expovariate(1.0 / g.patient_inter)
yield self.env.timeout(sampled_inter)
# Generator function that represents the DES generator for caller arrivals
def generator_callers(self):
while True:
self.caller_counter += 1
c = Caller(self.caller_counter)
self.caller_objects.append(c) ##NEW
self.env.process(self.call_gp_surgery(c))
sampled_inter = random.expovariate(1.0 / g.call_inter)
yield self.env.timeout(sampled_inter)
# Generator function representing pathway for patients attending the GP
# surgery to see a GP
def attend_gp_surgery(self, patient):
# Registration activity
start_q_reg = self.env.now
self.patient_results_df.at[patient.id, "Arrival Time"] = (
start_q_reg
)
with self.receptionist.request() as req:
yield req
end_q_reg = self.env.now
patient.q_time_reg = end_q_reg - start_q_reg
self.patient_results_df.at[patient.id, "Queue Time Reg"] = (
patient.q_time_reg
)
self.patient_results_df.at[patient.id, "Time Seen For Registration"] = (
start_q_reg + patient.q_time_reg
)
sampled_reg_time = random.expovariate(
1.0 / g.mean_reg_time
)
patient.time_with_receptionist += sampled_reg_time
yield self.env.timeout(sampled_reg_time)
# GP Consultation activity
start_q_gp = self.env.now
with self.gp.request() as req:
yield req
end_q_gp = self.env.now
patient.q_time_gp = end_q_gp - start_q_gp
self.patient_results_df.at[patient.id, "Queue Time GP"] = (
patient.q_time_gp
)
self.patient_results_df.at[patient.id, "Time Seen By GP"] = (
start_q_gp + patient.q_time_gp
)
sampled_gp_time = random.expovariate(
1.0 / g.mean_gp_time
)
patient.time_with_gp += sampled_gp_time
yield self.env.timeout(sampled_gp_time)
# Branching path check to see if patient needs to book a test
if random.uniform(0,1) < g.prob_book_test:
# Book test activity
start_q_book_test = self.env.now
with self.receptionist.request() as req:
yield req
end_q_book_test = self.env.now
patient.q_time_book_test = end_q_book_test - start_q_book_test
self.patient_results_df.at[patient.id, "Queue Time Book Test"] = (
patient.q_time_book_test
)
self.patient_results_df.at[patient.id, "Time Test Booking Started"] = (
start_q_book_test + patient.q_time_book_test
)
sampled_book_test_time = random.expovariate(
1.0 / g.mean_book_test_time
)
patient.time_with_receptionist += sampled_book_test_time
yield self.env.timeout(sampled_book_test_time)
self.patient_results_df.at[patient.id, "Departure Time"] = (
self.env.now
)
# Generator function representing callers phoning the GP surgery
def call_gp_surgery(self, caller):
# Answering call activity
start_q_call = self.env.now
self.caller_results_df.at[caller.id, "Call Start Time"] = (
start_q_call
)
with self.receptionist.request() as req:
yield req
end_q_call = self.env.now
caller.q_time_call = end_q_call - start_q_call
self.caller_results_df.at[caller.id, "Queue Time Call"] = (
caller.q_time_call
)
self.caller_results_df.at[caller.id, "Call Answered At"] = (
self.env.now
)
sampled_call_time = random.expovariate(
1.0 / g.mean_call_time
)
caller.time_with_receptionist += sampled_call_time
yield self.env.timeout(sampled_call_time)
self.caller_results_df.at[caller.id, "Call End Time"] = (
self.env.now
)
# Method to calculate and store results over the run
def calculate_run_results(self):
self.mean_q_time_reg = self.patient_results_df["Queue Time Reg"].mean()
self.mean_q_time_gp = self.patient_results_df["Queue Time GP"].mean()
self.mean_q_time_book_test = (
self.patient_results_df["Queue Time Book Test"].mean()
)
self.mean_q_time_call = self.caller_results_df["Queue Time Call"].mean()
gp_utilisation_mins = sum([i.time_with_gp for i in self.patient_objects])
receptionist_utilisation_mins = sum(
[i.time_with_receptionist for i in self.patient_objects]
) + sum(
[i.time_with_receptionist for i in self.caller_objects]
)
self.gp_utilisation_prop = (
gp_utilisation_mins / (g.number_of_gps * g.sim_duration)
)
self.receptionist_utilisation_prop = (
receptionist_utilisation_mins / (g.number_of_receptionists * g.sim_duration)
)
# Method to run a single run of the simulation
def run(self):
# Start up DES generators
self.env.process(self.generator_patient_arrivals())
self.env.process(self.generator_callers())
# Run for the duration specified in g class
self.env.run(until=g.sim_duration)
# Calculate results over the run
self.calculate_run_results()
return self.caller_results_df, self.patient_results_df
# Class representing a trial for our simulation
class Trial:
# Constructor
def __init__(self):
self.df_trial_results = pd.DataFrame()
self.df_trial_results["Run Number"] = [1]
self.df_trial_results["Mean Queue Time Reg"] = [0.0]
self.df_trial_results["Mean Queue Time GP"] = [0.0]
self.df_trial_results["Mean Queue Time Book Test"] = [0.0]
self.df_trial_results["Mean Queue Time Call"] = [0.0]
self.df_trial_results["GP Utilisation - Percentage"] = [0.0]
self.df_trial_results["Receptionist Utilisation - Percentage"] = [0.0]
self.df_trial_results.set_index("Run Number", inplace=True)
# Method to calculate and store means across runs in the trial
def calculate_means_over_trial(self):
self.mean_q_time_reg_trial = (
self.df_trial_results["Mean Queue Time Reg"].mean()
)
self.mean_q_time_gp_trial = (
self.df_trial_results["Mean Queue Time GP"].mean()
)
self.mean_q_time_book_test_trial = (
self.df_trial_results["Mean Queue Time Book Test"].mean()
)
self.mean_q_time_call_trial = (
self.df_trial_results["Mean Queue Time Call"].mean()
)
# Method to run trial
def run_trial(self):
caller_dfs = []
patient_dfs = []
for run in range(1, g.number_of_runs+1):
my_model = Model(run)
caller_df, patient_df = my_model.run()
caller_df["Run"] = run
caller_df["What"] = "Callers"
patient_df["Run"] = run
patient_df["What"] = "Patients"
caller_dfs.append(caller_df)
patient_dfs.append(patient_df)
self.df_trial_results.loc[run] = [my_model.mean_q_time_reg,
my_model.mean_q_time_gp,
my_model.mean_q_time_book_test,
my_model.mean_q_time_call,
round(my_model.gp_utilisation_prop * 100, 2),
round(my_model.receptionist_utilisation_prop*100, 2)
]
return self.df_trial_results.round(1), pd.concat(caller_dfs), pd.concat(patient_dfs)