Sympy based mathematical programming language
Optlang is a Python package for solving mathematical optimization problems, i.e. maximizing or minimizing an objective function over a set of variables subject to a number of constraints. Optlang provides a common interface to a series of optimization tools, so different solver backends can be changed in a transparent way. Optlang’s object-oriented API takes advantage of the symbolic math library sympy to allow objective functions and constraints to be easily formulated from symbolic expressions of variables (see examples).
Show us some love by staring this repo if you find optlang useful!
Also, please use the GitHub issue tracker to let us know about bugs or feature requests, or our gitter channel if you have problems or questions regarding optlang.
Installation
Install using pip
pip install optlang
This will also install swiglpk, an interface to the open source (mixed integer) LP solver GLPK. Quadratic programming (and MIQP) is supported through additional optional solvers (see below).
Dependencies
The following dependencies are needed.
The following are optional dependencies that allow other solvers to be used.
Example
Formulating and solving the problem is straightforward (example taken from GLPK documentation):
from optlang import Model, Variable, Constraint, Objective
# All the (symbolic) variables are declared, with a name and optionally a lower and/or upper bound.
x1 = Variable('x1', lb=0)
x2 = Variable('x2', lb=0)
x3 = Variable('x3', lb=0)
# A constraint is constructed from an expression of variables and a lower and/or upper bound (lb and ub).
c1 = Constraint(x1 + x2 + x3, ub=100)
c2 = Constraint(10 * x1 + 4 * x2 + 5 * x3, ub=600)
c3 = Constraint(2 * x1 + 2 * x2 + 6 * x3, ub=300)
# An objective can be formulated
obj = Objective(10 * x1 + 6 * x2 + 4 * x3, direction='max')
# Variables, constraints and objective are combined in a Model object, which can subsequently be optimized.
model = Model(name='Simple model')
model.objective = obj
model.add([c1, c2, c3])
status = model.optimize()
print("status:", model.status)
print("objective value:", model.objective.value)
print("----------")
for var_name, var in model.variables.iteritems():
print(var_name, "=", var.primal)
The example will produce the following output:
status: optimal objective value: 733.333333333 ---------- x2 = 66.6666666667 x3 = 0.0 x1 = 33.3333333333
Using a particular solver
If you have more than one solver installed, it’s also possible to specify which one to use, by importing directly from the
respective solver interface, e.g. from optlang.glpk_interface import Model, Variable, Constraint, Objective
Documentation
Documentation for optlang is provided at readthedocs.org.
Citation
Please cite if you use optlang in a scientific publication. In case you would like to reference a specific version of of optlang you can also include the respective Zenodo DOI (
points to the latest version).
Contributing
Please read CONTRIBUTING.md.
Funding
The development of optlang was partly support by the Novo Nordisk Foundation.
Future outlook
Mosek interface (provides academic licenses)
GAMS output (support non-linear problem formulation)
DEAP (support for heuristic optimization)
Interface to NEOS optimization server (for testing purposes and solver evaluation)
Automatically handle fractional and absolute value problems when dealing with LP/MILP/QP solvers (like GLPK, CPLEX etc.)
Metadata
Release files for optlang 1.9.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| optlang-1.9.1.tar.gz | 137.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| optlang-1.9.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 279.5 kB
Release files / optlang-1.9.1.tar.gz
| Download URL | optlang-1.9.1.tar.gz |
|---|---|
| Size | 137.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7807e14f9fa7183fca6b97769f5ab3f930048140324f3ba7e4a02febaf8c6178
|
|
BLAKE2b-256 checksum How to use checksums |
989ca9d91bf33b7a68f201f7c4b9686378dfbe04dde54f8b9b8056c01fd740bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 2, 2026.
Transparency logRelease files / optlang-1.9.1-py2.py3-none-any.whl
| Download URL | optlang-1.9.1-py2.py3-none-any.whl |
|---|---|
| Size | 142.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
f3375984ea74ae0c40bce111e86df419c12919c410ccfebf06a60b82dae4ac64
|
|
BLAKE2b-256 checksum How to use checksums |
64bed316545e9b62dc5a345ffdc1a04b620d4bb041d5640bce7c2a84b53e0321
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 2, 2026.
Transparency log