Using MOSEK from Java

 
There are two ways to use MOSEK from Java:
Since MOSEK 7.0 we recommend using the Fusion API unless fine-grained control over the generated model is required.

Optimizer Java API

The optimizer API operates on a model with one vector of variables and one vector of constraints. It allows adding or deleting elements and reoptimizing, and allows use of advanced call-back functions.
The optimizer Java API is fully documented in MOSEK optimizer Java API.

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. private static final String[]  
  3. securities = { "hardware", "software", "show-biz", "t-bills" }; 
  4. // Mean returns on securities 
  5. private static final double[]  
  6. mean = { 8.0, 9.0, 12.0, 7.0 }; 
  7. // Target mean return 
  8. private static final double  
  9. target = 10.0
  10.  
  11. private static final int numsec = securities.length
  12.  
  13. // Factor of covariance matrix. 
  14. private static final double[][] U_data =  
  15. { { 2.0 , 1.5 , -0.5 , 0.0 }, 
  16. { 0.0 , 1.93649167, 0.90369611, 0.0 }, 
  17. { 0.0 , 0.0 , 2.98886824, 0.0 }, 
  18. { 0.0 , 0.0 , 0.0 , 0.0 } }; 
  19. private static final Matrix U = new DenseMatrix(U_data); 
Then we can create the Model object:
  1. Model M = new Model("alan"); 
  2. try 
  3. { 
Then we define the variables as
  1. Variable x = M.variable("x", numsec, Domain.greaterThan(0.0)); 
  2. Variable t = M.variable("t", 1, Domain.greaterThan(0.0)); 
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(mean, x), Domain.greaterThan(target)); 
and the conic constraint
  1. M.constraint("t > ||Ux||_2",Expr.vstack(0.5, t, Expr.mul(U,x)), Domain.inRotatedQCone()); 
We optimize the model using
  1. M.solve(); 
and extract the x-solution as
  1. double[] solx = x.level(); 

Compiling and running on Windows

Assuming that MOSEK has been installed, the example can be compiled with
  1. javac -classpath "C:\Program Files\mosek\7\tools\platform\win64x86\binmosek.jar" -d . alan.java 
then run with
  1. java -classpath ".;C:\Program Files\mosek\7\tools\platform\win64x86\bin\mosek.jar" com.mosek.fusion.examples.alan 

Compiling and running on Linux/Mac OS X

Assuming that MOSEK has been installed in the user's home directory', the example can be compiled with
  1. javac -classpath $HOME/mosek/7/tools/platform/win64x86/bin/mosek.jar -d . alan.java 
then run with
  1. java -classpath $HOME/mosek/7/tools/platform/win64x86/bin/mosek.jar:. com.mosek.fusion.examples.alan 

Example alan for fusion

  1. // 
  2. // Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved. 
  3. // 
  4. // File: alan.java 
  5. // 
  6. // Purpose: This file contains an implementation of the alan.gms (as 
  7. // found in the GAMS online model collection) using Java/MBI.  
  8. // 
  9. // The model is a simple portfolio choice model. The objective is to 
  10. // invest in a number of assets such that we minimize the risk, while 
  11. // requiring a certain expected return. 
  12. // 
  13. // We operate with 4 assets (hardware,software, show-biz and treasure 
  14. // bill). The risk is defined by the covariance matrix 
  15. // Q = [[ 4.0, 3.0, -1.0, 0.0 ], 
  16. // [ 3.0, 6.0, 1.0, 0.0 ], 
  17. // [ -1.0, 1.0, 10.0, 0.0 ], 
  18. // [ 0.0, 0.0, 0.0, 0.0 ]] 
  19. //  
  20. // 
  21. // We use the form Q = U^T * U, where U is a Cholesky factor of Q. 
  22. // 
  23.  
  24. package com.mosek.fusion.examples
  25. import mosek.fusion.*; 
  26.  
  27. public class alan 
  28. ///////////////////////////////////////////////////////////////////// 
  29. // Problem data. 
  30. // Security names 
  31. private static final String[]  
  32. securities = { "hardware", "software", "show-biz", "t-bills" }; 
  33. // Mean returns on securities 
  34. private static final double[]  
  35. mean = { 8.0, 9.0, 12.0, 7.0 }; 
  36. // Target mean return 
  37. private static final double  
  38. target = 10.0
  39.  
  40. private static final int numsec = securities.length
  41.  
  42. // Factor of covariance matrix. 
  43. private static final double[][] U_data =  
  44. { { 2.0 , 1.5 , -0.5 , 0.0 }, 
  45. { 0.0 , 1.93649167, 0.90369611, 0.0 }, 
  46. { 0.0 , 0.0 , 2.98886824, 0.0 }, 
  47. { 0.0 , 0.0 , 0.0 , 0.0 } }; 
  48. private static final Matrix U = new DenseMatrix(U_data); 
  49.  
  50. public static void main(String[] args
  51. throws SolutionError 
  52. Model M = new Model("alan"); 
  53. try 
  54.  
  55. Variable x = M.variable("x", numsec, Domain.greaterThan(0.0)); 
  56. Variable t = M.variable("t", 1, Domain.greaterThan(0.0)); 
  57. M.objective("minvar", ObjectiveSense.Minimize, t); 
  58.  
  59. // sum securities to 1.0 
  60. M.constraint("wealth", Expr.sum(x), Domain.equalsTo(1.0)); 
  61. // define target expected return  
  62. M.constraint("dmean", Expr.dot(mean, x), Domain.greaterThan(target)); 
  63.  
  64. M.constraint("t > ||Ux||_2",Expr.vstack(0.5, t, Expr.mul(U,x)), Domain.inRotatedQCone()); 
  65. M.setLogHandler(new java.io.PrintWriter(System.out));  
  66. System.out.println("Solve..."); 
  67. M.solve(); 
  68.  
  69. System.out.println("... Solved."); 
  70.  
  71. double[] solx = x.level(); 
  72. System.out.printf("Solution = %e", solx[0]); 
  73. for (int i = 1; i < numsec; ++i
  74. System.out.printf(", %e", solx[i]); 
  75. System.out.print("\n"); 
  76. finally 
  77. M.dispose(); 
  78. } 

Online documentation