Wrapper around Linear Programming (LP) solvers in Python, with a unified interface.
conda install -c conda-forge lpsolvers
To install the library and all available LP solvers at the same time:
pip install lpsolvers[open_source_solvers]
To install the library only, assuming LP solvers are installed separately: pip install lpsolvers
.
The function solve_lp
is called with the solver
keyword argument to select the backend solver. The linear program it solves is, in standard form:
Vector inequalities are taken coordinate by coordinate.
To solve a linear program, build the matrices that define it and call the solve_lp
function:
from numpy import array
from lpsolvers import solve_lp
c = array([1., 2., 3.])
G = array([[1., 2., -1.], [2., 0., 1.], [1., 2., 1.], [-1., -1., -1.]])
h = array([4., 1., 3., 2.])
x = solve_lp(c, G, h, solver="cvxopt") # select solver here
print(f"LP solution: {x=}")
This example outputs the solution [2.2, -0.8, -3.4]
.
The list of supported solvers currently includes: