It looks to me that currently you’re using something like
mutable struct Patient
ctStatus::Int
mriStatus::Int
labStatus::Int
xrayStatus::Int
ctStartTime::Float64
mriStartTime::Float64
labStartTime::Float64
xrayStartTime::Float64
dxComplete::Bool
end
Instead you could (for example) use
mutable struct PatientLabInfo
status::Int
start_time::Float64
end
mutable struct Patient
labs_info::Dict{String, PatientLabInfo}
dx_complete::Bool
end
and something similar for your Model
(type of model
).
(I switched the naming scheme to snake_case to make the difference between variables and types clearer, as this can occasionally get confusing for me. But this is not too important. The names themselves might also not be ideal (e.g. PatientLabInfo
refers to the information about a Patient
some Lab
might have). Proper naming is hard without complete understanding of the problem you want to model, and also just in general.)
The lab names (relevant for a given Patient
) are then encoded as the keys in the labs_info
Dict
. The equivalent of your code would be
patient = Patient(
Dict("ct" => PatientLabInfo(1, 1.23),
"mri" => PatientLabInfo(4, 0.01),
"lab" => PatientLabInfo(3, 2.31),
"xray" => PatientLabInfo(0, 3.21)),
false
)
lab_name = rand(findall(lab_info -> 0 < lab_info.status < 3, patient.labs_info))
if isempty(lab_name)
patient.dx_complete = true # (You wrote ==, but probably mean = ?)
else
# pass the testname to the test processing function
process_test(model, patient, lab_name)
end
### the function
function process_test(model, patient, lab_name)
# check to see if there is space in the lab
if count(p->p.labs_info[lab_name].status == 2, allagents(model)) < model.labs_info[lab_name].capacity
# if capacity, send patient to lab by changing status in process and noting start time.
patient.labs_info[lab_name].status = 2
patient.labs_info[lab_name].start_time = abmtime(model)
end
end
By the way, at the moment you check if the lab_name
String
is empty (i.e. ""
), which is presumably never the case? Perhaps you mean that no labs for this patient have status 1 or 2. But in this case the code will already have errored by calling rand
on an empty collection. Instead you should call isempty
on the result of the findall
.
In any case, using this approach you don’t need any string interpolation of lab names into variable names. You simply use the lab names directly as indices in a dictionary.