简体   繁体   English

PuLP:坚持目标函数定义

[英]PuLP: Stuck with objective function definition

I am a first time user of PuLP and I the last time I did linear programming, Python did not exist.我是 PuLP 的第一次用户,上次我做线性编程时,Python 并不存在。 I can solve this problem with LibreOffice's Solve extension (which does LP)我可以用 LibreOffice 的 Solve 扩展(它做 LP)来解决这个问题

But I need to do it in code.但我需要在代码中做到这一点。

I want to optimise a stock picking problem.我想优化一个选股问题。 We need to pick a certain quantity of screws, say 98. Screws are packed in packs of 25 and 100. I name those pack sizes '25' and '100'.我们需要挑选一定数量的螺钉,比如 98 个。螺钉包装在 25 个和 100 个包装中。我将这些包装尺寸命名为“25”和“100”。 The cost of the pick needs to be minimised.选择的成本需要最小化。 There is a cost to pick each pack, and there is a cost to the excess quantity picked.挑选每个包装都有成本,而且挑选的多余数量也有成本。 The constraint is that the quantity picked >= target_qty约束是选择的数量 >= target_qty

For example, if the cost to each unit of excess was 0.1 and the cost to pick the '25' pack was 1 and the cost to pack the '100' pack is 1.1., the cost of picking is 1 x 100 pack is例如,如果每个多余单位的成本是 0.1,拣选 '25' 包的成本是 1,而拣选 '100' 包的成本是 1.1,那么拣选成本是 1 x 100 包是

(100 - 98) *.1 + 0*1 + 1*1.1 (100 - 98) *.1 + 0*1 + 1*1.1

This is cheaper than picking 4*'25' pack.这比挑选 4*'25' 包装便宜。

Assuming that there are dicts pack_cost{} and pack_capacity{} which both have the key pack_name, eg pack_cost = {'25':1,'100':1.1} and therefore list_of_pack_names = ['25','100']假设有 dicts pack_cost{} 和 pack_capacity{} 都具有关键字 pack_name,例如pack_cost = {'25':1,'100':1.1}因此list_of_pack_names = ['25','100']

I try this:我试试这个:

lp_prob = pulp.LpProblem('PackSizes', pulp.LpMinimize)
packs_used = pulp.LpVariable.dicts("Packs",list_of_pack_names,lowBound=0,cat="Integer")
pack_cost = [pack_costs[pack_name]*packs_used[pack_name] for pack_name in list_of_pack_names]
excess_cost = cost_per_unit * ( sum([pack_sizes[pack_name]*packs_used[pack_name] for pack_name in list_of_pack_names])- original_qty)

lp_prob += pulp.lpSum(pack_cost) + pulp.lpSum(excess_cost)  #objective function

# and constraint: total picked >= needed
lp_prob +=   pulp.lpSum(sum([pack_sizes[pack_name]*packs_used[pack_name] for pack_name in list_of_pack_names]) >= target_qty)

Results:结果:

 print("Status:",pulp.LpStatus[lp_prob.status])

shows Optimal显示最佳

lp_prob.objective is 10*Packs_10 + 15*Packs_15 + 30*Packs_30 - 16.5 lp_prob.objective10*Packs_10 + 15*Packs_15 + 30*Packs_30 - 16.5

but the solution is 0 of each pack size但解决方案是每个包装尺寸为 0

You may check your problem with你可以检查你的问题

print(lp_prob)

You do not add any essential constraint that prevents all vars from becoming zero.您没有添加任何防止所有变量变为零的基本约束。 Probably, you misprinted in the constraint statement.可能,您在约束声明中打印错误。 This constraint makes the problem not trivial (check brackets):此约束使问题变得不重要(检查括号):

lp_prob += pulp.lpSum(sum([pack_sizes[pack_name]*packs_used[pack_name] for pack_name in list_of_pack_names])) >= target_qty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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