You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use PuLP to minimize total cost to find quantity to produce of each product and quantity to order more for material.
The code below are input for product ,material , margin and material usage of each product. and I have defined variable already.
but I don't know how to create objective function for Total_Excess cost. because normally we use lpsum but our constraint need sum in sum as equation in the picture. I tried to code as below but It showed error when I did.
List
product = ['S1', 'S2', 'S3', 'S4'] # i
material list
material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M'] # m
produce = LpVariable.dicts("produce", product, lowBound=0, cat='Integer')
How many quantity of material to order more ?
order = LpVariable.dicts("order", material, lowBound=0, cat='Continuous')
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)
Total_ExcessCost = lpSum((inv[m]+order[m] for m in material - lpSum(produce[i]*usage[i][m] for m in material for i in product))
* cost[m] for m in material)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to use PuLP to minimize total cost to find quantity to produce of each product and quantity to order more for material.
The code below are input for product ,material , margin and material usage of each product. and I have defined variable already.
but I don't know how to create objective function for Total_Excess cost. because normally we use lpsum but our constraint need sum in sum as equation in the picture. I tried to code as below but It showed error when I did.
List
product = ['S1', 'S2', 'S3', 'S4'] # i
material list
material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M'] # m
Dictionary
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
How many unit will produce ?
produce = LpVariable.dicts("produce", product, lowBound=0, cat='Integer')
How many quantity of material to order more ?
order = LpVariable.dicts("order", material, lowBound=0, cat='Continuous')
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)
Total_ExcessCost = lpSum((inv[m]+order[m] for m in material - lpSum(produce[i]*usage[i][m] for m in material for i in product))
* cost[m] for m in material)
objective = Total_ProCost + Total_MatCost + Total_ExcessCost
model.setObjective(objective)
Define Constraints
Material Quantity used
for m in material:
model += lpSum(produce[i]*usage[i][m]for m in material for i in product) <= 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) <= inv_bf
model.solve()
#Output
for v in model.variables():
print(v.name,'=',v.varValue)
produce = [v.varValue for v in model.variables()]
The optimised objective function value is printed to the console
print("Total cost = ", pulp.value(model.objective))
What am I missing here?
Thank you for your answers in advance.
Beta Was this translation helpful? Give feedback.
All reactions