How to perform operations on cvxopt-matrices a la numpy?
Question:
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?
Answers:
(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!).
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. 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. (Other norms are implemented in the Norm
, NuclearNorm
, and SpectralNorm
classes.)
There are two options to pose an upper bound $b$ on the maximum entry-wise absolute value of a matrix expression $A$. The first is to use the $L_{p,q}$-norm for $p = q = infty$:
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.
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?
(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
inabs(M*theta - b)
- see: doc
- this results in type
Norm
(a picos-based type)
- picos probably does not overload
max
inmax(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!).
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. 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. (Other norms are implemented in the Norm
, NuclearNorm
, and SpectralNorm
classes.)
There are two options to pose an upper bound $b$ on the maximum entry-wise absolute value of a matrix expression $A$. The first is to use the $L_{p,q}$-norm for $p = q = infty$:
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.