简体   繁体   English

使用 Pulp 进行优化,只给出 0 作为结果

[英]Using Pulp for optimization giving only 0 as results

I am writing a code which maximizes the value for my objective function given a set of constraints.我正在编写一个代码,在给定一组约束的情况下最大化我的目标 function 的价值。 It has four variables labeled x1 to x4, with two equality constraints and two inequality constraints.它有四个标记为 x1 到 x4 的变量,有两个等式约束和两个不等式约束。 Solving with Linprog gives me a proper result.用 Linprog 求解给了我一个正确的结果。 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.我可以看到LpStatus[model.status]说解决方案是未定义的。

Same set of equations gives me a solution in LinProg as [ 6.60059411, 3.9736669, -0.52664072, 1.09008012]相同的方程组在 LinProg 中为我提供了一个解 [ 6.60059411, 3.9736669, -0.52664072, 1.09008012]

Your solution does not satisfy the 2nd constraint.您的解决方案不满足第二个约束。 Check: 2x6.60059411 - 3x3.9736669 - 7x(-0.52664072) + 3x1.09008012 = 8.2369 < 10检查:2x6.60059411 - 3x3.9736669 - 7x(-0.52664072) + 3x1.09008012 = 8.2369 < 10

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

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