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

♀尐吖头ヾ 提交于 2019-12-24 06:33:34

问题


I am working with cvxopt matrices in order to use them in picos library. 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:

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?


回答1:


(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)
    • see: doc
    • this results in type Norm (a picos-based type)
  • picos probably does not overload max in max(abs(M*theta - b.T))
    • python's max-operator (not something customized from picos!) will be used which is based on linear-search on some iterable
    • the iterable here would be the Norm object; but it's not iterable as the error shows

See also: list of overloaded operators

It seems to me, this feature max is missing. 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!).



来源:https://stackoverflow.com/questions/59398822/how-to-perform-operations-on-cvxopt-matrices-a-la-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!