表达式

CVXPY 将数学对象表示为表达式树。表达式树是由一个或多个原子连接在一起的数学表达式的集合。表达式树被编码为Expression类的实例,树中的每个Leaf是一个VariableParameterConstant

表达式

class cvxpy.expressions.expression.Expression[source]

基础类:Canonical

凸优化问题中的数学表达式。

重载了许多运算符,以便于方便地创建复合表达式(例如,两个表达式的和)和约束。

property T

表达式的转置。

Type:

Expression

__add__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:将两个表达式相加。

__div__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:一个表达式除以另一个表达式。

__eq__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])[source]

相等性:创建一个约束 self == other

__ge__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])[source]

返回 self>=value。

__le__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])[source]

不等式:创建一个不等式约束 self <= other

__lshift__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) PSD[source]

PSD : 创建一个负半定不等式。

__matmul__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:两个表达式的矩阵乘法。

__mul__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:两个表达式的乘积。

__pow__(power: float) Expression[源代码]

将表达式提升到幂。

Parameters:
power : float

表达式要提升到的幂。

Returns:

表达式被提升到 power

Return type:

Expression

__radd__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[源代码]

表达式:将两个表达式相加。

__rdiv__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[源代码]

表达式:用于数字/表达式。

__rlshift__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) PSD[source]

PSD : 创建一个负半定不等式。

__rmatmul__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:用于矩阵 @ 表达式的调用。

__rmul__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:用于数字 * 表达式。

__rrshift__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) PSD[source]

PSD : 创建一个半正定不等式。

__rshift__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) PSD[源代码]

PSD : 创建一个半正定不等式。

__rsub__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[源代码]

表达式:两个表达式的差异。

__rtruediv__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:用于数字/表达式。

__sub__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[源代码]

表达式:两个表达式的差异。

__truediv__(other: Expression | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Expression[source]

表达式:一个表达式除以另一个表达式。

property curvature : str

表达式的曲率。

Type:

字符串

abstract property domain

描述表达式有限的区域闭合的约束条件。

Type:

列表

abstract property grad

给出表达式相对于每个变量的(子/超)梯度。

矩阵表达式是向量化的,因此梯度是一个矩阵。

Returns:

变量到SciPy CSC稀疏矩阵的映射;如果变量值缺失则为None。

Return type:

字典

is_affine() bool[来源]

表达式是仿射的吗?

abstract is_concave() bool[源代码]

表达式是凹的吗?

is_constant() bool[source]

表达式是常量吗?

abstract is_convex() bool[source]

这个表达式是凸的吗?

is_dcp(dpp: bool = False) bool[source]

检查表达式是否为DCP。

Parameters:
dpp : bool, optional

如果为True,强制执行规范参数化编程(DPP)规则集;仅当问题涉及参数时相关。

Returns:

如果表达式是DCP,则为True,否则为False。

Return type:

布尔

is_dgp(dpp: bool = False) bool[源代码]

检查表达式是否为对数-对数DCP。

Returns:

如果表达式是log-log DCP,则为True,否则为False。

Return type:

布尔

abstract is_dpp(context: str = 'dcp') bool[来源]

表达式是一个有纪律的参数化表达式。

is_dqcp() bool[source]

检查表达式是否为DQCP。

Returns:

如果表达式是DQCP,则为True,否则为False。

Return type:

布尔

is_log_log_affine() bool[来源]

表达式是仿射的吗?

abstract is_log_log_concave() bool[source]

表达式是对数-对数凹的吗?

abstract is_log_log_convex() bool[source]

表达式是否为对数-对数凸函数?

abstract is_nonneg() bool[来源]

表达式是正的吗?

abstract is_nonpos() bool[来源]

表达式是否为负?

is_zero() bool[来源]

表达式是否全为零?

abstract name() str[source]

str : 表达式的字符串表示。

property ndim : int

表达式中形状的维度数量。

Type:

整数

abstract property shape : tuple[int, ...]

表达式的维度。

Type:

元组

property sign : str

表达式的符号。

Type:

字符串

property size : int

表达式中的条目数。

Type:

整数

abstract property value

表达式的数值。

Type:

NumPy.ndarray 或 None

Leaf

class cvxpy.expressions.leaf.Leaf(shape: int | tuple[int, ...], value=None, nonneg: bool = False, nonpos: bool = False, complex: bool = False, imag: bool = False, symmetric: bool = False, diag: bool = False, PSD: bool = False, NSD: bool = False, hermitian: bool = False, boolean: Iterable | bool = False, integer: Iterable | bool = False, sparsity: Iterable | bool = False, pos: bool = False, neg: bool = False, bounds: Iterable | None = None)[source]

基础类:Expression

表达式树的叶节点;即变量、常量或参数。

一个叶子可能携带属性,这些属性限制了它允许的值的集合。叶子最多只能有一个属性,除了一个叶子可能同时是nonposnonneg,或者在某些索引中是boolean而在其他索引中是integer

如果给叶子节点分配了一个与其一个或多个属性相矛盾的值,则会引发错误。有关将值投影到叶子节点域的便捷方法,请参见project方法。

Parameters:
shape : int or tuple of ints

叶子的形状,例如,(3, 2)2

value : numeric type

要分配给叶节点的值。

nonneg : bool

变量是否被限制为非负?

nonpos : bool

变量是否被限制为非正数?

complex : bool

变量是复数值吗?

symmetric : bool

变量是对称的吗?

diag : bool

变量是对角线的吗?

PSD : bool

变量是否被约束为半正定?

NSD : bool

变量是否被约束为半负定?

Hermitian : bool

变量是厄米特矩阵吗?

boolean : bool or Iterable

变量是否为布尔类型?True,这将约束整个变量为布尔类型,False,或者是一个应该被约束为布尔类型的索引列表,其中每个索引是一个长度恰好等于形状长度的元组。

integer : bool or Iterable

变量是整数吗?语义与布尔参数相同。

sparsity : Iterable

变量是稀疏的吗?

pos : bool

变量是正的吗?

neg : bool

变量是否为负数?

bounds : Iterable

一个长度为2的可迭代对象,指定下限和上限。

property T

表达式的转置。

Type:

Expression

property ndim : int

表达式中形状的维度数量。

Type:

整数

project(val)[source]

将值投影到叶子的属性集上。

一个合理的惯用法是 leaf.value = leaf.project(val)

Parameters:
val : numeric type

分配的值。

Returns:

四舍五入到属性类型的值。

Return type:

数值类型

project_and_assign(val) None[source]

项目并给变量赋值。

property shape : tuple[int, ...]

表达式的维度。

property size : int

表达式中的条目数。

Type:

整数

property value : ndarray | None

表达式的数值。

变量

class cvxpy.expressions.variable.Variable(shape: int | Iterable[int] = (), name: str | None = None, var_id: int | None = None, **kwargs: Any)[source]

基础类: Leaf

问题中的优化变量。

property T

表达式的转置。

Type:

Expression

name() str[source]

变量的名称。

property ndim : int

表达式中形状的维度数量。

Type:

整数

project(val)

将值投影到叶子的属性集上。

一个合理的惯用法是 leaf.value = leaf.project(val)

Parameters:
val : numeric type

分配的值。

Returns:

四舍五入到属性类型的值。

Return type:

数值类型

project_and_assign(val) None

项目并给变量赋值。

property shape : tuple[int, ...]

表达式的维度。

property size : int

表达式中的条目数。

Type:

整数

property value : ndarray | None

表达式的数值。

参数

class cvxpy.expressions.constants.parameter.Parameter(shape: int | tuple[int, ...] = (), name: str | None = None, value=None, id=None, **kwargs)[来源]

基础类: Leaf

优化问题中的参数。

参数是常量表达式,其值可以在问题创建后指定。创建问题后修改问题的唯一方法是通过参数。例如,您可能选择将机器学习模型的超参数声明为Parameter对象;更一般地说,参数对于计算权衡曲线非常有用。

property T

表达式的转置。

Type:

Expression

property ndim : int

表达式中形状的维度数量。

Type:

整数

project(val)

将值投影到叶子的属性集上。

一个合理的惯用法是 leaf.value = leaf.project(val)

Parameters:
val : numeric type

分配的值。

Returns:

四舍五入到属性类型的值。

Return type:

数值类型

project_and_assign(val) None

项目并给变量赋值。

property shape : tuple[int, ...]

表达式的维度。

property size : int

表达式中的条目数。

Type:

整数

property value

参数的数值。

Type:

NumPy.ndarray 或 None

常量

class cvxpy.expressions.constants.Constant(value, name: str | None = None)[来源]

基础类: Leaf

一个常量值。

原始数值常量,如Python原始类型或NumPy ndarrays,通过表达式运算符重载隐式转换为常量。例如,如果x是一个表达式,而``c``是一个原始常量,那么x + c通过将c转换为常量来创建一个表达式。

property T

表达式的转置。

Type:

Expression

property ndim : int

表达式中形状的维度数量。

Type:

整数

property shape : tuple[int, ...]

返回表达式的(行,列)维度。

property size : int

表达式中的条目数。

Type:

整数

property value

常量的数值。

Type:

NumPy.ndarray 或 None

CallbackParam

class cvxpy.expressions.constants.callback_param.CallbackParam(callback: Callable, shape: int | tuple[int, ...] = (), **kwargs)[source]

基础类:Parameter

一个参数,其值来自回调函数。

启用写入替换表达式,该表达式不会通过自动更新其值的新参数成为DPP。

示例: 使用 p 和 q 参数,p * q 不是 DPP,但 pq = CallbackParameter(callback=lambda: p.value * q.value) 是 DPP。

当只有p和q应该暴露给用户,但内部需要pq时,这很有用。

property T

表达式的转置。

Type:

Expression

property ndim : int

表达式中形状的维度数量。

Type:

整数

project(val)

将值投影到叶子的属性集上。

一个合理的惯用法是 leaf.value = leaf.project(val)

Parameters:
val : numeric type

分配的值。

Returns:

四舍五入到属性类型的值。

Return type:

数值类型

project_and_assign(val) None

项目并给变量赋值。

property shape : tuple[int, ...]

表达式的维度。

property size : int

表达式中的条目数。

Type:

整数

property value

评估回调以获取值。