Search code examples
rsimulationevent-simulation

Simmer Get_attribute | there is no arrival running error


first of all, this simmer_vignette and this linkadvanced_simmer_usage seem to indicate that the error stems from the fact that "get_name, get_attribute and get_prioritization are meant to be used inside a trajectory; otherwise, there will be no arrival running and these functions will throw an error" A minimal workable example:

  patient_traj <- trajectory(name = "patient_trajectory") %>%
  set_attribute("my_key", 123) %>%
  timeout(5) %>%
  set_attribute("my_key", function() get_attribute(env, "my_key") + 1) %>%
  timeout(5) %>%
  set_attribute("dependent_key", function() ifelse(get_attribute(env, "my_key")<=123, 1, 0)) %>%
  timeout(5) %>%
  set_attribute("independent_key", function() runif(1))

env<- simmer() %>%
  add_generator("patient", patient_traj, at(0), mon = 2)
env %>% run()
#> simmer environment: anonymous | now: 15 | next: 
#> { Generator: patient | monitored: 2 | n_generated: 1 }

get_mon_attributes(env)
#>   time     name             key       value replication
#> 1    0 patient0          my_key 123.0000000           1
#> 2    5 patient0          my_key 124.0000000           1
#> 3   10 patient0   dependent_key   0.0000000           1
#> 4   15 patient0 independent_key   0.9234335           1

Now this works as it's supposed to work, the problem starts when I try to call get_attribute() in any other sense. Adding this line after set_attribute() at the very end of the trajectory definition:

log_(get_attribute(env, "independent_key"))

throws the abovementioned error. What I actually want to do is call the "leave" function and give it as a probability an attribute. I still do this in the trajectory.

leave(prob = get_attribute(env, "independent_key"))

Needless to say, this also throws the error "Error in get_attribute_(private$sim_obj, key, global) : there is no arrival running".
Does anyone know what might cause this? I feel like the only option is the above explanatio "get_attribute is meant to be used inside a trajectory" - but I feel like I am doing this.

Thanks already!


Solution

  • Okay, I am embarrased to say this but the problem was rather easily fixed. It seems as if the problem was to access the attribute directly.
    So log_(get_attribute(env, "independent_key")) does not work, but log_(function() get_attribute(env, "independent_key")) does.
    That's all it takes.
    If anyone has an explanation as to why that is all it takes, I would highly appreciate it.