Python中的纸浆找不到最大值
我已经使用下面的代码一段时间了,除了现在,它总是能够找到最大/最小值 我得到一个角点:x=168,y=192,objective=3288 但是有一个角点是真正的最大值:x=0,y=304,objective=3344 我做错了什么,使代码无法找到真正最大化目标的x,yPython中的纸浆找不到最大值,python,math,linear-programming,maxima,pulp,Python,Math,Linear Programming,Maxima,Pulp,我已经使用下面的代码一段时间了,除了现在,它总是能够找到最大/最小值 我得到一个角点: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 = LpVa
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))))
考虑约束
prob+=1/2*x+1/4*y==132
如果设置
x=0
和y=304
,则会违反此约束(76≠ 132)。要测试解决方案,只需添加约束:
prob+= x == 0
prob+= y == 304
status = prob.solve()
print(LpStatus[status])
输出不可行。我认为您可能希望每个约束的左侧小于或等于相应的右侧。如果是这样,那么您报告的角点确实是最佳的。如果在约束中强制等式,则有两个变量和两个方程,可行空间只有一个点,对应线性等式系统的解。