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:
- Initialization : A warehouse with unlimoted capacity and initial inventory on hand.
- 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
- 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 self.env.process(self.chek_inventory_and_order()) self.env.process(self.serve_customer()) # 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(self.env.now, demand)) else: print("{:.2f}, sold {} (out of stock)".format(self.env.now, 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(self.env.now, 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.env.now, self.on_hand_inv)) def run_simulation(print_=False): np.random.seed(0) env = simpy.Environment() s = warehouse(env, 40, 5, 40, 2.) env.run(until=10.)
Advertisement
Answer
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.