Constraints¶
A constraint is an equality or inequality that restricts the domain of an optimization problem. CVXPY has five types of constraints: nonpositive, equality or zero, positive semidefinite, secondorder cone, and exponential cone. The vast majority of users will need only create constraints of the first three types. Additionally, most users need not know anything more about constraints other than how to create them. The constraint APIs do nonetheless provide methods that advanced users may find useful; for example, some of the APIs allow you to inspect dual variable values and residuals.
Constraint¶

class
cvxpy.constraints.constraint.
Constraint
(args, constr_id=None)[source]¶ Bases:
cvxpy.utilities.canonical.Canonical
The base class for constraints.
A constraint is an equality, inequality, or more generally a generalized inequality that is imposed upon a mathematical expression or a list of thereof.
 Parameters
args (
list
) – A list of expression trees.constr_id (
int
) – A unique id for the constraint.

abstract
is_dcp
(dpp=False)[source]¶ Checks whether the constraint is DCP.
 Returns
True if the constraint is DCP, False otherwise.
 Return type
bool

value
(tolerance=1e08)[source]¶ Checks whether the constraint violation is less than a tolerance.
 Parameters
tolerance (
float
) – The absolute tolerance to impose on the violation. Returns
True if the violation is less than
tolerance
, False otherwise. Return type
bool
 Raises
ValueError – If the constrained expression does not have a value associated with it.

violation
()[source]¶ The numeric residual of the constraint.
The violation is defined as the distance between the constrained expression’s value and its projection onto the domain of the constraint:
\[\Pi(v)  v_2^2\]where \(v\) is the value of the constrained expression and \(\Pi\) is the projection operator onto the constraint’s domain .
 Returns
The residual value.
 Return type
NumPy.ndarray
 Raises
ValueError – If the constrained expression does not have a value associated with it.
NonPos¶

class
cvxpy.constraints.nonpos.
NonPos
(expr, constr_id=None)[source]¶ Bases:
cvxpy.constraints.constraint.Constraint
A constraint of the form \(x \leq 0\).
The preferred way of creating a
NonPos
constraint is through operator overloading. To constrain an expressionx
to be nonpositive, simply writex <= 0
; to constrainx
to be nonnegative, writex >= 0
. The former creates aNonPos
constraint withx
as its argument, while the latter creates one withx
as its argument. Strict inequalities are not supported, as they do not make sense in a numerical setting. Parameters
expr (
Expression
) – The expression to constrain.constr_id (
int
) – A unique id for the constraint.

property
dual_value
¶ The value of the dual variable.
 Type
NumPy.ndarray

property
shape
¶ The shape of the constrained expression.
 Type
int

property
size
¶ The size of the constrained expression.
 Type
int

value
(tolerance=1e08)¶ Checks whether the constraint violation is less than a tolerance.
 Parameters
tolerance (
float
) – The absolute tolerance to impose on the violation. Returns
True if the violation is less than
tolerance
, False otherwise. Return type
bool
 Raises
ValueError – If the constrained expression does not have a value associated with it.

violation
()[source]¶ The numeric residual of the constraint.
The violation is defined as the distance between the constrained expression’s value and its projection onto the domain of the constraint:
\[\Pi(v)  v_2^2\]where \(v\) is the value of the constrained expression and \(\Pi\) is the projection operator onto the constraint’s domain .
 Returns
The residual value.
 Return type
NumPy.ndarray
 Raises
ValueError – If the constrained expression does not have a value associated with it.
Zero¶

class
cvxpy.constraints.zero.
Zero
(expr, constr_id=None)[source]¶ Bases:
cvxpy.constraints.constraint.Constraint
A constraint of the form \(x = 0\).
The preferred way of creating a
Zero
constraint is through operator overloading. To constrain an expressionx
to be zero, simply writex == 0
. The former creates aZero
constraint withx
as its argument.
value
(tolerance=1e08)¶ Checks whether the constraint violation is less than a tolerance.
 Parameters
tolerance (
float
) – The absolute tolerance to impose on the violation. Returns
True if the violation is less than
tolerance
, False otherwise. Return type
bool
 Raises
ValueError – If the constrained expression does not have a value associated with it.

violation
()¶ The numeric residual of the constraint.
The violation is defined as the distance between the constrained expression’s value and its projection onto the domain of the constraint:
\[\Pi(v)  v_2^2\]where \(v\) is the value of the constrained expression and \(\Pi\) is the projection operator onto the constraint’s domain .
 Returns
The residual value.
 Return type
NumPy.ndarray
 Raises
