Translate CVXPY problems into solvers' native models
Project description
CVXPY translation
This small library provides an alternative way to solve CVXPY problems by building solver's native models. It currently supports Gurobi and SCIP.
Usage
The library provides a solver that will translate a CVXPY Problem into a
gurobipy.Model or a pyscipopt.Model, and solve using the direct interface:
import cvxpy as cp
problem = cp.Problem(cp.Maximize(cp.Variable(name="x", nonpos=True)))
cvxpy_translation.solve(problem, solver=cp.GUROBI)
assert problem.value == 0
This solver is a simple wrapper for the most common use case:
from cvxpy_translation import build_model, backfill_problem
model = build_model(problem, solver=cp.SCIP)
model.optimize()
backfill_problem(problem, model)
assert model.getObjVal() == problem.value
The build_model function provided by this library translates the Problem
instance into an equivalent 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 the problem and the model.
The output of the build_model function is a Model instance, which can be
further customized prior to solving. This approach enables you to manage how the
model will be optimized, set parameters, or use features that aren't available
through CVXPY's interface.
Installation
pip install cvxpy-translation
CVXPY has an interface to Gurobi and SCIP, why is this needed?
When using CVXPY's interface, the problems fed to the solver 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 and pyscipopt allow you to express your models at a
higher-level.
Providing the raw model to the solver can be a better idea in general to let the solver use its own heuristics. The chosen algorithm can be different depending on the way it is modelled.
In addition, CVXPY does not give access to the model before solving it. CVXPY
must therefore make some choices for you, such as setting some parameters on the
generated model. Having access to the model can help if you want to handle the
call to .optimize() in a non-standard way, e.g. by calling .optimizeAsync()
in gurobipy or solveConcurrent() in pyscipopt. It is also required to set
callbacks.
Another feature is the ability to use the latest features of the solvers, such as non-linear expressions in Gurobi, which are not yet supported by the Gurobi interface in CVXPY.
Example with Gurobi
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 or pyscipopt directly?
CVXPY has 2 main features: a modelling API and interfaces to many solvers. The
modelling API has a great design, whereas gurobipy and pyscipopt feel 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 and CVXPY should work.
The same goes for gurobipy. 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.
Only versions of pyscipopt after 5.5.0 are supported as that is when the
matrix API was introduced.
Contributing
Hatch is used for development. It will handle all the dependencies when testing on multiple versions.
For testing, run:
hatch run latest:tests
This will test the latest version of dependencies. You can also run
hatch run oldest:tests to test the minimum required dependency versions.
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.
To lint the code, run:
ruff check
To format the code, run:
ruff format
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cvxpy_translation-2.0.2.tar.gz.
File metadata
- Download URL: cvxpy_translation-2.0.2.tar.gz
- Upload date:
- Size: 77.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88d2c34d34acaa0ebc39fec5c7291f90a2fefdb756ba02ef1e1379bda06bc2d4
|
|
| MD5 |
48d0bd8a73a7d0d519d7bb8c73e2f2b4
|
|
| BLAKE2b-256 |
d9d26b4b27c7dc6e5360e7e3777265c65cc0aff7d6e7a06ea1d7604849beac63
|
Provenance
The following attestation bundles were made for cvxpy_translation-2.0.2.tar.gz:
Publisher:
cd.yml on jonathanberthias/cvxpy-translation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cvxpy_translation-2.0.2.tar.gz -
Subject digest:
88d2c34d34acaa0ebc39fec5c7291f90a2fefdb756ba02ef1e1379bda06bc2d4 - Sigstore transparency entry: 1107830309
- Sigstore integration time:
-
Permalink:
jonathanberthias/cvxpy-translation@27d739958e4c6093c00ae2b7e0de5463b0bc091e -
Branch / Tag:
refs/tags/v2.0.2 - Owner: https://github.com/jonathanberthias
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@27d739958e4c6093c00ae2b7e0de5463b0bc091e -
Trigger Event:
release
-
Statement type:
File details
Details for the file cvxpy_translation-2.0.2-py3-none-any.whl.
File metadata
- Download URL: cvxpy_translation-2.0.2-py3-none-any.whl
- Upload date:
- Size: 27.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8b19fb9736ddab51c469e75fafa8190d9b9bc9a5e4d8d0ade663b0d38daa72e
|
|
| MD5 |
ff95b477ae24e10fb6b16ee75b6dde19
|
|
| BLAKE2b-256 |
902d1dccccebaef85d95a2b18c0b47c15a46476f5bb499086c8381ea7fd898e9
|
Provenance
The following attestation bundles were made for cvxpy_translation-2.0.2-py3-none-any.whl:
Publisher:
cd.yml on jonathanberthias/cvxpy-translation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cvxpy_translation-2.0.2-py3-none-any.whl -
Subject digest:
d8b19fb9736ddab51c469e75fafa8190d9b9bc9a5e4d8d0ade663b0d38daa72e - Sigstore transparency entry: 1107830313
- Sigstore integration time:
-
Permalink:
jonathanberthias/cvxpy-translation@27d739958e4c6093c00ae2b7e0de5463b0bc091e -
Branch / Tag:
refs/tags/v2.0.2 - Owner: https://github.com/jonathanberthias
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@27d739958e4c6093c00ae2b7e0de5463b0bc091e -
Trigger Event:
release
-
Statement type: