How to add Logical constraints in PuLP

偶尔善良 提交于 2019-12-12 02:42:40

问题


I am trying to solve FLP using PuLP. I want to add logical constraint for variable value.

I have LpVariable f and C is list of LpVariables . I want to add f to the constraint of problem and which depends on values of c[i].

Below is code snippet >

prob = LpProblem("The MILP problem", LpMinimize)

Added 1st constraint :

prob += lpSum(c[i] for i in range (len(c))) == 2

Now I want to add following constraint:

  if`lpSum(c[i] for i in range (len(c))) > 1:
`     prob += f == 1  
  else:
      prob += f == 0


prob += lpSum(c[i] for i in range (len(c)) + f )

Now problem is LpVariables c[i] are initialized with None and hence it throws error while calculating lpSum().

I hope I am clear. Let me know if need any help in understanding this query but I think given code snippet is sufficient enough.


回答1:


Three points:

(1) Your first constraint forces the lpsum to be equal to 2, so f will always be 1 in your example - are you sure your formulation is correct?

(2) If statements can't be used in combination with the lpSum - you should formulate it as an actual constraint.

For example, you could define f as a binary variable and add this constraint:

prob += lpSum(c[i] for i in range (len(c))) - 1 <= M*f 

where M is a sufficiently large number. Then, if f==0 we have that "lpsum() <= 1" and if f==1 we have that lpsum can be anything. Play around with that type of constraints to get f to behave the way you want.

(3) The constraint "prob += lpSum(c[i] for i in range (len(c)) + f )" does nothing unless it's supposed to be the objective of your MILP? If so, you should add it immediately after prob = LpProblem("The MILP problem", LpMinimize)

Good luck



来源:https://stackoverflow.com/questions/33360898/how-to-add-logical-constraints-in-pulp

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