XiongPengNUS / rsome

Robust Stochastic Optimization Made Easy
GNU General Public License v3.0
283 stars 54 forks source link

How to model constraint on sum of absolue values: sum(abs(x)) #6

Closed oldrichsmejkal closed 2 years ago

oldrichsmejkal commented 3 years ago

I would need to implement constraint on sum of absolute values. (E.g.: natural constraint for "Leverage" in portfolio optimization problem, while allowing negative weights)

Naive solution would be: max_leverage = 2 model.st(sum(abs(x)) <= max_leverage)

But this will ends up with error: "TypeError: 'Convex' object is not iterable"

Is there reccomended way how to approach this problem?

XiongPengNUS commented 3 years ago

This is in fact 1-norm and you could use the code:

from rsome import norm

model.st(norm(x, 1) <= max_leverage)