Mosek - unsupported operand type(s) for -: 'list' and 'LinearVariable'

69 Views Asked by At

I am trying to optimize a function. The goal is to optimize this function :

√((weigths_b-x)^T (b)(weights_b-x))

My goal is to use Mosek. However I am quite new and I don't know how to beginn with the problem. I went through the Cookbook, however I don't understand how I could implement.

Here is an example how I tried to implement it :

b = [[1.0,2.0,3.0], 
     [2.0,6.0,8.0], 
     [8.0,1.0,5.0]]

weights_b =[[0.3],[0.5],[0.2]]

n_opt = 3

import sys
from mosek.fusion import *

def main(args):


    # Create a model with the name 'lo1'
    with Model("lo1") as M:

        # Create variable 'x' of length 4
        x = M.variable("x", n_opt, Domain.greaterThan(0.0))

        # Create constraints
        # For now, I don't have any constraint


        # Set the objective function to (c^t * x)
        M.objective("obj", ObjectiveSense.Minimize, Expr.mul(Expr.mul(Expr.transpose(Expr.sub(weights_b-x)),b),Expr.sub(weights_b-x)))

        # Solve the problem
        M.solve()

        # Get the solution values
        sol = x.level()
        print('\n'.join(["x[%d] = %f" % (i, sol[i]) for i in range(n_opt)]))

if __name__ == '__main__':
    main(sys.argv[1:])

However my code returns this error: unsupported operand type(s) for -: 'list' and 'LinearVariable'

Also I didn't found an smart solution to implement the square root in the function (as in the picture).

Do you maybe know a good example that I could reproduce? Or a good explanation, other than the Cookbook?

I thank you in advance for your help !

0

There are 0 best solutions below