0

I have 20 components and 5 models that I am working with. Using the inventory of component in stock I am trying to identify what additional inventory to order and which models to build in order to minimise inventory.

#Importing libraries
import pulp
from pulp import *

#Data
row_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

component = {1: "C 1", 2: "C 2", 3: "C 3", 4: "C 4", 5: "C 5", 6: "C 6", 7: "C 7", 8: "C 8", 9: "C 9", 10: "C 10", 11: "C 11", 12: "C 12", 13: "C 13", 14: "C 14", 15: "C 15", 16: "C 16", 17: "C 17", 18: "C 18", 19: "C 19", 20: "C 20"}

stock = {1: 250, 2: 20, 3: 0, 4: 300, 5: 750, 6: 400, 7: 100, 8: 150, 9: 200, 10: 250, 11: 500, 12: 1000, 13: 10, 14: 300, 15: 250, 16: 100, 17: 150, 18: 200, 19: 400, 20: 300}

std_cost = {1: 10, 2: 5, 3: 100, 4: 10, 5: 5, 6: 100, 7: 100, 8: 5, 9: 100, 10: 10, 11: 5, 12: 10, 13: 5, 14: 100, 15: 10, 16: 5, 17: 100, 18: 10, 19: 5, 20: 100}

#Utilization of 20 components for Model 1
Mod1 = {1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 6: 0, 7: 2, 8: 0, 9: 0, 10: 0, 11: 1, 12: 0, 13: 0, 14: 3, 15: 1, 16: 0, 17: 0, 18: 0, 19: 2, 20: 0}

#Utilization of 20 components for Model 2
Mod2 = {1: 0, 2: 1, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 1, 17: 0, 18: 2, 19: 1, 20: 2}

#Utilization of 20 components for Model 3
Mod3 = {1: 3, 2: 2, 3: 1, 4: 4, 5: 2, 6: 3, 7: 2, 8: 1, 9: 2, 10: 2, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0}

#Utilization of 20 components for Model 4
Mod4 = {1: 2, 2: 0, 3: 0, 4: 3, 5: 3, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 3, 12: 4, 13: 1, 14: 0, 15: 0, 16: 0, 17: 0, 18: 1, 19: 0, 20: 0}

#Utilization of 20 components for Model 5
Mod5 = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 1, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 2, 13: 0, 14: 0, 15: 5, 16: 1, 17: 1, 18: 0, 19: 0, 20: 1}

max_range = 6 #(as we have 5 models)

# Problem- Minimize
prob = LpProblem("Inventory",LpMinimize)

# Decision Varibales
#Dictionary for quantities of model to be built 
for i in range(1, max_range): 
    exec(f'Mod{i}_vars = LpVariable("Mod{i}",0,cat="Integer")')

#dictionary of additional components to order 
additional_vars = LpVariable.dicts("Additional",row_number,0,cat='Integer')

# Objective function
prob += lpSum([(stock[i] + additional_vars[i] - (Mod1[i] * Mod1_vars) - (Mod2[i] * Mod2_vars) - (Mod3[i] * Mod3_vars) - (Mod4[i] * Mod4_vars) - (Mod5[i] * Mod5_vars)) * std_cost[i] for i in row_number])

# Constraints
#Inventory cannot be negative
for i in row_number: 
     prob += sum([stock[i] + additional_vars[i] - (Mod1[i] * Mod1_vars) - (Mod2[i] * Mod2_vars) - (Mod3[i] * Mod3_vars) - (Mod4[i] * Mod4_vars) - (Mod5[i] * Mod5_vars)]) >= 0

#Additional cost cannot exceed budget for additional inventory 
prob += lpSum([std_cost[i] * additional_vars[i] for i in row_number]) <= 10000

prob.solve()
print("Status:", LpStatus[prob.status])
value(prob.objective) 

Output: 71810

I am getting the output with the above code. However, I am working with 5 models at the moment which will increase to more when I work with complete data set.

I tried to change objective function to:

prob += lpSum([(stock[i] + additional_vars[i] - exec(f'(sum([Mod{f}[i] * Mod{f}_vars]) for f in range(1,6))')) * std_cost[i] for i in row_number]), "Objective Function"

NameError: name 'f' is not defined

How can I make my objective function dynamic?

0 Answers0