How to apply conditional constraints to Python Pulp function

冷暖自知 提交于 2019-12-24 23:37:13

问题


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

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.

Here is my code:

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]

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.


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/57949190/how-to-apply-conditional-constraints-to-python-pulp-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!