cvxpy-gurobi


Namecvxpy-gurobi JSON
Version 1.1.0 PyPI version JSON
download
home_pageNone
SummaryTranslate CVXPY problems into gurobipy models
upload_time2024-12-01 09:36:30
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseApache-2.0
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/ac/a6/051f93c620e3a3bc882b7f36916b44c2d41fe8b8912d2f16d67a067b755b/cvxpy_gurobi-1.1.0.tar.gz",
    "platform": null,
    "description": "# CVXPY x GUROBI\n\nThis small library provides an alternative way to solve CVXPY problems with\nGurobi.\n\n## Usage\n\nThe library provides a solver that will translate a CVXPY `Problem` into a\n`gurobipy.Model`, and 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\n`cvxpy.Problem` instance into an equivalent `gurobipy.Model`, and\n`backfill_problem` sets the optimal values on the original problem.\n\n> [!NOTE] Both functions must be used together as they rely on naming\n> conventions to map variables and constraints between CVXPY and Gurobi.\n\nThe output of the `build_model` function is a standard `gurobipy.Model`\ninstance, which can be further customized prior to solving. This approach\nenables you to manage how the model will be optimized.\n\n## Installation\n\n```sh\npip install cvxpy-gurobi\n```\n\n## CVXPY has an interface to Gurobi, why is this needed?\n\nWhen using CVXPY's interface to Gurobi, the problems fed to Gurobi have been\npre-compiled by CVXPY, meaning the model is not exactly the same as the one you\nhave written. This is great for solvers with low-level APIs, such as SCS or\nOSQP, but `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 the Gurobi\nsolver is able to compile the problem with a better accuracy. The chosen\nalgorithm can also be different depending on the way it is modelled, potentially\nleading to better performance.\n\nIn addition, CVXPY does not give access to the model before solving it. CVXPY\nmust therefore make some choices for you, such as setting `QCPDual` to 1 on all\nnon-MIP models. Having access to the model can help if you want to handle the\ncall to `.optimize()` in a non-standard way, e.g. by sending it to an async\nloop.\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\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\nmodelling API has a great design, whereas `gurobipy` feels like a thin layer\nover the C API. The interfaces to other solvers can be useful to not have to\nrewrite the problem when switching solvers.\n\n# Supported versions\n\nAll supported versions of Python, CVXPY and `gurobipy` should work. However, due\nto licensing restrictions, old versions of `gurobipy` cannot be tested in CI. If\nyou run into a bug, please open an issue in this repo specifying the versions\nused.\n\n# Contributing\n\nIt is _highly recommended_ to use [Hatch](https://hatch.pypa.io/latest/) for\ndevelopment. It will handle all virtual environment management.\n\nTo lint and format the code, run:\n\n```sh\nhatch fmt\n```\n\nFor testing, run:\n\n```sh\nhatch test\n```\n\nThis will test the latest version of dependencies. You can also run\n`hatch test --all` to 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\ncategory, then run:\n\n```sh\nhatch run update-snapshots\n```\n\nYou can then check the output in the `tests/snapshots` folder is as expected.\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Translate CVXPY problems into gurobipy models",
    "version": "1.1.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": "78b1e0f0570ce957f3b642c23006fef4239e82e6325fb2b968cef01423962cf8",
                "md5": "53e486c6427650d7db535e57b5b593d3",
                "sha256": "7f302648cfd367d8f9121ad20337318c29f984923c016f682ca5e2a197ac4755"
            },
            "downloads": -1,
            "filename": "cvxpy_gurobi-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "53e486c6427650d7db535e57b5b593d3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 15751,
            "upload_time": "2024-12-01T09:36:22",
            "upload_time_iso_8601": "2024-12-01T09:36:22.101721Z",
            "url": "https://files.pythonhosted.org/packages/78/b1/e0f0570ce957f3b642c23006fef4239e82e6325fb2b968cef01423962cf8/cvxpy_gurobi-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "aca6051f93c620e3a3bc882b7f36916b44c2d41fe8b8912d2f16d67a067b755b",
                "md5": "4cc1b3f924a31dcdd82999a29f3153c0",
                "sha256": "27ff957052ab4be9d5a2313df36cbda33edac4e1291e4bd0e4dddd41d8889611"
            },
            "downloads": -1,
            "filename": "cvxpy_gurobi-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4cc1b3f924a31dcdd82999a29f3153c0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 45335,
            "upload_time": "2024-12-01T09:36:30",
            "upload_time_iso_8601": "2024-12-01T09:36:30.797692Z",
            "url": "https://files.pythonhosted.org/packages/ac/a6/051f93c620e3a3bc882b7f36916b44c2d41fe8b8912d2f16d67a067b755b/cvxpy_gurobi-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-01 09:36:30",
    "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"
}
        
Elapsed time: 0.86429s