CVXPY Base
CVXPY is a domain-specific language (DSL) for modeling convex optimization problems in Python. The `cvxpy-base` package provides the core functionality without bundling default solvers. It is currently at version 1.8.2 and follows a regular release cadence with major versions released periodically and patch releases for bug fixes and solver updates.
Common errors
-
cvxpy.error.SolverError: Either candidate solvers are not installed or cannot handle the problem.
cause The `cvxpy-base` package does not include any solvers by default, and no compatible solver was found in your Python environment to handle the specified problem type.fixInstall one or more compatible solvers. For example, `pip install ecos` or `pip install osqp`. For a common set of open-source solvers, install the full `cvxpy` package: `pip install cvxpy`. -
cvxpy.error.DCPError: Problem does not satisfy DCP rules.
cause The mathematical expression defined for the objective or constraints violates the rules of Disciplined Convex Programming (DCP), meaning CVXPY cannot guarantee its convexity.fixCarefully review the objective function and constraints to ensure they are convex and adhere to CVXPY's DCP rules. Consult the CVXPY documentation on DCP rules for guidance. -
AttributeError: 'float' object has no attribute 'is_atom'
cause You are attempting to use a raw Python float or NumPy array in a context where CVXPY expects an `Expression`, `Variable`, or `Parameter` object, often within an atom or constraint definition.fixEnsure all mathematical operations within CVXPY objectives and constraints are performed using CVXPY objects. For example, convert constants to `cp.Constant(value)` if they are part of complex expressions, or ensure variables are correctly initialized as `cp.Variable()`.
Warnings
- gotcha The `cvxpy-base` package only contains the core library and does NOT include any default solvers. Users installing `cvxpy-base` will need to manually install compatible solvers (e.g., `ecos`, `osqp`, `scs`) for problem solving.
- breaking CVXPY 1.8.x and newer versions have updated Python support, requiring Python 3.11 through 3.14. Older Python versions (e.g., 3.9, 3.10) are no longer officially supported.
- gotcha CVXPY is designed for *convex* optimization problems. Attempting to solve a non-convex problem will often result in a `DCPError`.
Install
-
pip install cvxpy-base -
pip install cvxpy
Imports
- cp
from cvxpy import Problem, Variable, Minimize
import cvxpy as cp
Quickstart
import cvxpy as cp
import numpy as np
# Define variables
x = cp.Variable()
y = cp.Variable()
# Define objective function
objective = cp.Minimize((x - y)**2 + 1)
# Define constraints
constraints = [x + y >= 0, x - y >= 0, x <= 3]
# Formulate the problem
problem = cp.Problem(objective, constraints)
# Solve the problem (requires a solver to be installed)
try:
problem.solve()
if problem.status == cp.OPTIMAL or problem.status == cp.OPTIMAL_INACCURATE:
print(f"Problem status: {problem.status}")
print(f"Optimal value: {problem.value:.4f}")
print(f"Optimal x: {x.value:.4f}")
print(f"Optimal y: {y.value:.4f}")
else:
print(f"Problem did not solve to optimality. Status: {problem.status}")
except cp.error.SolverError as e:
print(f"Solver Error: {e}")
print("\nHint: To solve problems with `cvxpy-base`, you must install a compatible solver separately. ")
print("For example: `pip install ecos` or `pip install osqp`. ")
print("Alternatively, install the full `cvxpy` package: `pip install cvxpy`.")
except Exception as e:
print(f"An unexpected error occurred: {e}")