如何将条件约束应用于Python Pulp函数 [英] How to apply conditional constraints to Python Pulp function

查看:569
本文介绍了如何将条件约束应用于Python Pulp函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Python中使用Pulp设置线性优化.我想对这个问题建立一个有条件的约束.

I'm setting up a linear optimization using Pulp in Python. I would like to set up a conditional constrain to the problem.

就像,我想最大化工厂的利润.对于材料成本,前1000个单位每个价格为$ 5,再增加一个单位为$ 3.例如,如果工厂定购1100个单位,则总成本将为1000 * 5 + 100 * 3.我有一个物料清单:material_list,一个物料基准基准字典:benchmark_dic={material_a: 1000, material_b:2000 ....},如果订单损失比基准基准:price_A_dic价格字典,如果订购的数量大于Benchark,则还有一个价格字典: price_B_dic.

Like, I want to Maximize the profit of a factory. For the cost of the material, the first 1000 units cost $5 each, any more unit cost $3. For example, if the factory order 1100 units, total cost will be 1000*5+100*3. I have a list of material: material_list, a dictionary of benchmark for the materials: benchmark_dic={material_a: 1000, material_b:2000 ....}, a dictionary of the price if order loss than benchmark :price_A_dic, and also a dictionary of the price if you order more than benchark:price_B_dic.

这是我的代码:

x=pulp.LpVariable.dicts('x',material_list,lowBound=0 , cat='Integer')  

New_cost_dic=pd.Series(0,index=dat.index).to_dict()

for seg in material_list:

  if x[seg]>benchmark_dic[seg]:

    New_cost_dic[seg]=(x[seg]-benchmark_dic[seg])*price_b_dic[seg]+benchmark[seg]*price_A_dic[seg]

  else:

    New_cost_DIC[seg]=x[seg]*price_A_dic[seg]

我对销售额也有类似的计算.我可以从中得到结果,但是我不知道我做对了没有.当我得到每种材料有多少个单位的最终结果时,我试图使用相同的计算方法来获得总成本和总销售额,但是我获得的利润(总销售额-总成本)不等于我获得的最高利润来自pulp.value(prob.objective).

I also have a similar calculation for sales. I can get a outcome from this but I don't know if I did it right. When I get a final result of how many units for each material I tried to get the total cost and total sales using the same calculation, but the profit I got by (total sales - total cost) is not equal to the Max profit I got from pulp.value(prob.objective).

我应该如何为这种条件约束或条件函数编写代码.

How should I code for this conditional constrains or conditional function.

推荐答案

我认为以条件方式实现它们的条件约束不会起作用.

I don't think conditional constraints they way you have implemented them will work.

相反,对于像这样的条件约束,您将需要重新构造问题以使用指标变量,这些指标变量是跟踪您感兴趣的条件(对或错)的二进制变量.

Instead for conditional constraints like this you will need to reformulate the problem to make use of indicator variables, which are binary variables which track the condition (true or false) that you are interested in.

对于您的特定问题,我建议类似以下内容,为每个跟踪基准的购买物料编号设置一组变量,例如x1[seg],然后再设置一组变量,例如x2[seg].会跟踪在基准线上方购买的价格,最后是一组二进制变量,例如z[seg],它跟踪我们是否已达到价格突破点.

For your specific problem I would suggest something like the following, have a set of variables, say x1[seg] for each material that tracks the No. bought up to the benchmark, and then another set of variables, say x2[seg] that tracks the No. bought above the benchmark, and finally a set of binary variables, say z[seg] which tracks whether we have reached the price break-point.

然后,费用项将分别为:

The cost terms would then each be:

x1[seg]*price_A_dic[seg] + x2[seg]*price_B_dic[seg]

然后,我们需要添加约束,以强制变量采用适当的值.我认为以下方法应该有效:

We then need to add constraints which enforce the variables to take on appropriate values. I think the following should work:

x1[seg] >= 0
x1[seg] >= benchmark_dic[seg] * z[seg]
x2[seg] >= 0
x2[seg] <= z[seg]*MAX_POSSIBLE_ORDER

其中MAX_POSSIBLE_ORDER是我们从采购数量上不会超过的某个上限.您可以看到,为了使z[seg]取值1,我们首先必须以较高的价格订购benchmark_dic[seg]数量.同样,如果z[seg]1上的价格,我们只能以较低的价格订购任何商品.

Where MAX_POSSIBLE_ORDER is some upper bound that we would never exceed in terms of purchase quantity. You can see that in order for z[seg] to take on value 1 we first have to order the benchmark_dic[seg] quantity at the higher price. Similarly we can only order any at the lower price if z[seg] taknes on value 1.

也许有一种更整洁/更有效的方法来执行此操作,但是以上方法应该可以工作.

There may be a neater/more efficient way to do this, but the above should work.

这篇关于如何将条件约束应用于Python Pulp函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