Skip to content

Inventory discrete event simulation with simpy object oriented

I m trying to formulate an inventory simulation with Simpy (a Python library for discrete event simulation) using an Object Oriented approach.

The simulation follow these steps:

  1. Initialization : A warehouse with unlimoted capacity and initial inventory on hand.
  2. Serve Customers : Customers arrive each inter arrival time and ask for a quantity of items demand.
    • If we have enough inventory on hand, the customer is served
    • If the demand is greater than the inventory on hand, all the items are sold
  3. Check Inventory for replenishment : If the inventory on hand drop at a certain level reorder point, we place an order to reach target inventory. We wait the lead time to fulfill the warehouse.

The problem

It s my first object oriented approach to simulation and I cannont see why the simulation doesn’t run properly. Actually, I don’t get any output and the script keeps running without displaying anaything.

import simpy
import numpy as np

class warehouse(object):

    # initialize warehouse object
    def __init__(self, env, initial_inv, reorder_point, target_inv, lead_time):
        self.env = env
        self.on_hand_inv = initial_inv
        self.reorder_point = reorder_point
        self.target_inv = target_inv
        self.lead_time = lead_time

        # start processes

    # process to serve Customer
    def serve_customer(self):
        while True:
            interarrival = np.random.exponential(1./5)
            yield self.env.timeout(interarrival)
            demand = np.random.randint(1,5)
            if demand < self.on_hand_inv:
                self.on_hand_inv -= demand
                print("{:.2f}, sold {}".format(, demand))
                print("{:.2f}, sold {} (out of stock)".format(, self.on_hand_inv))
                self.on_hand_inv = 0

    # process to place order
    def chek_inventory_and_order(self):
        while True:
            if self.on_hand_inv <= self.reorder_point:
                order_qty = self.target_inv - self.on_hand_inv
                print("{:.2f}, place order of {}".format(, order_qty))
                yield self.env.timeout(self.lead_time)
                self.on_hand_inv += order_qty
                order_qty = 0
                print("{:.2f}, order recieved. {} in inventory".format(, self.on_hand_inv))

def run_simulation(print_=False):
    env = simpy.Environment()

    s = warehouse(env, 40, 5, 40, 2.)



In your method chek_inventory_and_order you have all of the logic, including the yield which would get you out of the method, contained under an if statement which is False given the initialization. Since that’s all happening within a while True, you’ve created an infinite loop. Looks to me like that’s why it keeps running forever with no output.

User contributions licensed under: CC BY-SA
10 People found this is helpful