High-level Linear Programming solver using the Simplex algorithm
Project description
No fuss Linear Programming solver
from pivotal import minimize, maximize, Variable
x = Variable("x")
y = Variable("y")
z = Variable("z")
objective = 2*x + y + 3*z
constraints = (
x - y == 4,
y + 2*z == 2
)
minimize(objective, constraints)
# -> value: 11.0
# -> variables: {'x': 4.0, 'y': 0.0, 'z': 1.0}
maximize(objective, constraints)
# -> value: 14.0
# -> variables: {'x': 6.0, 'y': 2.0, 'z': 0.0}
About
Pivotal
is not aiming to compete with commerical solvers like Gurobi. Rather, it is aiming to simplify the process of creating and solving linear programs thanks to its very simple and intuitive API. The solver itself uses a 2-phase Simplex algorithm.
Installation
Python >=3.10 is required.
Install via pip:
pip install pivotal-solver
API
Variables
Variable
instances implement __add__
, __sub__
and other magic methods, so you can use them directly in expressions such as 2*x + 10 - y
.
Here are some examples of what you can do with them:
x = Variable("x")
y = Variable("y")
z = Variable("z")
2*x + 10 - y
x + (y - z)*10
-x
-(x + y)
sum([x, y, z])
X = [Variable(f"x{i}") for i in range(5)]
sum(X)
Note that variables are considered equal if they have the same name, so for examples this expression:
Variable("x") + 2 + Variable("x")
will be treated as simply 2*x+2
.
The first argument to minimize
and maximize
is the objective function which must be either a single variable or a linear combination as in the example above.
Constraints
There are three supported constraints: ==
(equality), >=
(greater than or equal) and <=
(less than or equal). You create a constraint simply by using these comparisons in expressions involving Variable
instances. For example:
x = Variable("x")
y = Variable("y")
z = Variable("z")
x == 4
2*x - y == z + 7
y >= -x + 3*z
x <= 0
There is no need to convert your constraints to the canonical form which uses only equality constraints. This is done automatically by the solver.
minimize
and maximize
expect a list of constraints as the second argument.
Output
The return value of minimize
and maximize
is a 2-tuple containing the value of the objective function and a dictionary of variables and their values.
The functions may raise pivotal.Infeasible
if the program is over-constrained (no solution exists) or pivotal.Unbounded
if the program is under-constrained (the objective can be made arbitrarily small).
from pivotal import minimize, maximize, Variable, Infeasible
x = Variable("x")
y = Variable("y")
objective = 2*x + y
constraints = (
x + 2*y == 4,
x + y == 10
)
try:
minimize(objective, constraints)
except Infeasible:
print("No solution")
Iterations & Tolerance
minimize
and maximize
take two keyword arguments max_iterations
and tolerance
. max_iterations
(default math.inf
) controls the maximum number of iterations of the second phase of the Simplex algorithm. If the maximum number of iterations is reached a potentially non-optimal solution is returned. tolerance
(default 1e-6
) controls the precision of floating point comparisons, e.g. when comparing against zero. Instead of x == 0.0
, the algorithm considers a value to be zero when it is within the given tolerance: abs(x) <= tolerance
.
Limitations
- Currently, all variables are assumed to be positive
TODO (Contributions welcome)
- ✔️ Setting tolerance & max number of iterations
- (WIP) Arbitrary variable bounds, e.g.
a <= x <= b
- (WIP) Support for absolute values
- MILP solver with branch & bound
Development
Setting up
git clone https://github.com/tomasr8/pivotal.git
cd pivotal
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"
Running tests
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pivotal-solver-0.0.3.tar.gz
.
File metadata
- Download URL: pivotal-solver-0.0.3.tar.gz
- Upload date:
- Size: 3.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56bae3e432e4760b3aba560326ac05c1ff411ad1863e379df66f033bda542947 |
|
MD5 | 29ea78ef225e5cafafa21aacbc0d3973 |
|
BLAKE2b-256 | 22bc0baf0bbe177cc9f4b1745377d242c6743e158fea5c2c62afc84c49530476 |
File details
Details for the file pivotal_solver-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: pivotal_solver-0.0.3-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e9aeca5e6e490928d9a4d86f8fe4c6a297072aae76e525add514a06f08ab038 |
|
MD5 | 3bcf9df002c5d62a2f69da9f69d84a92 |
|
BLAKE2b-256 | f121c52ea432f722b71f7000ec1ea05a312c0167d233f8e14dc742ceb098ee7c |