5 Design Overview

5.1 Modeling

The API for MATLAB consists of two components:

  • The main conic interface for specifying linear, conic and mixed-integer optimization problems in conic format:

    \[\begin{split}\begin{array}{ll} \minimize & c^Tx \\ \st & Fx+g\in \mathcal{D} \end{array}\end{split}\]

    specified by describing the matrix \(F\), vectors \(c,g\) and a list of domains \(\mathcal{D}\). The domains define linear and conic constraints on the corresponding affine expressions in \(Fx+g\). See Sec. 7.1 (The conic interface tutorial) for an introductory tutorial.

  • The auxiliary linear/simplex interface for users who want to exploit features of the simplex algorithm, such as warm-start and basic solution. See Sec. 7.7 (The linear/simplex interface tutorial) for an introductory tutorial.

The main characteristics of the API for MATLAB are:

  • Simplicity: once the problem data is assembled in matrix form, it is straightforward to input it into the optimizer.

  • Extensibility: the data can be input all at once or in chunks, corresponding for instance to consecutive constraint blocks in the model.

  • Exploiting sparsity: data is entered in sparse format, enabling huge, sparse problems to be defined and solved efficiently.

  • Efficiency: the API incurs almost no overhead between the user’s representation of the problem and MOSEK’s internal one.

API for MATLAB does not aid with modeling. It is the user’s responsibility to express the problem in MOSEK’s standard form, introducing, if necessary, auxiliary variables and constraints. See Sec. 11 (Problem Formulation and Solutions) for the precise formulations of problems MOSEK solves.

5.2 “Hello World!” in MOSEK

Here we present the most basic workflow pattern when using API for MATLAB.

Create a mosekmodel structure

Optimization problems using API for MATLAB are specified using a mosekmodel class that describes the numerical data of the problem.

Retrieving the solutions

When the problem is set up, the optimizer is invoked with the call to mosekmodel.solve, and subsequently the solution can be checked with mosekmodel.hassolution and retrieved with mosekmodel.getsolution.

We refer also to Sec. 6 (Solver Interaction Tutorials) for information about more advanced mechanisms of interacting with the solver.

Source code example

Below is the most basic code sample that defines and solves a simple optimization problem

\[\begin{split}\begin{array}{ll} \minimize & x + 2y \\ \st & x\geq 2.0,\ y\geq 3.0 \\ & 3x-y \leq 1 \end{array}\end{split}\]

For simplicity the example does not contain any error or status checks.

Listing 5.1 “Hello World!” in MOSEK
%%
%  Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved.
%
%  File:      helloworld.m
%
%  The most basic example of how to get started with MOSEK.
%

function [xx,prosta,solsta] = helloworld()
    model = mosekmodel(...
                  name = "helloworld", ...
                  objsense = "min", ...
                  objective = [ 1, 2 ]', ...
                  numvar = 2, ...
                  F = [ 1 0 ; ...
                        0 1 ; ...
                        3 -1 ], ...
                  domain = [ mosekdomain("greater than", rhs = 2), ...   % x >= 2,    1st row of F
                             mosekdomain("greater than", rhs = 3), ...   % y >= 3,    2nd row of F
                             mosekdomain("less than", rhs = 1) ]);       % 3x-y <= 1, 3rd row of F
    model.solve();
    
    if model.hassolution("interior")
        [xx,prosta,solsta] = model.getsolution("any", "x");
        xx
    end

end