Skip to main content

Translate CVXPY problems into gurobipy models

Project description

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:

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:

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:

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

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:

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 for development. It will handle all virtual environment management.

To lint and format the code, run:

hatch fmt

For testing, run:

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:

hatch run update-snapshots

You can then check the output in the tests/snapshots folder is as expected.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cvxpy_gurobi-1.1.1.tar.gz (45.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cvxpy_gurobi-1.1.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file cvxpy_gurobi-1.1.1.tar.gz.

File metadata

  • Download URL: cvxpy_gurobi-1.1.1.tar.gz
  • Upload date:
  • Size: 45.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for cvxpy_gurobi-1.1.1.tar.gz
Algorithm Hash digest
SHA256 d06fc2621525a331f5b94cc4ee797ce193fb7c15e25f14b8d2ff5d3a6b9d6efe
MD5 f5b583290c0001cd54674a32a7fccc19
BLAKE2b-256 116710faf6d85ed6467c4b07940ba6362645d28c8dad32e7734610d5611dc165

See more details on using hashes here.

Provenance

The following attestation bundles were made for cvxpy_gurobi-1.1.1.tar.gz:

Publisher: cd.yml on jonathanberthias/cvxpy-gurobi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cvxpy_gurobi-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: cvxpy_gurobi-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for cvxpy_gurobi-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa113de0a4e6010e626ec48c70d317063881a7c31e36f915a680c58467074bf7
MD5 40f6ecf4bd637534b9ebb8503e1c268b
BLAKE2b-256 e1063f8a94d4de081759fb320b6e70bef82c59524d699f63bad9c58a8af4666b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cvxpy_gurobi-1.1.1-py3-none-any.whl:

Publisher: cd.yml on jonathanberthias/cvxpy-gurobi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page