Discrete Event Simulation in Python
Diogo Costa (PhD, MSc)
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
Description of a natural or human-driven system using mathematical concepts and language.
Models can be classified as:
Mathematical models can be simple or complex.
George Box: "All models are wrong, but some are useful."
Model of flow in a river
This model predicts streamflow for the Goaya hydrologic station, China.
The prediction is based on rainfall information that is used as model input.
Other examples
Predicting Inflation
Other examples
# Define model parameters
processes = {"process_1": 5,
"process_2": 2,
"process_3": 3}
# Simulation period
simulation_time = 365
# Run model
discrete_model(processes, simulation_time)
def discrete_model(processes, simulation_time): # 1) Run end-condition while (time < simulation_time): process_names = list(processes.keys())
# 2) Loop over all processes for p in range(len(process_names)): process_name_p = process_names[p]
# 3) Account for effect of each process time += processes[process_name_p]
Example of a discrete-event model output for a manufacturing activity
=> START OF SIMULATION (Time = 0 days)
Time = 6.00 days | Process Complete: Transport of raw material
Time = 9.00 days | Process Complete: Building components
Time = 11.00 days | Process Complete: Assembling parts
Time = 14.00 days | Process Complete: Selling product
=> COMPLETED: Supply-Chain cycle #1 | Time = 15.5 days
Time = 21.50 days | Process Complete: Transport raw material
Time = 24.50 days | Process Complete: Building components
Time = 26.50 days | Process Complete: Assembling parts
Time = 29.50 days | Process Complete: Selling product
=> COMPLETED: Supply-Chain cycle #2 | Time = 31.0 days
Graphical visualizing of model results: helpful to identify patterns and tipping points in the system.
Visualization should be tailored to simulation objectives.
Many useful visualization packages available: matplotlib
, seaborn
, plotly
.
Examples
y
) vs. the corresponding time values (x
)plt.plot(x, y, color='green', marker='o',
markersize=12, linestyle='dashed',
linewidth=2)
x
and count the number of values in each binplt.hist(x, 50, density=True,
facecolor='g', alpha=0.75)
Discrete Event Simulation in Python