R simmer: release resources outside of the trajectories where they are seized

52 views Asked by At

I'm a beginner in both simmer and R. I'm trying to use simmer to model a simple manufacturing process only involving mixing ingredients in different vessels. A minimal example of such process would look like this:

  1. a gel vessel, where it takes 1.5 hours to prepare the gel
  2. a millbase vessel, where it takes 10 hours to mix all the ingredients of the millbase together
  3. a finalization vessel, where I get the millbase vessel content (take 1 hour to pump), the gel vessel content (takes another hour to pump) and add few more liquids (takes 5 more hours) to get the final batch.

The catch is that the finalization step can only happen when both the gel and the millbase are ready, so the vessels will stay "seized" until the pumping is finished.

I tried to model the simplified system in simmer using the following code:

library(simmer)

# Define the simulation environment
env <- simmer()

# Define resources
env %>% 
  add_resource("gel_vessel") %>% 
  add_resource("millbase_vessel") %>%
  add_resource("finalization_vessel")

# Define the gel trajectory
gel_trajectory <- trajectory("gel_process") %>%
  seize("gel_vessel") %>% log_("Gel vessel seized") %>%
  timeout(1.5) %>% log_("Gel done. Waiting...")

# Define the millbase trajectory
millbase_trajectory <- trajectory("millbase_process") %>%
  seize("millbase_vessel") %>% log_("Premix vessel seized") %>%
  timeout(10) %>% log_("Millbase done. Waiting...")

# Define the finalization trajectory
finalization_trajectory <- trajectory("finalization_process") %>%
  
  clone(
    n = 2,
    gel_trajectory,
    millbase_trajectory
  ) %>%
  
  # Synchronize with both gel and millbase processes
  synchronize(wait = TRUE, mon_all = TRUE)  %>% log_("Both gel and millbase ready! Start formulating") %>%
  
  # Start finalization
  seize("finalization_vessel")  %>% log_("FO vessel seized") %>%
  # Millbase and finalization vessels both seized when transferring between the two
  timeout(1)  %>% log_("Millbase charge done") %>%
  # Release the millbase_vessel after transferring its contents
  release("millbase_vessel")  %>% log_("Millbase vessel released") %>%
  # Gel and finalization vessels both seized when transferring between the two
  timeout(1)  %>% log_("Gel charge done") %>%
  # Release the gel_vessel after transferring its contents
  release("gel_vessel")  %>% log_("Gel vessel released") %>%
  # Finalization time after adding both the gel and the millbase
  timeout(5)  %>% log_("Finalization done") %>%
  # Finalization done, releasing the finalization vessel
  release("finalization_vessel") %>% log_("Batch finished :)")

# Add batch request generator to the simulation
env %>% add_generator("finalization_batch", finalization_trajectory, at(0))

# Run the simulation
env %>% run(until=100)

# Get the monitoring data for arrivals, resources and attributes
arrivals <- get_mon_arrivals(env, per_resource = TRUE)
resources <- get_mon_resources(env)
attributes <- get_mon_attributes(env)

This is the output that I get:

> env %>% run(until=100)
0: finalization_batch0: Gel vessel seized
0: finalization_batch0: Premix vessel seized
1.5: finalization_batch0: Gel done. Waiting...
10: finalization_batch0: Millbase done. Waiting...
10: finalization_batch0: Both gel and millbase ready! Start formulating
10: finalization_batch0: FO vessel seized
11: finalization_batch0: Millbase charge done
11: finalization_batch0: Millbase vessel released
12: finalization_batch0: Gel charge done
Error: 'finalization_batch0' at 12.00 in [Log]->Release->[Log]:
 'gel_vessel' not previously seized

Since I'm using synchronize(wait = TRUE) I can correctly released millbase_vessel (seized in the millbase_trajectory) in the finalization_trajectory, but since the clone going through the gel_trajectory has been destroyed, so is the seizing of gel_vessel, thus giving the error.

I also tried with select, seize_selected and release_selected, but it didn't change a thing.

Now, the fact that I might have understood what went wrong is not bringing me any closer to the solution. So, would you have any input on how I would be able to get past this?

0

There are 0 answers