表达式¶
CVXPY 将数学对象表示为表达式树。表达式树是由一个或多个原子连接在一起的数学表达式的集合。表达式树被编码为Expression类的实例,树中的每个Leaf是一个Variable、Parameter或Constant。
表达式¶
- class cvxpy.expressions.expression.Expression[source]¶
基础类:
Canonical凸优化问题中的数学表达式。
重载了许多运算符,以便于方便地创建复合表达式(例如,两个表达式的和)和约束。
- __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[源代码]¶
将表达式提升到幂。
- __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]¶
表达式:一个表达式除以另一个表达式。
- abstract property grad¶
给出表达式相对于每个变量的(子/超)梯度。
矩阵表达式是向量化的,因此梯度是一个矩阵。
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表达式树的叶节点;即变量、常量或参数。
一个叶子可能携带属性,这些属性限制了它允许的值的集合。叶子最多只能有一个属性,除了一个叶子可能同时是
nonpos和nonneg,或者在某些索引中是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 shape : tuple[int, ...]¶
表达式的维度。
- 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 shape : tuple[int, ...]¶
表达式的维度。
- 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 shape : tuple[int, ...]¶
表达式的维度。
常量¶
-
class cvxpy.expressions.constants.Constant(value, name: str | None =
None)[来源]¶ 基础类:
Leaf一个常量值。
原始数值常量,如Python原始类型或NumPy ndarrays,通过表达式运算符重载隐式转换为常量。例如,如果
x是一个表达式,而``c``是一个原始常量,那么x + c通过将c转换为常量来创建一个表达式。- property shape : tuple[int, ...]¶
返回表达式的(行,列)维度。
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 shape : tuple[int, ...]¶
表达式的维度。
- property value¶
评估回调以获取值。