Constraints¶
A constraint is an equality or inequality that restricts the domain of an optimization problem. CVXPY has seven types of constraints: non-positive, equality or zero, positive semidefinite, second-order cone, exponential cone, 3-dimensional power cones, and N-dimensional power cones. 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: bool = False) bool [source]¶
Checks whether the constraint is DCP.
- Returns
True if the constraint is DCP, False otherwise.
- Return type
bool
- value(tolerance: float = 1e-08)[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 non-positive, simply writex <= 0
; to constrainx
to be non-negative, writex >= 0
. The former creates aNonPos
constraint withx
as its argument, while the latter creates one with-x
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
- is_dcp(dpp: bool = False) bool [source]¶
A non-positive constraint is DCP if its argument is convex.
- property shape¶
The shape of the constrained expression.
- Type
int
- property size¶
The size of the constrained expression.
- Type
int
- value(tolerance: float = 1e-08)¶
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: float = 1e-08)¶
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 two-dimensional 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 two-dimensional.
constr_id (int) – A unique id for the constraint.
- is_dcp(dpp: bool = False) bool [source]¶
A PSD constraint is DCP if the constrained expression is affine.
- value(tolerance: float = 1e-08)¶
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: int = 0, constr_id=None)[source]¶
Bases:
cvxpy.constraints.constraint.Constraint
A second-order 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 second-order constraint.
- X¶
A matrix whose rows/columns are each a cone.
- axis¶
Slice by column 0 or row 1.
- is_dcp(dpp: bool = False) bool [source]¶
An SOC constraint is DCP if each of its arguments is affine.
- value(tolerance: float = 1e-08)¶
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
- is_dcp(dpp: bool = False) bool [source]¶
An exponential constraint is DCP if each argument is affine.
- value(tolerance: float = 1e-08)¶
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.
PowCone3D¶
- class cvxpy.constraints.power.PowCone3D(x, y, z, alpha, constr_id=None)[source]¶
Bases:
cvxpy.constraints.constraint.Constraint
An object representing a collection of 3D power cone constraints
x[i]**alpha[i] * y[i]**(1-alpha[i]) >= |z[i]| for all i x >= 0, y >= 0
If the parameter alpha is a scalar, it will be promoted to a vector matching the (common) sizes of x, y, z. The numeric value of alpha (or its components, in the vector case) must be a number in the open interval (0, 1).
We store flattened representations of the arguments (x, y, z, and alpha) as Expression objects. We construct dual variables with respect to these flattened representations.
- is_dcp(dpp: bool = False) bool [source]¶
Checks whether the constraint is DCP.
- Returns
True if the constraint is DCP, False otherwise.
- Return type
bool
- value(tolerance: float = 1e-08)¶
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.
PowConeND¶
- class cvxpy.constraints.power.PowConeND(W, z, alpha, axis: int = 0, constr_id=None)[source]¶
Bases:
cvxpy.constraints.constraint.Constraint
Represents a collection of N-dimensional power cone constraints that is mathematically equivalent to the following code snippet (which makes incorrect use of numpy functions on cvxpy objects):
np.prod(np.power(W, alpha), axis=axis) >= np.abs(z), W >= 0
All arguments must be Expression-like, and z must satisfy z.ndim <= 1. The columns (rows) of alpha must sum to 1 when axis=0 (axis=1).
Note: unlike PowCone3D, we make no attempt to promote alpha to the appropriate shape. The dimensions of W and alpha must match exactly.
Note: Dual variables are not currently implemented for this type of constraint.
- value(tolerance: float = 1e-08)¶
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.