PuLP 为一个简单的约束问题产生了非最优解
PuLP yields not-optimal solution for a simple constraint problem
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<=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
这是一个约束而不是 objective:
prob+=a+b+c+d+e>=1,"main objective"
基本上你没有 objective 所以求解器只是寻找一个可行的解决方案。
尝试
prob+=a+b+c+d+e,"main objective"
prob+=a+b+c+d+e>=1,"constraint"
现在你既要最小化 a+b+c+d+e
又要对此有约束。
结论:我认为 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))
对于这个常量。
结果是:
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
这是一个约束而不是 objective:
prob+=a+b+c+d+e>=1,"main objective"
基本上你没有 objective 所以求解器只是寻找一个可行的解决方案。
尝试
prob+=a+b+c+d+e,"main objective"
prob+=a+b+c+d+e>=1,"constraint"
现在你既要最小化 a+b+c+d+e
又要对此有约束。
结论:我认为 PuLP 在这里是正确的。