|
| 1 | +""" |
| 2 | +Code for the simulation. |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import simpy |
| 7 | +from sim_tools.distributions import Exponential |
| 8 | + |
| 9 | + |
| 10 | +class Patient: |
| 11 | + """ |
| 12 | + Represents a patient. |
| 13 | +
|
| 14 | + Attributes |
| 15 | + ---------- |
| 16 | + patient_id: int, float or str |
| 17 | + Unique patient identifier. |
| 18 | + patient_type: str |
| 19 | + Patient type ("stroke", "tia", "neuro" or "other"). |
| 20 | + arrival_time: float |
| 21 | + Arrival time for the patient (in days). |
| 22 | + """ |
| 23 | + def __init__(self, patient_id, patient_type): |
| 24 | + """ |
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + patient_id: int, float or str |
| 28 | + Unique patient identifier. |
| 29 | + patient_type: str |
| 30 | + Patient type ("stroke", "tia", "neuro" or "other"). |
| 31 | + """ |
| 32 | + self.patient_id = patient_id |
| 33 | + self.patient_type = patient_type |
| 34 | + self.arrival_time = np.nan |
| 35 | + |
| 36 | + |
| 37 | +class Model: |
| 38 | + """ |
| 39 | + Simulation model. |
| 40 | +
|
| 41 | + Attributes |
| 42 | + ---------- |
| 43 | + param: Param |
| 44 | + Run parameters. |
| 45 | + run_number: int |
| 46 | + Replication / run number. |
| 47 | + env: simpy.Environment |
| 48 | + Simulation environment. |
| 49 | + patients: list |
| 50 | + Stores the Patient objects. |
| 51 | + distributions: dictionary |
| 52 | + Contains the sampling distributions. |
| 53 | + """ |
| 54 | + def __init__(self, param, run_number): |
| 55 | + """ |
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + param: Param |
| 59 | + Run parameters. |
| 60 | + run_number: int |
| 61 | + Replication / run number. |
| 62 | + """ |
| 63 | + # Set parameters |
| 64 | + self.param = param |
| 65 | + self.run_number = run_number |
| 66 | + |
| 67 | + # Create SimPy environment |
| 68 | + self.env = simpy.Environment() |
| 69 | + |
| 70 | + # Create attributes to store results |
| 71 | + # The patients list will store a reference to the patient objects, so |
| 72 | + # any updates to the patient attributes after they are saved to the |
| 73 | + # list, will be reflected in the list as well |
| 74 | + self.patients = [] |
| 75 | + |
| 76 | + # Create seeds |
| 77 | + ss = np.random.SeedSequence(entropy=self.run_number) |
| 78 | + seed_generator = iter(ss.spawn(20)) |
| 79 | + |
| 80 | + # Create distributions |
| 81 | + self.create_distributions(seed_generator) |
| 82 | + |
| 83 | + def create_distributions(self, seed_generator): |
| 84 | + """ |
| 85 | + Creates distributions for sampling arrivals for all units and patient |
| 86 | + types. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + seed_generator: Iterator |
| 91 | + Iterator that generates random seeds. |
| 92 | + """ |
| 93 | + # Create dictionary to store distributions |
| 94 | + self.distributions = {} |
| 95 | + |
| 96 | + # Loop through each unit |
| 97 | + for unit, unit_param in [("asu", self.param.asu_arrivals), |
| 98 | + ("rehab", self.param.rehab_arrivals)]: |
| 99 | + |
| 100 | + # Make sub-dictionary to store that unit's distributions |
| 101 | + self.distributions[unit] = {} |
| 102 | + |
| 103 | + # Get a list of the patients in that unit (ignore other attributes) |
| 104 | + patient_types = [attr for attr in dir(unit_param) if attr in |
| 105 | + ["stroke", "tia", "neuro", "other"]] |
| 106 | + |
| 107 | + for patient_type in patient_types: |
| 108 | + |
| 109 | + # Create distributions for each patient type in that unti |
| 110 | + self.distributions[unit][patient_type] = Exponential( |
| 111 | + mean=getattr(unit_param, patient_type), |
| 112 | + random_seed=next(seed_generator) |
| 113 | + ) |
| 114 | + |
| 115 | + def patient_generator(self, patient_type, distribution, unit): |
| 116 | + """ |
| 117 | + Generic patient generator for any patient type and unit. |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + patient_type: str |
| 122 | + Type of patient ("stroke", "tia", "neuro", "other"). |
| 123 | + distribution: Distribution |
| 124 | + The inter-arrival time distribution to sample from. |
| 125 | + unit: str |
| 126 | + The unit the patient is arriving at ("asu", "rehab"). |
| 127 | + """ |
| 128 | + while True: |
| 129 | + # Sample and pass time to arrival |
| 130 | + sampled_iat = distribution.sample() |
| 131 | + yield self.env.timeout(sampled_iat) |
| 132 | + |
| 133 | + # Create a new patient |
| 134 | + p = Patient( |
| 135 | + patient_id=len(self.patients)+1, |
| 136 | + patient_type=patient_type |
| 137 | + ) |
| 138 | + |
| 139 | + # Record arrival time |
| 140 | + p.arrival_time = self.env.now |
| 141 | + |
| 142 | + # Print arrival time |
| 143 | + print(f"{patient_type} patient arrive at {unit}: {p.arrival_time}") |
| 144 | + |
| 145 | + # Add to the patients list |
| 146 | + self.patients.append(p) |
| 147 | + |
| 148 | + def run(self): |
| 149 | + """ |
| 150 | + Run the simulation. |
| 151 | + """ |
| 152 | + # Calculate the total run length |
| 153 | + run_length = (self.param.warm_up_period + |
| 154 | + self.param.data_collection_period) |
| 155 | + |
| 156 | + # Schedule patient generators to run during the simulation |
| 157 | + for unit, patient_types in self.distributions.items(): |
| 158 | + for patient_type, distribution in patient_types.items(): |
| 159 | + self.env.process( |
| 160 | + self.patient_generator( |
| 161 | + patient_type=patient_type, |
| 162 | + distribution=distribution, |
| 163 | + unit=unit |
| 164 | + ) |
| 165 | + ) |
| 166 | + |
| 167 | + # Run the simulation |
| 168 | + self.env.run(until=run_length) |
0 commit comments