knapsack optimization using Python pulp - how to set constraints?

49 Views Asked by At

I want to develop inventory fulfillment optimization model. I want to fulfil from Plt_Inv to WH_Inv if WH_Inv is less than SS for each SKUs, with total fulfillment volume should be less than 10000KG.

I write my code as below and also show results. but I receive status as " infeasible" and results does not match to the requirements. I assume that the constraints model is not correct. Can anyone help to see where are incorrect codes.


import pandas as pd
!pip install pulp
import pulp

Dataframe

df1 = pd.DataFrame({'SS':[10,20,100,5,500],'SKU':['A','B','C','D','E']})
df2 = pd.DataFrame({'WH_Inv':[30,15,200,7,300],'SKU':['A','B','C','D','E']})
df3 = pd.DataFrame({'Plt_Inv':[100,0,300,0,100],'SKU':['A','B','C','D','E']})
df4 = pd.DataFrame({'KG':[20,12,10,12,20],'SKU':['A','B','C','D','E']})

Define list

SKU = df1['SKU'].tolist()
SS =df1['SS'].tolist()
WH =df2['WH_Inv'].tolist()
PLT = df3['Plt_Inv'].tolist()
KG = df4['KG'].tolist()

Define constant

safety = {row.SKU:row.SS for row in df1.itertuples()}
warehouse = {row.SKU:row.WH_Inv for row in df2.itertuples()}
plant = {row.SKU:row.Plt_Inv for row in df3.itertuples()}
kgs = {row.SKU:row.KG for row in df4.itertuples()}

Optimization Model

problem =pulp.LpProblem('Optimization',pulp.LpMaximize)

Set variables

x={}
 for s in SKU:

  x[s]=pulp.LpVariable(f'x_{s}',cat='Integer')

Constraints

for s in SKU:
    problem += x[s]>=0
    problem+= x[s]>= safety[s]
    problem+= x[s]<= plant[s]
    problem += pulp.lpDot(x[s],KG)<=10000

Objective

problem += pulp.lpDot(x[s],KG)

Solve

status = problem.solve()
print('status:',pulp.LpStatus[status])

for s in SKU:
  print(s,x[s].value())
  print('obj=', problem.objective.value())

Results: Status is infeasible and fulfillment qty does not match to requirements

status: Infeasible A 10.0 B 0.0 C 100.0 D 0.0 E 500.0 obj= 37000.0

0

There are 0 best solutions below