17 List of examples¶
List of examples shipped in the distribution of Optimizer API for .NET:
File |
Description |
---|---|
A simple problem with one affine conic constraint (ACC) |
|
A simple problem with two affine conic constraints (ACC) |
|
Demonstrates the MOSEK interface to BLAS/LAPACK linear algebra routines |
|
An example of data/progress callback |
|
A simple conic exponential problem |
|
Implementation of a concurrent optimizer for linear and mixed-integer problems |
|
A simple conic quadratic problem |
|
A simple problem with disjunctive constraints (DJC) |
|
A simple example of how to repair an infeasible problem |
|
A simple geometric program (GP) in conic form |
|
A Hello World example |
|
A simple linear problem |
|
A simple linear problem |
|
A simple linear problem |
|
Implements logistic regression and simple log-sum-exp (CEO) |
|
A simple mixed-integer conic problem |
|
A simple mixed-integer linear problem |
|
A simple mixed-integer linear problem with an initial guess |
|
Uses MOSEK OptServer to solve an optimization problem asynchronously |
|
Uses MOSEK OptServer to solve an optimization problem synchronously |
|
Demonstrates parallel optimization using a batch method in MOSEK |
|
Shows how to set optimizer parameters and read information items |
|
Shows how to obtain and analyze a primal infeasibility certificate |
|
Portfolio optimization - basic Markowitz model |
|
Portfolio optimization - efficient frontier |
|
Portfolio optimization - market impact costs |
|
Portfolio optimization - transaction costs |
|
Portfolio optimization - cardinality constraints |
|
Portfolio optimization - factor model |
|
A simple power cone problem |
|
A simple quadratically constrained quadratic problem |
|
A simple quadratic problem |
|
Demonstrate how to modify and re-optimize a linear problem |
|
Demonstrates proper response handling |
|
A simple semidefinite problem with one matrix variable and a quadratic cone |
|
A simple semidefinite problem with two matrix variables |
|
A simple semidefinite problem with an LMI using the SVEC domain. |
|
Sensitivity analysis performed on a small linear problem |
|
A simple I/O example: read problem from a file, solve and write solutions |
|
Demonstrates how to examine the quality of a solution |
|
Demonstrates solving a linear system with the basis matrix |
|
Demonstrates solving a general linear system |
|
Shows how to find a Cholesky factorization of a sparse matrix |
Additional examples can be found on the MOSEK website and in other MOSEK publications.
acc1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: acc1.cs
Purpose : Tutorial example for affine conic constraints.
Models the problem:
maximize c^T x
subject to sum(x) = 1
gamma >= |Gx+h|_2
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class acc1
{
public static void Main ()
{
/* Problem dimensions */
const int n = 3;
const int k = 2;
int i,j;
long quadDom;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
// Create a task object.
using (mosek.Task task = new mosek.Task()) {
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
// Create n free variables
task.appendvars(n);
task.putvarboundsliceconst(0, n, mosek.boundkey.fr, -infinity, infinity);
// Set up the objective
double[] c = {2, 3, -1};
int[] cind = {0, 1, 2};
task.putobjsense(mosek.objsense.maximize);
task.putclist(cind, c);
// One linear constraint - sum(x) = 1
task.appendcons(1);
task.putconbound(0, mosek.boundkey.fx, 1.0, 1.0);
for(i = 0; i < n; i++) task.putaij(0, i, 1.0);
// Append empty AFE rows for affine expression storage
task.appendafes(k + 1);
// F matix in sparse form
long[] Fsubi = {1, 1, 2, 2}; // The G matrix starts in F from row 1
int[] Fsubj = {0, 1, 0, 2};
double[] Fval = {1.5, 0.1, 0.3, 2.1};
// Other data
double[] h = {0, 0.1};
double gamma = 0.03;
// Fill in F storage
task.putafefentrylist(Fsubi, Fsubj, Fval);
// Fill in g storage;
task.putafeg(0, gamma);
task.putafegslice(1, k+1, h);
// Define a conic quadratic domain
quadDom = task.appendquadraticconedomain(k + 1);
// Create the ACC
long[] afeidx = {0, 1, 2};
task.appendacc(quadDom, // Domain index
afeidx, // Indices of AFE rows [0,...,k]
null); // Ignored
// Solve the problem
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
/* Get status information about the solution */
mosek.solsta solsta;
task.getsolsta(mosek.soltype.itr, out solsta);
switch (solsta)
{
case mosek.solsta.optimal:
// Fetch solution
double[] xx = new double[n];
task.getxx(mosek.soltype.itr, // Interior solution.
xx);
Console.WriteLine ("Optimal primal solution");
for (j = 0; j < n; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
// Fetch doty dual of the ACC
double[] doty = new double[k+1];
task.getaccdoty(mosek.soltype.itr, // Interior solution.
0, // ACC index
doty);
Console.WriteLine ("Dual doty of ACC");
for (j = 0; j < k+1; ++j)
Console.WriteLine ("doty[{0}]: {1}", j, doty[j]);
// Fetch activity of the ACC
double[] activity = new double[k+1];
task.evaluateacc(mosek.soltype.itr, // Interior solution.
0, // ACC index
activity);
Console.WriteLine ("Activity of ACC");
for (j = 0; j < n; ++j)
Console.WriteLine ("activity[{0}]: {1}", j, activity[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
}
}
acc2.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: acc2.cs
Purpose : Tutorial example for affine conic constraints.
Models the problem:
maximize c^T x
subject to sum(x) = 1
gamma >= |Gx+h|_2
This version inputs the linear constraint as an affine conic constraint.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class acc1
{
public static void Main ()
{
/* Problem dimensions */
const int n = 3;
const int k = 2;
int i,j;
long quadDom, zeroDom;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
// Create a task object.
using (mosek.Task task = new mosek.Task()) {
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
// Create n free variables
task.appendvars(n);
task.putvarboundsliceconst(0, n, mosek.boundkey.fr, -infinity, infinity);
// Set up the objective
double[] c = {2, 3, -1};
int[] cind = {0, 1, 2};
task.putobjsense(mosek.objsense.maximize);
task.putclist(cind, c);
// Set AFE rows representing the linear constraint
task.appendafes(1);
task.putafeg(0, -1.0);
for(i = 0; i < n; i++) task.putafefentry(0, i, 1.0);
// F matix in sparse form
long[] Fsubi = {2, 2, 3, 3}; // The G matrix starts in F from row 2
int[] Fsubj = {0, 1, 0, 2};
double[] Fval = {1.5, 0.1, 0.3, 2.1};
// Other data
double[] h = {0, 0.1};
double gamma = 0.03;
task.appendafes(k + 1);
task.putafefentrylist(Fsubi, Fsubj, Fval);
task.putafeg(1, gamma);
task.putafegslice(2, k+2, h);
// Define domains
zeroDom = task.appendrzerodomain(1);
quadDom = task.appendquadraticconedomain(k + 1);
// Create the linear ACC
long[] afeidxZero = {0};
task.appendacc(zeroDom, // Domain index
afeidxZero,// Indices of AFE rows
null); // Ignored
// Create the quadratic ACC
long[] afeidxQuad = {1, 2, 3};
task.appendacc(quadDom, // Domain index
afeidxQuad, // Indices of AFE rows
null); // Ignored
// Solve the problem
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
/* Get status information about the solution */
mosek.solsta solsta;
task.getsolsta(mosek.soltype.itr, out solsta);
switch (solsta)
{
case mosek.solsta.optimal:
// Fetch solution
double[] xx = new double[n];
task.getxx(mosek.soltype.itr, // Interior solution.
xx);
Console.WriteLine ("Optimal primal solution");
for (j = 0; j < n; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
// Fetch doty dual of the ACC
double[] doty = new double[k+1];
task.getaccdoty(mosek.soltype.itr, // Interior solution.
1, // ACC index
doty);
Console.WriteLine ("Dual doty of ACC");
for (j = 0; j < k+1; ++j)
Console.WriteLine ("doty[{0}]: {1}", j, doty[j]);
// Fetch activity of the ACC
double[] activity = new double[k+1];
task.evaluateacc(mosek.soltype.itr, // Interior solution.
1, // ACC index
activity);
Console.WriteLine ("Activity of ACC");
for (j = 0; j < n; ++j)
Console.WriteLine ("activity[{0}]: {1}", j, activity[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
}
}
blas_lapack.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: blas_lapack.cs
Purpose: To demonstrate how to call BLAS/LAPACK routines for whose MOSEK provides simplified interfaces.
*/
using System;
namespace mosek.example
{
public class blas_lapack
{
public static void Main ()
{
const int n = 3, m = 2, k = 3;
double alpha = 2.0, beta = 0.5;
double[] x = {1.0, 1.0, 1.0};
double[] y = {1.0, 2.0, 3.0};
double[] z = {1.0, 1.0};
/*A has m=2 rows and k=3 cols*/
double[] A = {1.0, 1.0, 2.0, 2.0, 3.0, 3.0};
/*B has k=3 rows and n=3 cols*/
double[] B = {1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0
};
double[] C = {1.0, 2.0, 3.0,
4.0, 5.0, 6.0
};
double[] D = {1.0, 1.0, 1.0, 1.0};
double[] Q = {1.0, 0.0, 0.0, 2.0};
double[] v = new double[2];
double xy;
using (mosek.Env env = new mosek.Env())
{
/* BLAS routines */
try
{
env.dot(n, x, y, out xy);
env.axpy(n, alpha, x, y);
env.gemv(mosek.transpose.no, m, n, alpha, A, x, beta, z);
Console.WriteLine("m = {0}, n = {1}, k = {2}, len A = {3}",m,n,k,A.Length);
env.gemm(mosek.transpose.no, mosek.transpose.no, m, n, k, alpha, A, B, beta, C);
env.syrk(mosek.uplo.lo, mosek.transpose.no, m, k, alpha, A, beta, D);
/* LAPACK routines*/
env.potrf(mosek.uplo.lo, m, Q);
env.syeig(mosek.uplo.lo, m, Q, v);
env.syevd(mosek.uplo.lo, m, Q, v);
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
}
finally
{
if (env != null) env.Dispose ();
}
}
}
}
}
callback.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: callback.cs
Purpose: To demonstrate how to use the progress
callback.
Use this script as follows:
callback psim 25fv47.mps
callback dsim 25fv47.mps
callback intpnt 25fv47.mps
The first argument tells which optimizer to use
i.e. psim is primal simplex, dsim is dual simplex
and intpnt is interior-point.
*/
using mosek;
using System;
namespace mosek.example
{
class myCallback : mosek.DataCallback
{
double maxtime;
public myCallback(double maxtime_)
{
maxtime = maxtime_;
}
public override int callback( callbackcode caller,
double[] douinf,
int[] intinf,
long[] lintinf )
{
double opttime = 0.0;
int itrn;
double pobj, dobj, stime;
switch (caller)
{
case callbackcode.begin_intpnt:
Console.WriteLine("Starting interior-point optimizer");
break;
case callbackcode.intpnt:
itrn = intinf[(int) iinfitem.intpnt_iter ];
pobj = douinf[(int) dinfitem.intpnt_primal_obj];
dobj = douinf[(int) dinfitem.intpnt_dual_obj ];
stime = douinf[(int) dinfitem.intpnt_time ];
opttime = douinf[(int) dinfitem.optimizer_time ];
Console.WriteLine("Iterations: {0,-3}",itrn);
Console.WriteLine(" Elapsed: Time: {0,6:F2}({1:F2})",opttime,stime);
Console.WriteLine(" Primal obj.: {0,-18:E6} Dual obj.: {1,018:E6}e",pobj,dobj);
break;
case callbackcode.end_intpnt:
Console.WriteLine("Interior-point optimizer finished.");
break;
case callbackcode.begin_primal_simplex:
Console.WriteLine("Primal simplex optimizer started.");
break;
case callbackcode.update_primal_simplex:
itrn = intinf[(int) iinfitem.sim_primal_iter ];
pobj = douinf[(int) dinfitem.sim_obj ];
stime = douinf[(int) dinfitem.sim_time ];
opttime = douinf[(int) dinfitem.optimizer_time ];
Console.WriteLine("Iterations: {0,-3}}", itrn);
Console.WriteLine(" Elapsed time: {0,6:F2}({1:F2})",opttime,stime);
Console.WriteLine(" Obj.: {0,-18:E6}", pobj );
break;
case callbackcode.end_primal_simplex:
Console.WriteLine("Primal simplex optimizer finished.");
break;
case callbackcode.begin_dual_simplex:
Console.WriteLine("Dual simplex optimizer started.");
break;
case callbackcode.update_dual_simplex:
itrn = intinf[(int) iinfitem.sim_dual_iter ];
pobj = douinf[(int) dinfitem.sim_obj ];
stime = douinf[(int) dinfitem.sim_time ];
opttime = douinf[(int) dinfitem.optimizer_time ];
Console.WriteLine("Iterations: {0,-3}}", itrn);
Console.WriteLine(" Elapsed time: {0,6:F2}({1:F2})",opttime,stime);
Console.WriteLine(" Obj.: {0,-18:E6}", pobj );
break;
case callbackcode.end_dual_simplex:
Console.WriteLine("Dual simplex optimizer finished.");
break;
case callbackcode.begin_bi:
Console.WriteLine("Basis identification started.");
break;
case callbackcode.end_bi:
Console.WriteLine("Basis identification finished.");
break;
default:
break;
}
if (opttime >= maxtime)
// mosek is spending too much time. Terminate it.
return 1;
return 0;
}
}
class myStream : Stream
{
public myStream () : base() { }
public override void streamCB (string msg)
{
Console.Write ("{0}",msg);
}
}
public class callback
{
public static void Main(string[] args)
{
string filename = "../data/25fv47.mps";
string slvr = "intpnt";
if (args.Length < 2)
{
Console.WriteLine("Usage: callback ( psim | dsim | intpnt ) filename");
}
if (args.Length >= 1) slvr = args[0];
if (args.Length >= 2) filename = args[1];
using (Env env = new mosek.Env())
{
using (Task task = new Task(env,0,0))
{
task.readdata(filename);
if (slvr == "psim")
task.putintparam(iparam.optimizer, optimizertype.primal_simplex);
else if (slvr == "dsim")
task.putintparam(iparam.optimizer, optimizertype.dual_simplex);
else if (slvr == "intpnt")
task.putintparam(iparam.optimizer, optimizertype.intpnt);
double maxtime = 0.06;
task.set_InfoCallback(new myCallback(maxtime));
task.optimize();
task.solutionsummary(streamtype.msg);
}
}
}
}
}
ceo1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: ceo1.cs
Purpose: Demonstrates how to solve a small conic exponential
optimization problem using the MOSEK API.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class ceo1
{
public static void Main ()
{
const int numcon = 1;
const int numvar = 3;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
mosek.boundkey bkc = mosek.boundkey.fx;
double blc = 1.0 ;
double buc = 1.0 ;
mosek.boundkey[] bkx = {mosek.boundkey.fr,
mosek.boundkey.fr,
mosek.boundkey.fr
};
double[] blx = { -infinity,
-infinity,
-infinity
};
double[] bux = { +infinity,
+infinity,
+infinity
};
double[] c = { 1.0,
1.0,
0.0
};
double[] a = { 1.0, 1.0, 1.0 };
int[] asub = { 0, 1, 2 };
int[] csub = new int[3];
// Create a task object.
using (mosek.Task task = new mosek.Task()) {
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
/* Set up the linear part of the problem */
task.putcslice(0, numvar, c);
task.putarow(0, asub, a);
task.putconbound(0, bkc, blc, buc);
task.putvarboundslice(0, numvar, bkx, blx, bux);
/* Add a conic constraint */
/* Create a 3x3 identity matrix F */
task.appendafes(3);
task.putafefentrylist(new long[]{0, 1, 2}, /* Rows */
new int[]{0, 1, 2}, /* Columns */
new double[]{1.0, 1.0, 1.0});
/* Exponential cone (x(0),x(1),x(2)) \in EXP */
long expdomain = task.appendprimalexpconedomain();
task.appendacc(expdomain, /* Domain */
new long[]{0, 1, 2}, /* Rows from F */
null); /* Unused */
task.putobjsense(mosek.objsense.minimize);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
mosek.solsta solsta;
/* Get status information about the solution */
task.getsolsta(mosek.soltype.itr, out solsta);
double[] xx = task.getxx(mosek.soltype.itr); // Interior solution
switch (solsta)
{
case mosek.solsta.optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
}
}
concurrent1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: concurrent1.cs
Purpose: Demonstrates a simple implementation of a concurrent optimizer.
The concurrent optimizer starts a few parallel optimizations
of the same problem using different algorithms, and reports
a solution when the first optimizer is ready.
This example also demonstrates how to define a simple callback handler
that stops the optimizer when requested.
*/
using System.Threading.Tasks;
using System;
namespace mosek.example
{
public class concurrent1
{
/** Takes a list of tasks and optimizes then in parallel. The
response code and termination code from each optimization is
stored in ``res`` and ``trm``.
When one task completes with rescode == ok, others are terminated.
Returns true if some optimizer returned without error. In this case
``firstOK`` is the index of the first task that returned
with rescode == ok. Whether or not this is the task firstOK contains the
most valuable answer, is for the caller to decide.
*/
public static bool optimize(mosek.Task[] tasks,
mosek.rescode[] res,
mosek.rescode[] trm,
out int firstOK)
{
var n = tasks.Length;
var jobs = new System.Threading.Tasks.Task[n];
int firstStop = -1;
// Set a callback function
var cb = new CallbackProxy();
for (var i = 0; i < n; ++i)
tasks[i].set_Progress(cb);
// Initialize
for (var i = 0; i < n; ++i)
{
res[i] = mosek.rescode.err_unknown;
trm[i] = mosek.rescode.err_unknown;
}
// Start parallel optimizations, one per task
for (var i = 0; i < n; ++i)
{
int num = i;
jobs[i] = System.Threading.Tasks.Task.Factory.StartNew( () => {
try
{
trm[num] = tasks[num].optimize();
res[num] = mosek.rescode.ok;
}
catch (mosek.Exception e)
{
trm[num] = mosek.rescode.err_unknown;
res[num] = e.Code;
}
finally
{
// If this finished with success, inform other tasks to interrupt
if (res[num] == mosek.rescode.ok)
{
if (!cb.Stop) firstStop = num;
cb.Stop = true;
}
}
} );
}
// Join all threads
foreach (var j in jobs)
j.Wait();
// For debugging, print res and trm codes for all optimizers
for (var i = 0; i < n; ++i)
Console.WriteLine("Optimizer {0} res {1} trm {2}", i, res[i], trm[i]);
firstOK = firstStop;
return cb.Stop;
}
/**
Given a continuous task, set up jobs to optimize it
with a list of different solvers.
Returns an index, corresponding to the optimization
task that is returned as winTask. This is the task
with the best possible status of those that finished.
If none task is considered successful returns -1.
*/
public static int optimizeconcurrent(mosek.Task task,
int[] optimizers,
out mosek.Task winTask,
out mosek.rescode winTrm,
out mosek.rescode winRes)
{
var n = optimizers.Length;
var tasks = new mosek.Task[n];
var res = new mosek.rescode[n];
var trm = new mosek.rescode[n];
// Clone tasks and choose various optimizers
for (var i = 0; i < n; ++i)
{
tasks[i] = new mosek.Task(task);
tasks[i].putintparam(mosek.iparam.optimizer, optimizers[i]);
}
// Solve tasks in parallel
bool success;
int firstOK;
success = optimize(tasks, res, trm, out firstOK);
if (success)
{
winTask = tasks[firstOK];
winTrm = trm[firstOK];
winRes = res[firstOK];
return firstOK;
}
else
{
winTask = null;
winTrm = 0;
winRes = 0;
return -1;
}
}
/**
Given a mixed-integer task, set up jobs to optimize it
with different values of seed. That will lead to
different execution paths of the optimizer.
Returns an index, corresponding to the optimization
task that is returned as winTask. This is the task
with the best value of the objective function.
If none task is considered successful returns -1.
Typically, the input task would contain a time limit. The two
major scenarios are:
1. Some clone ends before time limit - then it has optimum.
2. All clones reach time limit - pick the one with best objective.
*/
public static int optimizeconcurrentMIO(mosek.Task task,
int[] seeds,
out mosek.Task winTask,
out mosek.rescode winTrm,
out mosek.rescode winRes)
{
var n = seeds.Length;
var tasks = new mosek.Task[n];
var res = new mosek.rescode[n];
var trm = new mosek.rescode[n];
// Clone tasks and choose various seeds for the optimizer
for (var i = 0; i < n; ++i)
{
tasks[i] = new mosek.Task(task);
tasks[i].putintparam(mosek.iparam.mio_seed, seeds[i]);
}
// Solve tasks in parallel
bool success;
int firstOK;
success = optimize(tasks, res, trm, out firstOK);
if (success)
{
// Pick the task that ended with res = ok
// and contains an integer solution with best objective value
mosek.objsense sense = task.getobjsense();
double bestObj = (sense == mosek.objsense.minimize) ? 1.0e+10 : -1.0e+10;
int bestPos = -1;
for (var i = 0; i < n; ++i)
Console.WriteLine("{0} {1} ", i, tasks[i].getprimalobj(mosek.soltype.itg));
for (var i = 0; i < n; ++i)
if ((res[i] == mosek.rescode.ok) &&
(tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.prim_feas ||
tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.integer_optimal) &&
((sense == mosek.objsense.minimize) ?
(tasks[i].getprimalobj(mosek.soltype.itg) < bestObj) :
(tasks[i].getprimalobj(mosek.soltype.itg) > bestObj) ) )
{
bestObj = tasks[i].getprimalobj(mosek.soltype.itg);
bestPos = i;
}
if (bestPos != -1)
{
winTask = tasks[bestPos];
winTrm = trm[bestPos];
winRes = res[bestPos];
return bestPos;
}
}
winTask = null;
winTrm = 0;
winRes = 0;
return -1;
}
/**
This is an example of how one can use the methods
optimizeconcurrent
optimizeconcurrentMIO
argv[0] : name of file with input problem
argv[1]: (optional) time limit
*/
public static void Main(string[] argv)
{
using (var env = new mosek.Env())
{
using (var task = new mosek.Task(env))
{
if (argv.Length >= 1)
{
task.readdata(argv[0]);
}
else
{
task.readdata("../data/25fv47.mps");
}
mosek.rescode res, trm;
mosek.Task t;
int idx;
int numint;
task.getnumintvar(out numint);
// Optional time limit
if (argv.Length >= 2)
{
double timeLimit = double.Parse(argv[1]);
task.putdouparam(mosek.dparam.optimizer_max_time, timeLimit);
}
if (numint == 0)
{
/* If the problem is continuous
optimize it with three continuous optimizers.
(Simplex will fail for non-linear problems)
*/
int[] optimizers = {
mosek.optimizertype.conic,
mosek.optimizertype.dual_simplex,
mosek.optimizertype.primal_simplex
};
idx = optimizeconcurrent(task, optimizers, out t, out trm, out res);
}
else
{
/* Mixed-integer problem.
Try various seeds.
*/
int[] seeds = { 42, 13, 71749373 };
idx = optimizeconcurrentMIO(task, seeds, out t, out trm, out res);
}
// Check results and print the best answer
if (idx >= 0)
{
Console.WriteLine("Result from optimizer with index {0}: res {1} trm {2}", idx, res, trm);
t.set_Stream(mosek.streamtype.log, new msgclass(""));
t.optimizersummary(mosek.streamtype.log);
t.solutionsummary(mosek.streamtype.log);
}
else
{
Console.WriteLine("All optimizers failed.");
}
}
}
}
/**
Defines a Mosek callback function whose only function
is to indicate if the optimizer should be stopped.
*/
private class CallbackProxy : mosek.Progress
{
private bool stop;
public CallbackProxy()
{
stop = false;
}
public override int progressCB(mosek.callbackcode caller)
{
// Return non-zero implies terminate the optimizer
return stop ? 1 : 0;
}
public bool Stop
{
get { return stop; }
set { stop = value; }
}
}
/**
A simple stream handler.
*/
class msgclass : mosek.Stream
{
public msgclass (string prfx) { }
public override void streamCB (string msg)
{
Console.Write("{0}", msg);
}
}
}
}
cqo1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: cqo1.cs
Purpose: Demonstrates how to solve a small conic quadratic
optimization problem using the MOSEK API.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class cqo1
{
public static void Main ()
{
const int numcon = 1;
const int numvar = 6;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
mosek.boundkey[] bkc = { mosek.boundkey.fx };
double[] blc = { 1.0 };
double[] buc = { 1.0 };
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.fr,
mosek.boundkey.fr,
mosek.boundkey.fr
};
double[] blx = { 0.0,
0.0,
0.0,
-infinity,
-infinity,
-infinity
};
double[] bux = { +infinity,
+infinity,
+infinity,
+infinity,
+infinity,
+infinity
};
double[] c = { 0.0,
0.0,
0.0,
1.0,
1.0,
1.0
};
double[][] aval = {
new double[] {1.0},
new double[] {1.0},
new double[] {2.0}
};
int[][] asub = {
new int[] {0},
new int[] {0},
new int[] {0}
};
int[] csub = new int[3];
// Create a task object.
using (mosek.Task task = new mosek.Task()) {
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
for (int j = 0; j < numvar; ++j)
{
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
}
for (int j = 0; j < aval.Length; ++j)
/* Input column j of A */
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/* Create a matrix F such that F * x = [x(3),x(0),x(1),x(4),x(5),x(2)] */
task.appendafes(6);
task.putafefentrylist(new long[]{0, 1, 2, 3, 4, 5}, /* Rows */
new int[]{3, 0, 1, 4, 5, 2}, /* Columns */
new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0});
/* Quadratic cone (x(3),x(0),x(1)) \in QUAD_3 */
long quadcone = task.appendquadraticconedomain(3);
task.appendacc(quadcone, /* Domain */
new long[]{0, 1, 2}, /* Rows from F */
null); /* Unused */
/* Rotated quadratic cone (x(4),x(5),x(2)) \in RQUAD_3 */
long rquadcone = task.appendrquadraticconedomain(3);
task.appendacc(rquadcone, /* Domain */
new long[]{3, 4, 5}, /* Rows from F */
null); /* Unused */
task.putobjsense(mosek.objsense.minimize);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
/* Get status information about the solution */
mosek.solsta solsta = task.getsolsta(mosek.soltype.itr);
double[] xx = task.getxx(mosek.soltype.itr); // Interior point solution
switch (solsta)
{
case mosek.solsta.optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
}
}
djc1.cs
////
// Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
//
// File: djc1.cs
//
// Purpose: Demonstrates how to solve the problem with two disjunctions:
//
// minimize 2x0 + x1 + 3x2 + x3
// subject to x0 + x1 + x2 + x3 >= -10
// (x0-2x1<=-1 and x2=x3=0) or (x2-3x3<=-2 and x1=x2=0)
// x0=2.5 or x1=2.5 or x2=2.5 or x3=2.5
////
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class djc1
{
public static void Main ()
{
// Since the value of infinity is ignored, we define it solely
// for symbolic purposes
const double inf = 0;
try
{
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Append free variables
int numvar = 4;
task.appendvars(numvar);
task.putvarboundsliceconst(0, numvar, mosek.boundkey.fr, -inf, inf);
// The linear part: the linear constraint
task.appendcons(1);
task.putarow(0, new int[]{0, 1, 2, 3}, new double[]{1, 1, 1, 1});
task.putconbound(0, mosek.boundkey.lo, -10.0, -10.0);
// The linear part: objective
task.putobjsense(mosek.objsense.minimize);
task.putclist(new int[]{0, 1, 2, 3}, new double[]{2, 1, 3, 1});
// Fill in the affine expression storage F, g
long numafe = 10;
task.appendafes(numafe);
long[] fafeidx = new long[]{0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] fvaridx = new int[]{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3};
double[] fval = new double[]{1.0, -2.0, 1.0, -3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
double[] g = new double[]{1.0, 2.0, 0.0, 0.0, 0.0, 0.0, -2.5, -2.5, -2.5, -2.5};
task.putafefentrylist(fafeidx, fvaridx, fval);
task.putafegslice(0, numafe, g);
// Create domains
long zero1 = task.appendrzerodomain(1);
long zero2 = task.appendrzerodomain(2);
long rminus1 = task.appendrminusdomain(1);
// Append disjunctive constraints
long numdjc = 2;
task.appenddjcs(numdjc);
// First disjunctive constraint
task.putdjc(0, // DJC index
new long[]{rminus1, zero2, rminus1, zero2}, // Domains (domidxlist)
new long[]{0, 4, 5, 1, 2, 3}, // AFE indices (afeidxlist)
null, // Unused
new long[]{2, 2} ); // Term sizes (termsizelist)
// Second disjunctive constraint
task.putdjc(1, // DJC index
new long[]{zero1, zero1, zero1, zero1}, // Domains (domidxlist)
new long[]{6, 7, 8, 9}, // AFE indices (afeidxlist)
null, // Unused
new long[]{1, 1, 1, 1} ); // Term sizes (termidxlist)
// Useful for debugging
task.writedata("djc.ptf");
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
// Solve the problem
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
// Get status information about the solution
mosek.solsta solsta = task.getsolsta(mosek.soltype.itg);
switch (solsta)
{
case mosek.solsta.integer_optimal:
double[] xx = task.getxx(mosek.soltype.itg);
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
break;
default:
Console.WriteLine("Another solution status");
break;
}
}
}
}
catch (mosek.Exception e) {
mosek.rescode res = e.Code;
Console.WriteLine("Response code {0}\nMessage {1}", res, e.Message);
}
}
}
}
feasrepairex1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: feasrepairex1.cs
Purpose: To demonstrate how to use the MSK_relaxprimal function to
locate the cause of an infeasibility.
Syntax: On command line
feasrepairex1 feasrepair.lp
feasrepair.lp is located in mosek\<version>\tools\examples.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
public msgclass () {}
public override void streamCB (string msg)
{
Console.Write ("{1}", msg);
}
}
public class feasrepairex1
{
public static void Main (String[] args)
{
string filename = "../data/feasrepair.lp";
if (args.Length >= 1) filename = args[0];
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
task.set_Stream (mosek.streamtype.log, new msgclass());
task.readdata(filename);
task.putintparam(mosek.iparam.log_feas_repair, 3);
task.primalrepair(null, null, null, null);
double sum_viol = task.getdouinf(mosek.dinfitem.primal_repair_penalty_obj);
Console.WriteLine("Minimized sum of violations = %{0}", sum_viol);
task.optimize();
task.solutionsummary(mosek.streamtype.msg);
}
}
}
}
}
gp1.cs
//
// Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
//
// File: gp1.cs
//
// Purpose: Demonstrates how to solve a simple Geometric Program (GP)
// cast into conic form with exponential cones and log-sum-exp.
//
// Example from
// https://gpkit.readthedocs.io/en/latest/examples.html//maximizing-the-volume-of-a-box
//
using System;
using mosek;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class gp1
{
// Since the value of infinity is ignored, we define it solely
// for symbolic purposes
static double inf = 0.0;
// maximize h*w*d
// subjecto to 2*(h*w + h*d) <= Awall
// w*d <= Afloor
// alpha <= h/w <= beta
// gamma <= d/w <= delta
//
// Variable substitutions: h = exp(x), w = exp(y), d = exp(z).
//
// maximize x+y+z
// subject log( exp(x+y+log(2/Awall)) + exp(x+z+log(2/Awall)) ) <= 0
// y+z <= log(Afloor)
// log( alpha ) <= x-y <= log( beta )
// log( gamma ) <= z-y <= log( delta )
public static double[] max_volume_box(double Aw, double Af,
double alpha, double beta, double gamma, double delta)
{
// Basic dimensions of our problem
int numvar = 3; // Variables in original problem
int x=0, y=1, z=2; // Indices of variables
int numcon = 3; // Linear constraints in original problem
// Linear part of the problem
double[] cval = {1, 1, 1};
int[] asubi = {0, 0, 1, 1, 2, 2};
int[] asubj = {y, z, x, y, z, y};
double[] aval = {1.0, 1.0, 1.0, -1.0, 1.0, -1.0};
boundkey[] bkc = {boundkey.up, boundkey.ra, boundkey.ra};
double[] blc = {-inf, Math.Log(alpha), Math.Log(gamma)};
double[] buc = {Math.Log(Af), Math.Log(beta), Math.Log(delta)};
using (Env env = new Env())
{
using (Task task = new Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method task_msg_obj.stream
task.set_Stream (mosek.streamtype.log, new msgclass (""));
// Add variables and constraints
task.appendvars(numvar);
task.appendcons(numcon);
// Objective is the sum of three first variables
task.putobjsense(objsense.maximize);
task.putcslice(0, numvar, cval);
task.putvarboundsliceconst(0, numvar, boundkey.fr, -inf, inf);
// Add the three linear constraints
task.putaijlist(asubi, asubj, aval);
task.putconboundslice(0, numvar, bkc, blc, buc);
// Affine expressions appearing in affine conic constraints
// in this order:
// u1, u2, x+y+log(2/Awall), x+z+log(2/Awall), 1.0, u1+u2-1.0
long numafe = 6;
int u1 = 3, u2 = 4; // Indices of slack variables
long[] afeidx = {0, 1, 2, 2, 3, 3, 5, 5};
int[] varidx = {u1, u2, x, y, x, z, u1, u2};
double[] fval = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
double[] gfull = {0, 0, Math.Log(2/Aw), Math.Log(2/Aw), 1.0, -1.0};
// New variables u1, u2
task.appendvars(2);
task.putvarboundsliceconst(u1, u2+1, boundkey.fr, -inf, inf);
// Append affine expressions
task.appendafes(numafe);
task.putafefentrylist(afeidx, varidx, fval);
task.putafegslice(0, numafe, gfull);
// Two affine conic constraints
long expdom = task.appendprimalexpconedomain();
// (u1, 1, x+y+log(2/Awall)) \in EXP
task.appendacc(expdom, new long[]{0, 4, 2}, null);
// (u2, 1, x+z+log(2/Awall)) \in EXP
task.appendacc(expdom, new long[]{1, 4, 3}, null);
// The constraint u1+u2-1 \in \ZERO is added also as an ACC
task.appendacc(task.appendrzerodomain(1), new long[]{5}, null);
// Solve and map to original h, w, d
task.optimize();
double[] xyz = task.getxxslice(soltype.itr, 0, numvar);
double[] hwd = new double[numvar];
for(int i = 0; i < numvar; i++) hwd[i] = Math.Exp(xyz[i]);
return hwd;
}
}
}
public static void Main(String[] args)
{
double Aw = 200.0;
double Af = 50.0;
double alpha = 2.0;
double beta = 10.0;
double gamma = 2.0;
double delta = 10.0;
double[] hwd = max_volume_box(Aw, Af, alpha, beta, gamma, delta);
Console.WriteLine("h={0:f4} w={1:f4} d={2:f4}", hwd[0], hwd[1], hwd[2]);
}
}
}
helloworld.cs
////
// Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
//
// File: helloworld.cs
//
// The most basic example of how to get started with MOSEK.
using mosek;
using System;
public class helloworld {
public static void Main() {
double[] x = new double[1];
using (Env env = new Env()) { // Create Environment
using (Task task = new Task(env, 0, 1)) { // Create Task
task.appendvars(1); // 1 variable x
task.putcj(0, 1.0); // c_0 = 1.0
task.putvarbound(0, boundkey.ra, 2.0, 3.0); // 2.0 <= x <= 3.0
task.putobjsense(objsense.minimize); // minimize
task.optimize(); // Optimize
task.getxx(soltype.itr, x); // Get solution
Console.WriteLine("Solution x = " + x[0]); // Print solution
}
}
}
}
lo1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: lo1.cs
Purpose: Demonstrates how to solve small linear
optimization problem using the MOSEK C# API.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class lo1
{
public static void Main ()
{
const int numcon = 3;
const int numvar = 4;
// Since the value of infinity is ignored, we define it solely
// for symbolic purposes
double infinity = 0;
double[] c = {3.0, 1.0, 5.0, 1.0};
int[][] asub = {
new int[] {0, 1},
new int[] {0, 1, 2},
new int[] {0, 1},
new int[] {1, 2}
};
double[][] aval = {
new double[] {3.0, 2.0},
new double[] {1.0, 1.0, 2.0},
new double[] {2.0, 3.0},
new double[] {1.0, 3.0}
};
mosek.boundkey[] bkc = {mosek.boundkey.fx,
mosek.boundkey.lo,
mosek.boundkey.up
};
double[] blc = {30.0,
15.0,
-infinity
};
double[] buc = {30.0,
+infinity,
25.0
};
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.ra,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0,
0.0
};
double[] bux = { +infinity,
10.0,
+infinity,
+infinity
};
try {
// Create a task object.
using (mosek.Task task = new mosek.Task())
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
// Append 'numcon' empty constraints.
// The constraints will initially have no bounds.
task.appendcons(numcon);
// Append 'numvar' variables.
// The variables will initially be fixed at zero (x=0).
task.appendvars(numvar);
for (int j = 0; j < numvar; ++j)
{
// Set the linear term c_j in the objective.
task.putcj(j, c[j]);
// Set the bounds on variable j.
// blx[j] <= x_j <= bux[j]
task.putvarbound(j, bkx[j], blx[j], bux[j]);
// Input column j of A
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
}
// Set the bounds on constraints.
// blc[i] <= constraint_i <= buc[i]
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
// Input the objective sense (minimize/maximize)
task.putobjsense(mosek.objsense.maximize);
// Solve the problem
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
// Get status information about the solution
mosek.solsta solsta;
task.getsolsta(mosek.soltype.bas, out solsta);
switch (solsta)
{
case mosek.solsta.optimal:
double[] xx = new double[numvar];
task.getxx(mosek.soltype.bas, // Request the basic solution.
xx);
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility certificate found.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
catch (mosek.Exception e) {
mosek.rescode res = e.Code;
Console.WriteLine("Response code {0}\nMessage {1}", res, e.Message);
}
}
}
}
lo1.vb
' Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
'
' File: lo1.vb
'
' Purpose: Demonstrates how to solve small linear
' optimization problem using the MOSEK .net API.
'
Imports System, mosek
Public Class MsgTest
Inherits mosek.Stream
Dim name As String
Public Sub New(ByVal e As mosek.Env, ByVal n As String)
' MyBase.New ()
name = n
End Sub
Public Overrides Sub streamCB(ByVal msg As String)
Console.Write("{0}: {1}", name, msg)
End Sub
End Class
Module Module1
Sub Main()
Dim infinity As Double = 0.0
Dim numcon As Integer = 3
Dim numvar As Integer = 4
Dim bkc As boundkey() = {boundkey.fx, boundkey.lo, boundkey.up}
Dim bkx As boundkey() = {boundkey.lo, boundkey.ra, boundkey.lo, boundkey.lo}
Dim asub(numvar)() As Integer
asub(0) = New Integer() {0, 1}
asub(1) = New Integer() {0, 1, 2}
asub(2) = New Integer() {0, 1}
asub(3) = New Integer() {1, 2}
Dim blc As Double() = {30.0, 15.0, -infinity}
Dim buc As Double() = {30.0, infinity, 25.0}
Dim cj As Double() = {3.0, 1.0, 5.0, 1.0}
Dim blx As Double() = {0.0, 0.0, 0.0, 0, 0}
Dim bux As Double() = {infinity, 10, infinity, infinity}
Dim aval(numvar)() As Double
aval(0) = New Double() {3.0, 2.0}
aval(1) = New Double() {1.0, 1.0, 2.0}
aval(2) = New Double() {2.0, 3.0}
aval(3) = New Double() {1.0, 3.0}
Dim xx As Double() = {0, 0, 0, 0, 0}
Dim msg As MsgTest
Dim i As Integer
Dim j As Integer
Try
Using env As New mosek.Env()
Using task As New mosek.task(env, 0, 0)
msg = New MsgTest(env, "msg")
task.set_Stream(streamtype.log, msg)
'Append 'numcon' empty constraints.
'The constraints will initially have no bounds.
Call task.appendcons(numcon)
'Append 'numvar' variables.
' The variables will initially be fixed at zero (x=0).
Call task.appendvars(numvar)
For j = 0 To numvar - 1
'Set the linear term c_j in the objective.
Call task.putcj(j, cj(j))
' Set the bounds on variable j.
'blx[j] <= x_j <= bux[j]
Call task.putvarbound(j, bkx(j), blx(j), bux(j))
'Input column j of A
Call task.putacol(j, asub(j), aval(j))
Next j
' for i=1, ...,numcon : blc[i] <= constraint i <= buc[i]
For i = 0 To numcon - 1
Call task.putconbound(i, bkc(i), blc(i), buc(i))
Next i
Call task.putobjsense(mosek.objsense.maximize)
Call task.optimize()
' Print a summary containing information
' about the solution for debugging purposes
Call task.solutionsummary(mosek.streamtype.msg)
Dim solsta As mosek.solsta
' Get status information about the solution
Call task.getsolsta(mosek.soltype.bas, solsta)
task.getxx(soltype.bas, xx)
For j = 0 To numvar - 1
Console.WriteLine("x[{0}]:{1}", j, xx(j))
Next
Console.WriteLine("Finished optimization")
End Using
End Using
Catch e As mosek.Exception
Console.WriteLine("MosekException caught, {0}", e)
Throw (e)
End Try
End Sub
End Module
lo2.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: lo2.cs
Purpose: Demonstrates how to solve small linear
optimization problem using the MOSEK C# API.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class lo2
{
public static void Main ()
{
const int numcon = 3;
const int numvar = 4;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double
infinity = 0;
double[] c = {3.0, 1.0, 5.0, 1.0};
int[][] asub = {
new int[] {0, 1, 2},
new int[] {0, 1, 2, 3},
new int[] {1, 3}
};
double[][] aval = {
new double[] {3.0, 1.0, 2.0},
new double[] {2.0, 1.0, 3.0, 1.0},
new double[] {2.0, 3.0}
};
mosek.boundkey[] bkc = {mosek.boundkey.fx,
mosek.boundkey.lo,
mosek.boundkey.up
};
double[] blc = {30.0,
15.0,
-infinity
};
double[] buc = {30.0,
+infinity,
25.0
};
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.ra,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0,
0.0
};
double[] bux = { +infinity,
10.0,
+infinity,
+infinity
};
mosek.Task task = null;
mosek.Env env = null;
double[] xx = new double[numvar];
try
{
// Make mosek environment.
env = new mosek.Env ();
// Create a task object linked with the environment env.
task = new mosek.Task (env, 0, 0);
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
/* Optionally add a constant term to the objective. */
task.putcfix(0.0);
for (int j = 0; j < numvar; ++j)
{
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
{
task.putconbound(i, bkc[i], blc[i], buc[i]);
/* Input row i of A */
task.putarow(i, /* Row index.*/
asub[i], /* Column indexes of non-zeros in row i.*/
aval[i]); /* Non-zero Values of row i. */
}
task.putobjsense(mosek.objsense.maximize);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
mosek.solsta solsta;
/* Get status information about the solution */
task.getsolsta(mosek.soltype.bas, out solsta);
task.getxx(mosek.soltype.bas, // Basic solution.
xx);
switch (solsta)
{
case mosek.solsta.optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
finally
{
if (task != null) task.Dispose ();
if (env != null) env.Dispose ();
}
}
}
}
logistic.cs
////
// Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
//
// File: logistic.cs
//
// Purpose: Implements logistic regression with regulatization.
//
// Demonstrates using the exponential cone and log-sum-exp in Optimizer API.
using System;
using mosek;
namespace mosek.example
{
public class logistic {
public static double inf = 0.0;
// Adds ACCs for t_i >= log ( 1 + exp((1-2*y[i]) * theta' * X[i]) )
// Adds auxiliary variables, AFE rows and constraints
public static void softplus(Task task, int d, int n, int theta, int t, double[,] X, bool[] y)
{
int nvar = task.getnumvar();
int ncon = task.getnumcon();
long nafe = task.getnumafe();
task.appendvars(2*n); // z1, z2
task.appendcons(n); // z1 + z2 = 1
task.appendafes(4*n); //theta * X[i] - t[i], -t[i], z1[i], z2[i]
int z1 = nvar, z2 = nvar+n;
int zcon = ncon;
long thetaafe = nafe, tafe = nafe+n, z1afe = nafe+2*n, z2afe = nafe+3*n;
int k = 0;
// Linear constraints
int[] subi = new int[2*n];
int[] subj = new int[2*n];
double[] aval = new double[2*n];
for(int i = 0; i < n; i++)
{
// z1 + z2 = 1
subi[k] = zcon+i; subj[k] = z1+i; aval[k] = 1; k++;
subi[k] = zcon+i; subj[k] = z2+i; aval[k] = 1; k++;
}
task.putaijlist(subi, subj, aval);
task.putconboundsliceconst(zcon, zcon+n, boundkey.fx, 1, 1);
task.putvarboundsliceconst(nvar, nvar+2*n, boundkey.fr, -inf, inf);
// Affine conic expressions
long[] afeidx = new long[d*n+4*n];
int[] varidx = new int[d*n+4*n];
double[] fval = new double[d*n+4*n];
k = 0;
// Thetas
for(int i = 0; i < n; i++) {
for(int j = 0; j < d; j++) {
afeidx[k] = thetaafe + i; varidx[k] = theta + j;
fval[k] = ((y[i]) ? -1 : 1) * X[i,j];
k++;
}
}
// -t[i]
for(int i = 0; i < n; i++) {
afeidx[k] = thetaafe + i; varidx[k] = t + i; fval[k] = -1; k++;
afeidx[k] = tafe + i; varidx[k] = t + i; fval[k] = -1; k++;
}
// z1, z2
for(int i = 0; i < n; i++) {
afeidx[k] = z1afe + i; varidx[k] = z1 + i; fval[k] = 1; k++;
afeidx[k] = z2afe + i; varidx[k] = z2 + i; fval[k] = 1; k++;
}
// Add the expressions
task.putafefentrylist(afeidx, varidx, fval);
// Add a single row with the constant expression "1.0"
long oneafe = task.getnumafe();
task.appendafes(1);
task.putafeg(oneafe, 1.0);
// Add an exponential cone domain
long expdomain = task.appendprimalexpconedomain();
// Conic constraints
for(int i = 0; i < n; i++)
{
task.appendacc(expdomain, new long[]{z1afe+i, oneafe, thetaafe+i}, null);
task.appendacc(expdomain, new long[]{z2afe+i, oneafe, tafe+i}, null);
}
}
// Model logistic regression (regularized with full 2-norm of theta)
// X - n x d matrix of data points
// y - length n vector classifying training points
// lamb - regularization parameter
public static double[] logisticRegression(Env env,
double[,] X,
bool[] y,
double lamb)
{
int n = X.GetLength(0);
int d = X.GetLength(1); // num samples, dimension
using (Task task = new Task(env, 0, 0))
{
// Variables [r; theta; t]
int nvar = 1+d+n;
task.appendvars(nvar);
task.putvarboundsliceconst(0, nvar, boundkey.fr, -inf, inf);
int r = 0, theta = 1, t = 1+d;
// Objective lambda*r + sum(t)
task.putobjsense(mosek.objsense.minimize);
task.putcj(r, lamb);
for(int i = 0; i < n; i++)
task.putcj(t+i, 1.0);
// Softplus function constraints
softplus(task, d, n, theta, t, X, y);
// Regularization
// Append a sequence of linear expressions (r, theta) to F
long numafe = task.getnumafe();
task.appendafes(1+d);
task.putafefentry(numafe, r, 1.0);
for(int i = 0; i < d; i++)
task.putafefentry(numafe + i + 1, theta + i, 1.0);
// Add the constraint
task.appendaccseq(task.appendquadraticconedomain(1+d), numafe, null);
// Solution
task.optimize();
return task.getxxslice(soltype.itr, theta, theta+d);
}
}
public static void Main(String[] args)
{
Env env = new Env();
// Test: detect and approximate a circle using degree 2 polynomials
int n = 30;
double[,] X = new double[n*n, 6];
bool[] Y = new bool[n*n];
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int k = i*n+j;
double x = -1 + 2.0*i/(n-1);
double y = -1 + 2.0*j/(n-1);
X[k,0] = 1.0; X[k,1] = x; X[k,2] = y; X[k,3] = x*y;
X[k,4] = x*x; X[k,5] = y*y;
Y[k] = (x*x+y*y>=0.69);
}
double[] theta = logisticRegression(env, X, Y, 0.1);
for(int i=0;i<6;i++)
Console.WriteLine(theta[i]);
}
}
}
mico1.cs
/*
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File : mico1.cs
Purpose : Demonstrates how to solve a small mixed
integer conic optimization problem.
minimize x^2 + y^2
subject to x >= e^y + 3.8
x, y - integer
*/
using System;
namespace mosek.example
{
public class MsgClass : mosek.Stream
{
public MsgClass () {}
public override void streamCB (string msg)
{
Console.Write ("{0}", msg);
}
}
public class mico1
{
public static void Main ()
{
mosek.Env env = new mosek.Env ();
mosek.Task task = new mosek.Task(env, 0, 0);
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
MsgClass task_msg_obj = new MsgClass ();
task.set_Stream (mosek.streamtype.log, task_msg_obj);
task.appendvars(3); // x, y, t
int x=0, y=1, t=2;
task.putvarboundsliceconst(0, 3, mosek.boundkey.fr, -0.0, 0.0);
// Integrality constraints for x, y
task.putvartypelist(new int[]{x,y},
new mosek.variabletype[]{mosek.variabletype.type_int, mosek.variabletype.type_int});
// Set up the affine expressions
// x, x-3.8, y, t, 1.0
task.appendafes(5);
task.putafefentrylist(new long[]{0,1,2,3},
new int[]{x,x,y,t},
new double[]{1,1,1,1});
task.putafegslice(0, 5, new double[]{0, -3.8, 0, 0, 1.0});
// Add constraint (x-3.8, 1, y) \in \EXP
task.appendacc(task.appendprimalexpconedomain(), new long[]{1, 4, 2}, null);
// Add constraint (t, x, y) \in \QUAD
task.appendacc(task.appendquadraticconedomain(3), new long[]{3, 0, 2}, null);
// Objective
task.putobjsense(mosek.objsense.minimize);
task.putcj(t, 1);
// Optimize the task
task.optimize();
task.solutionsummary(mosek.streamtype.msg);
double[] xx = task.getxxslice(mosek.soltype.itg, 0, 2);
Console.WriteLine ("x = {0}, y = {1}", xx[0], xx[1]);
}
}
}
milo1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: milo1.cs
Purpose: Demonstrates how to solve a small mixed
integer linear optimization problem using the MOSEK C# API.
*/
using System;
namespace mosek.example
{
public class MsgClass : mosek.Stream
{
public MsgClass ()
{
/* Construct the object */
}
public override void streamCB (string msg)
{
Console.Write ("{0}", msg);
}
}
public class milo1
{
public static void Main ()
{
const int numcon = 2;
const int numvar = 2;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
mosek.boundkey[] bkc = { mosek.boundkey.up,
mosek.boundkey.lo
};
double[] blc = { -infinity,
-4.0
};
double[] buc = { 250.0,
infinity
};
mosek.boundkey[] bkx = { mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = { 0.0,
0.0
};
double[] bux = { infinity,
infinity
};
double[] c = {1.0, 0.64 };
int[][] asub = { new int[] {0, 1}, new int[] {0, 1} };
double[][] aval = { new double[] {50.0, 3.0}, new double[] {31.0, -2.0} };
double[] xx = new double[numvar];
try {
// Create a task object linked with the environment env.
using (var task = new mosek.Task ()) {
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
MsgClass task_msg_obj = new MsgClass ();
task.set_Stream (mosek.streamtype.log, task_msg_obj);
/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
/* Optionally add a constant term to the objective. */
task.putcfix(0.0);
for (int j = 0; j < numvar; ++j)
{
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
/* Input column j of A */
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/* Specify integer variables. */
for (int j = 0; j < numvar; ++j)
task.putvartype(j, mosek.variabletype.type_int);
task.putobjsense(mosek.objsense.maximize);
/* Set max solution time */
task.putdouparam(mosek.dparam.mio_max_time, 60.0);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
mosek.solsta solsta;
/* Get status information about the solution */
task.getsolsta(mosek.soltype.itg, out solsta);
task.getxx(mosek.soltype.itg, // Integer solution.
xx);
switch (solsta)
{
case mosek.solsta.optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:", xx[j]);
break;
case mosek.solsta.prim_feas:
Console.WriteLine ("Feasible primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:", xx[j]);
break;
case mosek.solsta.unknown:
mosek.prosta prosta;
task.getprosta(mosek.soltype.itg, out prosta);
switch (prosta)
{
case mosek.prosta.prim_infeas_or_unbounded:
Console.WriteLine("Problem status Infeasible or unbounded");
break;
case mosek.prosta.prim_infeas:
Console.WriteLine("Problem status Infeasible.");
break;
case mosek.prosta.unknown:
Console.WriteLine("Problem status unknown.");
break;
default:
Console.WriteLine("Other problem status.");
break;
}
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
}
}
}
mioinitsol.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: mioinitsol.cs
Purpose: Demonstrates how to solve a MIP with a start guess.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class mioinitsol
{
public static void Main ()
{
mosek.Env env = null;
mosek.Task task = null;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
int numvar = 4;
int numcon = 1;
int NUMINTVAR = 3;
double[] c = { 7.0, 10.0, 1.0, 5.0 };
mosek.boundkey[] bkc = {mosek.boundkey.up};
double[] blc = { -infinity};
double[] buc = {2.5};
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0,
0.0
};
double[] bux = {infinity,
infinity,
infinity,
infinity
};
int[] ptrb = {0, 1, 2, 3};
int[] ptre = {1, 2, 3, 4};
double[] aval = {1.0, 1.0, 1.0, 1.0};
int[] asub = {0, 0, 0, 0};
int[] intsub = {0, 1, 2};
double[] xx = new double[numvar];
try
{
// Make mosek environment.
env = new mosek.Env ();
// Create a task object linked with the environment env.
task = new mosek.Task (env, numcon, numvar);
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass ("[task]"));
task.inputdata(numcon, numvar,
c,
0.0,
ptrb,
ptre,
asub,
aval,
bkc,
blc,
buc,
bkx,
blx,
bux);
for (int j = 0 ; j < NUMINTVAR ; ++j)
task.putvartype(intsub[j], mosek.variabletype.type_int);
task.putobjsense(mosek.objsense.maximize);
// Assign values to integer variables.
// We only set a slice of xx
double[] values = {1.0, 1.0, 0.0};
task.putxxslice(mosek.soltype.itg, 0, 3, values);
// Request constructing the solution from integer variable values
task.putintparam(mosek.iparam.mio_construct_sol, mosek.onoffkey.on);
try
{
task.optimize();
task.solutionsummary(mosek.streamtype.log);
}
catch (mosek.Warning w)
{
Console.WriteLine("Mosek warning:");
Console.WriteLine (w.Code);
Console.WriteLine (w);
}
task.getxx(mosek.soltype.itg, xx);
Console.WriteLine("Solution:");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:{1}", j, xx[j]);
// Was the initial solution used?
int constr = task.getintinf(mosek.iinfitem.mio_construct_solution);
double constrVal = task.getdouinf(mosek.dinfitem.mio_construct_solution_obj);
Console.WriteLine("Construct solution utilization: " + constr);
Console.WriteLine("Construct solution objective: " + constrVal);
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
if (task != null) task.Dispose ();
if (env != null) env.Dispose ();
}
}
}
opt_server_async.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: opt_server_async.cs
Purpose : Demonstrates how to use MOSEK OptServer
to solve optimization problem asynchronously
*/
using System;
using System.Threading;
namespace mosek.example
{
class msgclass : mosek.Stream
{
public override void streamCB (string msg)
{
Console.Write ("{0}", msg);
}
}
public class opt_server_async
{
public static void Main (string[] args)
{
if (args.Length == 0) {
Console.WriteLine ("Missing argument, syntax is:");
Console.WriteLine (" opt_server inputfile host:port numpolls [cert]");
}
else {
string inputfile = args[0];
string addr = args[1];
int numpolls = Convert.ToInt32(args[2]);
String cert = args.Length < 4 ? null : args[3];
using (mosek.Env env = new mosek.Env())
{
string token;
using (mosek.Task task = new mosek.Task(env))
{
task.readdata (inputfile);
if (cert != null)
task.putstrparam(sparam.remote_tls_cert_path,cert);
token = task.asyncoptimize (addr,"");
}
using (mosek.Task task = new mosek.Task(env))
{
task.readdata (inputfile);
if (cert != null)
task.putstrparam(sparam.remote_tls_cert_path,cert);
task.set_Stream (mosek.streamtype.log, new msgclass ());
Console.WriteLine("Starting polling loop...");
int i = 0;
while ( true )
{
Thread.Sleep(500);
Console.WriteLine("poll {0}...\n", i);
mosek.rescode trm;
mosek.rescode resp;
bool respavailable = task.asyncpoll( addr,
"",
token,
out resp,
out trm);
Console.WriteLine("polling done");
if (respavailable)
{
Console.WriteLine("solution available!");
task.asyncgetresult(addr,
"",
token,
out resp,
out trm);
task.solutionsummary (mosek.streamtype.log);
break;
}
if (i == numpolls)
{
Console.WriteLine("max num polls reached, stopping host.");
task.asyncstop (addr,"", token);
break;
}
i++;
}
}
}
}
}
}
}
opt_server_sync.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: opt_server_sync.cs
Purpose : Demonstrates how to use MOSEK OptServer
to solve optimization problem synchronously
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
public override void streamCB (string msg)
{
Console.Write ("{0}", msg);
}
}
public class simple
{
public static void Main (string[] args)
{
if (args.Length == 0)
{
Console.WriteLine ("Missing arguments, syntax is:");
Console.WriteLine (" opt_server_sync inputfile addr [certpath]");
}
else
{
String inputfile = args[0];
String addr = args[1];
String cert = args.Length < 3 ? null : args[2];
mosek.rescode trm;
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env))
{
task.set_Stream (mosek.streamtype.log, new msgclass ());
// Load some data into the task
task.readdata (inputfile);
// Set OptServer URL
task.putoptserverhost(addr);
// Path to certificate, if any
if (cert != null)
task.putstrparam(sparam.remote_tls_cert_path, cert);
// Optimize remotely, no access token
task.optimize (out trm);
task.solutionsummary (mosek.streamtype.log);
}
}
}
}
}
}
parallel.cs
/*
File : parallel.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Demonstrates parallel optimization
using optimizebatch()
*/
using System.Threading.Tasks;
using System;
namespace mosek.example
{
public class Parallel
{
/** Example of how to use env.optimizebatch().
Optimizes tasks whose names were read from command line.
*/
public static void Main(string[] argv)
{
int n = argv.Length;
mosek.Task[] tasks = new mosek.Task[n];
mosek.rescode[] res = new mosek.rescode[n];
mosek.rescode[] trm = new mosek.rescode[n];
/* Size of thread pool available for all tasks */
int threadpoolsize = 6;
using (var env = new mosek.Env())
{
/* Create an example list of tasks to optimize */
for(int i = 0; i < n; i++)
{
tasks[i] = new mosek.Task(env);
tasks[i].readdata(argv[i]);
// We can set the number of threads for each task
tasks[i].putintparam(mosek.iparam.num_threads, 2);
}
// Optimize all the given tasks in parallel
env.optimizebatch(false, // No race
-1.0, // No time limit
threadpoolsize,
tasks, // Array of tasks to optimize
trm,
res);
for(int i = 0; i < n; i++)
Console.WriteLine("Task {0} res {1} trm {2} obj_val {3} time {4}",
i,
res[i],
trm[i],
tasks[i].getdouinf(mosek.dinfitem.intpnt_primal_obj),
tasks[i].getdouinf(mosek.dinfitem.optimizer_time));
}
}
}
}
parameters.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: parameters.cs
Purpose: Demonstrates a very simple example about how to get/set
parameters with MOSEK .NET API
*/
using System;
namespace mosek.example
{
public class parameters
{
public static void Main()
{
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
Console.WriteLine("Test MOSEK parameter get/set functions");
// Set log level (integer parameter)
task.putintparam(mosek.iparam.log, 1);
// Select interior-point optimizer... (integer parameter)
task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.intpnt);
// ... without basis identification (integer parameter)
task.putintparam(mosek.iparam.intpnt_basis, mosek.basindtype.never);
// Set relative gap tolerance (double parameter)
task.putdouparam(mosek.dparam.intpnt_co_tol_rel_gap, 1.0e-7);
// The same using explicit string names
task.putparam ("MSK_DPAR_INTPNT_CO_TOL_REL_GAP", "1.0e-7");
task.putnadouparam("MSK_DPAR_INTPNT_CO_TOL_REL_GAP", 1.0e-7 );
// Incorrect value
try
{
task.putdouparam(mosek.dparam.intpnt_co_tol_rel_gap, -1.0);
}
catch (mosek.Error)
{
Console.WriteLine("Wrong parameter value");
}
double param = task.getdouparam(mosek.dparam.intpnt_co_tol_rel_gap);
Console.WriteLine("Current value for parameter intpnt_co_tol_rel_gap = " + param);
/* Define and solve an optimization problem here */
/* task.optimize() */
/* After optimization: */
Console.WriteLine("Get MOSEK information items");
double tm = task.getdouinf(mosek.dinfitem.optimizer_time);
int iter = task.getintinf(mosek.iinfitem.intpnt_iter);
Console.WriteLine("Time: " + tm);
Console.WriteLine("Iterations: " + iter);
}
}
}
}
}
pinfeas.cs
// File : pinfeas.cs
//
// Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
//
// Purpose: Demonstrates how to fetch a primal infeasibility certificate
// for a linear problem
//
using System;
namespace mosek.example {
class msgclass : mosek.Stream {
public msgclass () {}
public override void streamCB (string msg) {
Console.Write (msg);
}
}
public class pinfeas {
static double inf = 0.0; // Infinity for symbolic purposes
// Set up a simple linear problem from the manual for test purposes
public static mosek.Task testProblem() {
mosek.Task task = new mosek.Task();
task.appendvars(7);
task.appendcons(7);
task.putclist(new int[]{0,1,2,3,4,5,6}, new double[]{1,2,5,2,1,2,1});
task.putaijlist(new int[]{0,0,1,1,2,2,2,3,3,4,5,5,6,6},
new int[]{0,1,2,3,4,5,6,0,4,1,2,5,3,6},
new double[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1});
mosek.boundkey up = mosek.boundkey.up,
fx = mosek.boundkey.fx,
lo = mosek.boundkey.lo;
task.putconboundslice(0, 7, new mosek.boundkey[]{up,up,up,fx,fx,fx,fx},
new double[]{-inf, -inf, -inf, 1100, 200, 500, 500},
new double[]{200, 1000, 1000, 1100, 200, 500, 500});
task.putvarboundsliceconst(0, 7, lo, 0, +inf);
return task;
}
// Analyzes and prints infeasibility contributing elements
// sl - dual values for lower bounds
// su - dual values for upper bounds
// eps - tolerance for when a nunzero dual value is significant
public static void analyzeCertificate(double[] sl, double[] su, double eps) {
for(int i = 0; i < sl.Length; i++) {
if (Math.Abs(sl[i]) > eps)
Console.WriteLine("#{0}, lower, dual = {1}", i, sl[i]);
if (Math.Abs(su[i]) > eps)
Console.WriteLine("#{0}, upper, dual = {1}", i, su[i]);
}
}
public static void Main () {
// In this example we set up a simple problem
// One could use any task or a task read from a file
mosek.Task task = testProblem();
// Useful for debugging
task.writedata("pinfeas.ptf"); // Write file in human-readable format
// Attach a log stream printer to the task
task.set_Stream (mosek.streamtype.log, new msgclass ());
// Perform the optimization.
task.optimize();
task.solutionsummary(mosek.streamtype.log);
// Check problem status, we use the interior point solution
if (task.getprosta(soltype.itr) == prosta.prim_infeas) {
// Set the tolerance at which we consider a dual value as essential
double eps = 1e-7;
Console.WriteLine("Variable bounds important for infeasibility: ");
analyzeCertificate(task.getslx(soltype.itr), task.getsux(soltype.itr), eps);
Console.WriteLine("Constraint bounds important for infeasibility: ");
analyzeCertificate(task.getslc(soltype.itr), task.getsuc(soltype.itr), eps);
}
else {
Console.WriteLine("The problem is not primal infeasible, no certificate to show");
}
}
}
}
portfolio_1_basic.cs
/*
File : portfolio_1_basic.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Implements a basic portfolio optimization model.
*/
using System;
using mosek;
namespace mosek.example
{
/* Log handler class */
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx) { prefix = prfx; }
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class portfolio_1_basic
{
public static void Main (String[] args)
{
// Since the value infinity is never used, we define
// 'infinity' for symbolic purposes only
double infinity = 0.0;
int n = 8;
double gamma = 36.0;
double[] mu = {0.07197349, 0.15518171, 0.17535435, 0.0898094 , 0.42895777, 0.39291844, 0.32170722, 0.18378628};
double[,] GT = {
{0.30758, 0.12146, 0.11341, 0.11327, 0.17625, 0.11973, 0.10435, 0.10638},
{0.0, 0.25042, 0.09946, 0.09164, 0.06692, 0.08706, 0.09173, 0.08506},
{0.0, 0.0, 0.19914, 0.05867, 0.06453, 0.07367, 0.06468, 0.01914},
{0.0, 0.0, 0.0, 0.20876, 0.04933, 0.03651, 0.09381, 0.07742},
{0.0, 0.0, 0.0, 0.0, 0.36096, 0.12574, 0.10157, 0.0571 },
{0.0, 0.0, 0.0, 0.0, 0.0, 0.21552, 0.05663, 0.06187},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.22514, 0.03327},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2202 }
};
int k = GT.GetLength(0);
double[] x0 = {8.0, 5.0, 3.0, 5.0, 2.0, 9.0, 3.0, 6.0};
double w = 59;
double totalBudget;
//Offset of variables into the API variable.
int numvar = n;
int voff_x = 0;
// Constraints offsets
int numcon = 1;
int coff_bud = 0;
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream
task.set_Stream(mosek.streamtype.log, new msgclass (""));
// Holding variable x of length n
// No other auxiliary variables are needed in this formulation
task.appendvars(numvar);
// Setting up variable x
for (int j = 0; j < n; ++j)
{
/* Optionally we can give the variables names */
task.putvarname(voff_x + j, "x[" + (j + 1) + "]");
/* No short-selling - x^l = 0, x^u = inf */
task.putvarbound(voff_x + j, mosek.boundkey.lo, 0.0, infinity);
}
// One linear constraint: total budget
task.appendcons(1);
task.putconname(coff_bud, "budget");
for (int j = 0; j < n; ++j)
{
/* Coefficients in the first row of A */
task.putaij(coff_bud, voff_x + j, 1.0);
}
totalBudget = w;
for (int i = 0; i < n; ++i)
{
totalBudget += x0[i];
}
task.putconbound(coff_bud, mosek.boundkey.fx, totalBudget, totalBudget);
// Input (gamma, GTx) in the AFE (affine expression) storage
// We need k+1 rows
task.appendafes(k + 1);
// The first affine expression = gamma
task.putafeg(0, gamma);
// The remaining k expressions comprise GT*x, we add them row by row
// In more realisic scenarios it would be better to extract nonzeros and input in sparse form
int[] vslice_x = new int[n];
double[] GT_row = new double[n];
for (int i = 0; i < n; ++i)
{
vslice_x[i] = voff_x + i;
}
for (int i = 0; i < k; ++i)
{
for (int j = 0; j < n; ++j) GT_row[j] = GT[i, j];
task.putafefrow(i + 1, vslice_x, GT_row);
}
// Input the affine conic constraint (gamma, GT*x) \in QCone
// Add the quadratic domain of dimension k+1
long qdom = task.appendquadraticconedomain(k + 1);
// Add the constraint
task.appendaccseq(qdom, 0, null);
task.putaccname(0, "risk");
// Objective: maximize expected return mu^T x
for (int j = 0; j < n; ++j)
{
task.putcj(voff_x + j, mu[j]);
}
task.putobjsense(mosek.objsense.maximize);
task.optimize();
/* Display solution summary for quick inspection of results */
task.solutionsummary(mosek.streamtype.log);
// Check if the interior point solution is an optimal point
solsta solsta = task.getsolsta(mosek.soltype.itr);
if (solsta != mosek.solsta.optimal)
{
// See https://docs.mosek.com/latest/dotnetapi/accessing-solution.html about handling solution statuses.
throw new Exception(rescode.err_unhandled_solution_status, String.Format("Unexpected solution status: {0}", solsta));
}
task.writedata("dump.ptf");
/* Read the results */
double expret = 0.0;
double[] xx = new double[n];
task.getxxslice(mosek.soltype.itr, voff_x, voff_x + n, xx);
for (int j = 0; j < n; ++j)
expret += mu[j] * xx[j + voff_x];
Console.WriteLine("\nExpected return {0:E} for gamma {1:E}", expret, gamma);
}
}
}
}
}
portfolio_2_frontier.cs
/*
File : portfolio_2_frontier.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Implements a basic portfolio optimization model.
Computes points on the efficient frontier.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class portfolio_2_frontier
{
public static void Main (String[] args)
{
double infinity = 0;
int n = 8;
double[] mu = {0.07197, 0.15518, 0.17535, 0.08981, 0.42896, 0.39292, 0.32171, 0.18379};
double[,] GT = {
{0.30758, 0.12146, 0.11341, 0.11327, 0.17625, 0.11973, 0.10435, 0.10638},
{0.0, 0.25042, 0.09946, 0.09164, 0.06692, 0.08706, 0.09173, 0.08506},
{0.0, 0.0, 0.19914, 0.05867, 0.06453, 0.07367, 0.06468, 0.01914},
{0.0, 0.0, 0.0, 0.20876, 0.04933, 0.03651, 0.09381, 0.07742},
{0.0, 0.0, 0.0, 0.0, 0.36096, 0.12574, 0.10157, 0.0571 },
{0.0, 0.0, 0.0, 0.0, 0.0, 0.21552, 0.05663, 0.06187},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.22514, 0.03327},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2202 }
};
int k = GT.GetLength(0);
double[] x0 = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
double w = 1.0;
double[] alphas = {0.0, 0.01, 0.1, 0.25, 0.30, 0.35, 0.4, 0.45, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 10.0};
int numalphas = 15;
double totalBudget;
// Offset of variables into the API variable.
int numvar = n + 1;
int voff_x = 0;
int voff_s = n;
// Offset of constraints
int coff_bud = 0;
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
task.appendvars(numvar);
// Setting up variable x
for (int j = 0; j < n; ++j)
{
/* Optionally we can give the variables names */
task.putvarname(voff_x + j, "x[" + (j + 1) + "]");
/* No short-selling - x^l = 0, x^u = inf */
task.putvarbound(voff_x + j, mosek.boundkey.lo, 0.0, infinity);
}
task.putvarname(voff_s, "s");
task.putvarbound(voff_s, mosek.boundkey.fr, -infinity, infinity);
// One linear constraint: total budget
task.appendcons(1);
task.putconname(coff_bud, "budget");
for (int j = 0; j < n; ++j)
{
/* Coefficients in the first row of A */
task.putaij(coff_bud, voff_x + j, 1.0);
}
totalBudget = w;
for (int i = 0; i < n; ++i)
{
totalBudget += x0[i];
}
task.putconbound(coff_bud, mosek.boundkey.fx, totalBudget, totalBudget);
// Input (gamma, GTx) in the AFE (affine expression) storage
// We build the following F and g for variables [x, s]:
// [0, 1] [0 ]
// F = [0, 0], g = [0.5]
// [GT,0] [0 ]
// We need k+2 rows
task.appendafes(k + 2);
// The first affine expression is variable s (last variable, index n)
task.putafefentry(0, n, 1.0);
// The second affine expression is constant 0.5
task.putafeg(1, 0.5);
// The remaining k expressions comprise GT*x, we add them row by row
// In more realisic scenarios it would be better to extract nonzeros and input in sparse form
int[] vslice_x = new int[n];
double[] GT_row = new double[n];
for (int i = 0; i < n; ++i)
{
vslice_x[i] = voff_x + i;
}
for (int i = 0; i < k; ++i)
{
for (int j = 0; j < n; ++j) GT_row[j] = GT[i, j];
task.putafefrow(i + 2, vslice_x, GT_row);
}
// Input the affine conic constraint (gamma, GT*x) \in QCone
// Add the quadratic domain of dimension k+1
long rqdom = task.appendrquadraticconedomain(k + 2);
// Add the constraint
task.appendaccseq(rqdom, 0, null);
task.putaccname(0, "risk");
// Objective: maximize expected return mu^T x
for (int j = 0; j < n; ++j)
{
task.putcj(voff_x + j, mu[j]);
}
task.putobjsense(mosek.objsense.maximize);
task.writedata("dump.ptf");
//Turn all log output off.
task.putintparam(mosek.iparam.log, 0);
Console.WriteLine("{0,-15}{1,-15}{2,-15}", "alpha", "exp ret", "std. dev.");
for (int i = 0; i < numalphas; ++i)
{
task.putcj(voff_s, -alphas[i]);
task.optimize();
task.solutionsummary(mosek.streamtype.log);
// Check if the interior point solution is an optimal point
solsta solsta = task.getsolsta(mosek.soltype.itr);
if (solsta != mosek.solsta.optimal)
{
// See https://docs.mosek.com/latest/dotnetapi/accessing-solution.html about handling solution statuses.
throw new Exception(rescode.err_unhandled_solution_status, String.Format("Unexpected solution status: {0}", solsta));
}
double expret = 0.0;
double[] xx = new double[numvar];
task.getxx(mosek.soltype.itr, xx);
for (int j = 0; j < n; ++j)
expret += mu[j] * xx[j + voff_x];
Console.WriteLine("{0:E6} {1:E} {2:E}", alphas[i], expret, Math.Sqrt(xx[voff_s]));
}
Console.WriteLine("\n");
}
}
}
}
}
portfolio_3_impact.cs
/*
File : portfolio_3_impact.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Implements a basic portfolio optimization model
with transaction costs of type x^(3/2)
*/
using System;
namespace mosek.example {
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class portfolio_3_impact
{
public static void Main (String[] args)
{
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
int n = 8;
double[] mu = {0.07197, 0.15518, 0.17535, 0.08981, 0.42896, 0.39292, 0.32171, 0.18379};
double[,] GT = {
{0.30758, 0.12146, 0.11341, 0.11327, 0.17625, 0.11973, 0.10435, 0.10638},
{0.0, 0.25042, 0.09946, 0.09164, 0.06692, 0.08706, 0.09173, 0.08506},
{0.0, 0.0, 0.19914, 0.05867, 0.06453, 0.07367, 0.06468, 0.01914},
{0.0, 0.0, 0.0, 0.20876, 0.04933, 0.03651, 0.09381, 0.07742},
{0.0, 0.0, 0.0, 0.0, 0.36096, 0.12574, 0.10157, 0.0571 },
{0.0, 0.0, 0.0, 0.0, 0.0, 0.21552, 0.05663, 0.06187},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.22514, 0.03327},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2202 }
};
int k = GT.GetLength(0);
double[] x0 = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
double w = 1.0;
double gamma = 0.36;
double totalBudget;
double[] m = new double[n];
for (int i = 0; i < n; ++i)
{
m[i] = 0.01;
}
// Offset of variables into the API variable.
int numvar = 3 * n;
int voff_x = 0;
int voff_c = n;
int voff_z = 2 * n;
// Offset of constraints.
int numcon = 2 * n + 1;
int coff_bud = 0;
int coff_abs1 = 1;
int coff_abs2 = 1 + n;
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream(mosek.streamtype.log, new msgclass(""));
// Variables (vector of x, c, z)
task.appendvars(numvar);
for (int j = 0; j < n; ++j)
{
/* Optionally we can give the variables names */
task.putvarname(voff_x + j, "x[" + (j + 1) + "]");
task.putvarname(voff_c + j, "c[" + (j + 1) + "]");
task.putvarname(voff_z + j, "z[" + (j + 1) + "]");
/* Apply variable bounds (x >= 0, c and z free) */
task.putvarbound(voff_x + j, mosek.boundkey.lo, 0.0, infinity);
task.putvarbound(voff_c + j, mosek.boundkey.fr, -infinity, infinity);
task.putvarbound(voff_z + j, mosek.boundkey.fr, -infinity, infinity);
}
// Linear constraints
// - Total budget
task.appendcons(1);
task.putconname(coff_bud, "budget");
for (int j = 0; j < n; ++j)
{
/* Coefficients in the first row of A */
task.putaij(coff_bud, voff_x + j, 1.0);
task.putaij(coff_bud, voff_c + j, m[j]);
}
totalBudget = w;
for (int i = 0; i < n; ++i)
{
totalBudget += x0[i];
}
task.putconbound(coff_bud, mosek.boundkey.fx, totalBudget, totalBudget);
// - Absolute value
task.appendcons(2 * n);
for (int i = 0; i < n; ++i)
{
task.putconname(coff_abs1 + i, "zabs1[" + (1 + i) + "]");
task.putaij(coff_abs1 + i, voff_x + i, -1.0);
task.putaij(coff_abs1 + i, voff_z + i, 1.0);
task.putconbound(coff_abs1 + i, mosek.boundkey.lo, -x0[i], infinity);
task.putconname(coff_abs2 + i, "zabs2[" + (1 + i) + "]");
task.putaij(coff_abs2 + i, voff_x + i, 1.0);
task.putaij(coff_abs2 + i, voff_z + i, 1.0);
task.putconbound(coff_abs2 + i, mosek.boundkey.lo, x0[i], infinity);
}
// ACCs
int aoff_q = 0;
int aoff_pow = k + 1;
// - (gamma, GTx) in Q(k+1)
// The part of F and g for variable x:
// [0, 0, 0] [gamma]
// F = [GT, 0, 0], g = [0 ]
task.appendafes(k + 1);
task.putafeg(aoff_q, gamma);
int[] vslice_x = new int[n];
double[] GT_row = new double[n];
for (int i = 0; i < n; ++i)
{
vslice_x[i] = voff_x + i;
}
for (int i = 0; i < k; ++i)
{
for (int j = 0; j < n; ++j) GT_row[j] = GT[i, j];
task.putafefrow(aoff_q + i + 1, vslice_x, GT_row);
}
long qdom = task.appendquadraticconedomain(k + 1);
task.appendaccseq(qdom, aoff_q, null);
task.putaccname(aoff_q, "risk");
// - (c_j, 1, z_j) in P3(2/3, 1/3)
// The part of F and g for variables [c, z]:
// [0, I, 0] [0]
// F = [0, 0, I], g = [0]
// [0, 0, 0] [1]
task.appendafes(2 * n + 1);
for (int i = 0; i < n; ++i)
{
task.putafefentry(aoff_pow + i, voff_c + i, 1.0);
task.putafefentry(aoff_pow + n + i, voff_z + i, 1.0);
}
task.putafeg(aoff_pow + 2 * n, 1.0);
// We use one row from F and g for both c_j and z_j, and the last row of F and g for the constant 1.
// NOTE: Here we reuse the last AFE and the power cone n times, but we store them only once.
double[] exponents = {2, 1};
long powdom = task.appendprimalpowerconedomain(3, exponents);
long[] flat_afe_list = new long[3 * n];
long[] dom_list = new long[n];
for (int i = 0; i < n; ++i)
{
flat_afe_list[3 * i + 0] = aoff_pow + i;
flat_afe_list[3 * i + 1] = aoff_pow + 2 * n;
flat_afe_list[3 * i + 2] = aoff_pow + n + i;
dom_list[i] = powdom;
}
task.appendaccs(dom_list, flat_afe_list, null);
for (int i = 0; i < n; ++i)
{
task.putaccname(i + 1, "market_impact[" + i + "]");
}
// Objective: maximize expected return mu^T x
for (int j = 0; j < n; ++j)
{
task.putcj(voff_x + j, mu[j]);
}
task.putobjsense(mosek.objsense.maximize);
//Turn all log output off.
//task.putintparam(mosek.iparam.log,0);
task.writedata("dump.ptf");
/* Solve the problem */
task.optimize();
task.solutionsummary(mosek.streamtype.log);
// Check if the interior point solution is an optimal point
solsta solsta = task.getsolsta(mosek.soltype.itr);
if (solsta != mosek.solsta.optimal)
{
// See https://docs.mosek.com/latest/dotnetapi/accessing-solution.html about handling solution statuses.
throw new Exception(rescode.err_unhandled_solution_status, String.Format("Unexpected solution status: {0}", solsta));
}
double expret = 0.0;
double[] xx = new double[numvar];
task.getxx(mosek.soltype.itr, xx);
for (int j = 0; j < n; ++j)
expret += mu[j] * xx[j + voff_x];
Console.WriteLine("Expected return {0:E6} for gamma {1:E6}\n\n", expret, gamma);
}
}
}
}
}
portfolio_4_transcost.cs
/*
File : portfolio_4_transcost.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Implements a basic portfolio optimization model
with fixed setup costs and transaction costs
as a mixed-integer problem.
*/
using System;
namespace mosek.example {
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class portfolio_4_transcost
{
public static void Main (String[] args)
{
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
int n = 8;
double[] mu = {0.07197, 0.15518, 0.17535, 0.08981, 0.42896, 0.39292, 0.32171, 0.18379};
double[,] GT = {
{0.30758, 0.12146, 0.11341, 0.11327, 0.17625, 0.11973, 0.10435, 0.10638},
{0.0, 0.25042, 0.09946, 0.09164, 0.06692, 0.08706, 0.09173, 0.08506},
{0.0, 0.0, 0.19914, 0.05867, 0.06453, 0.07367, 0.06468, 0.01914},
{0.0, 0.0, 0.0, 0.20876, 0.04933, 0.03651, 0.09381, 0.07742},
{0.0, 0.0, 0.0, 0.0, 0.36096, 0.12574, 0.10157, 0.0571 },
{0.0, 0.0, 0.0, 0.0, 0.0, 0.21552, 0.05663, 0.06187},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.22514, 0.03327},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2202 }
};
int k = GT.GetLength(0);
double[] x0 = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
double w = 1.0;
double gamma = 0.36;
double totalBudget;
double[] f = new double[n];
double[] g = new double[n];
for (int i = 0; i < n; ++i)
{
f[i] = 0.01;
g[i] = 0.001;
}
// Offset of variables.
int numvar = 3 * n;
int voff_x = 0;
int voff_z = n;
int voff_y = 2 * n;
// Offset of constraints.
int numcon = 3 * n + 1;
int coff_bud = 0;
int coff_abs1 = 1;
int coff_abs2 = 1 + n;
int coff_swi = 1 + 2 * n;
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream(mosek.streamtype.log, new msgclass(""));
// Variables (vector of x, z, y)
task.appendvars(numvar);
for (int j = 0; j < n; ++j)
{
/* Optionally we can give the variables names */
task.putvarname(voff_x + j, "x[" + (j + 1) + "]");
task.putvarname(voff_z + j, "z[" + (j + 1) + "]");
task.putvarname(voff_y + j, "y[" + (j + 1) + "]");
/* Apply variable bounds (x >= 0, z free, y binary) */
task.putvarbound(voff_x + j, mosek.boundkey.lo, 0.0, infinity);
task.putvarbound(voff_z + j, mosek.boundkey.fr, -infinity, infinity);
task.putvarbound(voff_y + j, mosek.boundkey.ra, 0.0, 1.0);
task.putvartype(voff_y + j, mosek.variabletype.type_int);
}
// Linear constraints
// - Total budget
task.appendcons(1);
task.putconname(coff_bud, "budget");
for (int j = 0; j < n; ++j)
{
/* Coefficients in the first row of A */
task.putaij(coff_bud, voff_x + j, 1.0);
task.putaij(coff_bud, voff_z + j, g[j]);
task.putaij(coff_bud, voff_y + j, f[j]);
}
double U = w;
for (int i = 0; i < n; ++i)
{
U += x0[i];
}
task.putconbound(coff_bud, mosek.boundkey.fx, U, U);
// - Absolute value
task.appendcons(2 * n);
for (int i = 0; i < n; ++i)
{
task.putconname(coff_abs1 + i, "zabs1[" + (1 + i) + "]");
task.putaij(coff_abs1 + i, voff_x + i, -1.0);
task.putaij(coff_abs1 + i, voff_z + i, 1.0);
task.putconbound(coff_abs1 + i, mosek.boundkey.lo, -x0[i], infinity);
task.putconname(coff_abs2 + i, "zabs2[" + (1 + i) + "]");
task.putaij(coff_abs2 + i, voff_x + i, 1.0);
task.putaij(coff_abs2 + i, voff_z + i, 1.0);
task.putconbound(coff_abs2 + i, mosek.boundkey.lo, x0[i], infinity);
}
// - Switch
task.appendcons(n);
for (int i = 0; i < n; ++i)
{
task.putconname(coff_swi + i, "switch[" + (1 + i) + "]");
task.putaij(coff_swi + i, voff_z + i, 1.0);
task.putaij(coff_swi + i, voff_y + i, -U);
task.putconbound(coff_swi + i, mosek.boundkey.up, -infinity, 0.0);
}
// ACCs
int aoff_q = 0;
// - (gamma, GTx) in Q(k+1)
// The part of F and g for variable x:
// [0, 0, 0] [gamma]
// F = [GT, 0, 0], g = [0 ]
task.appendafes(k + 1);
task.putafeg(aoff_q, gamma);
int[] vslice_x = new int[n];
double[] GT_row = new double[n];
for (int i = 0; i < n; ++i)
{
vslice_x[i] = voff_x + i;
}
for (int i = 0; i < k; ++i)
{
for (int j = 0; j < n; ++j) GT_row[j] = GT[i, j];
task.putafefrow(aoff_q + i + 1, vslice_x, GT_row);
}
long qdom = task.appendquadraticconedomain(k + 1);
task.appendaccseq(qdom, aoff_q, null);
task.putaccname(aoff_q, "risk");
// Objective: maximize expected return mu^T x
for (int j = 0; j < n; ++j)
{
task.putcj(voff_x + j, mu[j]);
}
task.putobjsense(mosek.objsense.maximize);
//Turn all log output off.
//task.putintparam(mosek.iparam.log,0);
task.writedata("dump.ptf");
/* Solve the problem */
task.optimize();
task.solutionsummary(mosek.streamtype.log);
// Check if the interior point solution is an optimal point
solsta solsta = task.getsolsta(mosek.soltype.itg);
if (solsta != mosek.solsta.integer_optimal)
{
// See https://docs.mosek.com/latest/dotnetapi/accessing-solution.html about handling solution statuses.
throw new Exception(rescode.err_unhandled_solution_status, String.Format("Unexpected solution status: {0}", solsta));
}
double expret = 0.0;
double[] xx = new double[numvar];
task.getxx(mosek.soltype.itg, xx);
for (int j = 0; j < n; ++j)
expret += mu[j] * xx[j + voff_x];
Console.WriteLine("Expected return {0:E6} for gamma {1:E6}\n\n", expret, gamma);
}
}
}
}
}
portfolio_5_card.cs
/*
File : portfolio_5_card.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Implements a basic portfolio optimization model
with cardinality constraints on number of assets traded.
*/
using System;
namespace mosek.example {
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class portfolio_5_card
{
public static double[] markowitz_with_card(int n,
int k,
double[] x0,
double w,
double gamma,
double[] mu,
double[,] GT,
int K)
{
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
// Offset of variables.
int numvar = 3 * n;
int voff_x = 0;
int voff_z = n;
int voff_y = 2 * n;
// Offset of constraints.
int numcon = 3 * n + 2;
int coff_bud = 0;
int coff_abs1 = 1;
int coff_abs2 = 1 + n;
int coff_swi = 1 + 2 * n;
int coff_card = 1 + 3 * n;
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream(mosek.streamtype.log, new msgclass(""));
// Variables (vector of x, z, y)
task.appendvars(numvar);
for (int j = 0; j < n; ++j)
{
/* Optionally we can give the variables names */
task.putvarname(voff_x + j, "x[" + (j + 1) + "]");
task.putvarname(voff_z + j, "z[" + (j + 1) + "]");
task.putvarname(voff_y + j, "y[" + (j + 1) + "]");
/* Apply variable bounds (x >= 0, z free, y binary) */
task.putvarbound(voff_x + j, mosek.boundkey.lo, 0.0, infinity);
task.putvarbound(voff_z + j, mosek.boundkey.fr, -infinity, infinity);
task.putvarbound(voff_y + j, mosek.boundkey.ra, 0.0, 1.0);
task.putvartype(voff_y + j, mosek.variabletype.type_int);
}
// Linear constraints
// - Total budget
task.appendcons(1);
task.putconname(coff_bud, "budget");
for (int j = 0; j < n; ++j)
{
/* Coefficients in the first row of A */
task.putaij(coff_bud, voff_x + j, 1.0);
}
double U = w;
for (int i = 0; i < n; ++i)
{
U += x0[i];
}
task.putconbound(coff_bud, mosek.boundkey.fx, U, U);
// - Absolute value
task.appendcons(2 * n);
for (int i = 0; i < n; ++i)
{
task.putconname(coff_abs1 + i, "zabs1[" + (1 + i) + "]");
task.putaij(coff_abs1 + i, voff_x + i, -1.0);
task.putaij(coff_abs1 + i, voff_z + i, 1.0);
task.putconbound(coff_abs1 + i, mosek.boundkey.lo, -x0[i], infinity);
task.putconname(coff_abs2 + i, "zabs2[" + (1 + i) + "]");
task.putaij(coff_abs2 + i, voff_x + i, 1.0);
task.putaij(coff_abs2 + i, voff_z + i, 1.0);
task.putconbound(coff_abs2 + i, mosek.boundkey.lo, x0[i], infinity);
}
// - Switch
task.appendcons(n);
for (int i = 0; i < n; ++i)
{
task.putconname(coff_swi + i, "switch[" + (1 + i) + "]");
task.putaij(coff_swi + i, voff_z + i, 1.0);
task.putaij(coff_swi + i, voff_y + i, -U);
task.putconbound(coff_swi + i, mosek.boundkey.up, -infinity, 0.0);
}
// - Cardinality
task.appendcons(1);
task.putconname(coff_card, "cardinality");
for (int i = 0; i < n; ++i)
{
task.putaij(coff_card, voff_y + i, 1.0);
}
task.putconbound(coff_card, mosek.boundkey.up, -infinity, K);
// ACCs
int aoff_q = 0;
// - (gamma, GTx) in Q(k+1)
// The part of F and g for variable x:
// [0, 0, 0] [gamma]
// F = [GT, 0, 0], g = [0 ]
task.appendafes(k + 1);
task.putafeg(aoff_q, gamma);
int[] vslice_x = new int[n];
double[] GT_row = new double[n];
for (int i = 0; i < n; ++i)
{
vslice_x[i] = voff_x + i;
}
for (int i = 0; i < k; ++i)
{
for (int j = 0; j < n; ++j) GT_row[j] = GT[i, j];
task.putafefrow(aoff_q + i + 1, vslice_x, GT_row);
}
long qdom = task.appendquadraticconedomain(k + 1);
task.appendaccseq(qdom, aoff_q, null);
task.putaccname(aoff_q, "risk");
// Objective: maximize expected return mu^T x
for (int j = 0; j < n; ++j)
{
task.putcj(voff_x + j, mu[j]);
}
task.putobjsense(mosek.objsense.maximize);
//Turn all log output off.
task.putintparam(mosek.iparam.log,0);
//task.writedata("dump.ptf");
/* Solve the problem */
task.optimize();
task.solutionsummary(mosek.streamtype.log);
// Check if the interior point solution is an optimal point
solsta solsta = task.getsolsta(mosek.soltype.itg);
if (solsta != mosek.solsta.integer_optimal)
{
// See https://docs.mosek.com/latest/dotnetapi/accessing-solution.html about handling solution statuses.
throw new Exception(rescode.err_unhandled_solution_status, String.Format("Unexpected solution status: {0}", solsta));
}
double[] xx = task.getxxslice(mosek.soltype.itg, voff_x, voff_x + n);
return xx;
}
}
}
public static void Main (String[] args)
{
int n = 8;
double[] mu = {0.07197, 0.15518, 0.17535, 0.08981, 0.42896, 0.39292, 0.32171, 0.18379};
double[,] GT = {
{0.30758, 0.12146, 0.11341, 0.11327, 0.17625, 0.11973, 0.10435, 0.10638},
{0.0, 0.25042, 0.09946, 0.09164, 0.06692, 0.08706, 0.09173, 0.08506},
{0.0, 0.0, 0.19914, 0.05867, 0.06453, 0.07367, 0.06468, 0.01914},
{0.0, 0.0, 0.0, 0.20876, 0.04933, 0.03651, 0.09381, 0.07742},
{0.0, 0.0, 0.0, 0.0, 0.36096, 0.12574, 0.10157, 0.0571 },
{0.0, 0.0, 0.0, 0.0, 0.0, 0.21552, 0.05663, 0.06187},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.22514, 0.03327},
{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2202 }
};
int k = GT.GetLength(0);
double[] x0 = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
double w = 1.0;
double gamma = 0.25;
for (int K = 1; K <= n; K++)
{
double[] xx = markowitz_with_card(n, k, x0, w, gamma, mu, GT, K);
double expret = 0;
Console.Write("Bound: {0:d} x = ", K);
for(int i=0; i<n; i++)
{
Console.Write("{0:f5} ", xx[i]);
expret += xx[i]*mu[i];
}
Console.WriteLine(" Return: {0:f5}", expret);
}
}
}
}
portfolio_6_factor.cs
/*
File : portfolio_6_factor.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Description : Implements a portfolio optimization model using factor model.
*/
using System;
namespace mosek.example
{
/* Log handler class */
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx) { prefix = prfx; }
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class portfolio_6_factor
{
public static double sum(double[] x)
{
double r = 0.0;
for (int i = 0; i < x.Length; ++i) r += x[i];
return r;
}
public static double dot(double[] x, double[] y)
{
double r = 0.0;
for (int i = 0; i < x.Length; ++i) r += x[i] * y[i];
return r;
}
// Vectorize matrix (column-major order)
public static double[] mat_to_vec_c(double[,] m)
{
int ni = m.GetLength(0);
int nj = m.GetLength(1);
double[] c = new double[nj * ni];
for (int j = 0; j < nj; ++j)
{
for (int i = 0; i < ni; ++i)
{
c[j * ni + i] = m[i, j];
}
}
return c;
}
// Reshape vector to matrix (column-major order)
public static double[,] vec_to_mat_c(double[] c, int ni, int nj)
{
double[,] m = new double[ni, nj];
for (int j = 0; j < nj; ++j)
{
for (int i = 0; i < ni; ++i)
{
m[i, j] = c[j * ni + i];
}
}
return m;
}
public static double[,] cholesky(double[,] m)
{
int n = m.GetLength(0);
double[] vecs = mat_to_vec_c(m);
LinAlg.potrf(mosek.uplo.lo, n, vecs);
double[,] s = vec_to_mat_c(vecs, n, n);
// Zero out upper triangular part (LinAlg.Potrf does not use it, original matrix values remain there)
for (int i = 0; i < n; ++i)
{
for (int j = i+1; j < n; ++j)
{
s[i, j] = 0.0;
}
}
return s;
}
public static double[,] matrix_mul(double[,] a, double[,] b)
{
int na = a.GetLength(0);
int nb = b.GetLength(1);
int k = b.GetLength(0);
double[] vecm = new double[na * nb];
Array.Clear(vecm, 0, vecm.Length);
LinAlg.gemm(mosek.transpose.no, mosek.transpose.no, na, nb, k, 1.0, mat_to_vec_c(a), mat_to_vec_c(b), 1.0, vecm);
double[,] m = vec_to_mat_c(vecm, na, nb);
return m;
}
public static void Main (String[] args)
{
// Since the value infinity is never used, we define
// 'infinity' for symbolic purposes only
double infinity = 0;
int n = 8;
double w = 1.0;
double[] mu = {0.07197, 0.15518, 0.17535, 0.08981, 0.42896, 0.39292, 0.32171, 0.18379};
double[] x0 = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
// Factor exposure matrix
double[,] B =
{
{0.4256, 0.1869},
{0.2413, 0.3877},
{0.2235, 0.3697},
{0.1503, 0.4612},
{1.5325, -0.2633},
{1.2741, -0.2613},
{0.6939, 0.2372},
{0.5425, 0.2116}
};
// Factor covariance matrix
double[,] S_F =
{
{0.0620, 0.0577},
{0.0577, 0.0908}
};
// Specific risk components
double[] theta = {0.0720, 0.0508, 0.0377, 0.0394, 0.0663, 0.0224, 0.0417, 0.0459};
double[,] P = cholesky(S_F);
double[,] G_factor = matrix_mul(B, P);
int k = G_factor.GetLength(1);
double[] gammas = {0.24, 0.28, 0.32, 0.36, 0.4, 0.44, 0.48};
double totalBudget;
//Offset of variables into the API variable.
int numvar = n;
int voff_x = 0;
// Constraint offset
int coff_bud = 0;
// Make mosek environment.
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream
task.set_Stream(mosek.streamtype.log, new msgclass (""));
// Constraints.
task.appendvars(numvar);
// Setting up variable x
for (int j = 0; j < n; ++j)
{
/* Optionally we can give the variables names */
task.putvarname(voff_x + j, "x[" + (j + 1) + "]");
/* No short-selling - x^l = 0, x^u = inf */
task.putvarbound(voff_x + j, mosek.boundkey.lo, 0.0, infinity);
}
// One linear constraint: total budget
task.appendcons(1);
task.putconname(coff_bud, "budget");
for (int j = 0; j < n; ++j)
{
/* Coefficients in the first row of A */
task.putaij(coff_bud, voff_x + j, 1.0);
}
totalBudget = w;
for (int i = 0; i < n; ++i)
{
totalBudget += x0[i];
}
task.putconbound(coff_bud, mosek.boundkey.fx, totalBudget, totalBudget);
// Input (gamma, G_factor_T x, diag(sqrt(theta))*x) in the AFE (affine expression) storage
// We need k+n+1 rows and we fill them in in three parts
task.appendafes(k + n + 1);
// 1. The first affine expression = gamma, will be specified later
// 2. The next k expressions comprise G_factor_T*x, we add them row by row
// transposing the matrix G_factor on the fly
int[] vslice_x = new int[n];
double[] G_factor_T_row = new double[n];
for (int i = 0; i < n; ++i)
{
vslice_x[i] = voff_x + i;
}
for (int i = 0; i < k; ++i)
{
for (int j = 0; j < n; ++j) G_factor_T_row[j] = G_factor[j, i];
task.putafefrow(i + 1, vslice_x, G_factor_T_row);
}
// 3. The remaining n rows contain sqrt(theta) on the diagonal
for (int i = 0; i < n; ++i)
{
task.putafefentry(k + 1 + i, voff_x + i, Math.Sqrt(theta[i]));
}
// Input the affine conic constraint (gamma, G_factor_T x, diag(sqrt(theta))*x) \in QCone
// Add the quadratic domain of dimension k+n+1
long qdom = task.appendquadraticconedomain(k + n + 1);
// Add the constraint
task.appendaccseq(qdom, 0, null);
task.putaccname(0, "risk");
// Objective: maximize expected return mu^T x
for (int j = 0; j < n; ++j)
{
task.putcj(voff_x + j, mu[j]);
}
task.putobjsense(mosek.objsense.maximize);
for (int i = 0; i < gammas.Length; i++)
{
double gamma = gammas[i];
// Specify gamma in ACC
task.putafeg(0, gamma);
task.optimize();
/* Display solution summary for quick inspection of results */
task.solutionsummary(mosek.streamtype.log);
// Check if the interior point solution is an optimal point
solsta solsta = task.getsolsta(mosek.soltype.itr);
if (solsta != mosek.solsta.optimal)
{
// See https://docs.mosek.com/latest/dotnetapi/accessing-solution.html about handling solution statuses.
throw new Exception(rescode.err_unhandled_solution_status, String.Format("Unexpected solution status: {0}", solsta));
}
task.writedata("dump.ptf");
/* Read the results */
double expret = 0.0;
double[] xx = new double[n];
task.getxxslice(mosek.soltype.itr, voff_x, voff_x + n, xx);
for (int j = 0; j < n; ++j)
expret += mu[j] * xx[j + voff_x];
Console.WriteLine("\nExpected return {0:E} for gamma {1:E}", expret, gamma);
}
}
}
}
}
}
pow1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: pow1.cs
Purpose: Demonstrates how to solve the problem
maximize x^0.2*y^0.8 + z^0.4 - x
st x + y + 0.5z = 2
x,y,z >= 0
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class ceo1
{
public static void Main ()
{
const int numcon = 1;
const int numvar = 5; // x,y,z and 2 auxiliary variables for conic constraints
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
double[] val = { 1.0, 1.0, -1.0 };
int[] sub = { 3, 4, 0 };
double[] aval = { 1.0, 1.0, 0.5 };
int[] asub = { 0, 1, 2 };
int i;
// Create a task object.
using (mosek.Task task = new mosek.Task()) {
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
/* Set up the linear part of the problem */
task.putclist(sub, val);
task.putarow(0, asub, aval);
task.putconbound(0, mosek.boundkey.fx, 2.0, 2.0);
task.putvarboundsliceconst(0,numvar,mosek.boundkey.fr,-infinity,infinity);
/* Add conic constraints */
/* Append two power cone domains */
long pc1 = task.appendprimalpowerconedomain(3, new double[]{0.2, 0.8});
long pc2 = task.appendprimalpowerconedomain(3, new double[]{4.0, 6.0});
/* Create data structures F,g so that
F * x + g = (x(0), x(1), x(3), x(2), 1.0, x(4))
*/
task.appendafes(6);
task.putafefentrylist(new long[]{0, 1, 2, 3, 5}, /* Rows */
new int[]{0, 1, 3, 2, 4}, /* Columns */
new double[]{1.0, 1.0, 1.0, 1.0, 1.0});
task.putafeg(4, 1.0);
/* Append the two conic constraints */
task.appendacc(pc1, /* Domain */
new long[]{0, 1, 2}, /* Rows from F */
null); /* Unused */
task.appendacc(pc2, /* Domain */
new long[]{3, 4, 5}, /* Rows from F */
null); /* Unused */
task.putobjsense(mosek.objsense.maximize);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
/* Get status information about the solution */
mosek.solsta solsta = task.getsolsta(mosek.soltype.itr);
double[] xx = task.getxx(mosek.soltype.itr); // Interior point solution.
switch (solsta)
{
case mosek.solsta.optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < 3; ++j)
Console.WriteLine ("x[{0}]: {1}", j, xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
}
}
qcqo1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: qcqo1.cs
Purpose: Demonstrate how to solve a quadratic
optimization problem using the MOSEK API.
minimize x0^2 + 0.1 x1^2 + x2^2 - x0 x2 - x1
s.t 1 <= x0 + x1 + x2 - x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2
x >= 0
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class qcqo1
{
public static void Main ()
{
const double inf = 0.0; /* We don't actually need any value for infinity */
const int numcon = 1; /* Number of constraints. */
const int numvar = 3; /* Number of variables. */
mosek.boundkey[]
bkc = { mosek.boundkey.lo },
bkx = { mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo };
int[][] asub = { new int[] {0}, new int[] {0}, new int[] {0} };
double[][] aval = { new double[]{1.0}, new double[]{1.0}, new double[]{1.0} };
double[]
blc = { 1.0 },
buc = { inf },
c = { 0.0, -1.0, 0.0 },
blx = { 0.0, 0.0, 0.0 },
bux = { inf, inf, inf },
xx = new double[numvar];
try
{
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env))
{
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
for (int j = 0; j < numvar; ++j)
{
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
/* Input column j of A */
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/*
* The lower triangular part of the Q
* matrix in the objective is specified.
*/
{
int[]
qsubi = { 0, 1, 2, 2 },
qsubj = { 0, 1, 0, 2 };
double[]
qval = { 2.0, 0.2, -1.0, 2.0 };
/* Input the Q for the objective. */
task.putqobj(qsubi, qsubj, qval);
}
/*
* The lower triangular part of the Q^0
* matrix in the first constraint is specified.
* This corresponds to adding the term
* - x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2
*/
{
int[]
qsubi = { 0, 1, 2, 2 },
qsubj = { 0, 1, 2, 0 };
double[]
qval = { -2.0, -2.0, -0.2, 0.2 };
/* put Q^0 in constraint with index 0. */
task.putqconk (0,
qsubi,
qsubj,
qval);
}
task.putobjsense(mosek.objsense.minimize);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
mosek.solsta solsta;
/* Get status information about the solution */
task.getsolsta(mosek.soltype.itr, out solsta);
task.getxx(mosek.soltype.itr, // Basic solution.
xx);
switch (solsta)
{
case mosek.solsta.optimal:
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:", xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e);
throw;
}
} /* Main */
}
}
qo1.cs
/*
File : qo1.cs
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
Purpose: Demonstrate how to solve a quadratic
optimization problem using the MOSEK .NET API.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class qo1
{
public static void Main ()
{
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
const double infinity = 0;
const int numcon = 1; /* Number of constraints. */
const int numvar = 3; /* Number of variables. */
double[] c = {0.0, -1.0, 0.0};
mosek.boundkey[] bkc = {mosek.boundkey.lo};
double[] blc = {1.0};
double[] buc = {infinity};
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0
};
double[] bux = { +infinity,
+infinity,
+infinity
};
int[][] asub = { new int[] {0}, new int[] {0}, new int[] {0}};
double[][] aval = { new double[] {1.0}, new double[] {1.0}, new double[] {1.0}};
double[] xx = new double[numvar];
try {
// Create a task object linked with the environment env.
using (var task = new mosek.Task ()) {
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
for (int j = 0; j < numvar; ++j)
{
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
/* Input column j of A */
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/*
* The lower triangular part of the Q
* matrix in the objective is specified.
*/
int[] qsubi = {0, 1, 2, 2 };
int[] qsubj = {0, 1, 0, 2 };
double[] qval = {2.0, 0.2, -1.0, 2.0};
/* Input the Q for the objective. */
task.putobjsense(mosek.objsense.minimize);
task.putqobj(qsubi, qsubj, qval);
task.optimize();
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
mosek.solsta solsta;
/* Get status information about the solution */
task.getsolsta(mosek.soltype.itr, out solsta);
switch (solsta)
{
case mosek.solsta.optimal:
task.getxx(mosek.soltype.itr, // Interior point solution.
xx);
Console.WriteLine ("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:", xx[j]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility.\n");
break;
case mosek.solsta.unknown:
Console.WriteLine("Unknown solution status.\n");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e);
throw;
}
} /* Main */
}
}
reoptimization.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: reoptimization.cs
Purpose: Demonstrates how to solve a linear
optimization problem using the MOSEK API
and modify and re-optimize the problem.
*/
using System;
namespace mosek.example
{
public class reoptimization
{
public static void Main ()
{
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double
infinity = 0;
int numcon = 3;
int numvar = 3;
double[] c = {1.5,
2.5,
3.0
};
mosek.boundkey[] bkc = {mosek.boundkey.up,
mosek.boundkey.up,
mosek.boundkey.up
};
double[] blc = { -infinity,
-infinity,
-infinity
};
double[] buc = {100000,
50000,
60000
};
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0
};
double[] bux = { +infinity,
+infinity,
+infinity
};
int[][] asub = new int[numvar][];
asub[0] = new int[] {0, 1, 2};
asub[1] = new int[] {0, 1, 2};
asub[2] = new int[] {0, 1, 2};
double[][] aval = new double[numvar][];
aval[0] = new double[] { 2.0, 3.0, 2.0 };
aval[1] = new double[] { 4.0, 2.0, 3.0 };
aval[2] = new double[] { 3.0, 3.0, 2.0 };
double[] xx = new double[numvar];
mosek.Task task = null;
mosek.Env env = null;
try
{
// Create mosek environment.
env = new mosek.Env ();
// Create a task object linked with the environment env.
task = new mosek.Task (env, numcon, numvar);
/* Append the constraints. */
task.appendcons(numcon);
/* Append the variables. */
task.appendvars(numvar);
/* Put C. */
task.putcfix(0.0);
for (int j = 0; j < numvar; ++j)
task.putcj(j, c[j]);
/* Put constraint bounds. */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/* Put variable bounds. */
for (int j = 0; j < numvar; ++j)
task.putvarbound(j, bkx[j], blx[j], bux[j]);
/* Put A. */
if ( numcon > 0 )
{
for (int j = 0; j < numvar; ++j)
task.putacol(j,
asub[j],
aval[j]);
}
task.putobjsense(mosek.objsense.maximize);
try
{
task.optimize();
}
catch (mosek.Warning w)
{
Console.WriteLine("Mosek warning:");
Console.WriteLine (w.Code);
Console.WriteLine (w);
}
task.getxx(mosek.soltype.bas, // Request the basic solution.
xx);
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:{1}", j, xx[j]);
/********************** Make a change to the A matrix ********************/
task.putaij(0, 0, 3.0);
task.optimize();
task.getxx(mosek.soltype.bas, // Request the basic solution.
xx);
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:{1}", j, xx[j]);
/********************** Add a new variable ********************/
/* Get index of new variable. */
int varidx;
task.getnumvar(out varidx);
/* Append a new varaible x_3 to the problem */
task.appendvars(1);
numvar++;
/* Set bounds on new varaible */
task.putvarbound(varidx,
mosek.boundkey.lo,
0,
+infinity);
/* Change objective */
task.putcj(varidx, 1.0);
/* Put new values in the A matrix */
int[] acolsub = new int[] {0, 2};
double[] acolval = new double[] {4.0, 1.0};
task.putacol(varidx, /* column index */
acolsub,
acolval);
/* Change optimizer to simplex free and reoptimize */
task.putintparam(mosek.iparam.optimizer, mosek.optimizertype.free_simplex);
task.optimize();
xx = new double[numvar];
task.getxx(mosek.soltype.bas, // Request the basic solution.
xx);
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:{1}", j, xx[j]);
/********************** Add a new constraint ********************/
/* Get index of new constraint */
int conidx;
task.getnumcon(out conidx);
/* Append a new constraint */
task.appendcons(1);
numcon++;
/* Set bounds on new constraint */
task.putconbound(conidx,
mosek.boundkey.up,
-infinity,
30000);
/* Put new values in the A matrix */
int[] arowsub = new int[] {0, 1, 2, 3};
double[] arowval = new double[] {1.0, 2.0, 1.0, 1.0};
task.putarow(conidx, /* row index */
arowsub,
arowval);
task.optimize();
task.getxx(mosek.soltype.bas, // Request the basic solution.
xx);
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:{1}", j, xx[j]);
/********************** Change constraint bounds ********************/
mosek.boundkey[] newbkc = {mosek.boundkey.up,
mosek.boundkey.up,
mosek.boundkey.up,
mosek.boundkey.up
};
double[] newblc = { -infinity,
-infinity,
-infinity,
-infinity
};
double[] newbuc = { 80000, 40000, 50000, 22000 };
task.putconboundslice(0, numcon, newbkc, newblc, newbuc);
task.optimize();
task.getxx(mosek.soltype.bas, // Request the basic solution.
xx);
for (int j = 0; j < numvar; ++j)
Console.WriteLine ("x[{0}]:{1}", j, xx[j]);
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
}
if (task != null) task.Dispose ();
if (env != null) env.Dispose ();
}
}
}
response.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: response.cs
Purpose: This example demonstrates proper response handling
for problems solved with the interior-point optimizers.
*/
using System;
using mosek;
using System.Text;
namespace mosek.example
{
// A log handler class
class msgclass : mosek.Stream
{
public msgclass () {}
public override void streamCB (string msg) { Console.Write ("{0}", msg); }
}
public class response
{
public static void Main(string[] argv)
{
StringBuilder symname = new StringBuilder();
StringBuilder desc = new StringBuilder();
string filename;
if (argv.Length >= 1) filename = argv[0];
else filename = "../data/cqo1.mps";
// Define environment and task
using (Env env = new Env())
{
using (Task task = new Task(env, 0, 0))
{
try
{
// (Optionally) set a log handler
// task.set_Stream (streamtype.log, new msgclass ());
// (Optionally) uncomment this to get solution status unknown
// task.putintparam(iparam.intpnt_max_iterations, 1);
// In this example we read data from a file
task.readdata(filename);
// Perform optimization
rescode trm = task.optimize();
task.solutionsummary(streamtype.log);
// Handle solution status. We expect Optimal
solsta solsta = task.getsolsta(soltype.itr);
switch ( solsta )
{
case solsta.optimal:
// Optimal solution. Print variable values
Console.WriteLine("An optimal interior-point solution is located.");
int numvar = task.getnumvar();
double[] xx = new double[numvar];
task.getxx(soltype.itr, xx);
for(int i = 0; i < numvar; i++)
Console.WriteLine("x[" + i + "] = " + xx[i]);
break;
case solsta.dual_infeas_cer:
Console.WriteLine("Dual infeasibility certificate found.");
break;
case solsta.prim_infeas_cer:
Console.WriteLine("Primal infeasibility certificate found.");
break;
case solsta.unknown:
/* The solutions status is unknown. The termination code
indicates why the optimizer terminated prematurely. */
Console.WriteLine("The solution status is unknown.");
Env.getcodedesc(trm, symname, desc);
Console.WriteLine(" Termination code: {0} {1}", symname, desc);
break;
default:
Console.WriteLine("An unexpected solution status " + solsta);
break;
}
}
catch (mosek.Error e)
{
Console.WriteLine("Unexpected optimization error ({0}) {1}", e.Code, e.Message);
}
}
}
}
}
}
sdo1.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: sdo1.cs
Purpose: Solves the following small semidefinite optimization problem
using the MOSEK API.
minimize Tr [2, 1, 0; 1, 2, 1; 0, 1, 2]*X + x0
subject to Tr [1, 0, 0; 0, 1, 0; 0, 0, 1]*X + x0 = 1
Tr [1, 1, 1; 1, 1, 1; 1, 1, 1]*X + x1 + x2 = 0.5
(x0,x1,x2) \in Q, X \in PSD
*/
using System;
namespace mosek.example
{
public class sdo1
{
public static void Main(string[] args)
{
int numcon = 2; /* Number of constraints. */
int numvar = 3; /* Number of conic quadratic variables */
int[] dimbarvar = { 3 }; /* Dimensions of semidefinite cones */
int[] lenbarvar = { 3 * (3 + 1) / 2 }; /* Number of scalar SD variables */
mosek.boundkey[] bkc = { mosek.boundkey.fx, mosek.boundkey.fx };
double[] blc = { 1.0, 0.5 };
double[] buc = { 1.0, 0.5 };
int[] barc_i = { 0, 1, 1, 2, 2 },
barc_j = { 0, 0, 1, 1, 2 };
double[] barc_v = { 2.0, 1.0, 2.0, 1.0, 2.0 };
int[][] asub = { new int[] {0}, new int[] {1, 2}}; /* column subscripts of A */
double[][] aval = { new double[] {1.0}, new double[] {1.0, 1.0}};
int[][] bara_i = { new int[] {0, 1, 2}, new int[] {0, 1 , 2, 1, 2, 2 } },
bara_j = { new int[] {0, 1, 2}, new int[] {0, 0 , 0, 1, 1, 2 } };
double[][] bara_v = { new double[] {1.0, 1.0, 1.0}, new double[] {1.0, 1.0, 1.0, 1.0, 1.0, 1.0}};
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Append 'NUMCON' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'NUMVAR' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
/* Append 'NUMBARVAR' semidefinite variables. */
task.appendbarvars(dimbarvar);
/* Optionally add a constant term to the objective. */
task.putcfix(0.0);
/* Set the linear term c_j in the objective.*/
task.putcj(0, 1.0);
for (int j = 0; j < numvar; ++j)
task.putvarbound(j, mosek.boundkey.fr, -0.0, 0.0);
/* Set the linear term barc_j in the objective.*/
{
long[] idx = new long[1];
double[] falpha = { 1.0 };
idx[0] = task.appendsparsesymmat(dimbarvar[0],
barc_i,
barc_j,
barc_v);
task.putbarcj(0, idx, falpha);
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, /* Index of constraint.*/
bkc[i], /* Bound key.*/
blc[i], /* Numerical value of lower bound.*/
buc[i]); /* Numerical value of upper bound.*/
/* Input A row by row */
for (int i = 0; i < numcon; ++i)
task.putarow(i,
asub[i],
aval[i]);
/* Append the conic quadratic constraint */
task.appendafes(3);
// Diagonal F matrix
task.putafefentrylist(new long[]{0,1,2}, new int[]{0,1,2}, new double[]{1.0,1.0,1.0});
task.appendaccseq(task.appendquadraticconedomain(3), 0, null);
/* Add the first row of barA */
{
long[] idx = new long[1];
double[] falpha = {1.0};
task.appendsparsesymmat(dimbarvar[0],
bara_i[0],
bara_j[0],
bara_v[0],
out idx[0]);
task.putbaraij(0, 0, idx, falpha);
}
{
long[] idx = new long[1];
double[] falpha = {1.0};
/* Add the second row of barA */
task.appendsparsesymmat(dimbarvar[0],
bara_i[1],
bara_j[1],
bara_v[1],
out idx[0]);
task.putbaraij(1, 0, idx, falpha);
}
/* Run optimizer */
task.optimize();
/* Print a summary containing information
about the solution for debugging purposes*/
task.solutionsummary (mosek.streamtype.msg);
mosek.solsta solsta = task.getsolsta (mosek.soltype.itr);
switch (solsta)
{
case mosek.solsta.optimal:
double[] xx = task.getxx(mosek.soltype.itr);
double[] barx = task.getbarxj(mosek.soltype.itr, 0); /* Request the interior solution. */
Console.WriteLine("Optimal primal solution");
for (int i = 0; i < numvar; ++i)
Console.WriteLine("x[{0}] : {1}", i, xx[i]);
for (int i = 0; i < lenbarvar[0]; ++i)
Console.WriteLine("barx[{0}]: {1}", i, barx[i]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility certificate found.");
break;
case mosek.solsta.unknown:
Console.WriteLine("The status of the solution could not be determined.");
break;
default:
Console.WriteLine("Other solution status.");
break;
}
}
}
}
}
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
}
sdo2.cs
/*
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File : sdo2.cs
Purpose : Solves the semidefinite problem with two symmetric variables:
min <C1,X1> + <C2,X2>
st. <A1,X1> + <A2,X2> = b
(X2)_{1,2} <= k
where X1, X2 are symmetric positive semidefinite,
C1, C2, A1, A2 are assumed to be constant symmetric matrices,
and b, k are constants.
*/
using System;
namespace mosek.example
{
public class sdo1
{
public static void Main(string[] args)
{
/* Input data */
int numcon = 2; /* Number of constraints. */
int numbarvar = 2;
int[] dimbarvar = {3, 4}; /* Dimension of semidefinite variables */
/* Objective coefficients concatenated */
int[] Cj = { 0, 0, 1, 1, 1, 1 }; /* Which symmetric variable (j) */
int[] Ck = { 0, 2, 0, 1, 1, 2 }; /* Which entry (k,l)->v */
int[] Cl = { 0, 2, 0, 0, 1, 2 };
double[] Cv = { 1.0, 6.0, 1.0, -3.0, 2.0, 1.0 };
/* Equality constraints coefficients concatenated */
int[] Ai = { 0, 0, 0, 0, 0, 0 }; /* Which constraint (i = 0) */
int[] Aj = { 0, 0, 0, 1, 1, 1 }; /* Which symmetric variable (j) */
int[] Ak = { 0, 2, 2, 1, 1, 3 }; /* Which entry (k,l)->v */
int[] Al = { 0, 0, 2, 0, 1, 3 };
double[] Av = { 1.0, 1.0, 2.0, 1.0, -1.0, -3.0 };
/* The second constraint - one-term inequality */
int[] A2i = { 1 }; /* Which constraint (i = 1) */
int[] A2j = { 1 }; /* Which symmetric variable (j = 1) */
int[] A2k = { 1 }; /* Which entry A(1,0) = A(0,1) = 0.5 */
int[] A2l = { 0 };
double[] A2v = { 0.5 };
mosek.boundkey[] bkc = { mosek.boundkey.fx,
mosek.boundkey.up
};
double[] blc = { 23.0, 0.0 };
double[] buc = { 23.0, -3.0 };
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method task_msg_obj.stream
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Append numcon empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append numbarvar semidefinite variables. */
task.appendbarvars(dimbarvar);
/* Set objective (6 nonzeros).*/
task.putbarcblocktriplet(Cj, Ck, Cl, Cv);
/* Set the equality constraint (6 nonzeros).*/
task.putbarablocktriplet(Ai, Aj, Ak, Al, Av);
/* Set the inequality constraint (1 nonzero).*/
task.putbarablocktriplet(A2i, A2j, A2k, A2l, A2v);
/* Set constraint bounds */
task.putconboundslice(0, 2, bkc, blc, buc);
/* Run optimizer */
task.optimize();
task.solutionsummary(mosek.streamtype.msg);
mosek.solsta solsta = task.getsolsta(mosek.soltype.itr);
switch (solsta) {
case mosek.solsta.optimal:
/* Retrieve the soution for all symmetric variables */
Console.WriteLine("Solution (lower triangular part vectorized):");
for(int i = 0; i < numbarvar; i++) {
int dim = dimbarvar[i] * (dimbarvar[i] + 1) / 2;
double[] barx = new double[dim];
task.getbarxj(mosek.soltype.itr, i, barx);
Console.Write("X" + (i+1) + ": ");
for (int j = 0; j < dim; ++j)
Console.Write(barx[j] + " ");
Console.WriteLine();
}
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility certificate found.");
break;
case mosek.solsta.unknown:
Console.WriteLine("The status of the solution could not be determined.");
break;
default:
Console.WriteLine("Other solution status.");
break;
}
}
}
}
}
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
}
sdo_lmi.cs
/*
Copyright : Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File : sdo_lmi.cs
Purpose : To solve a problem with an LMI and an affine conic constrained problem with a PSD term
minimize Tr [1, 0; 0, 1]*X + x(1) + x(2) + 1
subject to Tr [0, 1; 1, 0]*X - x(1) - x(2) >= 0
x(1) [0, 1; 1, 3] + x(2) [3, 1; 1, 0] - [1, 0; 0, 1] >> 0
X >> 0
*/
using System;
namespace mosek.example
{
public class sdo_lmi
{
public static void Main(string[] args)
{
int numafe = 4; /* Number of affine expressions. */
int numvar = 2; /* Number of scalar variables */
int[] dimbarvar = { 2 }; /* Dimension of the semidefinite variable */
int[] lenbarvar = { 2 * (2 + 1) / 2 }; /* Number of scalar SD variables */
int[] barc_j = { 0, 0 },
barc_k = { 0, 1 },
barc_l = { 0, 1 };
double[] barc_v = { 1.0, 1.0 };
long[] afeidx = {0, 0, 1, 2, 2, 3};
int[] varidx = {0, 1, 1, 0, 1, 0};
double[] f_val = {-1, -1, 3, Math.Sqrt(2), Math.Sqrt(2), 3},
g = {0, -1, 0, -1};
long[] barf_i = { 0, 0 };
int[] barf_j = { 0, 0 },
barf_k = { 0, 1 },
barf_l = { 0, 0 };
double[] barf_v = { 0.0, 1.0 };
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
// Directs the log task stream to the user specified
// method msgclass.streamCB
task.set_Stream (mosek.streamtype.log, new msgclass (""));
/* Append 'NUMAFE' empty affine expressions.*/
task.appendafes(numafe);
/* Append 'NUMVAR' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
/* Append 'NUMBARVAR' semidefinite variables. */
task.appendbarvars(dimbarvar);
/* Optionally add a constant term to the objective. */
task.putcfix(1.0);
/* Set the linear term c_j in the objective.*/
task.putcj(0, 1.0);
task.putcj(1, 1.0);
for (int j = 0; j < numvar; ++j)
task.putvarbound(j, mosek.boundkey.fr, -0.0, 0.0);
/* Set the linear term barc_j in the objective.*/
task.putbarcblocktriplet(barc_j, barc_k, barc_l, barc_v);
/* Set up the affine conic constraints */
/* Construct the affine expressions */
/* F matrix */
task.putafefentrylist(afeidx, varidx, f_val);
/* g vector */
task.putafegslice(0, 4, g);
/* barF block triplets */
task.putafebarfblocktriplet(barf_i, barf_j, barf_k, barf_l, barf_v);
/* Append R+ domain and the corresponding ACC */
task.appendacc(task.appendrplusdomain(1), new long[]{0}, null);
/* Append SVEC_PSD domain and the corresponding ACC */
task.appendacc(task.appendsvecpsdconedomain(3), new long[]{1,2,3}, null);
/* Run optimizer */
task.optimize();
/* Print a summary containing information
about the solution for debugging purposes*/
task.solutionsummary (mosek.streamtype.msg);
mosek.solsta solsta = task.getsolsta (mosek.soltype.itr);
switch (solsta)
{
case mosek.solsta.optimal:
double[] xx = task.getxx(mosek.soltype.itr);
double[] barx = task.getbarxj(mosek.soltype.itr, 0); /* Request the interior solution. */
Console.WriteLine("Optimal primal solution");
for (int i = 0; i < numvar; ++i)
Console.WriteLine("x[{0}] : {1}", i, xx[i]);
for (int i = 0; i < lenbarvar[0]; ++i)
Console.WriteLine("barx[{0}]: {1}", i, barx[i]);
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility certificate found.");
break;
case mosek.solsta.unknown:
Console.WriteLine("The status of the solution could not be determined.");
break;
default:
Console.WriteLine("Other solution status.");
break;
}
}
}
}
}
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
}
sensitivity.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: sensitivity.cs
Purpose: To demonstrate how to perform sensitivity
analysis from the API on a small problem:
minimize
obj: +1 x11 + 2 x12 + 5 x23 + 2 x24 + 1 x31 + 2 x33 + 1 x34
st
c1: + x11 + x12 <= 400
c2: + x23 + x24 <= 1200
c3: + x31 + x33 + x34 <= 1000
c4: + x11 + x31 = 800
c5: + x12 = 100
c6: + x23 + x33 = 500
c7: + x24 + x34 = 500
The example uses basis type sensitivity analysis.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class sensitivity
{
public static void Main ()
{
const double
infinity = 0;
mosek.boundkey[] bkc = new mosek.boundkey[] {
mosek.boundkey.up, mosek.boundkey.up,
mosek.boundkey.up, mosek.boundkey.fx,
mosek.boundkey.fx, mosek.boundkey.fx,
mosek.boundkey.fx
};
mosek.boundkey[] bkx = new mosek.boundkey[] {
mosek.boundkey.lo, mosek.boundkey.lo,
mosek.boundkey.lo, mosek.boundkey.lo,
mosek.boundkey.lo, mosek.boundkey.lo,
mosek.boundkey.lo
};
int[] ptrb = new int[] {0, 2, 4, 6, 8, 10, 12};
int[] ptre = new int[] {2, 4, 6, 8, 10, 12, 14};
int[] sub = new int[] {0, 3, 0, 4, 1, 5, 1, 6, 2, 3, 2, 5, 2, 6};
double[] blc = new double[] {
-infinity, -infinity,
-infinity, 800, 100, 500, 500
};
double[] buc = new double[] {400, 1200, 1000, 800, 100, 500, 500};
double[] c = new double[] {1.0, 2.0, 5.0, 2.0, 1.0, 2.0, 1.0};
double[] blx = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
double[] bux = new double[] {infinity,
infinity,
infinity,
infinity,
infinity,
infinity,
infinity
};
double[] val = new double[] {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
};
int numcon = 7; /* Number of constraints. */
int numvar = 7; /* Number of variables. */
try
{
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env))
{
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
task.set_Stream(mosek.streamtype.log, new msgclass ("[task]"));
task.inputdata(numcon, numvar,
c,
0.0,
ptrb,
ptre,
sub,
val,
bkc,
blc,
buc,
bkx,
blx,
bux);
/* A maximization problem */
task.putobjsense(mosek.objsense.minimize);
try
{
task.optimize();
}
catch (mosek.Warning w)
{
Console.WriteLine("Mosek warning:");
Console.WriteLine (w.Code);
Console.WriteLine (w);
}
/* Analyze upper bound on c1 and the equality constraint on c4 */
int[] subi = new int [] {0, 3};
mosek.mark[] marki = new mosek.mark[] {mosek.mark.up,
mosek.mark.up
};
/* Analyze lower bound on the variables x12 and x31 */
int[] subj = new int [] {1, 4};
mosek.mark[] markj = new mosek.mark[] {mosek.mark.lo,
mosek.mark.lo
};
double[] leftpricei = new double[2];
double[] rightpricei = new double[2];
double[] leftrangei = new double[2];
double[] rightrangei = new double[2];
double[] leftpricej = new double[2];
double[] rightpricej = new double[2];
double[] leftrangej = new double[2];
double[] rightrangej = new double[2];
task.primalsensitivity( subi,
marki,
subj,
markj,
leftpricei,
rightpricei,
leftrangei,
rightrangei,
leftpricej,
rightpricej,
leftrangej,
rightrangej);
Console.Write("Results from sensitivity analysis on bounds:\n");
Console.Write("For constraints:\n");
for (int i = 0; i < 2; ++i)
Console.Write(
"leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n",
leftpricei[i], rightpricei[i], leftrangei[i], rightrangei[i]);
Console.Write("For variables:\n");
for (int i = 0; i < 2; ++i)
Console.Write(
"leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange ={3}\n",
leftpricej[i], rightpricej[i], leftrangej[i], rightrangej[i]);
double[] leftprice = new double[2];
double[] rightprice = new double[2];
double[] leftrange = new double[2];
double[] rightrange = new double[2];
int[] subc = new int[] {2, 5};
task.dualsensitivity( subc,
leftprice,
rightprice,
leftrange,
rightrange
);
Console.Write("Results from sensitivity analysis on objective coefficients:");
for (int i = 0; i < 2; ++i)
Console.Write(
"leftprice = {0}, rightprice = {1},leftrange = {2}, rightrange = {3}\n",
leftprice[i], rightprice[i], leftrange[i], rightrange[i]);
}
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
}
}
}
simple.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: simple.cs
Purpose: Demonstrates a very simple example using MOSEK by
reading a problem file, solving the problem and
writing the solution to a file.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
public override void streamCB (string msg)
{
Console.Write ("{0}", msg);
}
}
public class simple
{
public static void Main (string[] args)
{
if (args.Length == 0)
{
Console.WriteLine ("Missing argument, syntax is:");
Console.WriteLine (" simple inputfile [ solutionfile ]");
}
else
{
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env))
{
task.set_Stream (mosek.streamtype.log, new msgclass ());
// We assume that a problem file was given as the first command
// line argument (received in `args')
task.readdata (args[0]);
// Solve the problem
task.optimize ();
// Print a summary of the solution
task.solutionsummary (mosek.streamtype.log);
// If an output file was specified, save problem to a file
if (args.Length >= 2)
{
// If using OPF format, these parameters will specify what to include in output
task.putintparam (mosek.iparam.opf_write_solutions, mosek.onoffkey.on);
task.putintparam (mosek.iparam.opf_write_problem, mosek.onoffkey.on);
task.putintparam (mosek.iparam.opf_write_hints, mosek.onoffkey.off);
task.putintparam (mosek.iparam.opf_write_parameters, mosek.onoffkey.off);
task.writedata(args[1]);
}
}
}
}
}
}
}
solutionquality.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: solutionquality.cs
Purpose: To demonstrate how to examine the quality of a solution.
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass(string prfx)
{
prefix = prfx;
}
public override void streamCB(string msg)
{
Console.Write("{0}{1}", prefix, msg);
}
}
public class solutionquality
{
public static void Main(String[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Missing argument, syntax is:");
Console.WriteLine(" solutionquality inputfile");
}
else
{
using (mosek.Env env = new mosek.Env())
{
// Create a task object.
using (mosek.Task task = new mosek.Task(env, 0, 0))
{
task.set_Stream(mosek.streamtype.log, new msgclass(""));
try
{
// We assume that a problem file was given as the first command
// line argument (received in `args')
task.readdata(args[0]);
// Solve the problem
task.optimize();
// Console.WriteLine (a summary of the solution
task.solutionsummary(mosek.streamtype.log);
mosek.solsta solsta;
task.getsolsta(mosek.soltype.bas, out solsta);
double pobj, pviolcon, pviolvar, pviolbarvar, pviolcones, pviolitg;
double dobj, dviolcon, dviolvar, dviolbarvar, dviolcones;
task.getsolutioninfo(mosek.soltype.bas,
out pobj, out pviolcon, out pviolvar, out pviolbarvar, out pviolcones, out pviolitg,
out dobj, out dviolcon, out dviolvar, out dviolbarvar, out dviolcones);
switch (solsta)
{
case mosek.solsta.optimal:
double abs_obj_gap = Math.Abs(dobj - pobj);
double rel_obj_gap = abs_obj_gap / (1.0 + Math.Min(Math.Abs(pobj), Math.Abs(dobj)));
double max_primal_viol = Math.Max(pviolcon, pviolvar);
max_primal_viol = Math.Max(max_primal_viol, pviolbarvar);
max_primal_viol = Math.Max(max_primal_viol, pviolcones);
double max_dual_viol = Math.Max(dviolcon, dviolvar);
max_dual_viol = Math.Max(max_dual_viol, dviolbarvar);
max_dual_viol = Math.Max(max_dual_viol, dviolcones);
// Assume the application needs the solution to be within
// 1e-6 ofoptimality in an absolute sense. Another approach
// would be looking at the relative objective gap
Console.WriteLine("Customized solution information.\n");
Console.WriteLine(" Absolute objective gap: " + abs_obj_gap);
Console.WriteLine(" Relative objective gap: " + rel_obj_gap);
Console.WriteLine(" Max primal violation : " + max_primal_viol);
Console.WriteLine(" Max dual violation : " + max_dual_viol);
bool accepted = true;
if (rel_obj_gap > 1e-6)
{
Console.WriteLine("Warning: The relative objective gap is LARGE.");
accepted = false;
}
// We will accept a primal infeasibility of 1e-8 and
// dual infeasibility of 1e-6. These number should chosen problem
// dependent.
if (max_primal_viol > 1e-8)
{
Console.WriteLine("Warning: Primal violation is too LARGE");
accepted = false;
}
if (max_dual_viol > 1e-6)
{
Console.WriteLine("Warning: Dual violation is too LARGE.");
accepted = false;
}
if (accepted)
{
int numvar = task.getnumvar();
double[] xx = new double[numvar];
Console.WriteLine("Optimal primal solution");
task.getxx(mosek.soltype.bas, xx);
for (int j = 0; j < numvar; j++)
Console.WriteLine("x[{0}]: {1}", j, xx[j]);
}
else
{
// print etailed information about the solution
task.analyzesolution(mosek.streamtype.log, mosek.soltype.bas);
}
break;
case mosek.solsta.dual_infeas_cer:
case mosek.solsta.prim_infeas_cer:
Console.WriteLine("Primal or dual infeasibility certificate found.");
break;
case mosek.solsta.unknown:
Console.WriteLine("The status of the solution is unknown.");
break;
default:
Console.WriteLine("Other solution status");
break;
}
}
catch (mosek.Exception e)
{
Console.WriteLine("\nAn error occourred: " + e.Code);
Console.WriteLine(e);
//throw;
}
}
}
}
}
}
}
solvebasis.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File : solvebasis.cs
Purpose : To demonstrate the usage of
MSK_solvewithbasis on the problem:
maximize x0 + x1
st.
x0 + 2.0 x1 <= 2
x0 + x1 <= 6
x0 >= 0, x1>= 0
The problem has the slack variables
xc0, xc1 on the constraints
and the variabels x0 and x1.
maximize x0 + x1
st.
x0 + 2.0 x1 -xc1 = 2
x0 + x1 -xc2 = 6
x0 >= 0, x1>= 0,
xc1 <= 0 , xc2 <= 0
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class solvebasis
{
public static void Main ()
{
const int numcon = 2;
const int numvar = 2;
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double
infinity = 0;
double[] c = {1.0, 1.0};
int[] ptrb = {0, 2};
int[] ptre = {2, 3};
int[] asub = {0, 1,
0, 1
};
double[] aval = {1.0, 1.0,
2.0, 1.0
};
mosek.boundkey[] bkc = {mosek.boundkey.up,
mosek.boundkey.up
};
double[] blc = { -infinity,
-infinity
};
double[] buc = {2.0,
6.0
};
mosek.boundkey[] bkx = {mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0
};
double[] bux = { +infinity,
+infinity
};
double[] w1 = {2.0, 6.0};
double[] w2 = {1.0, 0.0};
try
{
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env))
{
task.set_Stream (mosek.streamtype.log, new msgclass ("[task]"));
task.inputdata(numcon, numvar,
c,
0.0,
ptrb,
ptre,
asub,
aval,
bkc,
blc,
buc,
bkx,
blx,
bux);
task.putobjsense(mosek.objsense.maximize);
try
{
task.optimize();
}
catch (mosek.Warning w)
{
Console.WriteLine("Mosek warning:");
Console.WriteLine (w.Code);
Console.WriteLine (w);
}
int[] basis = new int[numcon];
task.initbasissolve(basis);
//List basis variables corresponding to columns of B
int[] varsub = {0, 1};
for (int i = 0; i < numcon; i++) {
if (basis[varsub[i]] < numcon)
Console.WriteLine ("Basis variable no {0} is xc{1}",
i,
basis[i]);
else
Console.WriteLine ("Basis variable no {0} is x{1}",
i,
basis[i] - numcon);
}
// solve Bx = w1
// varsub contains index of non-zeros in b.
// On return b contains the solution x and
// varsub the index of the non-zeros in x.
int nz = 2;
nz = task.solvewithbasis(false, nz, varsub, w1);
Console.WriteLine ("nz = {0}", nz);
Console.WriteLine ("Solution to Bx = w1:\n");
for (int i = 0; i < nz; i++) {
if (basis[varsub[i]] < numcon)
Console.WriteLine ("xc {0} = {1}",
basis[varsub[i]],
w1[varsub[i]] );
else
Console.WriteLine ("x{0} = {1}",
basis[varsub[i]] - numcon,
w1[varsub[i]]);
}
// Solve B^Tx = w2
nz = 1;
varsub[0] = 0; // Only w2[0] is nonzero.
nz = task.solvewithbasis(true, nz, varsub, w2);
Console.WriteLine ("\nSolution to B^Ty = w2:\n");
for (int i = 0; i < nz; i++)
{
Console.WriteLine ("y {0} = {1}",
varsub[i],
w2[varsub[i]]);
}
}
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
}
}
}
solvelinear.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File : solvelinear.cs
Purpose : To demonstrate the usage of MSK_solvewithbasis
when solving the linear system:
1.0 x1 = b1
-1.0 x0 + 1.0 x1 = b2
with two different right hand sides
b = (1.0, -2.0)
and
b = (7.0, 0.0)
*/
using System;
namespace mosek.example
{
class msgclass : mosek.Stream
{
string prefix;
public msgclass (string prfx)
{
prefix = prfx;
}
public override void streamCB (string msg)
{
Console.Write ("{0}{1}", prefix, msg);
}
}
public class solvelinear
{
static public void setup(mosek.Task task,
double[][] aval,
int[][] asub,
int[] ptrb,
int[] ptre,
int numvar,
int[] basis
)
{
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double
infinity = 0;
mosek.stakey[] skx = new mosek.stakey [numvar];
mosek.stakey[] skc = new mosek.stakey [numvar];
for (int i = 0; i < numvar ; ++i)
{
skx[i] = mosek.stakey.bas;
skc[i] = mosek.stakey.fix;
}
task.appendvars(numvar);
task.appendcons(numvar);
for (int i = 0; i < numvar ; ++i)
task.putacol(i,
asub[i],
aval[i]);
for (int i = 0 ; i < numvar ; ++i)
task.putconbound(
i,
mosek.boundkey.fx,
0.0,
0.0);
for (int i = 0 ; i < numvar ; ++i)
task.putvarbound(
i,
mosek.boundkey.fr,
-infinity,
infinity);
/* Define a basic solution by specifying
status keys for variables & constraints. */
task.deletesolution(mosek.soltype.bas);
task.putskcslice(mosek.soltype.bas, 0, numvar, skc);
task.putskxslice(mosek.soltype.bas, 0, numvar, skx);
task.initbasissolve(basis);
}
public static void Main ()
{
const int numcon = 2;
const int numvar = 2;
double[][]
aval = new double[numvar][];
aval[0] = new double[] { -1.0 };
aval[1] = new double[] {1.0, 1.0};
int[][]
asub = new int[numvar][];
asub[0] = new int[] {1};
asub[1] = new int[] {0, 1};
int [] ptrb = {0, 1};
int [] ptre = {1, 3};
int[] bsub = new int[numvar];
double[] b = new double[numvar];
int[] basis = new int[numvar];
try
{
using (mosek.Env env = new mosek.Env())
{
using (mosek.Task task = new mosek.Task(env))
{
// Directs the log task stream to the user specified
// method task_msg_obj.streamCB
task.set_Stream(mosek.streamtype.log, new msgclass ("[task]"));
/* Put A matrix and factor A.
Call this function only once for a given task. */
setup(
task,
aval,
asub,
ptrb,
ptre,
numvar,
basis
);
/* now solve rhs */
b[0] = 1;
b[1] = -2;
bsub[0] = 0;
bsub[1] = 1;
int nz = 2;
nz = task.solvewithbasis(false, nz, bsub, b);
Console.WriteLine ("\nSolution to Bx = b:\n\n");
/* Print solution and show correspondents
to original variables in the problem */
for (int i = 0; i < nz; ++i)
{
if (basis[bsub[i]] < numcon)
Console.WriteLine ("This should never happen\n");
else
Console.WriteLine ("x{0} = {1}\n", basis[bsub[i]] - numcon , b[bsub[i]] );
}
b[0] = 7;
bsub[0] = 0;
nz = 1;
nz = task.solvewithbasis(false, nz, bsub, b);
Console.WriteLine ("\nSolution to Bx = b:\n\n");
/* Print solution and show correspondents
to original variables in the problem */
for (int i = 0; i < nz; ++i)
{
if (basis[bsub[i]] < numcon)
Console.WriteLine ("This should never happen\n");
else
Console.WriteLine ("x{0} = {1}\n", basis[bsub[i]] - numcon , b[bsub[i]] );
}
}
}
}
catch (mosek.Exception e)
{
Console.WriteLine (e.Code);
Console.WriteLine (e);
throw;
}
}
}
}
sparsecholesky.cs
/*
Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
File: sparsecholesky.cs
Purpose: Demonstrate the sparse Cholesky factorization.
*/
using System;
namespace mosek.example
{
public class Sparsecholesky
{
public static void printsparse(int n,
int[] perm,
double[] diag,
int[] lnzc,
long[] lptrc,
long lensubnval,
int[] lsubc,
double[] lvalc)
{
long i, j;
Console.Write("P = [ ");
for (i = 0; i < n; i++) Console.Write("{0} ", perm[i]);
Console.WriteLine("]");
Console.Write("diag(D) = [ ");
for (i = 0; i < n; i++) Console.Write("{0} ", diag[i]);
Console.WriteLine("]");
double[] l = new double[n * n];
for (j = 0; j < n; j++)
for (i = lptrc[j]; i < lptrc[j] + lnzc[j]; i++)
l[lsubc[i]*n + j] = lvalc[i];
Console.WriteLine("L=");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) Console.Write("{0} ", l[i * n + j]);
Console.WriteLine("");
}
}
public static void Main ()
{
/* Create the mosek environment. */
using (mosek.Env env = new mosek.Env())
{
{
//Example from the manual
//Observe that anzc, aptrc, asubc and avalc only specify the lower triangular part.
const int n = 4;
int[] anzc = {4, 1, 1, 1};
int[] asubc = {0, 1, 2, 3, 1, 2, 3};
long[] aptrc = {0, 4, 5, 6};
double[] avalc = {4.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
double[] b = {13.0, 3.0, 4.0, 5.0};
int[] perm, lnzc, lsubc;
long lensubnval;
double[] diag, lvalc;
long[] lptrc;
env.computesparsecholesky(0, //Mosek chooses number of threads
1, //Apply reordering heuristic
1.0e-14, //Singularity tolerance
anzc, aptrc, asubc, avalc,
out perm, out diag,
out lnzc, out lptrc, out lensubnval, out lsubc, out lvalc);
printsparse(n, perm, diag, lnzc, lptrc, lensubnval, lsubc, lvalc);
/* Permuted b is stored as x. */
double[] x = new double[n];
for (int i = 0; i < n; i++) x[i] = b[perm[i]];
/*Compute inv(L)*x.*/
env.sparsetriangularsolvedense(mosek.transpose.no, lnzc, lptrc, lsubc, lvalc, x);
/*Compute inv(L^T)*x.*/
env.sparsetriangularsolvedense(mosek.transpose.yes, lnzc, lptrc, lsubc, lvalc, x);
Console.Write("\nSolution A x = b, x = [ ");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) if (perm[j] == i) Console.Write("{0} ", x[j]);
Console.WriteLine("]\n");
}
{
const int n = 3;
int[] anzc = {3, 2, 1};
int[] asubc = {0, 1, 2, 1, 2, 2};
long[] aptrc = {0, 3, 5, };
double[] avalc = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
int[] perm, lnzc, lsubc;
long lensubnval;
double[] diag, lvalc;
long[] lptrc;
env.computesparsecholesky(0, //Mosek chooses number of threads
1, //Apply reordering heuristic
1.0e-14, //Singularity tolerance
anzc, aptrc, asubc, avalc,
out perm, out diag,
out lnzc, out lptrc, out lensubnval, out lsubc, out lvalc);
printsparse(n, perm, diag, lnzc, lptrc, lensubnval, lsubc, lvalc);
}
}
}
}
}