Using MOSEK from MATLAB

 
There are two ways to use MOSEK from MATLAB:
The optimization toolbox is the simplest to use for models that are expressed on matrix form whereas the Fusion API is more advantageous for complicated models. In particular this is the case for many semi-definite optimization problems.
Users that are accustomed with ane like the MathWorks optimization toolbox are likely to prefer the MOSEK optimization toolbox.

Optimization toolbox

The optimization toolbox provides a number of functions that allows the user to:
The optimization toolbox is matrix oriented and fairly similar to optimization toolbox by MathWorks. Moreover, the MOSEK optimization toolbox provides several functions that are compatible with optimization toolbox available from MathWorks.

MOSEK/Fusion

The Fusion API provides a model-oriented API with objects for representing variables and constraints, and mechanisms for handling sparse and multi-dimensional variable sets. The Fusion API is more restrictive than the optimizer API: Some properties of variables and constraints are immutable once created, and it is not possible to delete them.
The Fusion API allows a simple declaration of variables x \in{} D as well as affine functions (Ax-b)\in{} S where D and S are simple predefined convex sets. For example, for a given matrix A\in{}\mathbb{R}^{m{\times} n} and vector b\in{}\mathbb{R}^{m} we may have
 (Ax-b)\in{}\mathcal{Q}^{m}, {\ }{\ }{\ } x \geq{} 0
where
 \mathcal{Q}^{m} = \left\{{} {\ } x \in{}\mathbb{R}^{m} {\ } | {\ } x_{1} \geq{}\sqrt{x_{2}^{2} + \ldots{} + x_{m}^{2}}{\ }\right\}{}
is a standard quadratic cone. More generally, variables and affine functions of variables are specified as belonging to either
The following sections discuss a simple model implemented using the MOSEK Fusion API.

Example: Portfolio selection

The following simple portfolio selection model, “alan”, comes from the GAMS online model collection. The objective is to invest the total wealth of 1.0 in a number of assets such that we minimize the risk, while requiring a certain expected return d.
We operate with 4 assets, hardware, software, show-biz and the risk-less treasure bill. The risk is defined by the covariance matrix
 Q = \left[{} \begin{array}{rrrr} \displaystyle{} 4.0 &\displaystyle{} 3.0 &\displaystyle{} -1.0 &\displaystyle{} 0.0 \\[0pt] \displaystyle{} 3.0 &\displaystyle{} 6.0 &\displaystyle{} 1.0 &\displaystyle{} 0.0 \\[0pt] \displaystyle{} -1.0 &\displaystyle{} 1.0 &\displaystyle{} 10.0 &\displaystyle{} 0.0 \\[0pt] \displaystyle{} 0.0 &\displaystyle{} 0.0 &\displaystyle{} 0.0 &\displaystyle{} 0.0 \\[0pt] \end{array} \right]{}
and the expected returns
 r = ( 8.0, 9.0, 12.0, 7.0 ),
respectively. A mathematical description of the model is then given as the quadratic optimization problem:
 \begin{array}{ll} \displaystyle{} \mbox{minimize}&\displaystyle{} x^{T}Qx \\[0pt] \displaystyle{} \mbox{subject to}&\displaystyle{} r^{T}x = d \\[0pt] \displaystyle{} &\displaystyle{} \sum_{i=1}^{n} x_{i} = 1 \\[0pt] \displaystyle{} &\displaystyle{} x \geq{} 0 \\[0pt] \end{array}
This is not directly applicable to Fusion, which requires a conic formulation. To that end, let
 Q = U^{T} U
be a Cholesky factorization with Cholesky factor
 U = \left[{} \begin{array}{rrrr} \displaystyle{} 2.00 &\displaystyle{} 1.50 &\displaystyle{} -0.50 &\displaystyle{} 0.00 \\[0pt] \displaystyle{} 0.00 &\displaystyle{} 1.94 &\displaystyle{} 0.90 &\displaystyle{} 0.00 \\[0pt] \displaystyle{} 0.00 &\displaystyle{} 0.00 &\displaystyle{} 2.99 &\displaystyle{} 0.00 \\[0pt] \displaystyle{} 0.00 &\displaystyle{} 0.00 &\displaystyle{} 0.00 &\displaystyle{} 0.00 \\[0pt] \end{array} \right]{}.
An equivalent formulation is then
 \begin{array}{ll} \displaystyle{} \mbox{minimize}&\displaystyle{} t \\[0pt] \displaystyle{} \mbox{subject to}&\displaystyle{} r^{T} x = d \\[0pt] \displaystyle{} &\displaystyle{} \sum_{i=1}^{n} x_{i} = 1 \\[0pt] \displaystyle{} &\displaystyle{} t \geq{}\left\Vert{} U x \right\Vert{}^{2} \\[0pt] \displaystyle{} &\displaystyle{} x \geq{} 0, \\[0pt] \end{array}
which we can write explicitly in conic form as
 \begin{array}{ll} \displaystyle{} \mbox{minimize}&\displaystyle{} t \\[0pt] \displaystyle{} \mbox{subject to}&\displaystyle{} r^{T} x = d \\[0pt] \displaystyle{} &\displaystyle{} \sum_{i=1}^{n} x_{i} = 1 \\[0pt] \displaystyle{} &\displaystyle{} (t, \frac{1}{2}, Ux) \in{}\mathcal{Q}_{r}^{6} \\[0pt] \displaystyle{} &\displaystyle{} x\geq{} 0, \\[0pt] \end{array}
where
 \mathcal{Q}_{r}^{n} = \left\{{} x \in{}\mathbb{R}^{n} {\ } | {\ } 2 x_{1} x_{2} \geq{} x_{3}^{2} + \ldots{} + x_{n}^{2} \right\}{}
is a standard rotated quadratic cone.
To implement the model, we first define the data for the problem:
  1. % Security names 
  2. securities = [ 'hardware', 'software', 'show-biz', 't-bills' ]; 
  3. % Mean returns on securities 
  4. meanreturn = [ 8.0; 9.0; 12.0; 7.0 ]; 
  5. % Target mean return 
  6. target = 10.0
  7.  
  8. % Factor of covariance matrix. 
  9. U_data = [ 2.0 , 1.5 , -0.5 , 0.0 ; ... 
  10. 0.0 , 1.93649167, 0.90369611 , 0.0 ; ... 
  11. 0.0 , 0.0 , 2.98886824 , 0.0 ; ... 
  12. 0.0 , 0.0 , 0.0 , 0.0 ];  
  13.  
  14. numsec = size(meanreturn,1); 
  15. U = DenseMatrix(U_data); 
Then we can create the Model object:
  1. M = Model('alan'); 
Then we define the variables as
  1. x = M.variable('x', numsec, Domain.greaterThan(0.0)); 
  2. v = M.variable('variance', 1, Domain.greaterThan(0.0)); 
  3. w = M.variable('w', 1, Domain.equalsTo(1.0)); 
  4. t = M.variable('t', 1, Domain.unbounded()); 
and the two linear constraints
  1. % sum securities to 1.0 
  2. M.constraint('wealth', Expr.sum(x), Domain.equalsTo(1.0)); 
  3. % define target expected return  
  4. M.constraint('dmean', Expr.dot(meanreturn', x), Domain.greaterThan(target)); 
and the conic constraint
  1. % (t,0.5,U*x) \in rQcone 
  2. M.constraint('cone', Expr.vstack(t, ... 
  3. 1.0, ... 
  4. Expr.mul(U,x)), ... 
  5. Domain.inRotatedQCone()); 
We optimize the model using
  1. disp('Solve...'); 
  2. M.solve(); 
and extract the x-solution as
  1. solx = x.level(); 

Example alan for Fusion

  1. function alan() 
  2. % 
  3. % Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved. 
  4. % 
  5. % File: alan.m 
  6. % 
  7. % Purpose: This file contains an implementation of the alan.gms (as 
  8. % found in the GAMS online model collection) using MOSEK Fusion.  
  9. % 
  10. % The model is a simple portfolio choice model. The objective is to 
  11. % invest in a number of assets such that we minimize the risk, while 
  12. % requiring a certain expected return. 
  13. % 
  14. % We operate with 4 assets (hardware,software, show-biz and treasure 
  15. % bill). The risk is defined by the covariance matrix 
  16. % Q = [[ 4.0, 3.0, -1.0, 0.0 ], 
  17. % [ 3.0, 6.0, 1.0, 0.0 ], 
  18. % [ -1.0, 1.0, 10.0, 0.0 ], 
  19. % [ 0.0, 0.0, 0.0, 0.0 ]] 
  20. %  
  21. % 
  22. % We use the form Q = U'*U, where U is a Cholesky factor of Q. 
  23. % 
  24.  
  25. import mosek.fusion.* 
  26.  
  27. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/ 
  28. % Problem data. 
  29. % Security names 
  30. securities = [ 'hardware', 'software', 'show-biz', 't-bills' ]; 
  31. % Mean returns on securities 
  32. meanreturn = [ 8.0; 9.0; 12.0; 7.0 ]; 
  33. % Target mean return 
  34. target = 10.0
  35.  
  36. % Factor of covariance matrix. 
  37. U_data = [ 2.0 , 1.5 , -0.5 , 0.0 ; ... 
  38. 0.0 , 1.93649167, 0.90369611 , 0.0 ; ... 
  39. 0.0 , 0.0 , 2.98886824 , 0.0 ; ... 
  40. 0.0 , 0.0 , 0.0 , 0.0 ];  
  41.  
  42. numsec = size(meanreturn,1); 
  43. U = DenseMatrix(U_data); 
  44.  
  45. M = Model('alan'); 
  46. x = M.variable('x', numsec, Domain.greaterThan(0.0)); 
  47. v = M.variable('variance', 1, Domain.greaterThan(0.0)); 
  48. w = M.variable('w', 1, Domain.equalsTo(1.0)); 
  49. t = M.variable('t', 1, Domain.unbounded()); 
  50.  
  51. % sum securities to 1.0 
  52. M.constraint('wealth', Expr.sum(x), Domain.equalsTo(1.0)); 
  53. % define target expected return  
  54. M.constraint('dmean', Expr.dot(meanreturn', x), Domain.greaterThan(target)); 
  55.  
  56. % (t,0.5,U*x) \in rQcone 
  57. M.constraint('cone', Expr.vstack(t, ... 
  58. 1.0, ... 
  59. Expr.mul(U,x)), ... 
  60. Domain.inRotatedQCone()); 
  61.  
  62. M.objective('minvar', ObjectiveSense.Minimize, t); 
  63.  
  64. disp('Solve...'); 
  65. M.solve(); 
  66. disp('... Solved.'); 
  67.  
  68. solx = x.level(); 
  69. disp([ 'Solution = ' mat2str(solx) ]); 

Online documentation