A domain-specific language for modeling convex optimization problems in Python.
npx @tessl/cli install tessl/pypi-cvxpy@1.7.0A 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.
pip install cvxpyimport cvxpy as cpCommon pattern for accessing all CVXPY functionality:
import cvxpy as cp
import numpy as npimport cvxpy as cp
import numpy as np
# Create optimization variables
x = cp.Variable()
y = cp.Variable()
# Define constraints
constraints = [x + y == 1, x - y >= 1]
# Define objective (minimize x^2 + y^2)
objective = cp.Minimize(x**2 + y**2)
# Create and solve the problem
problem = cp.Problem(objective, constraints)
problem.solve()
# Get results
print(f"Status: {problem.status}")
print(f"Optimal value: {problem.value}")
print(f"x = {x.value}, y = {y.value}")CVXPY follows a disciplined convex programming (DCP) approach with these key components:
This design enables automatic problem verification, transformation to solver-compatible formats, and integration with multiple solvers while maintaining mathematical rigor through DCP rules.
Fundamental building blocks for optimization problems including variables, parameters, constants, and the base expression class that enables mathematical operations.
class Variable:
def __init__(self, shape=(), name=None, **kwargs): ...
class Parameter:
def __init__(self, shape=(), name=None, value=None, **kwargs): ...
class Constant:
def __init__(self, value): ...Classes for defining and solving optimization problems, including objective functions and the main Problem class that coordinates solving.
class Problem:
def __init__(self, objective, constraints=None): ...
def solve(self, solver=None, **kwargs): ...
class Minimize:
def __init__(self, expr): ...
class Maximize:
def __init__(self, expr): ...Constraint types that define the feasible region of optimization problems, including equality, inequality, and specialized cone constraints.
class Zero: # Equality constraints
def __init__(self, expr): ...
class NonNeg: # x >= 0
def __init__(self, expr): ...
class PSD: # Positive semidefinite
def __init__(self, expr): ...
class SOC: # Second-order cone
def __init__(self, t, X): ...Pre-defined mathematical functions organized by category that preserve convexity properties and enable complex mathematical expressions.
# Linear algebra
def matmul(lh_exp, rh_exp): ...
def trace(expr): ...
def diag(expr, k=0): ...
# Norms and distances
def norm(x, p=2, axis=None): ...
def norm1(x): ...
def norm2(x, axis=None): ...
# Elementwise functions
def abs(x): ...
def square(x): ...
def sqrt(x): ...
def exp(x): ...
def log(x): ...Solver interface, status constants, and configuration options for controlling optimization behavior and accessing different solver backends.
# Status constants
OPTIMAL: str
INFEASIBLE: str
UNBOUNDED: str
SOLVER_ERROR: str
# Solver names
CLARABEL: str
SCS: str
OSQP: str
CVXOPT: str
# Utility functions
def installed_solvers(): ...
def get_num_threads(): ...
def set_num_threads(num_threads): ...Problem transformation utilities for advanced optimization techniques including linearization and partial optimization.
def linearize(expr, var_id): ...
def partial_optimize(expr, vars): ...
def suppfunc(expr): ...Exception classes for different types of optimization and solver errors, plus warning control functions.
class DCPError(Exception): ...
class DGPError(Exception): ...
class SolverError(Exception): ...
def disable_warnings(): ...
def enable_warnings(): ...
def warnings_enabled(): ...Version string for the CVXPY package.
__version__: str # Version string (e.g., "1.7.2")class Expression:
"""Base class for all mathematical expressions in CVXPY."""
def __add__(self, other): ...
def __sub__(self, other): ...
def __mul__(self, other): ...
def __truediv__(self, other): ...
def __pow__(self, other): ...
def __neg__(self): ...
@property
def value(self): ...
@property
def shape(self): ...
@property
def size(self): ...
class Constraint:
"""Base class for all constraints."""
@property
def value(self): ...
class Objective:
"""Base class for optimization objectives."""
@property
def value(self): ...