Sample Inventory Example 1
The simple inventory example simulates an inventory policy where every 5 periods, the firm will reorder inventory, with the amount based on the current inventory level.full Script
Simulation Context
In this example, there arent multiple items to simulate, there is only one, the inventory. The inventory is maintained in the simulation context.The function below is the context function used by the simulation.
def context(date, contexts):
context = {"inventory":100, "backorder":0} if len(contexts)==0 else contexts[-1]
rcontext = context.copy()
if 'reorder' in rcontext: rcontext.pop('reorder')
return rcontext
Simulate One Date
For this simulation, we assume a single item to simulate (inventory for a single product). We take a sequence of 100 numbers sa the "dates" of the simuation
products = [{'name':'product', 'price':100}]
dates = [str(x) for x in range(1,100)]
The simiulate_one_item function runs the simulation for one date. It generates a random number, here using a lognormal distribution and takes this as the demand for the product during the period. Because the context contains the total inventory amount, we adjust the context to subtract out the new demand. If there isnt enough inventory, the demand becomes a back order.
def reorder(context):
return context['backorder'] + 50
'''
takes an item, a date, and set of contexts (which in this case, we arent using)
and returns an item representing the inputted item simulated
'''
def simulate_item(item, contexts, date):
context = contexts[-1]
if int(date)%5 == 0:
context['reorder'] = reorder(context)
context['inventory']+=context['reorder'] - context['backorder']
context['backorder'] = 0
pass
sigma = 2
mu=1
samples = stats.lognorm.rvs(s=sigma, scale=4, size=1)
demand = round(float(samples[0]))
if demand>context['inventory']:
residual = demand - context['inventory']
context['inventory'] = 0
context['backorder'] = context['backorder']+residual
pass
else:
context['inventory'] -= demand
pass
return context['inventory']
The reorder function dictates how much is ordered at each period. Here we utilize a simple policy that fills whatever backorder is present and adds 50 units.