2

I want to solve the optimization of maximizing the objective which is the sum of the absolute value of a matrix. However, I cannot find the accurate way to get that:

for example:

import mosek.fusion as mf
from mosek.fusion import Model, Domain, Expr, ObjectiveSense

with Model('lol') as M:
    x = M.variable('x', [5,1], Domain.inRange(-1,1))
    M.objective('obj', ObjectiveSense.Maximize, abs(x))
    M.solve()

does not work

Type Error: bad operand type for abs(): 'RangedVariable'

How can I get the absolute value of x?

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Zihan Yang
  • 31
  • 3

1 Answers1

0

First, you can't just apply arbitrary Python functions to mosek.fusion objects! Consult https://docs.mosek.com/8.1/pythonfusion/api-reference.html for what is available.

Second, maximizing absolute value is not a convex problem! You will have to use mixed-integer programming if you really want to model it. Consult https://docs.mosek.com/modeling-cookbook/mio.html

  • You really helped me a lot...so could I combine this two different model?.. Recently, I wanna add the turnover ratio constrain in my stock model, however it refers to the absolute value of difference between previous position and current position...I have no idea whether Mosek can work on that.. do you have some experience about this? Professor! – Zihan Yang Feb 11 '19 at 10:50
  • @ZihanYang are you sure you want to maximize the abs() ? If you write a mathematical model of the problem then we can think about it. I would recommend the mosek google group since this is a modeling problem. – Michal Adamaszek Feb 11 '19 at 12:00
  • pre_w is the previous day weight of stocks in my portfolio, size of [n,1]. cur_w is the variable should be solved to maximize portfolio's score, which is the today weight of stocks in the portfolio. Now I have every stock's score and other constraint refers to risk and industry. The Mosek works well so far. Then I wanna add a constraint which is the absolute value of difference between pre_w and cur_w that less than one number to limit the turnover ratio per day... Does this make sense?......Thank you very much!!!! – Zihan Yang Feb 13 '19 at 09:40
  • So you want to minimize (upper bound) and not maximize absolute value. This is easy. t >= abs(a-b) is the same as t>= a-b and t>= b-a. See also cookbook linked above. – Michal Adamaszek Feb 13 '19 at 12:48
  • hmm..I have watched this cookbook.However, my question refers to the minimize sum value of diff of matrix..so , t>=b-a and t>= a-b does not work...this is so hard.. – Zihan Yang Feb 22 '19 at 10:07