Convert Figma logo to code with AI

coin-or logopulp

A python Linear Programming API

2,080
383
2,080
98

Top Related Projects

11,147

Google's Operations Research tools:

5,370

A Python-embedded modeling language for convex optimization problems.

12,986

SciPy library main repository

2,000

An object-oriented algebraic modeling language in Python for structured optimization problems.

2,230

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)

Quick Overview

PuLP is an open-source linear programming modeler written in Python. It allows users to describe optimization problems using Python objects and then solve these problems using a variety of solvers. PuLP is designed to be intuitive and easy to use, making it accessible for both beginners and experienced optimization practitioners.

Pros

  • Easy to learn and use, with a Pythonic interface
  • Supports multiple solvers, including open-source and commercial options
  • Handles both linear and integer programming problems
  • Actively maintained with good documentation and community support

Cons

  • Performance may be slower compared to specialized commercial solvers
  • Limited to linear and integer programming (no support for nonlinear optimization)
  • Dependency on external solvers for solving problems
  • May require additional setup for some solvers

Code Examples

  1. Creating a simple linear programming problem:
from pulp import *

# Create the model
model = LpProblem(name="maximize-production", sense=LpMaximize)

# Define variables
x = LpVariable(name="x", lowBound=0)
y = LpVariable(name="y", lowBound=0)

# Add constraints
model += (2 * x + y <= 100, "material_constraint")
model += (x + y <= 80, "labor_constraint")

# Set objective
model += 3 * x + 2 * y

# Solve the problem
model.solve()

# Print the results
print(f"Optimal solution: x = {x.value()}, y = {y.value()}")
print(f"Objective value: {model.objective.value()}")
  1. Solving a binary knapsack problem:
from pulp import *

# Define problem data
items = ["A", "B", "C", "D"]
values = {"A": 6, "B": 4, "C": 5, "D": 7}
weights = {"A": 3, "B": 2, "C": 4, "D": 5}
capacity = 10

# Create the model
model = LpProblem(name="knapsack-problem", sense=LpMaximize)

# Define variables
x = LpVariable.dicts("item", items, cat="Binary")

# Add constraint
model += lpSum(weights[i] * x[i] for i in items) <= capacity

# Set objective
model += lpSum(values[i] * x[i] for i in items)

# Solve the problem
model.solve()

# Print results
print("Selected items:")
for i in items:
    if x[i].value() == 1:
        print(f"- {i}")
print(f"Total value: {model.objective.value()}")
  1. Solving a transportation problem:
from pulp import *

# Define problem data
sources = ["A", "B"]
destinations = ["1", "2", "3"]
costs = {("A", "1"): 2, ("A", "2"): 3, ("A", "3"): 4,
         ("B", "1"): 3, ("B", "2"): 2, ("B", "3"): 1}
supply = {"A": 50, "B": 70}
demand = {"1": 30, "2": 40, "3": 50}

# Create the model
model = LpProblem(name="transportation-problem", sense=LpMinimize)

# Define variables
x = LpVariable.dicts("route", costs, lowBound=0)

# Add constraints
for s in sources:
    model += lpSum(x[s, d] for d in destinations) <= supply[s]
for d in destinations:
    model += lpSum(x[s, d] for s in sources) >= demand[d]

# Set objective
model += lpSum(costs[route] * x[route] for route in costs)

# Solve the problem
model.solve()

# Print results
print("Optimal routes:")
for route in costs:
    if x[route].value() > 0:
        print(f"{route}: {x[route].value()}")
print(f"Total cost

Competitor Comparisons

11,147

Google's Operations Research tools:

Pros of OR-Tools

  • More comprehensive optimization toolkit with support for various problem types
  • Better performance for large-scale optimization problems
  • Extensive documentation and examples

Cons of OR-Tools

  • Steeper learning curve due to its complexity
  • Requires compilation and installation of C++ libraries

Code Comparison

PuLP example:

from pulp import *
x = LpVariable("x", 0, 3)
y = LpVariable("y", 0, 1)
prob = LpProblem("myProblem", LpMaximize)
prob += x + y <= 2
prob += -4*x + y

OR-Tools example:

from ortools.linear_solver import pywraplp
solver = pywraplp.Solver.CreateSolver('GLOP')
x = solver.NumVar(0, 3, 'x')
y = solver.NumVar(0, 1, 'y')
solver.Add(x + y <= 2)
solver.Maximize(-4 * x + y)

Both PuLP and OR-Tools are powerful optimization libraries, but they cater to different needs. PuLP is more user-friendly and easier to get started with, making it suitable for simpler optimization problems and educational purposes. OR-Tools, on the other hand, offers a wider range of optimization capabilities and better performance for complex problems, but comes with a steeper learning curve and more complex setup process.

5,370

A Python-embedded modeling language for convex optimization problems.

Pros of CVXPY

  • More powerful and flexible, supporting a wider range of optimization problems
  • Better suited for advanced mathematical modeling and research applications
  • Integrates well with other scientific Python libraries like NumPy and SciPy

Cons of CVXPY

  • Steeper learning curve, especially for beginners in optimization
  • May be overkill for simpler linear programming problems
  • Potentially slower execution for basic linear programming tasks

Code Comparison

PuLP example:

from pulp import *
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
prob = LpProblem("MyProblem", LpMaximize)
prob += x + y <= 5
prob += x + 2*y <= 6
prob += x + y
prob.solve()

CVXPY example:

import cvxpy as cp
x = cp.Variable()
y = cp.Variable()
constraints = [x >= 0, y >= 0, x + y <= 5, x + 2*y <= 6]
objective = cp.Maximize(x + y)
prob = cp.Problem(objective, constraints)
prob.solve()

Both PuLP and CVXPY are popular optimization libraries for Python, but they cater to different user needs and problem complexities. PuLP is generally easier to use for beginners and more efficient for simple linear programming problems, while CVXPY offers more advanced features and flexibility for complex optimization tasks.

12,986

SciPy library main repository

Pros of SciPy

  • Broader scope, covering a wide range of scientific computing tasks
  • More extensive documentation and community support
  • Highly optimized and efficient implementations of numerical algorithms

Cons of SciPy

  • Steeper learning curve due to its extensive functionality
  • May be overkill for simple linear programming tasks
  • Requires additional dependencies for certain functionalities

Code Comparison

PuLP example:

from pulp import *
prob = LpProblem("Simple LP Problem", LpMaximize)
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
prob += x + y <= 2
prob += -4*x + y

SciPy example:

from scipy.optimize import linprog
c = [-1, -1]
A = [[1, 1]]
b = [2]
res = linprog(c, A_ub=A, b_ub=b, method="simplex")

PuLP is more intuitive for linear programming problems, while SciPy's linprog function requires a different approach to problem formulation. SciPy offers a broader range of optimization methods but may be less straightforward for simple LP problems compared to PuLP's declarative style.

2,000

An object-oriented algebraic modeling language in Python for structured optimization problems.

Pros of Pyomo

  • More extensive modeling capabilities, including support for nonlinear and discrete optimization
  • Ability to handle larger and more complex problems
  • Greater flexibility in model formulation and solver selection

Cons of Pyomo

  • Steeper learning curve and more complex syntax
  • Slower model construction for simpler problems
  • Requires separate installation of solvers

Code Comparison

Pyomo example:

from pyomo.environ import *

model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
model.obj = Objective(expr=model.x + 2*model.y, sense=maximize)
model.con = Constraint(expr=3*model.x + 4*model.y <= 12)

PuLP example:

from pulp import *

prob = LpProblem("MyProblem", LpMaximize)
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
prob += x + 2*y
prob += 3*x + 4*y <= 12

Both Pyomo and PuLP are powerful optimization modeling tools, but they cater to different user needs. Pyomo offers more advanced features and flexibility, making it suitable for complex problems, while PuLP provides a simpler interface for straightforward linear programming tasks.

2,230

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)

Pros of JuMP.jl

  • Higher performance due to Julia's speed and JIT compilation
  • More expressive modeling language with support for complex constraints
  • Better integration with Julia's ecosystem for scientific computing

Cons of JuMP.jl

  • Steeper learning curve, especially for those unfamiliar with Julia
  • Smaller community and fewer resources compared to PuLP's Python ecosystem
  • Less mature and potentially less stable than PuLP

Code Comparison

PuLP example:

from pulp import *
x = LpVariable("x", 0, 3)
y = LpVariable("y", 0, 1)
prob = LpProblem("myProblem", LpMinimize)
prob += x + y <= 2
prob += -4*x + y

JuMP.jl example:

using JuMP, GLPK
model = Model(GLPK.Optimizer)
@variable(model, 0 <= x <= 3)
@variable(model, 0 <= y <= 1)
@constraint(model, x + y <= 2)
@objective(model, Min, -4x + y)

Both PuLP and JuMP.jl are powerful optimization modeling languages, but they cater to different ecosystems and user preferences. PuLP is more accessible for Python users, while JuMP.jl offers higher performance and tighter integration with Julia's scientific computing capabilities.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

pulp


.. image:: https://travis-ci.org/coin-or/pulp.svg?branch=master :target: https://travis-ci.org/coin-or/pulp .. image:: https://img.shields.io/pypi/v/pulp :target: https://pypi.org/project/PuLP/ :alt: PyPI .. image:: https://img.shields.io/pypi/dm/pulp :target: https://pypi.org/project/PuLP/ :alt: PyPI - Downloads

PuLP is an linear and mixed integer programming modeler written in Python. With PuLP, it is simple to create MILP optimisation problems and solve them with the latest open-source (or proprietary) solvers. PuLP can generate MPS or LP files and call solvers such as GLPK_, COIN-OR CLP/CBC, CPLEX, GUROBI_, MOSEK_, XPRESS_, CHOCO_, MIPCL_, HiGHS_, SCIP_/FSCIP_.

The documentation for PuLP can be found here <https://coin-or.github.io/pulp/>_.

PuLP is part of the COIN-OR project <https://www.coin-or.org/>_.

Installation

PuLP requires Python 3.7 or newer.

The easiest way to install PuLP is with pip. If pip is available on your system, type::

 python -m pip install pulp

Otherwise follow the download instructions on the PyPi page <https://pypi.python.org/pypi/PuLP>_.

Quickstart

Use LpVariable to create new variables. To create a variable x with 0 ≤ x ≤ 3::

 from pulp import *
 x = LpVariable("x", 0, 3)

To create a binary variable, y, with values either 0 or 1::

 y = LpVariable("y", cat="Binary")

Use LpProblem to create new problems. Create a problem called "myProblem" like so::

 prob = LpProblem("myProblem", LpMinimize)

Combine variables in order to create expressions and constraints, and then add them to the problem.::

 prob += x + y <= 2

An expression is a constraint without a right-hand side (RHS) sense (one of =, <= or >=). If you add an expression to a problem, it will become the objective::

 prob += -4*x + y

To solve the problem with the default included solver::

 status = prob.solve()

If you want to try another solver to solve the problem::

 status = prob.solve(GLPK(msg = 0))

Display the status of the solution::

 LpStatus[status]
 > 'Optimal'

You can get the value of the variables using value. ex::

 value(x)
 > 2.0

Essential Classes

  • LpProblem -- Container class for a Linear or Integer programming problem

  • LpVariable -- Variables that are added into constraints in the LP problem

  • LpConstraint -- Constraints of the general form

    a1x1 + a2x2 + ... + anxn (<=, =, >=) b
    
  • LpConstraintVar -- A special type of constraint for constructing column of the model in column-wise modelling

Useful Functions

  • value() -- Finds the value of a variable or expression
  • lpSum() -- Given a list of the form [a1x1, a2x2, ..., an*xn] will construct a linear expression to be used as a constraint or variable
  • lpDot() -- Given two lists of the form [a1, a2, ..., an] and [x1, x2, ..., xn] will construct a linear expression to be used as a constraint or variable

More Examples

Several tutorial are given in documentation <https://coin-or.github.io/pulp/CaseStudies/index.html>_ and pure code examples are available in examples/ directory <https://github.com/coin-or/pulp/tree/master/examples>_ .

The examples use the default solver (CBC). To use other solvers they must be available (installed and accessible). For more information on how to do that, see the guide on configuring solvers <https://coin-or.github.io/pulp/guides/how_to_configure_solvers.html>_.

For Developers

If you want to install the latest version from GitHub you can run::

python -m pip install -U git+https://github.com/coin-or/pulp

On Linux and MacOS systems, you must run the tests to make the default solver executable::

 sudo pulptest

Building the documentation

The PuLP documentation is built with Sphinx <https://www.sphinx-doc.org>. We recommended using a virtual environment <https://docs.python.org/3/library/venv.html> to build the documentation locally.

To build, run the following in a terminal window, in the PuLP root directory

::

cd pulp
python -m pip install -r requirements-dev.txt
cd doc
make html

A folder named html will be created inside the build/ directory. The home page for the documentation is doc/build/html/index.html which can be opened in a browser.

Contributing to PuLP

Instructions for making your first contribution to PuLP are given here <https://coin-or.github.io/pulp/develop/contribute.html>_.

Comments, bug reports, patches and suggestions are very welcome!

Copyright and License

PuLP is distributed under an MIT license.

 Copyright J.S. Roy, 2003-2005
 Copyright Stuart A. Mitchell
 See the LICENSE file for copyright information.

.. _Python: http://www.python.org/

.. _GLPK: http://www.gnu.org/software/glpk/glpk.html .. _CBC: https://github.com/coin-or/Cbc .. _CPLEX: http://www.cplex.com/ .. _GUROBI: http://www.gurobi.com/ .. _MOSEK: https://www.mosek.com/ .. _XPRESS: https://www.fico.com/es/products/fico-xpress-solver .. _CHOCO: https://choco-solver.org/ .. _MIPCL: http://mipcl-cpp.appspot.com/ .. _SCIP: https://www.scipopt.org/ .. _HiGHS: https://highs.dev .. _FSCIP: https://ug.zib.de