# Constraints¶

A constraint is an equality or inequality that restricts the domain of an optimization problem. CVXPY has five types of constraints: non-positive, equality or zero, positive semidefinite, second-order 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=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]

A constraint of the form $$x \leq 0$$.

The preferred way of creating a NonPos constraint is through operator overloading. To constrain an expression x to be non-positive, simply write x <= 0; to constrain x to be non-negative, write x >= 0. The former creates a NonPos constraint with x 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=False)[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=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]

A constraint of the form $$x = 0$$.

The preferred way of creating a Zero constraint is through operator overloading. To constrain an expression x to be zero, simply write x == 0. The former creates a Zero constraint with x as its argument.

is_dcp(dpp=False)[source]

A zero constraint is DCP if its argument is affine.

value(tolerance=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]

A constraint of the form $$\frac{1}{2}(X + X^T) \succcurlyeq_{S_n^+} 0$$

Applying a PSD constraint to a two-dimensional expression X constrains its symmetric part to be positive semidefinite: i.e., it constrains X 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 expression X to be PSD, write X >> 0; to constrain it to be negative semidefinite, write X << 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=False)[source]

A PSD constraint is DCP if the constrained expression is affine.

value(tolerance=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=0, constr_id=None)[source]

A second-order cone constraint for each row/column.

Assumes t is a vector the same length as X’s columns (rows) for axis == 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=False)[source]

An SOC constraint is DCP if each of its arguments is affine.

value(tolerance=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]

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.

is_dcp(dpp=False)[source]

An exponential constraint is DCP if each argument is affine.

value(tolerance=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.