简体   繁体   English

如何对 la numpy 的 cvxopt 矩阵执行操作?

[英]How to perform operations on cvxopt-matrices a la numpy?

I am working with cvxopt matrices in order to use them in picos library.我正在使用 cvxopt 矩阵,以便在 picos 库中使用它们。 In general I want to take a matrix, evaluate it on a certain vector, subtract something, then take the biggest absolute value of its entries一般来说,我想取一个矩阵,在某个向量上对其进行评估,减去一些东西,然后取其条目的最大绝对值

import picos as pic
import cvxopt as cvx
import numpy as np

(...)

P = pic.Problem()
theta = P.add_variable('theta', size=k, vtype='continuous', lower=-10, upper=10)
theta

P.add_constraint(max(abs(M*theta - b)) <= 5)
P.minimize(theta)

(Here b is some vector treated as cvxopt matrix.) However, the error I get is the following: (这里 b 是一些被视为 cvxopt 矩阵的向量。)但是,我得到的错误如下:

TypeError                                 Traceback (most recent call last)
<ipython-input-11-8884e5cb14dc> in <module>
      3 theta
      4 
----> 5 P.add_constraint(max(abs(M*theta - b.T)) < 45)
      6 P.minimize(theta)
      7 

TypeError: 'Norm' object is not iterable

I was wondering if there is an alternative way of making these computations that would be acceptable to cvxopt?我想知道是否有另一种方法可以进行 cvxopt 可接受的这些计算?

(Never really used this lib apart from smaller experiments years ago) (除了几年前的小实验之外,从未真正使用过这个库)

This looks like the culprit is the classic case of hidden-magic in those highly-complex automatic-transformation modelling system tools.看起来罪魁祸首是那些高度复杂的自动转换建模系统工具中隐藏魔法的经典案例。

  • picos overloads abs in abs(M*theta - b) picos 在abs(M*theta - b)重载abs
    • see: doc见: 文档
    • this results in type Norm (a picos-based type)这导致类型Norm (基于微微的类型)
  • picos probably does not overload max in max(abs(M*theta - bT)) picos可能不会在max(abs(M*theta - bT))超载max
    • python's max-operator (not something customized from picos!) will be used which is based on linear-search on some iterable将使用 python 的 max-operator(不是从 picos 定制的东西!),它基于一些可迭代的线性搜索
    • the iterable here would be the Norm object;这里的可迭代对象是Norm对象; but it's not iterable as the error shows但它不是可迭代的,因为错误显示

See also: list of overloaded operators另请参阅: 重载运算符列表

It seems to me, this feature max is missing.在我看来,缺少此功能max You can linearize it manually, but well... that's annoying.您可以手动对其进行线性化,但是……这很烦人。

If you don't need something special of picos, cvxpy is very similar and also supports abs and max (and is based on scipy's sparse-matrices + numpy-arrays; thank god!).如果您不需要特殊的picoscvxpy非常相似,并且还支持absmax (并且基于 scipy 的稀疏矩阵 + numpy-arrays;感谢上帝!)。

I will attempt a late clarification in case other users stumble upon this question.如果其他用户偶然发现了这个问题,我将尝试进行较晚的澄清。

As @sascha pointed out, PICOS uses the Python builtin function abs to denote a norm as opposed to an entry-wise absolute value.正如@sascha 所指出的,PICOS 使用内置的 Python function abs来表示一个范数,而不是一个条目式的绝对值。 More precisely, abs denotes the absolute value of a real scalar, the modulus of a complex scalar, the Euclidean norm of a vector, and the Frobenius norm of a matrix.更准确地说, abs表示实标量的绝对值、复标量的模、向量的欧几里德范数和矩阵的弗罗贝尼乌斯范数。 (Other norms are implemented in the Norm , NuclearNorm , and SpectralNorm classes.) (其他规范在NormNuclearNormSpectralNorm类中实现。)

There are two options to pose an upper bound $b$ on the maximum entry-wise absolute value of a matrix expression $A$.有两个选项可以在矩阵表达式 $A$ 的最大条目绝对值上设置上限 $b$。 The first is to use the $L_{p,q}$-norm for $p = q = \infty$:第一种是对 $p = q = \infty$ 使用 $L_{p,q}$ 范数:

P += Norm(A, float("inf")) <= b

The second option is to give the bound via two linear inequalities:第二种选择是通过两个线性不等式给出界限:

P += A <= b, -A <= b

This works as $b$ is broadcasted to the shape of $A$ and this is also what the solver will see with the first option.这是因为 $b$ 被广播为 $A$ 的形状,这也是求解器将在第一个选项中看到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM