【发布时间】: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
【问题讨论】: