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_, and OR-Tools `CP-SAT`_ (via the ``CPSAT`` API). The documentation for PuLP can be `found here `_. PuLP is part of the `COIN-OR project `_. .. note:: **Upgrading from PuLP 3.x?** PuLP 4.0 has breaking changes: variables are created with ``prob.add_variable(...)``, ``prob.constraints()`` returns a list, and ``prob.solve()`` returns an ``LpSolveStats`` object instead of a status code. See the `guide to migrate from PuLP 3.x to 4.0 `_ (source: `doc/source/guides/how_to_migrate_to_v4.rst `_). .. important:: **CBC is not shipped inside the PuLP package.** Older releases bundled a CBC binary and exposed it as ``PULP_CBC_CMD``; that API and the bundled solver are **removed**. To solve models with CBC through PuLP you should: * install PuLP with the optional CBC extra: ``python -m pip install pulp[cbc]`` (this installs the `cbcbox `_ wheel, which provides a CBC executable PuLP can find automatically), **or** * install a CBC build yourself and ensure the ``cbc`` (or ``cbc.exe`` on Windows) executable is on your ``PATH``, then use the ``COIN_CMD`` solver (or call ``prob.solve()`` with no arguments when CBC is available; otherwise install another solver such as GLPK or pass an explicit solver). Without CBC or another available solver, the default solve path will raise ``PulpError: No solver available``. Installation ================ PuLP requires Python 3.12 or newer. **Recommended:** install with CBC support:: python -m pip install pulp[cbc] Plain ``python -m pip install pulp`` installs only the modeler; you must then supply your own CBC on ``PATH`` or another solver. Otherwise follow the download instructions on the `PyPi page `_. Installing solvers ---------------------- PuLP can use a variety of solvers. When CBC is available (via ``pulp[cbc]`` or ``cbc`` on ``PATH``), ``COIN_CMD`` is the usual open-source MIP/LP choice and is selected as the default ahead of GLPK. PuLP can also install other solvers via optional PyPI extras (some require a commercial license for running or for large models):: python -m pip install pulp[gurobi] python -m pip install pulp[cplex] python -m pip install pulp[xpress] python -m pip install pulp[scip] python -m pip install pulp[highs] python -m pip install pulp[copt] python -m pip install pulp[mosek] python -m pip install pulp[ortools] python -m pip install pulp[cylp] python -m pip install pulp[cbc] python -m pip install pulp[glpk] If you want to install all open source solvers (scip, highs, cbc, glpk), you can use the shortcut:: python -m pip install pulp[open_py] For more information on how to install solvers, see the `guide on configuring solvers `_. Quickstart =============== Use ``LpProblem`` to create a problem, then add variables with ``add_variable``. Create a problem called "myProblem" and a variable x with 0 ≤ x ≤ 3:: from pulp import * prob = LpProblem("myProblem", LpMinimize) x = prob.add_variable("x", 0, 3) To create a binary variable y (values 0 or 1):: y = prob.add_variable("y", cat="Binary") Combine variables to create expressions and constraints and 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 solver (CBC when installed via ``pulp[cbc]`` or ``cbc`` on ``PATH``, otherwise another available backend):: stats = prob.solve() If you want to try another solver to solve the problem:: stats = prob.solve(GLPK(msg = 0)) ``GLPK`` uses GLPK's command-line tool (``glpsol``) and requires it to be on your ``PATH``. To use GLPK's python API instead, install ``python -m pip install pulp[glpk]`` and use ``PYGLPK``:: status = prob.solve(PYGLPK(msg = 0)) To use the OR-Tools CP-SAT solver (install with ``python -m pip install pulp[ortools]``). Every variable must have finite lower and upper bounds; continuous variables are solved on their integer-rounded domain:: from pulp import CPSAT stats = prob.solve(CPSAT(msg=False)) Display the status of the solution:: stats.status_str > 'Optimal' You can get the value of the variables using ``value``. ex:: value(x) > 2.0 Essential Classes ------------------ These types form the usual modelling workflow: create an ``LpProblem``, attach ``LpVariable`` instances with ``add_variable``, build linear expressions (often with ``lpSum`` / ``lpDot`` / ``lpSum_vars`` / ``lpSum_vars_coefs``), combine them into ``LpConstraint`` rows, and call ``solve``. * ``LpProblem`` -- Container for an LP or MIP: variables, objective, constraints, and solve API * ``LpVariable`` -- A decision variable belonging to one problem; used inside expressions and constraints * ``LpAffineExpression`` -- A linear combination of variables and a constant (objectives, constraint bodies, intermediate terms) * ``LpConstraint`` -- A single row relating an affine expression to a bound with ``<=``, ``=``, or ``>=``: a1x1 + a2x2 + ... + anxn (<=, =, >=) b Useful Functions ------------------ * ``value()`` -- Finds the value of a variable or expression * ``lpSum()`` -- Given a list of the form [a1*x1, a2*x2, ..., 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 * ``lpSum_vars()`` -- Sum of variables with coefficient 1 each (batch construction) * ``lpSum_vars_coefs()`` -- Sum of ``coeff * var`` pairs from an iterable of ``(variable, coefficient)`` (batch construction) More Examples ================ Several tutorial are given in `documentation `_ and pure code examples are available in `examples/ directory `_ . The examples assume CBC is available (for example after ``pip install pulp[cbc]``). To use other solvers they must be available (installed and accessible). For more information, see the `guide on configuring solvers `_. For Developers ================ If you want to install the latest version from GitHub you can run:: python -m pip install -U "pulp[cbc] @ git+https://github.com/coin-or/pulp.git" Building from source -------------------------- This version of PuLP includes a Rust extension (``pulp._rustcore``) that provides the core model, variables, constraints, and expressions. The build uses `maturin `_ and requires a Rust toolchain in addition to Python. **Requirements** * **Python** 3.12 or newer * **Rust** (latest stable). Install from https://rustup.rs/ * **uv** (recommended for install and dev). See the `uv documentation `_ for installation. * **OS**: Windows, macOS (x86_64, arm64), or Linux (x86_64, arm64). The Rust extension is built for the host platform. **Build steps** From the PuLP root directory, create a virtual environment and install the package in editable mode with dev dependencies:: uv venv uv pip install --group dev -e .[cbc] Or with plain pip (maturin will be used automatically by the build backend):: python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate python -m pip install --upgrade pip python -m pip install -e ".[cbc]" **Running tests** :: uv run python -m unittest discover -s pulp/tests -v Building the documentation -------------------------- The PuLP documentation is built with `Sphinx `_. Use a virtual environment and the dev install above, then:: cd doc make html A folder named ``html`` will be created inside ``doc/build/``. Open ``doc/build/html/index.html`` in a browser. Contributing to PuLP ----------------------- Instructions for making your first contribution to PuLP are given `here `_. **Comments, bug reports, patches and suggestions are very welcome!** * Comments and suggestions: https://github.com/coin-or/pulp/discussions * `PuLP Google Group `_ — community discussion and Q&A * Bug reports: https://github.com/coin-or/pulp/issues * Patches: https://github.com/coin-or/pulp/pulls 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 .. _CP-SAT: https://developers.google.com/optimization/cp/cp_solver