使用纸浆进行优化仅给出0作为结果
我正在编写一个代码,该代码在给定一组约束的情况下最大化了我的目标函数的值。它具有四个标记X1至X4的变量,具有两个等式约束和两个不等式约束。使用linprog解决给我适当的结果。但是,使用纸浆方法仅给我零作为结果。
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable
import numpy as np
# Create the model
model = LpProblem(name="optimize", sense=LpMaximize)
# Initialize the decision variables
x1 = LpVariable(name="x1", lowBound= 0, upBound = None, cat='Continuous')
x2 = LpVariable(name="x2", lowBound= 0, upBound = 5, cat='Continuous')
x3 = LpVariable(name="x3", lowBound=None, upBound = 0.5, cat='Continuous')
x4 = LpVariable(name="x4", lowBound=-3, upBound = None, cat='Continuous')
#Objective function of the model
obj_func = (29 * x1 + 45 * x2)
model += obj_func
# Add the constraints to the model
model += (x1 - x2 - 3 * x3 <= 5, "Constraint_1")
model += (2 * x1 - 3 * x2 -7 * x3 + 3 * x4 >= 10, "Constraint_2")
model += (2 * x1 + 8 * x2 + x3 == 60, "Constraint_3")
model += (4 * x1 + 4 * x2 + x4 == 60, "Constraint_4")
model
# Solve the problem
status = model.solve()
LpStatus[model.status]
model.variables()
for var in model.variables():
print(f"{var.name}: {var.value()}")
我可以看到lpstatus [model.status]
在说解决方案是未定义的。
相同的一组方程为我提供了linprog中的解决方案 [6.60059411,3.973669,-0.52664072,1.09008012]
I am writing a code which maximizes the value for my objective function given a set of constraints. It has four variables labeled x1 to x4, with two equality constraints and two inequality constraints. Solving with Linprog gives me a proper result. But using pulp method is only giving me zero as results.
from pulp import LpMaximize, LpProblem, LpStatus, lpSum, LpVariable
import numpy as np
# Create the model
model = LpProblem(name="optimize", sense=LpMaximize)
# Initialize the decision variables
x1 = LpVariable(name="x1", lowBound= 0, upBound = None, cat='Continuous')
x2 = LpVariable(name="x2", lowBound= 0, upBound = 5, cat='Continuous')
x3 = LpVariable(name="x3", lowBound=None, upBound = 0.5, cat='Continuous')
x4 = LpVariable(name="x4", lowBound=-3, upBound = None, cat='Continuous')
#Objective function of the model
obj_func = (29 * x1 + 45 * x2)
model += obj_func
# Add the constraints to the model
model += (x1 - x2 - 3 * x3 <= 5, "Constraint_1")
model += (2 * x1 - 3 * x2 -7 * x3 + 3 * x4 >= 10, "Constraint_2")
model += (2 * x1 + 8 * x2 + x3 == 60, "Constraint_3")
model += (4 * x1 + 4 * x2 + x4 == 60, "Constraint_4")
model
# Solve the problem
status = model.solve()
LpStatus[model.status]
model.variables()
for var in model.variables():
print(f"{var.name}: {var.value()}")
I can see that the LpStatus[model.status]
is saying that the solutions are Undefined.
Same set of equations gives me a solution in LinProg as
[ 6.60059411, 3.9736669 , -0.52664072, 1.09008012]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的解决方案无法满足第二个约束。查看:
2x6.60059411-3x3.973669-7x(-0.52664072) + 3x1.09008012 = 8.2369&lt; 10
Your solution does not satisfy the 2nd constraint. Check:
2x6.60059411 - 3x3.9736669 - 7x(-0.52664072) + 3x1.09008012 = 8.2369 < 10