# Beginning Inventory simulation using SimPy

Production simulation for the industrial process has been around since the 1950s and is a powerful technique for testing business conditions, logic, and changes in processes. Traditionally simulations were done using spreadsheets tools like excel or statistical-based packages such as SPSS or Minitab. In this blog, we will use the “Discrete event simulation” technique to model and test inventory simulations or scenarios. Discrete event simulation or DES creates events at a specific interval within the given time frame and these are then processed subsequently using blocks of processes based on some business conditions, this difference to the continuous event simulation like using differential equations to solve rocket trajectories, circuit analysis and other physical systems operating in real-time. The important point is the state of the program for DES is observed at fixed regular time points and is build on discrete stochastics mathematics such as queueing theory(e.g. works of Agner Erlang). The purpose of the simulation is to understand the interactions and impacts of events, processes, the sequence of actions to model physical processes.

There are several paid options when it comes to discrete event simulations such as Rockwell’s Arena, Matlab, etc., however, we will use the python simpy package, an open-source discrete-event simulation package based on Python.

Let's build a simple shop scenario where we are selling large cookie packs at 100 dollars a piece while the purchase and delivery cost is 50 dollars. We will use an (r, Q) model, a well-known policy of modeling over inventory strategy such that we reorder stuff only when the inventory reaches r unit level and order Q quantity. Obviously, in a real-world scenario, the overall model is much more complicated involving multiple strategies to optimize costs and lead-times.

The details of the simulation we are going to build are as follows

1. generate customer arrival based on exponential distribution with a rate factor
2. generate a random demand of widgets by the customer based on a uniform distribution
3. Reorder is generated based on the difference of reorder level and current inventory level. I will try to round off my reorder using the ceiling function

Let set up the environment first. Env create a simulation object and until variable sets the run cycle

import simpy

SimPy is based on generators or processes which are invoked based on logic provided in the program. We will create a shop_runner generator or process

We will create a simulation class with reorder level, reorder quantity, starting cost balance, num of units ordered, inventory level, observation time, and demand.

We will create a simple reorder level reorder Quantity model such that reorder is triggered when inventory reaches the reorder limit.

class inventory_simulation:

Let's add demand and customer interarrival period modeled randomly. Customers enter the shop randomly based on rate parameter exp. distribution

def generate_interarrival(self) -> np.array:

Let's create a function to handle reorders as generated based on reorder level and EOQ / reorder quantity

def handle_order(self) -> None:

Let's create a runner function to orchestrate different sub-processes of customer arrivals, demands, and reorders

def runner_setup(self):

Finally, let's create an observation function to record inventory levels

def observe(self):

The plot shows a typical inventory behavior where items are sold and restocked based on lead-times and policy models

import numpy as np

## Visual Introduction to Git

Get the Medium app