Hello,
I am looking for help with implementing a custom helper function.
In the simple example below, we visit A, visit B, then visit C.
If it is the 1st or 2nd time we have visited C, we then rollback to B with probability 1.
If it is the 3rd or greater time, we finish with probably 1/2, rollback to A with probability 1/4, or rollback to B with probability 1/4.
I have implemented this model successfully:
``` r
library(simmer) # requires feature/rollback_tag branch
Nice! :) Please note that this may not be merged. I still need to decide what's the final interface for this. Currently, the activity(..., tag=something) option has more votes. What do you think about this? Any preference and any reason for it?
[snip]
This is great because I can use an arbitrary function for c_transition_probs to implement more complex behavior.
My ultimate goal is to have several of these branch steps that have different transition_probs functions.
Because of this, it would be nice to create a helper function that creates these branch functions for me.
Here is a first attempt:
```r
probabilistic_rollback <- function(.trj, targets, probs) {
branch(.trj = .trj,
option = function() sample.int(n = length(targets) + 1, size = 1, prob = probs) - 1,
continue = F,
lapply(X = targets, FUN = function(target) rollback(.trj = trajectory(), target = target)))
}
```
However, it doesn't work as desired. It just rolls back to B every time:
This is because probs is a fixed value.
```r
env2 <- simmer()
traj2 <-
trajectory() %>%
tag("A")$seize("A") %>%
timeout(1) %>%
release("A") %>%
tag("B")$seize("B") %>%
timeout(1) %>%
release("B") %>%
tag("C")$set_attribute("C_attempts", 1, mod = "+", 0) %>%
seize("C") %>%
timeout_from_attribute("C_attempts") %>%
release("C") %>%
probabilistic_rollback(targets = targets, probs = c_transition_probs(get_attribute(env2, "C_attempts")))
And here you are passing the value. You need this to be re-evaluated every time the branch evaluates the option. An easy fix is:
probabilistic_rollback(targets = targets, probs = function() c_transition_probs(get_attribute(env2, "C_attempts")))
And then probs is a function call above.