Simmer资源使用记录:如何在代码中记录患者就诊的医生信息
Hey there! Great question—tracking which doctor each patient sees in Simmer is totally doable, and there are a couple of clean ways to implement this. Let’s break down the most common approaches with code examples you can adapt for your simulation.
1. Use set_attribute() to Capture Assigned Doctors Automatically
The easiest way to log doctor-patient matches is to use Simmer’s set_attribute() function right after a patient seizes a doctor resource. This lets you store the assigned doctor’s ID directly as an attribute on the patient’s trajectory.
Here’s a complete example:
library(simmer) # Initialize the simulation environment env <- simmer("Clinic Simulation") # Add a doctor resource with 2 individual providers (servers) env <- env %>% add_resource("doctor", capacity = 2) # Define the patient's journey through the clinic patient_trajectory <- trajectory("Patient Flow") %>% # Seize an available doctor seize("doctor") %>% # Store the ID of the doctor who's treating this patient set_attribute("assigned_doctor", function() { # Get the current server status and find which doctor is active for this patient get_resource(env, "doctor")$server %>% which.max() }) %>% # Simulate a 15-minute consultation (with some variation) timeout(function() rnorm(1, mean = 15, sd = 3)) %>% # Release the doctor resource release("doctor") %>% # Optional: Log the assignment to the console for real-time feedback log_(function() paste0("Patient ", get_name(env), " seen by Doctor ", get_attribute(env, "assigned_doctor"))) # Add patients arriving at a rate of 6 per hour (exponential inter-arrival time) env <- env %>% add_generator("patient_", patient_trajectory, function() rexp(1, rate = 6/60)) %>% run(until = 200) # Run the simulation for 200 minutes # Extract the stored attributes into a data frame patient_doctor_log <- get_mon_attributes(env) print(patient_doctor_log)
When you run this, the patient_doctor_log data frame will include every patient’s name, the time the attribute was set, and the ID of their assigned doctor.
2. Explicitly Assign Doctors with branch() (For Custom Rules)
If you need to assign patients to specific doctors based on rules (e.g., patient type, priority), use the branch() function to route patients to dedicated server slots and log the assignment explicitly.
Example of conditional doctor assignment:
patient_trajectory <- trajectory("Patient Flow") %>% # Branch based on patient ID (even IDs go to Doctor 1, odd to Doctor 2) branch(function() { patient_id <- as.integer(sub("patient_", "", get_name(env))) if (patient_id %% 2 == 0) 1 else 2 }, continue = TRUE, # Branch 1: Assign to Doctor 1 trajectory() %>% seize("doctor", amount = 1, server = 1) %>% set_attribute("assigned_doctor", 1), # Branch 2: Assign to Doctor 2 trajectory() %>% seize("doctor", amount = 1, server = 2) %>% set_attribute("assigned_doctor", 2) ) %>% timeout(15) %>% release("doctor") # Run the simulation as before env <- simmer("Clinic Simulation") %>% add_resource("doctor", capacity = 2) %>% add_generator("patient_", patient_trajectory, function() rexp(1, rate = 6/60)) %>% run(until = 200) patient_doctor_log <- get_mon_attributes(env) print(patient_doctor_log)
This ensures each patient is assigned to the exact doctor you specify, with the assignment logged reliably.
3. Combine Data for Full Patient Context
To get a complete picture, merge the doctor assignment data with other simulation metrics (like waiting time, consultation duration) using get_mon_arrivals():
# Get arrival data (wait times, start/end times) arrival_data <- get_mon_arrivals(env) # Merge with doctor assignment data full_patient_data <- merge( arrival_data[, c("name", "start_time", "end_time", "waiting_time")], patient_doctor_log[, c("name", "value")], by = "name" ) # Rename columns for clarity colnames(full_patient_data)[colnames(full_patient_data) == "value"] <- "assigned_doctor" print(full_patient_data)
Now you have a single data frame with all the details you need for analysis!
内容的提问来源于stack exchange,提问作者Bryan Adams

