I try to make minimize cost model by PuLP many times and still have the error. I would like to make decision for 2 variables below. produce = How many unit will produce for each product ? order = How many qty. will order more for each material item ?
from pulp import *
product = ['S1', 'S2', 'S3', 'S4']
material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M']
margin = {'S1': 25.68, 'S2': 25.68,
'S3': 25.68, 'S4': 25.68}
capacity = 300
inv_bf = 42753.63
product_cost = {'S1': 4.28, 'S2': 4.28,
'S3': 4.28, 'S4': 4.28}
usage = {'S1': {'A': 12.24,
'D': 12.24,
'E': 0.014,
'F': 0.095,
'G': 12.24,
'H': 0.589,
'J': 24.24,
'K': 0.005,
'L': 0.0105},
'S2': {'A': 12.24,
'D': 12.24,
'E': 0.014,
'F': 0.095,
'G': 12.24,
'H': 0.589,
'J': 24.24,
'K': 0.005,
'L': 0.0105},
'S3': {'H': 0.26,
'K': 0.014,
'B': 12.24,
'C': 12.24,
'I': 0.624,
'G': 12.18,
'J': 24.24,
'M': 0.005},
'S4': {'H': 0.26,
'K': 0.014,
'B': 12.24,
'C': 12.24,
'I': 0.624,
'G': 12.18,
'J': 24.24,
'M': 0.005}}
inv = {'A': 7645.8, 'B': 2470, 'C': 4526,
'D': 6678, 'J': 4180.92, 'G': 6879,
'E': 159.5, 'F': 717.4, 'I': 764.1,
'H': 1302.69, 'K': 248.79, 'L': 235,
'M': 179.4}
cost = {'A': 0.03, 'B': 0.03, 'C': 0.056,
'D': 0.151, 'J': 0.024, 'G': 0.88,
'E': 5.156, 'F': 13.04, 'I': 11.09,
'H': 6.833, 'K': 11.261, 'L': 10.118,
'M': 11.914}
# Define variables
produce = LpVariable.dicts("produce", product, lowBound=0, cat='Integer')
order = LpVariable.dicts("order", material, lowBound=0, cat='Integer')
# Define and initialize model
model = LpProblem("total_cost", LpMinimize)
# Objective Function
Total_ProCost = lpSum(product_cost[i]*produce[i] for i in product)
Total_MatCost = lpSum(order[m]*cost[m] for m in material)
Excess = lpSum(inv[m]+order[m]for m in material) - \
lpSum(produce[i]*usage[i][m]for m in material for i in product if m in usage[i].keys())
objective = Total_ProCost + Total_MatCost + lpSum(Excess*cost[m]for m in material)
model.setObjective(objective)
# Material Quantity used
for i in product:
model += lpSum(produce[i]*usage[i][m]for m in material for i in product if m in usage[i].keys()) \
<= lpSum(inv[m] + order[m] for m in material)
# Capacity limited
for i in product:
model += lpSum(produce[i]) <= capacity
# Profit
for i in product:
model += lpSum(produce[i]*margin[i]) >= Total_ProCost + Total_MatCost
# Reduce inv cost
for i in product:
model += lpSum(inv[m]+order[m] - produce[i]*usage[i][m] for m in material for i in product if m in usage[i].keys()) <= inv_bf
model.solve()
#Output
for v in model.variables():
print(v.name,'=',v.varValue)
produce = [v.varValue for v in model.variables()]
I found the result show 0 to order all material and produce 4 products. That is impossible and strange . because Actually we need to use material in dict usage to produce for each product. Example is we can produce S1 when we have material enough for all items . It means that need to use A,D,E,F,G,H,J,K and L.
order_A = 0.0
order_B = 0.0
order_C = 0.0
order_D = 0.0
order_E = 0.0
order_F = 0.0
order_G = 0.0
order_H = 0.0
order_I = 0.0
order_J = 0.0
order_K = 0.0
order_L = 0.0
order_M = 0.0
produce_S1 = 97.11436
produce_S2 = 97.11436
produce_S3 = 97.11436
produce_S4 = 291.34308
Total cost = 2493.887783819344
Please advise the way to fix my constraints or other ways to do. Thanks in advance.