【问题标题】:PuLP in Python Not Finding MaximumPython中的纸浆没有找到最大值
【发布时间】:2018-11-03 04:13:27
【问题描述】:

我使用下面的代码已经有一段时间了,除了现在,它总是能够找到最大/最小值。

我得到一个角点:x=168,y=192,objective=3288

但是有一个角点是真正的最大值:x=0,y=304,objective=3344

我做错了什么导致这段代码无法找到真正最大化目标的 x,y?

from pulp import LpVariable, LpProblem, LpMaximize, LpStatus, value, LpMinimize

# declare your variables
x = LpVariable("y1", 0, None)
y = LpVariable("y2", 0, None)

# defines the problem
prob = LpProblem("problem", LpMaximize)

# defines the constraints
prob += 1/2*x+3/4*y == 228
prob += 1/2*x+1/4*y == 132

# defines the objective function to maximize
prob += 7*x+11*y

# solve the problem
status = prob.solve()
LpStatus[status]
# print the results
print('x={0},y={1}.'.format(round(value(x)),round(value(y))))
print("The objective is ${}.".format(round(value(prob.objective))))

【问题讨论】:

  • 我认为您可能希望每个约束的左侧小于或等于相应的右侧。如果是这样,那么您报告的角点确实是最佳的。如果你在约束中强制等式,你有两个变量和两个方程,并且可行空间是只有一个点,对应的线性方程组的解。

标签: python math linear-programming maxima pulp


【解决方案1】:

考虑约束prob += 1/2*x+1/4*y == 132
如果您设置 x=0y=304,则会违反此约束 (76 ≠ 132)。
要测试解决方案,您只需添加约束:

prob+= x == 0
prob+= y == 304

status = prob.solve()
print(LpStatus[status])

输出不可行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    相关资源
    最近更新 更多