pivotal-solver


Namepivotal-solver JSON
Version 0.0.3 PyPI version JSON
download
home_page
SummaryHigh-level Linear Programming solver using the Simplex algorithm
upload_time2023-07-06 19:36:30
maintainer
docs_urlNone
author
requires_python>=3.10
licenseCopyright (c) Tomas Roun Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords linear programming linear programming solver lp solver lp solver linprog numerical optimization convex optimization simplex simplex algorithm
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
    <img src="https://raw.githubusercontent.com/tomasr8/pivotal/master/logo.svg">
</p>

# No fuss Linear Programming solver

```python
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:

```bash
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:

```python
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:

```python
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:

```python
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).

```python
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

```python
git clone https://github.com/tomasr8/pivotal.git
cd pivotal
python -m venv venv
source venv/bin/activate
pip install -e ".[dev]"
```

### Running tests

```python
pytest
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pivotal-solver",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "linear programming,linear programming solver,lp solver,lp,solver,linprog,numerical optimization,convex optimization,simplex,simplex algorithm",
    "author": "",
    "author_email": "Tomas Roun <tomas.roun8@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/22/bc/0baf0bbe177cc9f4b1745377d242c6743e158fea5c2c62afc84c49530476/pivotal-solver-0.0.3.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n    <img src=\"https://raw.githubusercontent.com/tomasr8/pivotal/master/logo.svg\">\n</p>\n\n# No fuss Linear Programming solver\n\n```python\nfrom pivotal import minimize, maximize, Variable\n\nx = Variable(\"x\")\ny = Variable(\"y\")\nz = Variable(\"z\")\n\nobjective = 2*x + y + 3*z\nconstraints = (\n    x - y == 4,\n    y + 2*z == 2\n)\n\nminimize(objective, constraints)\n# -> value: 11.0\n# -> variables: {'x': 4.0, 'y': 0.0, 'z': 1.0}\n\nmaximize(objective, constraints)\n# -> value: 14.0\n# -> variables: {'x': 6.0, 'y': 2.0, 'z': 0.0}\n```\n\n## About\n\n`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.\n\n## Installation\n\nPython >=3.10 is required.\n\nInstall via pip:\n\n```bash\npip install pivotal-solver\n```\n\n## API\n\n### Variables\n\n`Variable` instances implement `__add__`, `__sub__` and other magic methods, so you can use them directly in expressions such as `2*x + 10 - y`.\n\nHere are some examples of what you can do with them:\n\n```python\nx = Variable(\"x\")\ny = Variable(\"y\")\nz = Variable(\"z\")\n\n2*x + 10 - y\nx + (y - z)*10\n-x\n-(x + y)\nsum([x, y, z])\n\nX = [Variable(f\"x{i}\") for i in range(5)]\nsum(X)\n```\n\nNote that variables are considered equal if they have the same name, so\nfor examples this expression:\n\n```python\nVariable(\"x\") + 2 + Variable(\"x\")\n```\n\nwill be treated as simply `2*x+2`.\n\nThe 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.\n\n### Constraints\n\nThere 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:\n\n```python\nx = Variable(\"x\")\ny = Variable(\"y\")\nz = Variable(\"z\")\n\nx == 4\n2*x - y == z + 7\ny >= -x + 3*z\nx <= 0\n```\n\nThere is no need to convert your constraints to the canonical form which uses only equality constraints. This is done automatically by the solver.\n\n`minimize` and `maximize` expect a list of constraints as the second argument.\n\n### Output\n\nThe 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.\n\nThe 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).\n\n```python\nfrom pivotal import minimize, maximize, Variable, Infeasible\n\nx = Variable(\"x\")\ny = Variable(\"y\")\n\nobjective = 2*x + y\nconstraints = (\n    x + 2*y == 4,\n    x + y == 10\n)\n\ntry:\n    minimize(objective, constraints)\nexcept Infeasible:\n    print(\"No solution\")\n```\n\n### Iterations & Tolerance\n\n`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`.\n\n## Limitations\n\n- Currently, all variables are assumed to be positive\n\n## TODO (Contributions welcome)\n\n- \u2714\ufe0f Setting tolerance & max number of iterations\n- (WIP) Arbitrary variable bounds, e.g. `a <= x <= b`\n- (WIP) Support for absolute values\n- MILP solver with branch & bound\n\n\n## Development\n\n### Setting up\n\n```python\ngit clone https://github.com/tomasr8/pivotal.git\ncd pivotal\npython -m venv venv\nsource venv/bin/activate\npip install -e \".[dev]\"\n```\n\n### Running tests\n\n```python\npytest\n```\n",
    "bugtrack_url": null,
    "license": "Copyright (c) Tomas Roun  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "High-level Linear Programming solver using the Simplex algorithm",
    "version": "0.0.3",
    "project_urls": {
        "Github": "https://github.com/tomasr8/pivotal",
        "Homepage": "https://github.com/tomasr8/pivotal"
    },
    "split_keywords": [
        "linear programming",
        "linear programming solver",
        "lp solver",
        "lp",
        "solver",
        "linprog",
        "numerical optimization",
        "convex optimization",
        "simplex",
        "simplex algorithm"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f121c52ea432f722b71f7000ec1ea05a312c0167d233f8e14dc742ceb098ee7c",
                "md5": "3bcf9df002c5d62a2f69da9f69d84a92",
                "sha256": "0e9aeca5e6e490928d9a4d86f8fe4c6a297072aae76e525add514a06f08ab038"
            },
            "downloads": -1,
            "filename": "pivotal_solver-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3bcf9df002c5d62a2f69da9f69d84a92",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 9280,
            "upload_time": "2023-07-06T19:36:29",
            "upload_time_iso_8601": "2023-07-06T19:36:29.050552Z",
            "url": "https://files.pythonhosted.org/packages/f1/21/c52ea432f722b71f7000ec1ea05a312c0167d233f8e14dc742ceb098ee7c/pivotal_solver-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "22bc0baf0bbe177cc9f4b1745377d242c6743e158fea5c2c62afc84c49530476",
                "md5": "29ea78ef225e5cafafa21aacbc0d3973",
                "sha256": "56bae3e432e4760b3aba560326ac05c1ff411ad1863e379df66f033bda542947"
            },
            "downloads": -1,
            "filename": "pivotal-solver-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "29ea78ef225e5cafafa21aacbc0d3973",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 3224277,
            "upload_time": "2023-07-06T19:36:30",
            "upload_time_iso_8601": "2023-07-06T19:36:30.602693Z",
            "url": "https://files.pythonhosted.org/packages/22/bc/0baf0bbe177cc9f4b1745377d242c6743e158fea5c2c62afc84c49530476/pivotal-solver-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-06 19:36:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tomasr8",
    "github_project": "pivotal",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pivotal-solver"
}
        
Elapsed time: 0.10893s