【问题标题】:Using Pulp for optimization giving only 0 as results使用 Pulp 进行优化,只给出 0 作为结果
【发布时间】:2022-08-19 02:37:23
【问题描述】:

我正在编写一个代码,在给定一组约束的情况下最大化我的目标函数的值。它有四个标记为 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.9736669,-0.52664072,1.09008012]

  • 你的问题是不可行的。我猜你从其他方法得到的值是无效的。

标签: python optimization model pulp


【解决方案1】:

您的解决方案不满足第二个约束。查看: 2x6.60059411 - 3x3.9736669 - 7x(-0.52664072) + 3x1.09008012 = 8.2369 < 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多