简体   繁体   English

Python中的PuLP找不到最大值

[英]PuLP in Python Not Finding Maximum

I've been using the code below for some time now and it's always been able to find the max/min except for now. 我已经使用下面的代码一段时间了,除了现在,它始终能够找到最大值/最小值。

I get one corner point to this: x=168, y=192, objective=3288 我得到一个角点:x = 168,y = 192,物镜= 3288

But there is a corner point that is the true max: x=0, y=304, objective=3344 但是有一个角点是真实的最大值:x = 0,y = 304,物镜= 3344

What am I doing wrong that makes this code unable to find the x,y that truly maximizes the objective? 我在做什么错使该代码无法找到真正使目标最大化的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))))

Consider the constraint prob += 1/2*x+1/4*y == 132 . 考虑约束prob += 1/2*x+1/4*y == 132
If you set x=0 and y=304 this constraint would be violated ( 76 ≠ 132). 如果您设置x=0y=304则将违反此约束(76≠132)。
To test a solution you can just add the constraints: 要测试解决方案,您可以添加约束:

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

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

which outputs Infeasible . 输出不可行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM