Skip to content

Commit 48315ff

Browse files
committed
feat(full_model): + nurse service func
1 parent d6af9c6 commit 48315ff

File tree

1 file changed

+87
-67
lines changed

1 file changed

+87
-67
lines changed

content/08_full_model.ipynb

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@
472472
"id": "33f7e4b8-46d1-4566-8307-2ad945455225",
473473
"metadata": {},
474474
"source": [
475-
"## 4. Model code"
475+
"## 4. Modified model code\n",
476+
"\n",
477+
"We will modify the model code and logic that we have already developed to include a nurse consultation for a proportion of the callers. We create a new function called `nurse_consultation` that contains all the logic. We also need to modify the `service` function so that a proportion of calls are sent to the nurse consultation process. "
476478
]
477479
},
478480
{
@@ -497,95 +499,114 @@
497499
},
498500
{
499501
"cell_type": "code",
500-
"execution_count": 9,
501-
"id": "564db16d-b2b1-4e2f-b1f3-6ef4b42a59f7",
502+
"execution_count": null,
503+
"id": "c65f9197-2319-42a8-81ab-fe0f10ac4534",
502504
"metadata": {},
503505
"outputs": [],
504506
"source": [
505-
"def service(identifier, args, env):\n",
507+
"def nurse_consultation(identifier, env, args):\n",
506508
" '''\n",
509+
" simulates the wait for an consultation with a nurse on the phone.\n",
510+
"\n",
511+
" 1. request and wait for a call operator\n",
512+
" 2. phone consultation (uniform)\n",
513+
" 3. release nurse and exit system\n",
514+
" \n",
515+
" '''\n",
516+
" trace(f'Patient {identifier} waiting for nurse call back')\n",
517+
" start_nurse_wait = env.now\n",
518+
"\n",
519+
" # request a nurse\n",
520+
" with args.nurses.request() as req:\n",
521+
" yield req\n",
522+
"\n",
523+
" # record the waiting time for nurse call back\n",
524+
" nurse_waiting_time = env.now - start_nurse_wait\n",
525+
" args.results['nurse_waiting_times'].append(nurse_waiting_time)\n",
526+
"\n",
527+
" # sample nurse the duration of the nurse consultation\n",
528+
" nurse_call_duration = args.nurse_dist.sample() \n",
529+
"\n",
530+
" trace(f'nurse called back patient {identifier} at ' \\\n",
531+
" + f'{env.now:.3f}')\n",
532+
"\n",
533+
" # schedule process to begin again after call duration\n",
534+
" yield env.timeout(nurse_call_duration)\n",
535+
"\n",
536+
" args.results['total_nurse_call_duration'] += nurse_call_duration\n",
537+
"\n",
538+
" trace(f'nurse consultation for {identifier}' \\\n",
539+
" + f' competed at {env.now:.3f}')"
540+
]
541+
},
542+
{
543+
"cell_type": "code",
544+
"execution_count": null,
545+
"id": "56fa8b72-c511-44a8-99d4-d958ef256821",
546+
"metadata": {},
547+
"outputs": [],
548+
"source": [
549+
"def service(identifier, env, args):\n",
550+
" \"\"\"\n",
507551
" simulates the service process for a call operator\n",
508552
"\n",
509553
" 1. request and wait for a call operator\n",
510554
" 2. phone triage (triangular)\n",
511-
" 3. exit system\n",
512-
" \n",
555+
" 3. release call operator\n",
556+
" 4. a proportion of call continue to nurse consultation\n",
557+
"\n",
513558
" Params:\n",
514559
" ------\n",
515-
" \n",
516-
" identifier: int \n",
560+
" identifier: int\n",
517561
" A unique identifer for this caller\n",
518-
" \n",
519-
" experiment: Experiment\n",
520-
" The settings and input parameters for the current experiment\n",
521-
" \n",
562+
"\n",
522563
" env: simpy.Environment\n",
523564
" The current environent the simulation is running in\n",
524565
" We use this to pause and restart the process after a delay.\n",
525-
" \n",
526-
" '''\n",
527-
" \n",
566+
"\n",
567+
" args: Experiment\n",
568+
" The settings and input parameters for the current experiment\n",
569+
"\n",
570+
" \"\"\"\n",
571+
"\n",
528572
" # record the time that call entered the queue\n",
529573
" start_wait = env.now\n",
530-
" \n",
531-
" # request an operator\n",
574+
"\n",
575+
" # request an operator - stored in the Experiment\n",
532576
" with args.operators.request() as req:\n",
533577
" yield req\n",
534-
" \n",
578+
"\n",
535579
" # record the waiting time for call to be answered\n",
536580
" waiting_time = env.now - start_wait\n",
537-
" \n",
538-
" # store the results for an experiment \n",
539-
" args.results['waiting_times'].append(waiting_time)\n",
540581
"\n",
541-
" trace(f'operator answered call {identifier} at ' \\\n",
542-
" + f'{env.now:.3f}')\n",
582+
" # store the results for an experiment\n",
583+
" args.results[\"waiting_times\"].append(waiting_time)\n",
584+
" trace(f\"operator answered call {identifier} at \" + f\"{env.now:.3f}\")\n",
543585
"\n",
544586
" # the sample distribution is defined by the experiment.\n",
545-
" call_duration = args.call_dist.sample() \n",
546-
" \n",
587+
" call_duration = args.call_dist.sample()\n",
588+
" \n",
547589
" # schedule process to begin again after call_duration\n",
548590
" yield env.timeout(call_duration)\n",
549-
" \n",
550-
" # update the total call_duration \n",
551-
" args.results['total_call_duration'] += call_duration\n",
552-
" \n",
591+
"\n",
592+
" # update the total call_duration\n",
593+
" args.results[\"total_call_duration\"] += call_duration\n",
594+
"\n",
553595
" # print out information for patient.\n",
554-
" trace(f'call {identifier} ended {env.now:.3f}; ' \\\n",
555-
" + f'waiting time was {waiting_time:.3f}')\n",
556-
" \n",
557-
" # ############################################################\n",
596+
" trace(\n",
597+
" f\"call {identifier} ended {env.now:.3f}; \"\n",
598+
" + f\"waiting time was {waiting_time:.3f}\"\n",
599+
" )\n",
600+
"\n",
601+
" # ##########################################################################\n",
558602
" # MODIFICATION NURSE CALL BACK\n",
559603
" # does nurse need to call back?\n",
560604
" # Note the level of the indented code. \n",
561605
" callback_patient = args.callback_dist.sample()\n",
562606
"\n",
563607
" if callback_patient:\n",
564-
" trace(f'Patient {identifier} waiting for nurse call back')\n",
565-
"\n",
566-
" start_nurse_wait = env.now\n",
567-
"\n",
568-
" # request a nurse\n",
569-
" with args.nurses.request() as req:\n",
570-
" yield req\n",
571-
"\n",
572-
" # record the waiting time for nurse call back\n",
573-
" nurse_waiting_time = env.now - start_nurse_wait\n",
574-
" args.results['nurse_waiting_times'].append(nurse_waiting_time)\n",
575-
"\n",
576-
" # sample nurse the duration of the nurse consultation\n",
577-
" nurse_call_duration = args.nurse_dist.sample() \n",
578-
"\n",
579-
" trace(f'nurse called back patient {identifier} at ' \\\n",
580-
" + f'{env.now:.3f}')\n",
581-
"\n",
582-
" # schedule process to begin again after call duration\n",
583-
" yield env.timeout(nurse_call_duration)\n",
584-
"\n",
585-
" args.results['total_nurse_call_duration'] += nurse_call_duration\n",
586-
"\n",
587-
" trace(f'nurse consultation for {identifier}' \\\n",
588-
" + f' competed at {env.now:.3f}')"
608+
" env.process(nurse_consultation(identifier, env, args))\n",
609+
" # ##########################################################################"
589610
]
590611
},
591612
{
@@ -596,30 +617,29 @@
596617
"outputs": [],
597618
"source": [
598619
"def arrivals_generator(env, args):\n",
599-
" '''\n",
620+
" \"\"\"\n",
600621
" IAT is exponentially distributed\n",
601622
"\n",
602623
" Parameters:\n",
603624
" ------\n",
604625
" env: simpy.Environment\n",
605626
" The simpy environment for the simulation\n",
606627
"\n",
607-
" experiment: Experiment\n",
628+
" args: Experiment\n",
608629
" The settings and input parameters for the simulation.\n",
609-
" '''\n",
610-
"\n",
611-
" # use itertools as it provides an infinite loop \n",
630+
" \"\"\"\n",
631+
" # use itertools as it provides an infinite loop\n",
612632
" # with a counter variable that we can use for unique Ids\n",
613633
" for caller_count in itertools.count(start=1):\n",
614634
"\n",
615-
" # the sample distribution is defined by the experiment.\n",
635+
" # rhe sample distribution is defined by the experiment.\n",
616636
" inter_arrival_time = args.arrival_dist.sample()\n",
617637
" yield env.timeout(inter_arrival_time)\n",
618638
"\n",
619-
" trace(f'call arrives at: {env.now:.3f}')\n",
639+
" trace(f\"call arrives at: {env.now:.3f}\")\n",
620640
"\n",
621-
" # we pass the experiment to the service function\n",
622-
" env.process(service(caller_count, args, env))"
641+
" # create a service process\n",
642+
" env.process(service(caller_count, env, args))"
623643
]
624644
},
625645
{

0 commit comments

Comments
 (0)