Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - PuLP - relative allocation constraint

I have a bucket and list of items with a packsize where a packsize is a collection of units.

bckdf=pd.DataFrame.from_dict({0:{'Bucket_Type': 'Veg', 'Capacity':1000},
                        1:{'Bucket_Type': 'Non-Veg', 'Capacity':500}}).T

items=pd.DataFrame.from_dict({'item_no': {0: '123', 1: '456', 2: '789'},
 'item_description': {0: 'Buffet Malabar Paratha 300 gm',
  1: 'Mc Cain French Fries Value Pk 750 Gm',
  2: 'Yummies Veg Sticks 320 gm Pouch'},
 'volume': {0: 0.0714286, 1: 0.214286, 2: 0.0357143}})

I need to fit in minimum 900 units in the bucket, while ensuring that more or less equal number of packs are put in the bucket for each item. Everytime, i choose 1 pack, I use up volume of that pack which I formulated as a cost.

prob = LpProblem("LP_Problem", LpMaximize)

buckettype = bckdf[bckdf['Bucket_Type']=='Veg']['Bucket_Type']
capacity = bckdf.set_index(['Bucket_Type'])['Capacity'].to_dict()
itemlist=items['item_no']
combos = [(w,b) for w in buckettype for b in itemlist]
costs = makeDict((buckettype, itemlist),[items['volume']])

itemqtydict = LpVariable.dicts("Combo",(buckettype, itemlist), 0, None, LpInteger)
## Objective function
prob += lpSum([(itemqtydict[w][b]) for (w,b) in combos])
prob += lpSum([(itemqtydict[w][b])*costs[w][b] for (w,b) in combos])>=900
prob += lpSum([(itemqtydict[w][b])*costs[w][b] for (w,b) in combos])<=bckdf[bckdf['Bucket_Type']=='Veg']['Capacity'].values[0]

status=prob.solve(pulp.PULP_CBC_CMD(timeLimit=5,msg=1, gapRel=1))

How do I build in constraint where

more or less equal number of packs are put in the bucket


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You basically already have it with

prob += lpSum([(itemqtydict[w][b])*costs[w][b] for (w,b) in combos])>=900

Here you're constraining the cost of the items in the bucket to be equal to or greater than 900.

But if you want just the number of items to be greater then you can use;

prob += lpSum([(itemqtydict[w][b])] for (w,b) in combos])>=900

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...