|
112 | 112 | "\n", |
113 | 113 | "INIT_COND_PARAMS = {\n", |
114 | 114 | " \"mode\": \"fixed\",\n", |
115 | | - " \"fixed\": 8,\n", |
| 115 | + " \"fixed\": 10,\n", |
116 | 116 | " \"rnd\": {\n", |
117 | 117 | " \"values\":[8, 9, 10, 11, 12, 13],\n", |
118 | | - " \"freq\":[25, 25, 2, 1, 1, 1]\n", |
119 | | - " }\n", |
| 118 | + " \"freq\":[15, 15, 25, 20, 5, 5]\n", |
| 119 | + " },\n", |
| 120 | + " \"collect_results\": False\n", |
120 | 121 | "}\n", |
121 | 122 | " \n", |
122 | 123 | "# sampling settings\n", |
|
178 | 179 | " \"\"\"\n", |
179 | 180 | " Encapsulates the concept of an experiment 🧪 for the stroke pathway\n", |
180 | 181 | " bed blocking simulator. Manages parameters, PRNG streams and results.\n", |
| 182 | + "\n", |
| 183 | + " There is a new parameter `init_cond_params` that stores the initial\n", |
| 184 | + " conditions to use in an experiment.\n", |
181 | 185 | " \"\"\"\n", |
182 | 186 | " def __init__(\n", |
183 | 187 | " self,\n", |
|
201 | 205 | " self.acute_los_mean = acute_los_mean\n", |
202 | 206 | " self.acute_los_std = acute_los_std\n", |
203 | 207 | "\n", |
204 | | - " # stored initial conditions\n", |
| 208 | + " # ---- stored initial conditions -------\n", |
205 | 209 | " self.init_cond_params = init_cond_params\n", |
206 | 210 | "\n", |
207 | 211 | " # place holder for resources\n", |
|
217 | 221 | " def set_random_no_set(self, random_number_set):\n", |
218 | 222 | " \"\"\"\n", |
219 | 223 | " Controls the random sampling\n", |
| 224 | + " \n", |
220 | 225 | " Parameters:\n", |
221 | | - " ----------\n", |
| 226 | + " -----------\n", |
222 | 227 | " random_number_set: int\n", |
223 | 228 | " Used to control the set of pseudo random numbers used by\n", |
224 | 229 | " the distributions in the simulation.\n", |
|
250 | 255 | " self.init_conds = FixedDistribution(\n", |
251 | 256 | " self.init_cond_params[\"fixed\"]\n", |
252 | 257 | " )\n", |
253 | | - " else:\n", |
| 258 | + " elif self.init_cond_params[\"mode\"] == \"rnd\":\n", |
254 | 259 | " self.init_conds = DiscreteEmpirical(\n", |
255 | 260 | " values = self.init_cond_params[\"rnd\"][\"values\"],\n", |
256 | 261 | " freq = self.init_cond_params[\"rnd\"][\"freq\"],\n", |
257 | 262 | " random_seed=self.seeds[2]\n", |
258 | 263 | " )\n", |
| 264 | + " else:\n", |
| 265 | + " raise ValueError(\"Initial conditions mode must be 'fixed' or 'rnd'\")\n", |
259 | 266 | " \n", |
260 | 267 | "\n", |
261 | 268 | " def init_results_variables(self):\n", |
|
326 | 333 | "outputs": [], |
327 | 334 | "source": [ |
328 | 335 | "def acute_stroke_pathway(patient_id, env, args, collect_results=True):\n", |
329 | | - " \"\"\"Process a patient through the acute ward\n", |
330 | | - " Simpy generator function.\n", |
| 336 | + " \"\"\"Process a patient through the acute ward. Simpy generator function.\n", |
331 | 337 | " \n", |
332 | 338 | " Parameters:\n", |
333 | 339 | " -----------\n", |
|
348 | 354 | " acute_admit_time = env.now\n", |
349 | 355 | " wait_for_acute = acute_admit_time - arrival_time\n", |
350 | 356 | "\n", |
| 357 | + " # used to avoid collecting stats from initial conditions...\n", |
351 | 358 | " if collect_results:\n", |
352 | 359 | " args.results['waiting_acute'].append(wait_for_acute)\n", |
353 | 360 | " \n", |
|
415 | 422 | "source": [ |
416 | 423 | "def setup_initial_conditions(\n", |
417 | 424 | " env: simpy.Environment, \n", |
418 | | - " args: Experiment,\n", |
419 | | - " collect_results: bool = False,\n", |
| 425 | + " args: Experiment\n", |
420 | 426 | "):\n", |
421 | | - " \"\"\"Set up initial conditions with patients already in the acute\n", |
422 | | - " stroke queue\n", |
| 427 | + " \"\"\"Set up initial conditions with patients already in the acute stroke queue\n", |
423 | 428 | "\n", |
424 | 429 | " Parameters:\n", |
425 | 430 | " -----------\n", |
426 | 431 | " env: simpy.Environment\n", |
427 | 432 | " The simpy environment for the simulation\n", |
428 | 433 | "\n", |
429 | 434 | " args: Experiment\n", |
430 | | - " The settings and input parameters for the simulation.\n", |
431 | | - "\n", |
432 | | - " collect_results: bool, optional (default = False)#\n", |
433 | | - " Should results be collected for initial conditions patients?\n", |
434 | | - " \n", |
| 435 | + " The settings and input parameters for the simulation. \n", |
435 | 436 | " \"\"\"\n", |
436 | 437 | " # sample the no. patients to load into queue\n", |
437 | 438 | " patients_to_load = args.init_conds.sample()\n", |
| 439 | + " trace(f\"Patient to load: {patients_to_load}\")\n", |
438 | 440 | "\n", |
| 441 | + " # collect results or not?\n", |
| 442 | + " collect_results = args.init_cond_params[\"collect_results\"]\n", |
| 443 | + " \n", |
439 | 444 | " for initial_id in range(1, patients_to_load+1):\n", |
440 | 445 | " # Create patients with negative IDs to distinguish them as init cond.\n", |
441 | 446 | " # we may or may not want collect results for initial conditions\n", |
|
496 | 501 | " # simpy resources\n", |
497 | 502 | " experiment.acute_ward = simpy.Resource(env, experiment.n_acute_beds)\n", |
498 | 503 | "\n", |
499 | | - " # load the acute stroke queue\n", |
500 | | - " setup_initial_conditions(env, experiment)\n", |
| 504 | + " trace(\"--- Pre-Simulation Actions ---\")\n", |
501 | 505 | "\n", |
502 | 506 | " # schedule a warm_up period\n", |
503 | 507 | " env.process(warmup_complete(wu_period, env, experiment))\n", |
504 | 508 | " \n", |
| 509 | + " # load the acute stroke queue\n", |
| 510 | + " setup_initial_conditions(env, experiment)\n", |
| 511 | + " \n", |
505 | 512 | " # we pass all arrival generators to simpy \n", |
506 | 513 | " env.process(stroke_arrivals_generator(env, experiment))\n", |
507 | 514 | "\n", |
| 515 | + " trace(\"--- Start Simulation ---\")\n", |
| 516 | + " \n", |
508 | 517 | " # run model\n", |
509 | 518 | " env.run(until=wu_period+rc_period)\n", |
510 | 519 | "\n", |
|
520 | 529 | }, |
521 | 530 | { |
522 | 531 | "cell_type": "code", |
523 | | - "execution_count": 11, |
| 532 | + "execution_count": null, |
524 | 533 | "id": "caf52390-5455-4fa1-bb22-60b5b91ad8d0", |
525 | 534 | "metadata": {}, |
526 | | - "outputs": [ |
527 | | - { |
528 | | - "name": "stdout", |
529 | | - "output_type": "stream", |
530 | | - "text": [ |
531 | | - "0.00: Patient -1 loaded into queue\n", |
532 | | - "0.00: Patient -2 loaded into queue\n", |
533 | | - "0.00: Patient -3 loaded into queue\n", |
534 | | - "0.00: Patient -4 loaded into queue\n", |
535 | | - "0.00: Patient -5 loaded into queue\n", |
536 | | - "0.00: Patient -6 loaded into queue\n", |
537 | | - "0.00: Patient -7 loaded into queue\n", |
538 | | - "0.00: Patient -8 loaded into queue\n", |
539 | | - "0.00: Patient -9 loaded into queue\n", |
540 | | - "0.00: Patient -10 loaded into queue\n", |
541 | | - "0.00: Patient -1 admitted to acute ward.(waited 0.00 days)\n", |
542 | | - "0.00: Patient -2 admitted to acute ward.(waited 0.00 days)\n", |
543 | | - "0.00: Patient -3 admitted to acute ward.(waited 0.00 days)\n", |
544 | | - "0.00: Patient -4 admitted to acute ward.(waited 0.00 days)\n", |
545 | | - "0.00: Patient -5 admitted to acute ward.(waited 0.00 days)\n", |
546 | | - "0.00: Patient -6 admitted to acute ward.(waited 0.00 days)\n", |
547 | | - "0.00: Patient -7 admitted to acute ward.(waited 0.00 days)\n", |
548 | | - "0.00: Patient -8 admitted to acute ward.(waited 0.00 days)\n", |
549 | | - "0.00: Patient -9 admitted to acute ward.(waited 0.00 days)\n", |
550 | | - "0.00: 🥵 Warm up complete.\n", |
551 | | - "2.74: Patient 1. Stroke arrival.\n", |
552 | | - "2.78: Patient 2. Stroke arrival.\n", |
553 | | - "3.36: Patient 3. Stroke arrival.\n", |
554 | | - "4.42: Patient 4. Stroke arrival.\n", |
555 | | - "4.68: Patient 5. Stroke arrival.\n", |
556 | | - "5.80: Patient -3 discharged.\n", |
557 | | - "5.80: Patient -10 admitted to acute ward.(waited 5.80 days)\n", |
558 | | - "5.80: Patient -8 discharged.\n", |
559 | | - "5.80: Patient 1 admitted to acute ward.(waited 3.06 days)\n", |
560 | | - "5.97: Patient 6. Stroke arrival.\n", |
561 | | - "6.08: Patient -6 discharged.\n", |
562 | | - "6.08: Patient 2 admitted to acute ward.(waited 3.30 days)\n", |
563 | | - "6.22: Patient -7 discharged.\n", |
564 | | - "6.22: Patient 3 admitted to acute ward.(waited 2.86 days)\n", |
565 | | - "6.33: Patient 7. Stroke arrival.\n", |
566 | | - "7.28: Patient 8. Stroke arrival.\n", |
567 | | - "7.41: Patient -4 discharged.\n", |
568 | | - "7.41: Patient 4 admitted to acute ward.(waited 2.99 days)\n", |
569 | | - "7.43: Patient 9. Stroke arrival.\n", |
570 | | - "7.55: Patient 10. Stroke arrival.\n", |
571 | | - "7.94: Patient -5 discharged.\n", |
572 | | - "7.94: Patient 5 admitted to acute ward.(waited 3.26 days)\n", |
573 | | - "8.11: Patient -2 discharged.\n", |
574 | | - "8.11: Patient 6 admitted to acute ward.(waited 2.14 days)\n", |
575 | | - "8.26: Patient 11. Stroke arrival.\n", |
576 | | - "8.42: Patient 12. Stroke arrival.\n", |
577 | | - "8.68: Patient 13. Stroke arrival.\n", |
578 | | - "8.72: Patient 14. Stroke arrival.\n", |
579 | | - "8.82: Patient -9 discharged.\n", |
580 | | - "8.82: Patient 7 admitted to acute ward.(waited 2.50 days)\n", |
581 | | - "8.95: Patient 15. Stroke arrival.\n", |
582 | | - "9.18: Patient 16. Stroke arrival.\n", |
583 | | - "9.87: Patient -1 discharged.\n", |
584 | | - "9.87: Patient 8 admitted to acute ward.(waited 2.58 days)\n", |
585 | | - "9.92: Patient 17. Stroke arrival.\n" |
586 | | - ] |
587 | | - }, |
588 | | - { |
589 | | - "data": { |
590 | | - "text/plain": [ |
591 | | - "{'mean_acute_wait': 2.8362718457918676}" |
592 | | - ] |
593 | | - }, |
594 | | - "execution_count": 11, |
595 | | - "metadata": {}, |
596 | | - "output_type": "execute_result" |
597 | | - } |
598 | | - ], |
| 535 | + "outputs": [], |
599 | 536 | "source": [ |
600 | 537 | "TRACE = True\n", |
| 538 | + "\n", |
| 539 | + "# settings dictionary\n", |
601 | 540 | "init_cond_params = INIT_COND_PARAMS.copy()\n", |
602 | 541 | "init_cond_params[\"mode\"] = \"fixed\"\n", |
603 | 542 | "\n", |
604 | | - "# uncomment to vary the fixed amount 10 = 1 in queue.\n", |
| 543 | + "# vary the fixed amount. Interpretation 10 = 1 in queue.\n", |
605 | 544 | "init_cond_params[\"fixed\"] = 10\n", |
606 | 545 | "\n", |
| 546 | + "# vary if we collect results from initial condition stroke patients\n", |
| 547 | + "init_cond_params[\"collect_results\"] = False\n", |
| 548 | + "\n", |
| 549 | + "# create experiment and pass in initial conditions\n", |
607 | 550 | "experiment = Experiment(init_cond_params=init_cond_params)\n", |
| 551 | + "\n", |
608 | 552 | "results = single_run(experiment, rep=1, wu_period=0.0, rc_period=10.0)\n", |
609 | 553 | "results" |
610 | 554 | ] |
611 | 555 | }, |
612 | 556 | { |
613 | 557 | "cell_type": "code", |
614 | | - "execution_count": 12, |
| 558 | + "execution_count": null, |
615 | 559 | "id": "0aaef408-09ca-49e0-8d39-f4a088a4ef1b", |
616 | 560 | "metadata": {}, |
617 | | - "outputs": [ |
618 | | - { |
619 | | - "data": { |
620 | | - "text/plain": [ |
621 | | - "{'n_arrivals': 17,\n", |
622 | | - " 'waiting_acute': [3.0586175577655674,\n", |
623 | | - " 3.303449502025928,\n", |
624 | | - " 2.857579305401165,\n", |
625 | | - " 2.9908615153438225,\n", |
626 | | - " 3.2622390398417513,\n", |
627 | | - " 2.136685286636955,\n", |
628 | | - " 2.496306611009193,\n", |
629 | | - " 2.58443594831056]}" |
630 | | - ] |
631 | | - }, |
632 | | - "execution_count": 12, |
633 | | - "metadata": {}, |
634 | | - "output_type": "execute_result" |
635 | | - } |
636 | | - ], |
| 561 | + "outputs": [], |
637 | 562 | "source": [ |
638 | 563 | "experiment.results" |
639 | 564 | ] |
| 565 | + }, |
| 566 | + { |
| 567 | + "cell_type": "code", |
| 568 | + "execution_count": null, |
| 569 | + "id": "9d5266e1-d8f3-4755-838b-89b9b416faef", |
| 570 | + "metadata": {}, |
| 571 | + "outputs": [], |
| 572 | + "source": [] |
640 | 573 | } |
641 | 574 | ], |
642 | 575 | "metadata": { |
|
0 commit comments