CVXPY Python
Translate an optimization problem into CVXPY without changing its mathematics.
Make dimensions, domains, curvature, solver capability, status handling, and
numerical validation explicit.
Core objects
| Object |
Meaning |
Rule |
Variable |
Unknown decision value, optionally with structural attributes. |
Domain and shape belong here or in explicit constraints. |
Parameter |
Symbolic constant whose .value changes between solves. |
Use for repeated instances; it is never optimized. |
Expression |
Symbolic computation with shape, sign, and curvature. |
Use CVXPY atoms/operators, not NumPy evaluation on variables. |
Constraint |
Symbolic equality, inequality, or cone relation. |
Store explicit constraints when dual values matter. |
Objective |
Minimize(convex) or Maximize(concave) under DCP. |
Direction is part of the model. |
Problem |
Immutable objective plus constraint list. |
solve() canonicalizes, invokes a solver, and populates status/values. |
Model construction does not solve. solve() returns an objective value and
updates problem.status, problem.value, variable values, constraint duals,
and solver statistics when available. Problems are immutable; use Parameters
for data changes or construct a new Problem for mathematical changes. Read
the symbolic and curvature model.
Ordered workflow
- Write the mathematical sets, indices, units, decision variables, objective,
constraints, and allowed approximation before code.
- Determine shapes and domains; distinguish scalar
(), vector (n,), and
column (n, 1) deliberately.
- Build symbolic expressions using CVXPY atoms and
@ for matrix products.
- Check
problem.is_dcp() or the intended ruleset before solver selection. Use
is_dcp(dpp=True) for a repeated DCP parameterization.
- Select a solver from the problem class and locally installed capabilities;
pass only supported options.
- Solve, branch on status, and reject missing/nonfinite values before use.
- Independently validate primal feasibility, integrality, objective, and—when
needed—duality/KKT residuals at tolerances justified by scale.
Decision rules
- Use a
Parameter when coefficients or bounds change but the optimization
structure remains. Require DPP for claimed recompilation speedups.
- Use variable attributes (
nonneg, boolean, integer, PSD, sparsity) when
analyzer information or reduced representation matters. Use explicit
constraints when dual variables are required; attributes do not record their
own duals.
- Use
cp.sum, cp.sum_squares, cp.norm, cp.maximum, and other atoms on
expressions. Python sum or NumPy functions can be slow, object-typed, or
semantically wrong.
- Use separate constraints
x >= 0 and x <= 1; chained comparisons and strict
inequalities do not define valid CVXPY constraints.
- If a convex expression is marked unknown, rewrite it using a recognized atom
or equivalent formulation; never suppress DCP errors without proving a
different ruleset applies.
- Choose mixed-integer, conic, quadratic, or nonlinear solvers from installed
solver support and licensing. Solver name is deployment configuration, not a
universally portable constant.
Read modeling, solving, and validation for DCP/DPP,
status gates, tolerances, solver choice, and repeated solves.
Canonical anchor
import cvxpy as cp
import numpy as np
def bounded_least_squares(design: np.ndarray, target: np.ndarray) -> tuple[np.ndarray, float]:
coefficients = cp.Variable(design.shape[1])
problem = cp.Problem(
cp.Minimize(cp.sum_squares(design @ coefficients - target)),
[coefficients >= 0, coefficients <= 1],
)
if not problem.is_dcp():
raise ValueError("model must satisfy DCP")
value = problem.solve()
if problem.status not in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}:
raise RuntimeError(f"solve failed: {problem.status}")
if value is None or not np.isfinite(value):
raise RuntimeError("solver returned no finite objective")
if coefficients.value is None or not np.isfinite(coefficients.value).all():
raise RuntimeError("solver returned no finite primal solution")
return coefficients.value.copy(), float(value)
Production code must decide whether OPTIMAL_INACCURATE is acceptable and
validate residuals accordingly. Do not return .value before the status gate.
High-risk rules
- Do not multiply decision variables together in a DCP model. Inspect curvature
and use a recognized atom/formulation if the mathematics is convex.
/, *, and @ have different scalar/elementwise/matrix meanings. Assert
every expression shape; do not rely on accidental broadcasting.
- Do not read
variable.value or constraint.dual_value until a successful
solve and appropriate status check.
- Infeasible, unbounded, and
infeasible_or_unbounded are distinct outcomes.
Handle them separately; an infinite objective is not a usable solution.
OPTIMAL_INACCURATE is evidence of a solver tolerance/status, not equivalent
to exact optimality. Validate domain residuals.
- Warm start and Parameters do not guarantee DPP or speed. Check the compiled
model and measure repeated solves.
- Scaling affects numerical reliability. Normalize units or use solver options
based on diagnostics, not by accepting a loose tolerance until tests pass.
- Solver-specific options and availability drift. Query
cp.installed_solvers()
and inspect the environment.
Version grounding and completion
The official current tutorial documents DCP, DPP, solver status, variable
attributes, and performance guidance; CVXPY is not installed locally at this
authoring stage. Inspect version, installed solvers, atom/signature, and solver
options before execution. Read verification.
Completion requires the code to match the written mathematics; ruleset checks
to pass; solver capability to be explicit; all statuses to be handled; primal
values and residuals to be validated; repeated solve behavior to be tested when
claimed; and deterministic fixtures for feasible, infeasible, and boundary
instances.
References
- Symbolic and curvature model
- Modeling, solving, and validation
- Verification and grounding
1---2name: cvxpy-python3description: Use for writing, reviewing, debugging, testing, or optimizing Python CVXPY optimization models. Trigger on Variable, Parameter, Expression, Constraint, Objective, Problem, DCP, DPP, DGP, DQCP, solver selection/status, dual values, mixed-integer, cone, or repeated parametric solves. Do not use for scipy.optimize-only, PyMC inference, symbolic algebra without optimization, or hand-written solver implementations.4---56# CVXPY Python78Translate an optimization problem into CVXPY without changing its mathematics.9Make dimensions, domains, curvature, solver capability, status handling, and10numerical validation explicit.1112## Core objects1314| Object | Meaning | Rule |15|---|---|---|16| `Variable` | Unknown decision value, optionally with structural attributes. | Domain and shape belong here or in explicit constraints. |17| `Parameter` | Symbolic constant whose `.value` changes between solves. | Use for repeated instances; it is never optimized. |18| `Expression` | Symbolic computation with shape, sign, and curvature. | Use CVXPY atoms/operators, not NumPy evaluation on variables. |19| `Constraint` | Symbolic equality, inequality, or cone relation. | Store explicit constraints when dual values matter. |20| `Objective` | `Minimize(convex)` or `Maximize(concave)` under DCP. | Direction is part of the model. |21| `Problem` | Immutable objective plus constraint list. | `solve()` canonicalizes, invokes a solver, and populates status/values. |2223Model construction does not solve. `solve()` returns an objective value and24updates `problem.status`, `problem.value`, variable values, constraint duals,25and solver statistics when available. Problems are immutable; use Parameters26for data changes or construct a new Problem for mathematical changes. Read27[the symbolic and curvature model](references/object-model.md).2829## Ordered workflow30311. Write the mathematical sets, indices, units, decision variables, objective,32 constraints, and allowed approximation before code.332. Determine shapes and domains; distinguish scalar `()`, vector `(n,)`, and34 column `(n, 1)` deliberately.353. Build symbolic expressions using CVXPY atoms and `@` for matrix products.364. Check `problem.is_dcp()` or the intended ruleset before solver selection. Use37 `is_dcp(dpp=True)` for a repeated DCP parameterization.385. Select a solver from the problem class and locally installed capabilities;39 pass only supported options.406. Solve, branch on status, and reject missing/nonfinite values before use.417. Independently validate primal feasibility, integrality, objective, and—when42 needed—duality/KKT residuals at tolerances justified by scale.4344## Decision rules4546- Use a `Parameter` when coefficients or bounds change but the optimization47 structure remains. Require DPP for claimed recompilation speedups.48- Use variable attributes (`nonneg`, `boolean`, `integer`, `PSD`, sparsity) when49 analyzer information or reduced representation matters. Use explicit50 constraints when dual variables are required; attributes do not record their51 own duals.52- Use `cp.sum`, `cp.sum_squares`, `cp.norm`, `cp.maximum`, and other atoms on53 expressions. Python `sum` or NumPy functions can be slow, object-typed, or54 semantically wrong.55- Use separate constraints `x >= 0` and `x <= 1`; chained comparisons and strict56 inequalities do not define valid CVXPY constraints.57- If a convex expression is marked unknown, rewrite it using a recognized atom58 or equivalent formulation; never suppress DCP errors without proving a59 different ruleset applies.60- Choose mixed-integer, conic, quadratic, or nonlinear solvers from installed61 solver support and licensing. Solver name is deployment configuration, not a62 universally portable constant.6364Read [modeling, solving, and validation](references/operations.md) for DCP/DPP,65status gates, tolerances, solver choice, and repeated solves.6667## Canonical anchor6869```python70import cvxpy as cp71import numpy as np727374def bounded_least_squares(design: np.ndarray, target: np.ndarray) -> tuple[np.ndarray, float]:75 coefficients = cp.Variable(design.shape[1])76 problem = cp.Problem(77 cp.Minimize(cp.sum_squares(design @ coefficients - target)),78 [coefficients >= 0, coefficients <= 1],79 )80 if not problem.is_dcp():81 raise ValueError("model must satisfy DCP")82 value = problem.solve()83 if problem.status not in {cp.OPTIMAL, cp.OPTIMAL_INACCURATE}:84 raise RuntimeError(f"solve failed: {problem.status}")85 if value is None or not np.isfinite(value):86 raise RuntimeError("solver returned no finite objective")87 if coefficients.value is None or not np.isfinite(coefficients.value).all():88 raise RuntimeError("solver returned no finite primal solution")89 return coefficients.value.copy(), float(value)90```9192Production code must decide whether `OPTIMAL_INACCURATE` is acceptable and93validate residuals accordingly. Do not return `.value` before the status gate.9495## High-risk rules9697- Do not multiply decision variables together in a DCP model. Inspect curvature98 and use a recognized atom/formulation if the mathematics is convex.99- `/`, `*`, and `@` have different scalar/elementwise/matrix meanings. Assert100 every expression shape; do not rely on accidental broadcasting.101- Do not read `variable.value` or `constraint.dual_value` until a successful102 solve and appropriate status check.103- Infeasible, unbounded, and `infeasible_or_unbounded` are distinct outcomes.104 Handle them separately; an infinite objective is not a usable solution.105- `OPTIMAL_INACCURATE` is evidence of a solver tolerance/status, not equivalent106 to exact optimality. Validate domain residuals.107- Warm start and Parameters do not guarantee DPP or speed. Check the compiled108 model and measure repeated solves.109- Scaling affects numerical reliability. Normalize units or use solver options110 based on diagnostics, not by accepting a loose tolerance until tests pass.111- Solver-specific options and availability drift. Query `cp.installed_solvers()`112 and inspect the environment.113114## Version grounding and completion115116The official current tutorial documents DCP, DPP, solver status, variable117attributes, and performance guidance; CVXPY is not installed locally at this118authoring stage. Inspect version, installed solvers, atom/signature, and solver119options before execution. Read [verification](references/verification.md).120121Completion requires the code to match the written mathematics; ruleset checks122to pass; solver capability to be explicit; all statuses to be handled; primal123values and residuals to be validated; repeated solve behavior to be tested when124claimed; and deterministic fixtures for feasible, infeasible, and boundary125instances.126127## References128129- [Symbolic and curvature model](references/object-model.md)130- [Modeling, solving, and validation](references/operations.md)131- [Verification and grounding](references/verification.md)