cvxpy-translation


Namecvxpy-translation JSON
Version 2.0.0 PyPI version JSON
download
home_pageNone
SummaryTranslate CVXPY problems into solvers' native models
upload_time2025-08-23 21:45:41
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords cvxpy gurobi gurobipy optimization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

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

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

<!-- prettier-ignore -->
> [!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

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

```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` 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](https://hatch.pypa.io/latest/) is used for development. It will handle
all the dependencies when testing on multiple versions.

For testing, run:

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

```sh
hatch run update-snapshots
```

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

To lint the code, run:

```sh
ruff check
```

To format the code, run:

```sh
ruff format
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "cvxpy-translation",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "cvxpy, gurobi, gurobipy, optimization",
    "author": null,
    "author_email": "Jonathan Berthias <jvberthias@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/cf/b3/27413020191cacb07a75a8dae3250c11edfaca495aa9304185712318ae1f/cvxpy_translation-2.0.0.tar.gz",
    "platform": null,
    "description": "# CVXPY translation\n\nThis small library provides an alternative way to solve CVXPY problems by\nbuilding solver's native models. It currently supports Gurobi and SCIP.\n\n## Usage\n\nThe library provides a solver that will translate a CVXPY `Problem` into a\n`gurobipy.Model` or a `pyscipopt.Model`, and solve using the direct interface:\n\n```python\nimport cvxpy as cp\n\nproblem = cp.Problem(cp.Maximize(cp.Variable(name=\"x\", nonpos=True)))\ncvxpy_translation.solve(problem, solver=cp.GUROBI)\nassert problem.value == 0\n```\n\nThis solver is a simple wrapper for the most common use case:\n\n```python\nfrom cvxpy_translation import build_model, backfill_problem\n\nmodel = build_model(problem, solver=cp.SCIP)\nmodel.optimize()\nbackfill_problem(problem, model)\nassert model.getObjVal() == problem.value\n```\n\nThe `build_model` function provided by this library translates the `Problem`\ninstance into an equivalent `Model`, and `backfill_problem` sets the optimal\nvalues on the original problem.\n\n<!-- prettier-ignore -->\n> [!NOTE]\n> Both functions must be used together as they rely on naming conventions to\n> map variables and constraints between the problem and the model.\n\nThe output of the `build_model` function is a `Model` instance, which can be\nfurther customized prior to solving. This approach enables you to manage how the\nmodel will be optimized, set parameters, or use features that aren't available\nthrough CVXPY's interface.\n\n## Installation\n\n```sh\npip install cvxpy-translation\n```\n\n## CVXPY has an interface to Gurobi and SCIP, why is this needed?\n\nWhen using CVXPY's interface, the problems fed to the solver 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` and `pyscipopt` allow you to express your models at a\nhigher-level.\n\nProviding the raw model to the solver can be a better idea in general to let the\nsolver use its own heuristics. The chosen algorithm can be different depending\non the way it is modelled.\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 some parameters on the\ngenerated model. Having access to the model can help if you want to handle the\ncall to `.optimize()` in a non-standard way, e.g. by calling `.optimizeAsync()`\nin `gurobipy` or `solveConcurrent()` in `pyscipopt`. It is also required to set\ncallbacks.\n\nAnother feature is the ability to use the latest features of the solvers, such\nas non-linear expressions in Gurobi, which are not yet supported by the Gurobi\ninterface in CVXPY.\n\n### Example with Gurobi\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` or `pyscipopt` directly?\n\nCVXPY has 2 main features: a modelling API and interfaces to many solvers. The\nmodelling API has a great design, whereas `gurobipy` and `pyscipopt` feel like a\nthin layer over the C API. The interfaces to other solvers can be useful to not\nhave to rewrite the problem when switching solvers.\n\n# Supported versions\n\nAll supported versions of Python and CVXPY should work.\n\nThe same goes for `gurobipy`. However, due to licensing restrictions, old\nversions of `gurobipy` cannot be tested in CI. If you run into a bug, please\nopen an issue in this repo specifying the versions used.\n\nOnly versions of `pyscipopt` after 5.5.0 are supported as that is when the\nmatrix API was introduced.\n\n# Contributing\n\n[Hatch](https://hatch.pypa.io/latest/) is used for development. It will handle\nall the dependencies when testing on multiple versions.\n\nFor testing, run:\n\n```sh\nhatch run latest:tests\n```\n\nThis will test the latest version of dependencies. You can also run\n`hatch run oldest:tests` to test the minimum required dependency versions.\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\nTo lint the code, run:\n\n```sh\nruff check\n```\n\nTo format the code, run:\n\n```sh\nruff format\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Translate CVXPY problems into solvers' native models",
    "version": "2.0.0",
    "project_urls": {
        "Documentation": "https://github.com/jonathanberthias/cvxpy-translation#readme",
        "Issues": "https://github.com/jonathanberthias/cvxpy-translation/issues",
        "Source": "https://github.com/jonathanberthias/cvxpy-translation"
    },
    "split_keywords": [
        "cvxpy",
        " gurobi",
        " gurobipy",
        " optimization"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b40426b2f41d2157f6a71f0ca13524cd7e500b1d4b4dd6b6bd29a6eee4079a72",
                "md5": "a305ec945ee98cb5a1e55e322ad38ad4",
                "sha256": "6d0f698786bf89294b11919a012fff3ee415c507ddf4d95725ec32b64bb2a8a4"
            },
            "downloads": -1,
            "filename": "cvxpy_translation-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a305ec945ee98cb5a1e55e322ad38ad4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 27030,
            "upload_time": "2025-08-23T21:45:39",
            "upload_time_iso_8601": "2025-08-23T21:45:39.881146Z",
            "url": "https://files.pythonhosted.org/packages/b4/04/26b2f41d2157f6a71f0ca13524cd7e500b1d4b4dd6b6bd29a6eee4079a72/cvxpy_translation-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "cfb327413020191cacb07a75a8dae3250c11edfaca495aa9304185712318ae1f",
                "md5": "3c5b604b3a071efa626dd44901f0052f",
                "sha256": "847f252f6e76f72699fe5b1513dcfbfd0121929d677fc73ad1f01fadd2ad5c50"
            },
            "downloads": -1,
            "filename": "cvxpy_translation-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3c5b604b3a071efa626dd44901f0052f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 72558,
            "upload_time": "2025-08-23T21:45:41",
            "upload_time_iso_8601": "2025-08-23T21:45:41.435060Z",
            "url": "https://files.pythonhosted.org/packages/cf/b3/27413020191cacb07a75a8dae3250c11edfaca495aa9304185712318ae1f/cvxpy_translation-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-23 21:45:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jonathanberthias",
    "github_project": "cvxpy-translation#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "cvxpy-translation"
}
        
Elapsed time: 0.50830s