简体   繁体   中英

I keep getting an error in my pulp (lot sizing with linear programming) code, what is wrong?

I need to analyse a lot sizing problem with linear programming, but my pulp code is not working properly.

from pulp import *

prob = LpProblem("Lot_Sizing_Problem", LpMinimize)

period = []
for i in range(H):
    period.append(i)

order = LpVariable.dicts('order', period, lowBound = 0, upBound = 1, cat='integer'),
orderamount = LpVariable.dicts('orderamount', period, lowBound = 0, cat='integer'),
inventory = LpVariable.dicts('orderamount', period, lowBound = 0, cat='integer'),

prob += lpSum([A*period[i]+h*(inventory[i]+orderamount[i]-D[i]) for i in period])

prob += (inventory[i+1] for i in period) == inventory[i]+orderamount[i]-D[i]
prob += (orderamount[i] for i in period) <= M*order[i]
prob += (orderamount[i] for i in period) == D[i] - inventory[i]

prob.solve()

I keep getting this error: TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

These are some example values to put in, the result shoiuld be 102.0: These are the meanings of the values: (Time horizon: H, holding cost: h, fixed setup cost: A, production capacity: C, demand list: D, and big M: M)

H=10
h=0.5
A=10
C=18
D=[12,10,13,14,13,15,17,20,19,14]
M=C

Thanks!

There were quite a few errors.

from pulp import *

prob = LpProblem("Lot_Sizing_Problem", LpMinimize)

H=10
h=0.5
A=10
C=18
D=[12,10,13,14,13,15,17,20,19,14]
M=100

period = list(range(H))

order = LpVariable.dicts('order', period, lowBound = 0, upBound = 1, cat='integer')
orderamount = LpVariable.dicts('orderamount', period, lowBound = 0, cat='integer')
inventory = LpVariable.dicts('inventory', period, lowBound = 0, cat='integer')

prob += lpSum([A*period[i]+h*(inventory[i]+orderamount[i]-D[i]) for i in period])

for i in period:
  if i<H-1:
    prob += inventory[i+1]  == inventory[i]+orderamount[i]-D[i]
  prob += orderamount[i]  <= M*order[i]
  prob += orderamount[i]  == D[i] - inventory[i]

prob.solve()

Note that using dicts for indexing by integers is not a normal approach. So, I probably would write:

order = [LpVariable(...) for i in period]

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM