Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Solving LPs in Python with pulp | Foundations of Optimization for Analytics
Optimization Methods for Data Analytics

bookSolving LPs in Python with pulp

Deslize para mostrar o menu

Linear programming (LP) problems frequently appear in analytics when you want to optimize an objective—such as minimizing costs or maximizing profit—while satisfying real-world constraints. Python’s PuLP library provides a simple and intuitive way to formulate and solve LPs without requiring any low-level solver knowledge.

What Is PuLP?

PuLP is a Python library that allows you to create optimization problems in a readable, model-oriented way. It automatically connects to solvers such as CBC (default), GLPK, or commercial ones if available.

Working with PuLP usually involves:

  • Defining a problem object
  • Declaring decision variables
  • Writing the objective function
  • Adding constraints
  • Solving the model and retrieving the results

Basic Structure of an LP in PuLP

Below is the general template of an LP model:

# 1. Create problem
problem = LpProblem("My_Problem", LpMinimize)  # or LpMaximize

# 2. Create variables
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)

# 3. Objective function
problem += 3*x + 5*y

# 4. Constraints
problem += x + 2*y <= 10
problem += 3*x + y <= 12

# 5. Solve
problem.solve()

Example: Resource Allocation Problem

Suppose a company produces two products using limited machine hours.

  • Product A requires 2 hours
  • Product B requires 3 hours
  • We have 40 total machine hours
  • Profit is 30 for A and $50 for B
  • Objective: maximize profit

Modeling the Problem

Let:

  • xx - number of units of Product A
  • yy - number of units of Product B

Constraints:

2x+3y402x + 3y \le 40

Objective:

max(30x+50y)\max(30x + 50y)
12345678910111213141516171819202122
from pulp import LpProblem, LpVariable, LpMaximize, value # Create optimization model model = LpProblem("Production_Optimization", LpMaximize) # Decision variables x = LpVariable("Product_A", lowBound=0) y = LpVariable("Product_B", lowBound=0) # Objective function model += 30 * x + 50 * y # Constraint: machine hours model += 2 * x + 3 * y <= 40 # Solve model.solve() print("Optimal Production Plan:") print("Product A:", value(x)) print("Product B:", value(y)) print("Max Profit:", value(model.objective))
copy

PuLP prints the optimal values:

  • How many units of each product should be produced
  • The maximum achievable profit
  • Which constraints are binding (tight) vs non-binding

The results give actionable insights: "Produce more of the item with higher marginal profit until the machine constraint becomes binding".

question mark

Which component in a PuLP model defines the quantity you want to minimize or maximize?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 3
some-alt