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.
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 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

as well as affine functions

where

and

are simple predefined convex sets. For example, for a given matrix

and vector

we may have
where
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.
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

.
We operate with 4 assets, hardware, software, show-biz and the risk-less treasure bill. The risk is defined by the covariance matrix
and the expected returns
respectively. A mathematical description of the model is then given as the quadratic optimization problem:
This is not directly applicable to Fusion, which requires a conic formulation. To that end, let
be a Cholesky factorization with Cholesky factor
An equivalent formulation is then
which we can write explicitly in conic form as
where
is a standard rotated quadratic cone.
To implement the model, we first define the data for the problem:
-
- private static final String[]
- securities = { "hardware", "software", "show-biz", "t-bills" };
-
- private static final double[]
- mean = { 8.0, 9.0, 12.0, 7.0 };
-
- private static final double
- target = 10.0;
-
- private static final int numsec = securities.length;
-
-
- private static final double[][] U_data =
- { { 2.0 , 1.5 , -0.5 , 0.0 },
- { 0.0 , 1.93649167, 0.90369611, 0.0 },
- { 0.0 , 0.0 , 2.98886824, 0.0 },
- { 0.0 , 0.0 , 0.0 , 0.0 } };
- private static final Matrix U = new DenseMatrix(U_data);
Then we can create the Model object:
- Model M = new Model("alan");
- try
- {
Then we define the variables as
- Variable x = M.variable("x", numsec, Domain.greaterThan(0.0));
- Variable t = M.variable("t", 1, Domain.greaterThan(0.0));
and the two linear constraints
-
- M.constraint("wealth", Expr.sum(x), Domain.equalsTo(1.0));
-
- M.constraint("dmean", Expr.dot(mean, x), Domain.greaterThan(target));
and the conic constraint
- M.constraint("t > ||Ux||_2",Expr.vstack(0.5, t, Expr.mul(U,x)), Domain.inRotatedQCone());
We optimize the model using
- M.solve();
and extract the

-solution as
- double[] solx = x.level();
Assuming that MOSEK has been installed, the example can be compiled with
- javac -classpath "C:\Program Files\mosek\7\tools\platform\win64x86\binmosek.jar" -d . alan.java
then run with
- java -classpath ".;C:\Program Files\mosek\7\tools\platform\win64x86\bin\mosek.jar" com.mosek.fusion.examples.alan
Assuming that MOSEK has been installed in the user's home directory', the example can be compiled with
- javac -classpath $HOME/mosek/7/tools/platform/win64x86/bin/mosek.jar -d . alan.java
then run with
- java -classpath $HOME/mosek/7/tools/platform/win64x86/bin/mosek.jar:. com.mosek.fusion.examples.alan
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- package com.mosek.fusion.examples;
- import mosek.fusion.*;
-
- public class alan
- {
-
-
-
- private static final String[]
- securities = { "hardware", "software", "show-biz", "t-bills" };
-
- private static final double[]
- mean = { 8.0, 9.0, 12.0, 7.0 };
-
- private static final double
- target = 10.0;
-
- private static final int numsec = securities.length;
-
-
- private static final double[][] U_data =
- { { 2.0 , 1.5 , -0.5 , 0.0 },
- { 0.0 , 1.93649167, 0.90369611, 0.0 },
- { 0.0 , 0.0 , 2.98886824, 0.0 },
- { 0.0 , 0.0 , 0.0 , 0.0 } };
- private static final Matrix U = new DenseMatrix(U_data);
-
- public static void main(String[] args)
- throws SolutionError
- {
- Model M = new Model("alan");
- try
- {
-
- Variable x = M.variable("x", numsec, Domain.greaterThan(0.0));
- Variable t = M.variable("t", 1, Domain.greaterThan(0.0));
- M.objective("minvar", ObjectiveSense.Minimize, t);
-
-
- M.constraint("wealth", Expr.sum(x), Domain.equalsTo(1.0));
-
- M.constraint("dmean", Expr.dot(mean, x), Domain.greaterThan(target));
-
- M.constraint("t > ||Ux||_2",Expr.vstack(0.5, t, Expr.mul(U,x)), Domain.inRotatedQCone());
- M.setLogHandler(new java.io.PrintWriter(System.out));
- System.out.println("Solve...");
- M.solve();
-
- System.out.println("... Solved.");
-
- double[] solx = x.level();
- System.out.printf("Solution = %e", solx[0]);
- for (int i = 1; i < numsec; ++i)
- System.out.printf(", %e", solx[i]);
- System.out.print("\n");
- }
- finally
- {
- M.dispose();
- }
- }
- }