【问题标题】:PuLP yields not-optimal solution for a simple constraint problemPuLP 对简单的约束问题产生非最优解
【发布时间】:2019-02-06 16:43:41
【问题描述】:

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))

对于这个常量。

资源是:

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

然后:

a=0.3

b=0.3

c=0.3

d=0.0

e=0.1

a+b+c+d+e=1

【问题讨论】:

    标签: linear-programming pulp


    【解决方案1】:

    这是一个约束,而不是一个目标:

    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在这里是正确的。

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多