0
# CVXPY
1
2
A domain-specific language for modeling convex optimization problems in Python. CVXPY enables developers to express optimization problems in a natural mathematical way rather than the restrictive standard form required by solvers, supporting convex optimization, mixed-integer convex optimization, geometric programs, and quasiconvex programs.
3
4
## Package Information
5
6
- **Package Name**: cvxpy
7
- **Package Type**: pypi
8
- **Language**: Python
9
- **Installation**: `pip install cvxpy`
10
- **Version**: 1.7.2
11
12
## Core Imports
13
14
```python
15
import cvxpy as cp
16
```
17
18
Common pattern for accessing all CVXPY functionality:
19
20
```python
21
import cvxpy as cp
22
import numpy as np
23
```
24
25
## Basic Usage
26
27
```python
28
import cvxpy as cp
29
import numpy as np
30
31
# Create optimization variables
32
x = cp.Variable()
33
y = cp.Variable()
34
35
# Define constraints
36
constraints = [x + y == 1, x - y >= 1]
37
38
# Define objective (minimize x^2 + y^2)
39
objective = cp.Minimize(x**2 + y**2)
40
41
# Create and solve the problem
42
problem = cp.Problem(objective, constraints)
43
problem.solve()
44
45
# Get results
46
print(f"Status: {problem.status}")
47
print(f"Optimal value: {problem.value}")
48
print(f"x = {x.value}, y = {y.value}")
49
```
50
51
## Architecture
52
53
CVXPY follows a disciplined convex programming (DCP) approach with these key components:
54
55
- **Expressions**: Mathematical expressions built from Variables, Parameters, and Constants
56
- **Atoms**: Pre-defined mathematical functions that preserve convexity
57
- **Constraints**: Restrictions defining the feasible set (equality, inequality, cone constraints)
58
- **Problems**: Combination of an objective and constraints
59
- **Solvers**: Backend optimization engines that solve the transformed problem
60
61
This design enables automatic problem verification, transformation to solver-compatible formats, and integration with multiple solvers while maintaining mathematical rigor through DCP rules.
62
63
## Capabilities
64
65
### Core Expressions
66
67
Fundamental building blocks for optimization problems including variables, parameters, constants, and the base expression class that enables mathematical operations.
68
69
```python { .api }
70
class Variable:
71
def __init__(self, shape=(), name=None, **kwargs): ...
72
73
class Parameter:
74
def __init__(self, shape=(), name=None, value=None, **kwargs): ...
75
76
class Constant:
77
def __init__(self, value): ...
78
```
79
80
[Core Expressions](./expressions.md)
81
82
### Problem Formulation
83
84
Classes for defining and solving optimization problems, including objective functions and the main Problem class that coordinates solving.
85
86
```python { .api }
87
class Problem:
88
def __init__(self, objective, constraints=None): ...
89
def solve(self, solver=None, **kwargs): ...
90
91
class Minimize:
92
def __init__(self, expr): ...
93
94
class Maximize:
95
def __init__(self, expr): ...
96
```
97
98
[Problem Formulation](./problems.md)
99
100
### Constraints
101
102
Constraint types that define the feasible region of optimization problems, including equality, inequality, and specialized cone constraints.
103
104
```python { .api }
105
class Zero: # Equality constraints
106
def __init__(self, expr): ...
107
108
class NonNeg: # x >= 0
109
def __init__(self, expr): ...
110
111
class PSD: # Positive semidefinite
112
def __init__(self, expr): ...
113
114
class SOC: # Second-order cone
115
def __init__(self, t, X): ...
116
```
117
118
[Constraints](./constraints.md)
119
120
### Mathematical Functions (Atoms)
121
122
Pre-defined mathematical functions organized by category that preserve convexity properties and enable complex mathematical expressions.
123
124
```python { .api }
125
# Linear algebra
126
def matmul(lh_exp, rh_exp): ...
127
def trace(expr): ...
128
def diag(expr, k=0): ...
129
130
# Norms and distances
131
def norm(x, p=2, axis=None): ...
132
def norm1(x): ...
133
def norm2(x, axis=None): ...
134
135
# Elementwise functions
136
def abs(x): ...
137
def square(x): ...
138
def sqrt(x): ...
139
def exp(x): ...
140
def log(x): ...
141
```
142
143
[Mathematical Functions](./atoms.md)
144
145
### Solvers and Settings
146
147
Solver interface, status constants, and configuration options for controlling optimization behavior and accessing different solver backends.
148
149
```python { .api }
150
# Status constants
151
OPTIMAL: str
152
INFEASIBLE: str
153
UNBOUNDED: str
154
SOLVER_ERROR: str
155
156
# Solver names
157
CLARABEL: str
158
SCS: str
159
OSQP: str
160
CVXOPT: str
161
162
# Utility functions
163
def installed_solvers(): ...
164
def get_num_threads(): ...
165
def set_num_threads(num_threads): ...
166
```
167
168
[Solvers and Settings](./solvers.md)
169
170
### Transform Functions
171
172
Problem transformation utilities for advanced optimization techniques including linearization and partial optimization.
173
174
```python { .api }
175
def linearize(expr, var_id): ...
176
def partial_optimize(expr, vars): ...
177
def suppfunc(expr): ...
178
```
179
180
[Transform Functions](./transforms.md)
181
182
### Error Handling
183
184
Exception classes for different types of optimization and solver errors, plus warning control functions.
185
186
```python { .api }
187
class DCPError(Exception): ...
188
class DGPError(Exception): ...
189
class SolverError(Exception): ...
190
191
def disable_warnings(): ...
192
def enable_warnings(): ...
193
def warnings_enabled(): ...
194
```
195
196
[Error Handling](./errors.md)
197
198
### Version Information
199
200
Version string for the CVXPY package.
201
202
```python { .api }
203
__version__: str # Version string (e.g., "1.7.2")
204
```
205
206
## Types
207
208
```python { .api }
209
class Expression:
210
"""Base class for all mathematical expressions in CVXPY."""
211
def __add__(self, other): ...
212
def __sub__(self, other): ...
213
def __mul__(self, other): ...
214
def __truediv__(self, other): ...
215
def __pow__(self, other): ...
216
def __neg__(self): ...
217
218
@property
219
def value(self): ...
220
@property
221
def shape(self): ...
222
@property
223
def size(self): ...
224
225
class Constraint:
226
"""Base class for all constraints."""
227
@property
228
def value(self): ...
229
230
class Objective:
231
"""Base class for optimization objectives."""
232
@property
233
def value(self): ...
234
```