R session fatal error during sim due to conditional batch()

31 views
Skip to first unread message

Philemon Cyclone

unread,
Jun 14, 2023, 10:49:55 AM6/14/23
to simmer-devel
I have come across a problem I am not able to debug, in which the R session aborts due to a "fatal error". The issue only occurs when I include a 
batch(n = n, timeout = t) |> ... |> separate()
component in the trajectory. If I comment out the batch() function or omit the timeout argument, the simulation runs without issue.

When starting the simulation with the same random seed, the fatal error occurs at somewhat consistent times during the simulation. After starting a new RStudio sessions, the crash consistently occurs at one timestamp, but when I run a successful simulation (i.e., without batch()) first and then include the batch() step, the crash consistently occurs at a later timestamp. I am also able to delay the crash to an even later, also consistent timestamp when I insert browser() debug calls, but eventually the simulation crashes anyway.

Unfortunately, I am not able to share the full code for proprietary reasons, but the batch aspect is shown below:

# Branch for batching if batch tool is selected
    ... |>
    simmer::branch(
      \() {
        if (simmer::get_selected(env) == 'batch_tool') {
          1
        } else {
          2
        }
      },
      simmer::trajectory() |>
        # Batch function to load up the tool with at most 8 lots and
        # collection time of at most 12 minutes
        simmer::batch(n = 8, timeout = 0.2, name = 'batch') |>
        simmer::select('50547') |>
        simmer::seize_selected(),
      simmer::trajectory() |>
        simmer::seize_selected(),
      continue = TRUE
      ) |> ...

    # If a batched lot operation, we need to separate the lots
    simmer::branch(
      \() {
        if (stringr::str_detect(simmer::get_name(env), 'batch')) {
          1
        } else {
          0
        }
      },
      simmer::trajectory() |>
        simmer::log_('Separating batch') |>
        simmer::separate(),
      continue = TRUE
    ) |> ...

Is there perhaps a way to emulate batch() timeout functionality as a workaround?

Thank you!

Philemon Cyclone

unread,
Jun 14, 2023, 1:11:25 PM6/14/23
to simmer-devel
I also discovered that the crash only occurs after the batch step on attempting to select or seize the resource that is interleaved with the batch tool (i.e., "50547" in my original post). During debugging R crashes immediately after stepping out of the bolded select() step below:

interleaved_traj <- simmer::trajectory() |>
    simmer::log_('Entering interleaved traj...', level = 3) |>
    # --- Select and seize the operator token ---
    simmer::select(
      \() {
        token_list
        },
      policy = 'first-available',
      tag = 'select_token'
      ) |>
    simmer::log_(
      \() {
        sprintf('Selected token %s', simmer::get_selected(env))
      },
      level = 3
    ) |>
    # Need an unfinished handler here in case the operator's shift ends
    # before transition can be completed
    simmer::handle_unfinished(
      handler = simmer::trajectory() |>
        simmer::log_(
          'Unfinished transition, finding new operator...',
          level = 2
        ) |>
        simmer::rollback(target = 'select_token')
    ) |>
    # Only wait 1 hour before looking for another operator to prevent
    # staying stuck in the same queue for the operator's entire shift
    simmer::renege_in(
      t = 1.0,
      out = simmer::trajectory() |>
        simmer::log_(
          \() {
            'Reneging & looking for new operator.'
          },
          level = 3
          ) |>
        simmer::rollback(target = 'select_token'),
      keep_seized = TRUE
    ) |>
    simmer::seize_selected() |>
    simmer::renege_abort() |>
    simmer::log_(
      \() {
        sprintf('Seized token %s', simmer::get_selected(env))
      },
      level = 3
    ) |>

    # --- Select and release the equipment ---
    # If equipment was already released and arrival is in a rollback trajectory
    # (i.e., either unfinished or reneged), then skip over the equipment release
    # step
    simmer::branch(
      continue = TRUE,
      option = \() {
        seized_resources <- res_df$EquipmentNbr[
          as.logical(simmer::get_seized(env, res_df$EquipmentNbr))
        ]
        eqp_name <- seized_resources[
          !stringr::str_detect(seized_resources, 'Operator')
        ]

        if (length(eqp_name) > 0) {

          1
        } else {
          0
        }
      },
      simmer::trajectory() |>
        simmer::select(
          \() {
            if (simmer::now(env) > t_break) {
              browser()
              }
            seized_resources <- res_df$EquipmentNbr[
              as.logical(simmer::get_seized(env, res_df$EquipmentNbr))
              ]
            eqp_name <- seized_resources[
              !stringr::str_detect(seized_resources, 'Operator')
              ]

            eqp_name
            }
          ) |>
        simmer::log_(
          \() {
            if (simmer::now(env) > t_break) {
              browser()
            }
            sprintf('Releasing eqp %s', simmer::get_selected(env))
            },
          level = 3
          ) |>
        simmer::release_selected()
      ) |>

    # --- Select and seize the interleaved operator ---
    simmer::select(
      \() {
        if (simmer::now(env) > t_break) {
          browser()
        }

        seized_resources <- res_df$EquipmentNbr[
          as.logical(simmer::get_seized(env, res_df$EquipmentNbr))
        ]

        token_name <- seized_resources[
          stringr::str_detect(seized_resources, 'Operator')
        ]

        # Get the operator name from the token name
        operator_name <- stringr::str_remove(token_name, '_token')

        operator_name
      }
    ) |>

    simmer::seize_selected() |>
    simmer::log_(
      \() {
        sprintf('Seized interleaved op %s', simmer::get_selected(env))
      },
      level = 3
    ) |>

    # --- Select and release the operator token ---
    simmer::select(\() {
      selected_resources <- res_df$EquipmentNbr[
        as.logical(simmer::get_seized(env, res_df$EquipmentNbr))
      ]

      token_name <- selected_resources[
        stringr::str_detect(selected_resources, '_token')
      ]

      token_name
      }
      ) |>
    simmer::release_selected() |>

    # --- Timeout with the operator and release ---
    simmer::select(
      \() {
        selected_resources <- res_df$EquipmentNbr[
          as.logical(simmer::get_seized(env, res_df$EquipmentNbr))
        ]

        operator_name <- selected_resources[
          stringr::str_detect(selected_resources, 'Operator')
        ]

        operator_name
      }
    ) |>
    simmer::log_(
      \() {
        sprintf('Transitioning with %s', simmer::get_selected(env))
      },
      level = 3
    ) |>

    # If a batched lot operation, we need to separate the lots
    simmer::branch(
      \() {
        if (stringr::str_detect(simmer::get_name(env), 'batch')) {
          1
        } else {
          0
        }
      },
      simmer::trajectory() |>
        simmer::log_('Separating batch') |>
        simmer::separate(),
      continue = TRUE
    ) |>
    simmer::timeout_from_attribute('transition_time') |>
    simmer::release_selected_all()

Iñaki Ucar

unread,
Jun 18, 2023, 12:00:55 PM6/18/23
to simmer...@googlegroups.com
Hi, thanks for the report. I see no batch in your last post. Does this mean that the crash happens without a batch? BTW, for issues and crashes like this, it would be nice if you could open an issue in the GitHub repository instead. Additionally, it would be helpful if you could provide your sessionInfo() and a trace of the simulation by setting simmer(verbose=TRUE). It would be even nicer if you could provide a small reproducible example. ;-)

Iñaki

--
You received this message because you are subscribed to the Google Groups "simmer-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simmer-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/simmer-devel/fd218b34-4b6d-4409-aedc-be83942267a2n%40googlegroups.com.


--
Iñaki Úcar
Reply all
Reply to author
Forward
0 new messages