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.
JavaScript
x
52
52
1
import simpy
2
import numpy as np
3
4
class warehouse(object):
5
6
# initialize warehouse object
7
def __init__(self, env, initial_inv, reorder_point, target_inv, lead_time):
8
self.env = env
9
self.on_hand_inv = initial_inv
10
self.reorder_point = reorder_point
11
self.target_inv = target_inv
12
self.lead_time = lead_time
13
14
# start processes
15
self.env.process(self.chek_inventory_and_order())
16
self.env.process(self.serve_customer())
17
18
19
# process to serve Customer
20
def serve_customer(self):
21
while True:
22
interarrival = np.random.exponential(1./5)
23
yield self.env.timeout(interarrival)
24
demand = np.random.randint(1,5)
25
if demand < self.on_hand_inv:
26
self.on_hand_inv -= demand
27
print("{:.2f}, sold {}".format(self.env.now, demand))
28
else:
29
print("{:.2f}, sold {} (out of stock)".format(self.env.now, self.on_hand_inv))
30
self.on_hand_inv = 0
31
32
# process to place order
33
def chek_inventory_and_order(self):
34
while True:
35
if self.on_hand_inv <= self.reorder_point:
36
order_qty = self.target_inv - self.on_hand_inv
37
print("{:.2f}, place order of {}".format(self.env.now, order_qty))
38
yield self.env.timeout(self.lead_time)
39
self.on_hand_inv += order_qty
40
order_qty = 0
41
print("{:.2f}, order recieved. {} in inventory".format(self.env.now, self.on_hand_inv))
42
43
44
45
def run_simulation(print_=False):
46
np.random.seed(0)
47
env = simpy.Environment()
48
49
s = warehouse(env, 40, 5, 40, 2.)
50
env.run(until=10.)
51
52
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.