ValueError – If the constrained expression does not have a value associated with it.

PSD¶

class
cvxpy.constraints.psd.
PSD
(expr, constr_id=None)[source]¶ Bases:
cvxpy.constraints.constraint.Constraint
A constraint of the form \(\frac{1}{2}(X + X^T) \succcurlyeq_{S_n^+} 0\)
Applying a
PSD
constraint to a twodimensional expressionX
constrains its symmetric part to be positive semidefinite: i.e., it constrainsX
to be such that\[z^T(X + X^T)z \geq 0,\]for all \(z\).
The preferred way of creating a
PSD
constraint is through operator overloading. To constrain an expressionX
to be PSD, writeX >> 0
; to constrain it to be negative semidefinite, writeX << 0
. Strict definiteness constraints are not provided, as they do not make sense in a numerical setting. Parameters
expr (
Expression.
) – The expression to constrain; must be twodimensional.constr_id (
int
) – A unique id for the constraint.

value
(tolerance=1e08)¶ Checks whether the constraint violation is less than a tolerance.
 Parameters
tolerance (
float
) – The absolute tolerance to impose on the violation. Returns
True if the violation is less than
tolerance
, False otherwise. Return type
bool
 Raises
ValueError – If the constrained expression does not have a value associated with it.

violation
()¶ The numeric residual of the constraint.
The violation is defined as the distance between the constrained expression’s value and its projection onto the domain of the constraint:
\[\Pi(v)  v_2^2\]where \(v\) is the value of the constrained expression and \(\Pi\) is the projection operator onto the constraint’s domain .
 Returns
The residual value.
 Return type
NumPy.ndarray
 Raises
ValueError – If the constrained expression does not have a value associated with it.
SOC¶

class
cvxpy.constraints.second_order.
SOC
(t, X, axis=0, constr_id=None)[source]¶ Bases:
cvxpy.constraints.constraint.Constraint
A secondorder cone constraint for each row/column.
Assumes
t
is a vector the same length asX
’s columns (rows) foraxis == 0
(1
).
t
¶ The scalar part of the secondorder constraint.

X
¶ A matrix whose rows/columns are each a cone.

axis
¶ Slice by column 0 or row 1.

value
(tolerance=1e08)¶ Checks whether the constraint violation is less than a tolerance.
 Parameters
tolerance (
float
) – The absolute tolerance to impose on the violation. Returns
True if the violation is less than
tolerance
, False otherwise. Return type
bool
 Raises
ValueError – If the constrained expression does not have a value associated with it.

violation
()¶ The numeric residual of the constraint.
The violation is defined as the distance between the constrained expression’s value and its projection onto the domain of the constraint:
\[\Pi(v)  v_2^2\]where \(v\) is the value of the constrained expression and \(\Pi\) is the projection operator onto the constraint’s domain .
 Returns
The residual value.
 Return type
NumPy.ndarray
 Raises
ValueError – If the constrained expression does not have a value associated with it.

ExpCone¶

class
cvxpy.constraints.exponential.
ExpCone
(x, y, z, constr_id=None)[source]¶ Bases:
cvxpy.constraints.constraint.Constraint
A reformulated exponential cone constraint.
Operates elementwise on \(x, y, z\).
Original cone:
\[K = \{(x,y,z) \mid y > 0, ye^{x/y} <= z\} \cup \{(x,y,z) \mid x \leq 0, y = 0, z \geq 0\}\]Reformulated cone:
\[K = \{(x,y,z) \mid y, z > 0, y\log(y) + x \leq y\log(z)\} \cup \{(x,y,z) \mid x \leq 0, y = 0, z \geq 0\}\] Parameters
x (
Variable
) – x in the exponential cone.y (
Variable
) – y in the exponential cone.z (
Variable
) – z in the exponential cone.

value
(tolerance=1e08)¶ Checks whether the constraint violation is less than a tolerance.
 Parameters
tolerance (
float
) – The absolute tolerance to impose on the violation. Returns
True if the violation is less than
tolerance
, False otherwise. Return type
bool
 Raises
ValueError – If the constrained expression does not have a value associated with it.

violation
()¶ The numeric residual of the constraint.
The violation is defined as the distance between the constrained expression’s value and its projection onto the domain of the constraint:
\[\Pi(v)  v_2^2\]where \(v\) is the value of the constrained expression and \(\Pi\) is the projection operator onto the constraint’s domain .
 Returns
The residual value.
 Return type
NumPy.ndarray
 Raises
ValueError – If the constrained expression does not have a value associated with it.