PuLP产生简单约束问题的非最优解

时间:2019-02-06 16:43:41

标签: linear-programming pulp

PuLP求解器无法产生最佳结果,但有可能。是未明确定义的问题,还是库或约束中存在问题?

通常,对于大多数约束可能性,它都提供了最佳的选择,但对于这种特定情况却没有。

from pulp import *
prob = LpProblem("minimization", LpMinimize)

#introducing vars
a=LpVariable("a", 0, None)
b=LpVariable("b", 0, None)
c=LpVariable("c", 0, None)
d=LpVariable("d", 0, None)
e=LpVariable("e", 0, None)

#introducing main obj and constraints
#basically addition of all vars should be minimized to 1.
prob+=a+b+c+d+e>=1,"main objective"
prob+=a<=0.3,"a const"
prob+=b<=0.3,"b const"
prob+=c<=0.3,"c const"
prob+=d<=0.3,"d const"   #can change to 0
prob+=e==0.1,"e const"

"""
for this const. 
the res is:
a=0.3
b=0.3
c=0.3
d=0.3
e=0.1
a+b+c+d+e=1.3

if you change prob+=d<=0.3 to prob+=d<=0
then:
a=0.3
b=0.3
c=0.3
d=0.0
e=0.1
a+b+c+d+e=1

"""


#solves the problem
status = prob.solve()
#checks if optimal or infeasible
print("status:",LpStatus[prob.status])

#shows all the vars
for variable in prob.variables():
    print("{} = {}".format(variable.name, variable.varValue))

此常量。

res是:

a = 0.3

b = 0.3

c = 0.3

d = 0.3

e = 0.1

a + b + c + d + e = 1.3

它应该是1,因为它是LpMinimize。

如果将prob + = d <= 0.3更改为prob + = d <= 0

然后:

a = 0.3

b = 0.3

c = 0.3

d = 0.0

e = 0.1

a + b + c + d + e = 1

1 个答案:

答案 0 :(得分:2)

这是一个约束而非目标:

prob+=a+b+c+d+e>=1,"main objective"

本质上来说,您没有目标,因此求解器只会寻找可行的解决方案。

尝试

prob+=a+b+c+d+e,"main objective"
prob+=a+b+c+d+e>=1,"constraint"

现在,您既要最小化a+b+c+d+e,又要对此施加约束。

结论:我认为PuLP在这里是正确的。