如何在Simmer for R中记录基于最短队列策略选中的高速公路闸口资源?
shortest-queue Policy in simmer? Problem Statement
I'm creating a simulation model for traffic conditions near a motorway gate. I've added resources using this code:
motorwayGate <- simmer("motorwayGate") %>% add_resource("counterCards", 1) %>% add_resource("counterCash1", 1) %>% add_resource("counterCash2", 1) %>% add_resource("counterCash3", 1) %>%
Then I'm selecting a resource using the shortest queue policy:
select(c("counterCards", "counterCash1", "counterCash2", "counterCash3"), policy = "shortest-queue") %>%
I need to record the name of the selected resource to track which one was chosen. I tried using log_(get_selected()) %>% and print(get_selected()), but neither worked. How can I properly implement this?
Solution
Great question! The issue here is that when you call get_selected() directly inside log_() or print() while defining the trajectory, it evaluates immediately (at definition time) instead of waiting for the simulation to run and actually select a resource. You need to wrap it in an anonymous function to delay evaluation until runtime.
Here are two reliable ways to record the selected resource:
1. Log the Selected Resource Directly
Use an anonymous function inside log_() to dynamically fetch the selected resource name when the simulation executes that step:
library(simmer) # Build the simulation model with a trajectory that logs selections motorwayGate <- simmer("motorwayGate") %>% add_resource("counterCards", 1) %>% add_resource("counterCash1", 1) %>% add_resource("counterCash2", 1) %>% add_resource("counterCash3", 1) %>% add_generator( name_prefix = "car", trajectory = trajectory() %>% # Select shortest queue resource select( c("counterCards", "counterCash1", "counterCash2", "counterCash3"), policy = "shortest-queue" ) %>% # Log the selected resource (uses anonymous function for runtime evaluation) log_(function() paste("Selected resource:", get_selected())) %>% # Proceed to use the selected resource seize_selected() %>% timeout(10) %>% # Example processing time release_selected(), # Generate a test arrival at time 0 at = 0 ) # Run the simulation run(motorwayGate) # Check the logs (look for the "Selected resource:" entry) get_mon_arrivals(motorwayGate)
2. Store the Selected Resource as an Arrival Attribute
If you want to keep track of the selected resource for later analysis (not just logging), store it as an attribute on the arrival using set_attribute():
trajectory() %>% select( c("counterCards", "counterCash1", "counterCash2", "counterCash3"), policy = "shortest-queue" ) %>% # Save selected resource to an attribute set_attribute("selected_resource", function() get_selected()) %>% # Log using the attribute (optional) log_(function() paste("Selected resource:", get_attribute(env, "selected_resource"))) %>% seize_selected() %>% timeout(10) %>% release_selected()
You can then access this attribute in the monitoring data:
arrivals_data <- get_mon_arrivals(motorwayGate, attributes = TRUE) print(arrivals_data$selected_resource)
Key Notes
get_selected()only works immediately after aselect()operation in the same trajectory, as it references the current arrival's selected resource.- Wrapping
get_selected()in an anonymous function (function() { ... }) ensures it runs when the simulation executes that step, not when you write the trajectory code.
内容的提问来源于stack exchange,提问作者igy234

