PuLP

3.3.0 · active · verified Thu Apr 09

PuLP is an LP (Linear Programming) modeler written in Python. It simplifies the creation of linear and mixed-integer programming optimization problems, allowing users to define problems using Pythonic syntax. It can generate MPS or LP files and call various open-source (like GLPK, COIN-OR CBC, HiGHS, SCIP) or proprietary (CPLEX, GUROBI, MOSEK, XPRESS) solvers to find optimal solutions. The current version is 3.3.0, and it is actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define and solve a simple integer linear programming problem using PuLP. It covers creating a problem, defining decision variables with bounds and categories, setting an objective function, adding constraints, and finally solving the problem to display the optimal solution and variable values.

from pulp import LpProblem, LpVariable, LpMinimize, lpSum, LpStatus, value

# 1. Create the problem variable, specifying minimization or maximization
prob = LpProblem("My Production Problem", LpMinimize)

# 2. Define decision variables
x = LpVariable("Product_A", lowBound=0, cat='Integer')
y = LpVariable("Product_B", lowBound=0, cat='Integer')

# 3. Define the objective function (e.g., minimize cost)
prob += 3 * x + 2 * y, "Total Cost"

# 4. Define constraints
prob += 2 * x + y >= 10, "Minimum Production"
prob += x + y <= 12, "Max Capacity"
prob += x >= 4, "Min Product A"

# 5. Solve the problem
status = prob.solve()

# 6. Print the results
print(f"Status: {LpStatus[status]}")
if LpStatus[status] == "Optimal":
    print(f"Optimal Total Cost: {value(prob.objective)}")
    print(f"Units of Product A: {value(x)}")
    print(f"Units of Product B: {value(y)}")
else:
    print("No optimal solution found.")

view raw JSON →