Name | cvxpy-gurobi JSON |
Version |
1.0.0
JSON |
| download |
home_page | None |
Summary | Translate CVXPY problems into gurobipy models |
upload_time | 2024-09-28 11:34:46 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | None |
keywords |
cvxpy
gurobi
gurobipy
optimization
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# CVXPY x GUROBI
This small library provides an alternative way to solve CVXPY problems with Gurobi.
## Usage
The library provides a solver that will translate a CVXPY `Problem` into a `gurobipy.Model`,
and optimize using Gurobi:
```python
import cvxpy as cp
import cvxpy_gurobi
problem = cp.Problem(cp.Maximize(cp.Variable(name="x", nonpos=True)))
cvxpy_gurobi.solve(problem)
assert problem.value == 0
```
The solver can also be registered with CVXPY and used as any other solver:
```python
import cvxpy as cp
from cvxpy_gurobi import GUROBI_TRANSLATION, solver
cvxpy_gurobi.register_solver()
# ^ this is the same as:
cp.Problem.register_solve_method(GUROBI_TRANSLATION, solver())
problem.solve(method=GUROBI_TRANSLATION)
```
This solver is a simple wrapper for the most common use case:
```python
from cvxpy_gurobi import build_model, backfill_problem
model = build_model(problem)
model.optimize()
backfill_problem(problem, model)
assert model.optVal == problem.value
```
The `build_model` function provided by this library translates the `cvxpy.Problem` instance
into an equivalent `gurobipy.Model`, and `backfill_problem` sets the optimal
values on the original problem.
> [!NOTE]
> Both functions must be used together as they rely on naming conventions to map variables and constraints between CVXPY and Gurobi.
The output of the `build_model` function is a standard `gurobipy.Model` instance,
which can be further customized prior to solving. This approach enables you to
manage how the model will be optimized.
## Installation
```sh
pip install cvxpy-gurobi
```
## CVXPY has an interface to Gurobi, why is this needed?
When using CVXPY's interface to Gurobi,
the problems fed to Gurobi have been pre-compiled by CVXPY,
meaning the model is not exactly the same as the one you have written.
This is great for solvers with low-level APIs, such as SCS or OSQP,
but `gurobipy` allows you to express your models at a higher-level.
Providing the raw model to Gurobi is a better idea in general since
the Gurobi solver is able to compile the problem with a better accuracy.
The chosen algorithm can also be different depending on the way it is modelled,
potentially leading to better performance.
In addition, CVXPY does not give access to the model before solving it.
CVXPY must therefore make some choices for you,
such as setting `QCPDual` to 1 on all non-MIP models.
Having access to the model can help
if you want to handle the call to `.optimize()` in a non-standard way,
e.g. by sending it to an async loop.
### Example
Consider this QP problem:
```python
import cvxpy as cp
x = cp.Variable(name="x")
problem = cp.Problem(cp.Minimize((x-1) ** 2))
```
The problem will be sent to Gurobi as (in LP format):
```
Minimize
[ 2 C0 ^2 ] / 2
Subject To
R0: - C0 + C1 = 1
Bounds
C0 free
C1 free
End
```
Using this package, it will instead send:
```
Minimize
- 2 x + Constant + [ 2 x ^2 ] / 2
Subject To
Bounds
x free
Constant = 1
End
```
Note that:
* the variable's name matches the user-defined problem;
* no extra (free) variables;
* no extra constraints.
## Why not use `gurobipy` directly?
CVXPY has 2 main features: a modelling API and interfaces to many solvers. The modelling API has a great design, whereas `gurobipy` feels like a thin layer over the C API. The interfaces to other solvers can be useful to not have to rewrite the problem when switching solvers.
# Supported versions
All supported versions of Python, CVXPY and `gurobipy` should work.
However, due to licensing restrictions, old versions of
`gurobipy` cannot be tested in CI.
If you run into a bug, please open an issue in this repo specifying
the versions used.
# Contributing
It is *highly recommended* to use [Hatch](https://hatch.pypa.io/latest/) for development.
It will handle all virtual
environment management.
To lint and format the code, run:
```sh
hatch fmt
```
For testing, run:
```sh
hatch test
```
This will test the latest version of dependencies. You can also run `hatch test --all`
to test several combinations of the supported version range.
Make sure any change is tested through a snapshot test. To add a new test case,
build a simple CVXPY problem in `tests/test_problems.py` in the appropriate category,
then run:
```sh
hatch run update-snapshots
```
You can then check the output in the `tests/snapshots` folder is as expected.
Raw data
{
"_id": null,
"home_page": null,
"name": "cvxpy-gurobi",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "cvxpy, gurobi, gurobipy, optimization",
"author": null,
"author_email": "Jonathan Berthias <jvberthias@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/45/d1/7f9227a608207cb2b937d18f620a945db18bf8b117ac2495145d743cf7e4/cvxpy_gurobi-1.0.0.tar.gz",
"platform": null,
"description": "# CVXPY x GUROBI\n\nThis small library provides an alternative way to solve CVXPY problems with Gurobi.\n\n## Usage\n\nThe library provides a solver that will translate a CVXPY `Problem` into a `gurobipy.Model`,\nand optimize using Gurobi:\n\n```python\nimport cvxpy as cp\nimport cvxpy_gurobi\n\nproblem = cp.Problem(cp.Maximize(cp.Variable(name=\"x\", nonpos=True)))\ncvxpy_gurobi.solve(problem)\nassert problem.value == 0\n```\n\nThe solver can also be registered with CVXPY and used as any other solver:\n\n```python\nimport cvxpy as cp\nfrom cvxpy_gurobi import GUROBI_TRANSLATION, solver\n\ncvxpy_gurobi.register_solver()\n# ^ this is the same as:\ncp.Problem.register_solve_method(GUROBI_TRANSLATION, solver())\n\nproblem.solve(method=GUROBI_TRANSLATION)\n```\n\nThis solver is a simple wrapper for the most common use case:\n\n```python\nfrom cvxpy_gurobi import build_model, backfill_problem\n\nmodel = build_model(problem)\nmodel.optimize()\nbackfill_problem(problem, model)\nassert model.optVal == problem.value\n```\n\nThe `build_model` function provided by this library translates the `cvxpy.Problem` instance\ninto an equivalent `gurobipy.Model`, and `backfill_problem` sets the optimal\nvalues on the original problem.\n\n> [!NOTE]\n> Both functions must be used together as they rely on naming conventions to map variables and constraints between CVXPY and Gurobi.\n\nThe output of the `build_model` function is a standard `gurobipy.Model` instance,\nwhich can be further customized prior to solving. This approach enables you to\nmanage how the model will be optimized.\n\n\n## Installation\n\n```sh\npip install cvxpy-gurobi\n```\n\n\n## CVXPY has an interface to Gurobi, why is this needed?\n\nWhen using CVXPY's interface to Gurobi,\nthe problems fed to Gurobi have been pre-compiled by CVXPY,\nmeaning the model is not exactly the same as the one you have written.\nThis is great for solvers with low-level APIs, such as SCS or OSQP,\nbut `gurobipy` allows you to express your models at a higher-level.\n\nProviding the raw model to Gurobi is a better idea in general since\nthe Gurobi solver is able to compile the problem with a better accuracy.\nThe chosen algorithm can also be different depending on the way it is modelled,\npotentially leading to better performance.\n\nIn addition, CVXPY does not give access to the model before solving it.\nCVXPY must therefore make some choices for you,\nsuch as setting `QCPDual` to 1 on all non-MIP models.\nHaving access to the model can help\nif you want to handle the call to `.optimize()` in a non-standard way,\ne.g. by sending it to an async loop.\n\n\n### Example\n\nConsider this QP problem:\n\n```python\nimport cvxpy as cp\n\nx = cp.Variable(name=\"x\")\nproblem = cp.Problem(cp.Minimize((x-1) ** 2))\n```\n\nThe problem will be sent to Gurobi as (in LP format):\n\n```\nMinimize\n [ 2 C0 ^2 ] / 2 \nSubject To\n R0: - C0 + C1 = 1\nBounds\n C0 free\n C1 free\nEnd\n```\n\nUsing this package, it will instead send:\n\n```\nMinimize\n - 2 x + Constant + [ 2 x ^2 ] / 2 \nSubject To\nBounds\n x free\n Constant = 1\nEnd\n```\n\nNote that:\n* the variable's name matches the user-defined problem;\n* no extra (free) variables;\n* no extra constraints.\n\n## Why not use `gurobipy` directly?\n\nCVXPY has 2 main features: a modelling API and interfaces to many solvers. The modelling API has a great design, whereas `gurobipy` feels like a thin layer over the C API. The interfaces to other solvers can be useful to not have to rewrite the problem when switching solvers.\n\n\n# Supported versions\n\nAll supported versions of Python, CVXPY and `gurobipy` should work.\nHowever, due to licensing restrictions, old versions of\n`gurobipy` cannot be tested in CI.\nIf you run into a bug, please open an issue in this repo specifying\nthe versions used.\n\n\n# Contributing\n\nIt is *highly recommended* to use [Hatch](https://hatch.pypa.io/latest/) for development.\nIt will handle all virtual\nenvironment management.\n\nTo lint and format the code, run:\n```sh\nhatch fmt\n```\n\nFor testing, run:\n```sh\nhatch test\n```\n\nThis will test the latest version of dependencies. You can also run `hatch test --all`\nto test several combinations of the supported version range.\n\nMake sure any change is tested through a snapshot test. To add a new test case,\nbuild a simple CVXPY problem in `tests/test_problems.py` in the appropriate category,\nthen run:\n```sh\nhatch run update-snapshots\n```\nYou can then check the output in the `tests/snapshots` folder is as expected.\n",
"bugtrack_url": null,
"license": null,
"summary": "Translate CVXPY problems into gurobipy models",
"version": "1.0.0",
"project_urls": {
"Documentation": "https://github.com/jonathanberthias/cvxpy-gurobi#readme",
"Issues": "https://github.com/jonathanberthias/cvxpy-gurobi/issues",
"Source": "https://github.com/jonathanberthias/cvxpy-gurobi"
},
"split_keywords": [
"cvxpy",
" gurobi",
" gurobipy",
" optimization"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6c97abba86114076963e1f1c1956ddaa52c37355d458a95b7efd759620fde1e6",
"md5": "41dcbc44fa1e7d0ae026664691cd4a0b",
"sha256": "676243598cac0623a583b3df296ed6372b906e3114bcec7fff51ae650ed0fb79"
},
"downloads": -1,
"filename": "cvxpy_gurobi-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "41dcbc44fa1e7d0ae026664691cd4a0b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 15642,
"upload_time": "2024-09-28T11:34:45",
"upload_time_iso_8601": "2024-09-28T11:34:45.050128Z",
"url": "https://files.pythonhosted.org/packages/6c/97/abba86114076963e1f1c1956ddaa52c37355d458a95b7efd759620fde1e6/cvxpy_gurobi-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "45d17f9227a608207cb2b937d18f620a945db18bf8b117ac2495145d743cf7e4",
"md5": "ee9ce3d9b840331c654995b2de8e645e",
"sha256": "f15e8565c66902f474b85ea728a49d6da5b70c5740c9153aa4593a4be9b76d38"
},
"downloads": -1,
"filename": "cvxpy_gurobi-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "ee9ce3d9b840331c654995b2de8e645e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 44561,
"upload_time": "2024-09-28T11:34:46",
"upload_time_iso_8601": "2024-09-28T11:34:46.477871Z",
"url": "https://files.pythonhosted.org/packages/45/d1/7f9227a608207cb2b937d18f620a945db18bf8b117ac2495145d743cf7e4/cvxpy_gurobi-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-28 11:34:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jonathanberthias",
"github_project": "cvxpy-gurobi#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "cvxpy-gurobi"
}