1. Learn
  2. /
  3. Courses
  4. /
  5. Discrete Event Simulation in Python

Connected

Exercise

Monte Carlo sampling for a discrete-event model with SimPy

Now let's build the same Monte-Carlo sampling analysis using a SimPy version of the model. The SimPy model has a generator named manufacturing_process, which simulates different processes, and a function called run_monte_carlo that runs the model multiple times, storing the information in a NumPy array named time_record.

The code that plots the results is similar to the one used in the previous exercise, but it has been moved to a function named plot_results() that is shown below.

def plot_results():

    df_disc = pd.DataFrame({cNam[0]: process_line_space, cNam[1]: time_record})
    fig = sns.lineplot(data=df_disc, x=cNam[0], y=cNam[1], marker="o")
    fig.set(xlim=(0, len(processes) + 1))
    plt.plot() 

The Monte-Carlo sampling loop will produce a series of possible process trajectories, as shown in the figure. Monte Carlo trajectories for different process scenario.

Instructions

100 XP
  • Clock-in and yield process_duration.
  • Save the current time in time_record.
  • Run a for-loop for n_trajectories samples with dummy variable t.
  • Create the SimPy environment, add processes and run the model.