Persistent looping issue encountered when attempting to solve a linear programming problem using the PuLP library in Python

73 Views Asked by At

I have problem of solver linear problem. Here is the functions.

$$ x+y+z+u+v=6\\ 47\le16x+12y+7z+4u+3v\le48\\ 0\le x\le3\\ 0\le y\le4\\ 0\le z\le7\\ 0\le u\le12\\ 0\le v\le11\\ $$

I want to get all possible solutions to this problem.

Here's the code to solve this linear programming problem using PuLP library in Python

from pulp import *

# Create a LP minimization problem
prob = LpProblem("LP Problem", LpMinimize)

# Create variables x, y, z, u, and v
x = LpVariable("x", 0, 51, LpInteger)
y = LpVariable("y", 0, 106, LpInteger)
z = LpVariable("z", 0, 43, LpInteger)
u = LpVariable("u", 0, 16, LpInteger)
v = LpVariable("v", 0, 11, LpInteger)

# Objective function
prob += 0, "Arbitrary Objective Function"

# Constraints
prob += x + y + z + u + v == 6
prob += 16*x + 12*y + 7*z + 4*u + 3*v >= 47
prob += 16*x + 12*y + 7*z + 4*u + 3*v <= 48

solutions = []

# Loop until there are no more feasible solutions
while True:
    # Solve the problem
    prob.solve()

    # Check if a feasible solution was found
    if prob.status != LpStatusOptimal:
        break

    # Save the current solution
    solution = (int(value(x)), int(value(y)), int(value(z)), int(value(u)), int(value(v)))
    solutions.append(solution)

    # Add a constraint to exclude the current solution
    prob += (x != value(x)) | (y != value(y)) | (z != value(z)) | (u != value(u)) | (v != value(v))

# Print all found solutions
print("All Feasible Solutions:")
for solution in solutions:
    print(f"x = {solution[0]}, y = {solution[1]}, z = {solution[2]}, u = {solution[3]}, v = {solution[4]}")

My idea is to use a loop to repeatedly solve the problem and add additional constraints to exclude the previously found solutions.

However, this approach results in an infinite loop.

I am thus inquiring as to whether there may be an error in my approach, or if there is a more effective alternative.

Gratitude in advance for any assistance provided.

PS

I can use the code

solutions = []
for x in range(0, 4):
    for y in range(0, 5):
        for z in range(0, 8):
            for u in range(0, 13):
                for v in range(0, 12):
                    if 16*x + 12*y + 7*z + 4*u + 3*v >= 47 and 16*x + 12*y + 7*z + 4*u + 3*v <= 48 and x+y+z+u+v == 6:
                        solutions.append((x, y, z, u, v))
print(solutions)

But this is not my purpose. I want to try to use Pulp to solve it.