简体   繁体   中英

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. 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]

Your solution does not satisfy the 2nd constraint. Check: 2x6.60059411 - 3x3.9736669 - 7x(-0.52664072) + 3x1.09008012 = 8.2369 < 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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