forked from ONSdigital/FOCUS
/
FFU.py
543 lines (399 loc) · 25.9 KB
/
FFU.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import math
import datetime as dt
import output_options as oo
import hq
import helper as h
from simpy.util import start_delayed
# a helper process that creates an instance of a StartFU class and starts it working
def start_fu(env, district):
StartFU(env, district)
yield env.timeout(0)
class StartFU(object):
"""represents the RMT creating the visit lists for the assigned districts and CO"""
def __init__(self, env, district):
self.env = env
self.district = district
self.update = self.district.input_data['RMT_update']
self.visit_list = []
self.env.process(self.create_visit_lists())
def create_visit_lists(self):
if h.responses_to_date(self.district) < self.district.input_data["trigger"]:
# only create action plans if the target rate has not been reached
self.visit_list = []
self.visit_list = [household for household in self.district.households
if not household.responded
and household.visits < household.input_data['max_visits']
and h.time_from_start(self.district.rep, household.input_data['FU_start_date']) <= self.env.now]
# order by priority
self.visit_list.sort(key=lambda hh: hh.priority, reverse=False)
j = 0
temp_co_list = []
for co in self.district.district_co:
if h.date_between(co.rep, co.start_date, self.env.now, co.end_date):
temp_co_list.append(co)
num_of_co = len(temp_co_list)
action_plan_list = []
for i in range(num_of_co):
action_plan = self.visit_list[i::num_of_co]
action_plan_list.append(action_plan)
#if co.rep.districts[0].district == "1":
# print("time ", self.env.now, " action plans", len(action_plan_list), " co's ", len(temp_co_list))
for co in temp_co_list:
co.action_plan = action_plan_list[j]
j += 1
yield self.env.timeout(self.update)
self.env.process(self.create_visit_lists())
else:
# returns to date over trigger point so no need to do any more FU...
for co in self.district.district_co:
co.action_plan = []
self.district.district_co = []
class CensusOfficer(object):
"""represents an individual Census Officer. Each instance can be different"""
def __init__(self, rep, env, district, input_data, co_id):
self.rep = rep
self.env = env
self.district = district
self.input_data = input_data
self.co_id = co_id
self.rnd = self.rep.rnd
self.action_plan = []
self.start_date = dt.datetime.strptime((self.input_data['start_date']), '%Y, %m, %d').date()
self.end_date = dt.datetime.strptime((self.input_data['end_date']), '%Y, %m, %d').date()
self.has_pq = h.str2bool(self.input_data['has_pq'])
self.has_postcard = h.str2bool(self.input_data['has_postcard'])
self.start_sim_time = h.get_entity_time(self) # self.co_start_time() # the sim time the co starts work
self.end_sim_time = h.get_entity_time(self, "end") # self.co_end_time() # the sim time the co ends work
self.active_time = [] # as above but for active time
# start work at correct time
start_delayed(self.env, self.co_working_test(), self.start_sim_time)
def optimal_time(self, household):
# determines if the current time is the best time to visit a hh that has asked for a visit
# get times avail
d = household.input_data['contact_rate'][str(h.current_day(household))]
# if current time is not best time don't visit
if d[h.return_time_key(d, self.env.now)] != d[max(d, key=d.get)]:
return False
else:
# visit
return True
def return_next_visit(self, live=False):
# if live tests if a return has been received since the action plans have been created.
# add a check to see if the hh has requested a visit - if yes go at optimal time for hh type
if self.action_plan:
household = self.action_plan.pop(0)
else:
return None
if live and household.returned:
return self.return_next_visit(live)
elif household.arranged_visit and not self.optimal_time(household):
# put hh back in list at correct place and then get the next one...unless no other hh to visit
if self.action_plan:
print(household.hh_id)
try:
self.action_plan.insert(10, household)
except ReferenceError:
print('error')
else:
return household
return self.return_next_visit(live)
else:
return household
def co_working_test(self):
if not self.district.district_co and not self.action_plan:
# if removed from district no more work to do
# timeout until end of sim
end_time = self.rep.sim_hours - self.env.now
yield self.env.timeout(end_time)
elif self.working() and self.action_plan:
# get the first household to visit
household = self.return_next_visit()
if not household:
# if no more visits to do that day finish until next day
yield self.env.timeout(self.next_available())
else:
yield self.env.process(self.fu_visit_contact(household))
self.env.process(self.co_working_test())
else:
yield self.env.timeout(self.next_available())
self.env.process(self.co_working_test())
def household_test(self, household, input_type):
# tests if hh is in or is converted to a return!
test_value = self.rnd.uniform(0, 100)
dict_value = household.input_data[input_type][str(h.current_day(self))]
if test_value <= dict_value[h.return_time_key(dict_value, self.env.now)]:
return True
else:
return False
def fu_visit_contact(self, household):
household.priority += 1 # automatically lower the priority of this hh after a visit
household.visits += 1
if oo.record_visit_summary:
for key, value in self.rep.visit_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += 1
for key, value in self.rep.visit_totals.items():
value[str(getattr(household, key))] += 1
if oo.record_visit:
self.rep.output_data['Visit'].append(oo.visit_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
household.visits,
self.env.now))
if household.responded:
if oo.record_visit_wasted:
self.rep.output_data['Visit_wasted'].append(oo.generic_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
elif household.resp_planned:
if oo.record_visit_unnecessary:
self.rep.output_data['Visit_unnecessary'].append(oo.generic_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
household_is_in = self.household_test(household, "contact_rate")
if household_is_in:
if oo.record_visit_contact:
self.rep.output_data['Visit_contact'].append(oo.generic_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
# household.visits_contacted += 1
yield self.rep.env.process(self.fu_visit_assist(household))
elif (not household_is_in and household.visits == household.input_data['max_visits'] and
h.str2bool(household.input_data['paper_after_max_visits'])):
household.paper_allowed = True
hq.schedule_paper_drop(household, 'Visit', 'pq', self.has_pq)
time_worked = self.input_data["visit_times"]["out_paper"] / 60 +\
self.district.travel_dist / self.input_data["travel_speed"]
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
else:
# out - add drop off of a note
if oo.record_visit_out:
self.rep.output_data['Visit_out'].append(oo.generic_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
self.env.process(hq.schedule_paper_drop(household, 'Visit', 'postcard', self.has_postcard))
time_worked = self.input_data["visit_times"]["out"]/60 + self.district.travel_dist / self.input_data["travel_speed"]
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
def fu_visit_assist(self, household):
da_test = self.rnd.uniform(0, 100)
da_effectiveness = self.input_data['da_effectiveness'][household.hh_type]
time_worked = self.input_data['visit_times']['query']/60
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
# if digital, have already responded or can use paper skip straight to the outcome of the visit
if household.digital or household.return_sent or household.paper_allowed:
yield self.rep.env.process(self.fu_visit_outcome(household))
# if not digital try to persuade them to complete online.
elif not household.digital and da_test <= da_effectiveness:
time_worked = self.input_data['visit_times']['convert']/60
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
household.digital = True
if oo.record_visit_convert:
self.rep.output_data['Visit_convert'].append(oo.generic_output(self.rep.reps,
household.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
yield self.rep.env.process(self.fu_visit_outcome(household))
# not digital, do not convince to go online but allowed to use paper so go to outcome
elif not household.digital and da_test > da_effectiveness and household.paper_allowed:
yield self.rep.env.process(self.fu_visit_outcome(household))
# if not digital, do not convince to complete online, and trigger and max visits not reached give paper if on.
elif (not household.digital and da_test > da_effectiveness and
h.responses_to_date(self.district) < self.district.input_data['paper_trigger'] and
household.visits == household.input_data['max_visits'] and
h.str2bool(household.input_data['paper_after_max_visits'])):
household.paper_allowed = True
self.env.process(hq.schedule_paper_drop(household, 'Visit', 'pq', self.has_pq))
time_worked = self.input_data['visit_times']['paper'] / 60 +\
self.district.travel_dist/self.input_data["travel_speed"]
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
else:
# or suggest other forms of assistance to be decided...
# non implemented at present so another visit will be scheduled.
if oo.record_visit_assist:
self.rep.output_data['Visit_assist'].append(oo.generic_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
time_worked = self.input_data['visit_times']['paper'] / 60 + \
self.district.travel_dist / self.input_data["travel_speed"]
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
def fu_visit_outcome(self, household):
household_returns = self.household_test(household, "success_rate")
if not household.return_sent and household_returns:
# hh have not responded yet and respond there and then either by paper or digital.
if oo.record_visit_success:
self.rep.output_data['Visit_success'].append(oo.visit_output(self.rep.reps,
self.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
household.visits,
self.env.now))
household.resp_planned = True
yield self.rep.env.process(household.household_returns(household.calc_delay()))
time_worked = self.input_data['visit_times']['success'] / 60 + \
self.district.travel_dist / self.input_data["travel_speed"]
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
elif (not household.return_sent and not household_returns and
h.responses_to_date(self.district) < self.district.input_data['paper_trigger'] and
household.visits == household.input_data['max_visits'] and
h.str2bool(household.input_data['paper_after_max_visits']) and
not household.paper_allowed):
# hh have not responded but do not respond as a result of the visit.
# need extra here for when you fail but not at max visits?
if oo.record_visit_failed:
self.rep.output_data['Visit_failed'].append(oo.generic_output(self.rep.reps,
household.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
# leave paper in hope they respond?
household.paper_allowed = True
hq.schedule_paper_drop(household, 'Visit', 'pq', self.has_pq)
if oo.record_paper_summary:
# add to the summary of the amount of paper given
for key, value in self.rep.paper_summary.items():
value[str(getattr(household, key))][0] += 1
for key, value in self.rep.paper_totals.items():
value[str(getattr(household, key))] += 1
time_worked = self.input_data['visit_times']['failed'] / 60 + \
self.district.travel_dist / self.input_data["travel_speed"]
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
elif not household.return_sent and not household_returns:
# failed but no max visits so do no more
if oo.record_visit_failed:
self.rep.output_data['Visit_failed'].append(oo.generic_output(self.rep.reps,
household.district.district,
household.la,
household.lsoa,
household.digital,
household.hh_type,
household.hh_id,
self.env.now))
time_worked = self.input_data['visit_times']['failed'] / 60 + \
self.district.travel_dist / self.input_data["travel_speed"]
if oo.record_time_summary:
for key, value in self.rep.time_summary.items():
value[str(getattr(household, key))][math.floor(self.rep.env.now / 24)] += time_worked
for key, value in self.rep.time_totals.items():
value[str(getattr(household, key))] += time_worked
yield self.rep.env.timeout(time_worked)
def working(self):
"""returns true or false depending on whether or not a CO is available at current date and time"""
day_of_week = h.current_day(self)
if self.start_sim_time <= self.env.now < self.end_sim_time and \
self.input_data['availability'][str(day_of_week)]:
for i in range(0, len(self.input_data['availability'][str(day_of_week)]), 2):
start_time = dt.time(*map(int, self.input_data['availability'][str(day_of_week)][i].split(':')))
end_time = dt.time(*map(int, self.input_data['availability'][str(day_of_week)][i+1].split(':')))
start_sim_time = h.make_time_decimal(start_time) + math.floor(self.env.now/24)*24
end_sim_time = h.make_time_decimal(end_time) + math.floor(self.env.now / 24) * 24
if start_sim_time <= self.env.now < end_sim_time:
return True
return False
def return_index(self):
return_index = 0
for household in self.action_plan:
if not household.arranged_visit:
return_index = self.action_plan.index(household) + 1
return max(return_index, 0)
def return_dow(self, days_gone_from_start, original_tod, count=0):
# get next relevant dow based on current sim time
current_dow = (self.rep.start_date + dt.timedelta(days=days_gone_from_start)).weekday()
if (not self.input_data['availability'][str(current_dow)] or
h.str_to_dec(self.input_data['availability'][str(current_dow)][-1]) <= original_tod):
# empty list
return self.return_dow(days_gone_from_start + 1, 0, count + 1)
else:
# not empty
return [str(current_dow), count]
def next_available(self):
"""return the number of hours until the CO is next available or remove from sim if finished"""
if self.env.now > self.end_sim_time:
return self.rep.sim_hours - self.env.now
current_dow = (self.rep.start_date + dt.timedelta(days=math.floor(self.env.now / 24))).weekday()
dow = self.return_dow(math.floor(self.env.now / 24), self.env.now % 24)
# if action plan is zero,so no houses to visits always delay to the start of the next valid day
if not self.action_plan:
# this actually just jumps it to the end of the day
return 24 - self.env.now % 24
elif int(dow[0]) == current_dow:
return h.str_to_dec(self.input_data['availability'][str(dow[0])][-2]) - self.env.now % 24
else:
return ((24 - self.env.now % 24) + (int(dow[1]) - 1) * 24 + h.str_to_dec(self.input_data['availability']
[str(dow[0])][0]))