Conditional sum constraint in CVXOPT

99 views
Skip to first unread message

Steven Wong

unread,
Jul 25, 2016, 4:49:03 AM7/25/16
to CVXOPT
Hi guys,

I'm using CVXOPT to minimise a problem where the sum of positives needs to equal to one and sum of negatives needs to equal to -1. I've figured I could do an element-wise max and min to just sum positive and negative numbers like the following:

import cvxopt as cvx
import numpy as np
from cvxopt.modeling import variable, op, dot


a
= np.random.rand(10, 1)
x
= variable(10, 'x')
x
.value = cvx.matrix(np.array([0.1] * 10))


# constraint
pos
= (sum(cvx.max(x, 0.0)) == 1.0)
neg
= (sum(cvx.min(x, 0.0)) == -1.0)


s
= op(dot(x, a), [pos, neg])
s
.solve()


Except, I get the following error:

In [8]: pos = (sum(cvx.max(x, 0.0)) == 1.0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-fde567f535bc> in <module>()
----> 1 pos = (sum(cvx.max(x, 0.0)) == 1.0)


C
:\Anaconda3\lib\site-packages\cvxopt\__init__.py in max(*args)
   
165             return omax(omax(args[0]), 0.0)
   
166     else:
--> 167         return +reduce(base.emax, args)
   
168
   
169


TypeError: arguments must be either matrices or python numbers

This seems to be because I can do cvx.max(x), or cvx.max(__some_CVX_matrix, 0.0), but not cvx.max(variable, 0.0). I've also tried numpy.maximum and that doesn't work either. Does anyone know how I can set conditional constraints or is this prohibited because I'm breaking the convexity precondition?

Cheers,
Steve

Martin

unread,
Jul 25, 2016, 5:50:23 AM7/25/16
to CVXOPT
First of all, your equality constraints `pos` and `neg` are nonconvex. However, that is not why you get an error: there is a difference between cvxopt.max and cvxopt.modeling.max, and you're using cvxopt.max instead of cvxopt.modeling.max.

Steven Wong

unread,
Jul 25, 2016, 7:05:23 PM7/25/16
to CVXOPT
Whoops, that is indeed the error. Thanks!

The next problem I ran into was that the constraint wasn't convex so it still didn't work, but that's my fault.
Reply all
Reply to author
Forward
0 new messages