Skip to main content

Formulate optimization problems using sympy expressions and solve them using interfaces to third-party optimization software (e.g. GLPK).

Project description

Sympy based mathematical programming language

Current PyPI Version Supported Python Versions Apache Software License Version 2.0 Code of Conduct GitHub Actions Codecov Documentation Status Join the chat at https://gitter.im/biosustain/optlang Publication Zenodo Source Code

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 Publication 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 (Zenodo Source Code 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.)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optlang-1.9.0.tar.gz (137.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optlang-1.9.0-py2.py3-none-any.whl (141.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file optlang-1.9.0.tar.gz.

File metadata

  • Download URL: optlang-1.9.0.tar.gz
  • Upload date:
  • Size: 137.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for optlang-1.9.0.tar.gz
Algorithm Hash digest
SHA256 2d974debdff89a8f30af3f25e23782e3440cb125f88a668981f9a6afcd5a7c4d
MD5 3fe2b033687e38c695c3e20240ed3597
BLAKE2b-256 a55a30ba05f5184d850bad2c2de4c13ef375b372a04e29b5fa9a8a35281d4705

See more details on using hashes here.

Provenance

The following attestation bundles were made for optlang-1.9.0.tar.gz:

Publisher: release.yml on opencobra/optlang

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file optlang-1.9.0-py2.py3-none-any.whl.

File metadata

  • Download URL: optlang-1.9.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 141.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for optlang-1.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 74aaf820dcbead6fdd09e3f1a73a5271ad7d167966e96ff526fbf4320bb13d79
MD5 bad890612b2eb829108a5e5a138db7f2
BLAKE2b-256 729ce71a75d9dc10670075323b38a899aa0652dc6703ed06ff60d54ddd2f4a5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for optlang-1.9.0-py2.py3-none-any.whl:

Publisher: release.yml on opencobra/optlang

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page