There are two ways to use MOSEK from .NET:
Since MOSEK 7.0 we recommend using the Fusion API unless fine-grained control over the generated model is required.
The flat API operatates 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 string[]
- securities = { "hardware", "software", "show-biz", "t-bills" };
-
- private static double[]
- mean = { 8.0, 9.0, 12.0, 7.0 };
-
- private static double
- target = 10.0;
-
-
- private static Matrix U =
- new DenseMatrix(
- new double[][]
- { new double[] { 2.0 , 1.5 , -0.5 , 0.0 },
- new double[] { 0.0 , 1.93649167, 0.90369611, 0.0 },
- new double[] { 0.0 , 0.0 , 2.98886824, 0.0 },
- new double[] { 0.0 , 0.0 , 0.0 , 0.0 } });
- private static int numsec = securities.Length;
Then we can create the Model object:
- using (Model M = new Model("alan"))
Then we define the variables as
- Variable x = M.Variable("x", numsec, Domain.GreaterThan(0.0));
- Variable t = M.Variable("variance", 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(Expr.Vstack(Expr.ConstTerm(1,0.5),
- t.AsExpr(),
- Expr.Mul(U,x)),
- Domain.InRotatedQCone());
We optimize the model using
- M.Solve();
and extract the

-solution as
- double[] solx = x.Level();
Visual studio 2012 project files are available in
- C:\Program Files\mosek\7\tools\examples\fusion\dotnet\vs2012
To compile these examples please first copy them to a directory where you have write permissions.
First, copy the mosekdotnet.dll to the source directory.
Start a DOS box with the Visual Studio tools available and go to the source directory The example can now be compiled with
- csc /r:mosekdotnet.dll /t:exe /o:alan.exe alan.cs
then, assuming that MOSEK has been correctly installed, run with
- alan.exe
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- using System;
- using mosek.fusion;
-
- namespace mosek
- {
- namespace fusion
- {
- namespace example
- {
- public class alan
- {
-
-
-
-
- private static string[]
- securities = { "hardware", "software", "show-biz", "t-bills" };
-
- private static double[]
- mean = { 8.0, 9.0, 12.0, 7.0 };
-
- private static double
- target = 10.0;
-
-
- private static Matrix U =
- new DenseMatrix(
- new double[][]
- { new double[] { 2.0 , 1.5 , -0.5 , 0.0 },
- new double[] { 0.0 , 1.93649167, 0.90369611, 0.0 },
- new double[] { 0.0 , 0.0 , 2.98886824, 0.0 },
- new double[] { 0.0 , 0.0 , 0.0 , 0.0 } });
- private static int numsec = securities.Length;
- public static void Main(String[] args)
- {
- using (Model M = new Model("alan"))
- {
- Variable x = M.Variable("x", numsec, Domain.GreaterThan(0.0));
- Variable t = M.Variable("variance", Domain.GreaterThan(0.0));
- M.Objective("minvar", ObjectiveSense.Minimize, t.AsExpr());
-
-
- M.Constraint("wealth", Expr.Sum(x), Domain.EqualsTo(1.0));
-
- M.Constraint("dmean", Expr.Dot(mean, x), Domain.GreaterThan(target));
-
- M.Constraint(Expr.Vstack(Expr.ConstTerm(1,0.5),
- t.AsExpr(),
- Expr.Mul(U,x)),
- Domain.InRotatedQCone());
- Console.WriteLine("Solve...");
- M.Solve();
- Console.WriteLine("... Solved.");
- double[] solx = x.Level();
-
- Console.WriteLine("Primal solution = {0}", solx[0]);
- for (int i = 1; i < numsec; ++i)
- Console.Write(", {0}", solx[i]);
- Console.WriteLine("");
- }
- }
- }
- }
- }
- }