This chapter introduces the MOSEK command line tool which allows the user to solve optimization problems specified in a text file. The main reasons to use the command line tool are
The syntax for the mosek command line tool is
mosek [options] filename
[options] are some options which modify the behavior of MOSEK such as whether the optimization problem is minimized or maximized. filename is the name of the file which contains the problem data. E.g the
mosek -min afiro.mps
command line tells MOSEK to read data from the afiro.mps file and to minimize the objective function.
By default the solution to the optimization problem is stored in the files afiro.sol and afiro.bas. The .sol and .bas files contains the interior and basis solution respectively. For problems with integer variables the solution is written to a file with the extension .int.
For a complete list of command line parameters type
mosek -h
or see Appendix A.
Using several examples we will subsequently demonstrate how to use the MOSEK command line tool.
A linear optimization problem is a problem where a linear objective function is optimized subject to linear constraints. An example of a linear optimization problem is
![]() |
(4.2.1) |
The solution of the example (4.2.1) using MOSEK consists of three steps:
The input file for MOSEK is a plain text file containing a description of the problem and it must be in either the MPS, the LP, or the OPF format. Below we present the example encoded as an OPF file:
[comment] Example lo1.mps converted to OPF. [/comment] [hints] # Give a hint about the size of the different elements in the problem. # These need only be estimates, but in this case they are exact. [hint NUMVAR] 2 [/hint] [hint NUMCON] 4 [/hint] [hint NUMANZ] 8 [/hint] [/hints] [variables] # All variables that will appear in the problem x1 x2 [/variables] [objective minimize 'obj'] - 10 x1 - 9 x2 [/objective] [constraints] [con 'c1'] 0.7 x1 + x2 <= 630 [/con] [con 'c2'] 0.5 x1 + 0.8333333333 x2 <= 600 [/con] [con 'c3'] x1 + 0.66666667 x2 <= 708 [/con] [con 'c4'] 0.1 x1 + 0.25 x2 <= 135 [/con] [/constraints] [bounds] # By default all variables are free. The following line will # change this to all variables being nonnegative. [b] 0 <= * [/b] [/bounds]
For details on the syntax of the OPF format please consult Appendix D.
After the input file has been created, the problem can be optimized. Assuming that the input file has been given the name lo1.opf, then the problem is optimized using the command line
mosek lo1.opf
Two solution report files lo1.sol and lo1.bas are generated where the first file contains the interior solution and the second file contains the basic solution. In this case the lo1.bas file has the format:
NAME : EXAMPLE
PROBLEM STATUS : PRIMAL_AND_DUAL_FEASIBLE
SOLUTION STATUS : OPTIMAL
OBJECTIVE NAME : obj
PRIMAL OBJECTIVE : -7.66799999e+003
DUAL OBJECTIVE : -7.66799999e+003
CONSTRAINTS
INDEX NAME AT ACTIVITY LOWER LIMIT UPPER LIMIT DUAL LOWER DUAL UPPER
1 c1 UL 6.30000000e+002 NONE 6.30000000e+002 0.00000000e+000 4.37499996e+000
2 c2 BS 4.80000000e+002 NONE 6.00000000e+002 0.00000000e+000 0.00000000e+000
3 c3 UL 7.08000000e+002 NONE 7.08000000e+002 0.00000000e+000 6.93750003e+000
4 c4 BS 1.17000000e+002 NONE 1.35000000e+002 0.00000000e+000 0.00000000e+000
VARIABLES
INDEX NAME AT ACTIVITY LOWER LIMIT UPPER LIMIT DUAL LOWER DUAL UPPER
1 x1 BS 5.39999998e+002 0.00000000e+000 NONE 0.00000000e+000 0.00000000e+000
2 x2 BS 2.52000001e+002 0.00000000e+000 NONE 0.00000000e+000 0.00000000e+000
The interpretation of the solution file should be obvious. E.g the optimal values of x1 and x2 are 539.99 and 252.00 respectively. A detailed discussion of the solution file format is given in Appendix F.
An example of a quadratic optimization problem is
![]() |
(4.2.2) |
The problem is a quadratic optimization problem because all the constraints are linear and the objective can be stated on the form
![]() |
where in this particular case we have that
![]() |
(4.2.3) |
MOSEK assumes that Q is symmetric and positive semi-definite. If these assumptions are not satisfied, MOSEK will most likely not compute a valid solution. Recall a matrix is symmetric if it satisfies the condition
![]() |
and it is positive semi-definite if
![]() |
An OPF file specifying the example can have the format:
[comment] Example qo1.mps converted to OPF. [/comment] [hints] [hint NUMVAR] 3 [/hint] [hint NUMCON] 1 [/hint] [hint NUMANZ] 3 [/hint] [/hints] [variables] x1 x2 x3 [/variables] [objective minimize 'obj'] # The quadratic terms are often multiplied by 1/2, # but this is not required. - x2 + 0.5 ( 2 x1 ^ 2 - 2 x3 * x1 + 0.2 x2 ^ 2 + 2 x3 ^ 2 ) [/objective] [constraints] [con 'c1'] 1 <= x1 + x2 + x3 [/con] [/constraints] [bounds] [b] 0 <= * [/b] [/bounds]
Please note that the quadratic terms in objective are stated very naturally in the OPF format as follows
- x2 + 0.5 ( 2 x1 ^ 2 - 2 x3 * x1 + 0.2 x2 ^ 2 + 2 x3 ^ 2 )
The example is solved using the
mosek qo1.opf
command line. In this case only one solution file named qo1.sol is produced. A .bas file is only produced for linear problems.
Conic optimization is a generalization of linear optimization which allows the formulation of nonlinear convex optimization problems.
The main idea in conic optimization is to include constraints of the form
![]() |
in the optimization problem where consists of a subset of the variables and
is a convex cone. Recall that
is a convex cone if and only if
is a convex set and
![]() |
MOSEK cannot handle arbitrary conic constraints, only the two types
![]() |
(4.2.4) |
and
![]() |
(4.2.5) |
(4.2.4) is called a quadratic cone whereas (4.2.5) is called a rotated quadratic cone.
Consider the problem
![]() |
(4.2.6) |
which may not initially look like a conic optimization problem. It can however be reformulated to
![]() |
(4.2.7) |
Problem (4.2.6) and problem (4.2.7) are equivalent because the constraints of (4.2.7)
![]() |
and hence
![]() |
The problem (4.2.7) is a conic quadratic optimization problem.
Using the MOSEK OPF format the problem can be represented as follows:
[comment] Example cqo1.mps converted to OPF. [/comment] [hints] [hint NUMVAR] 6 [/hint] [hint NUMCON] 1 [/hint] [hint NUMANZ] 2 [/hint] [/hints] [variables] x1 x2 x3 x4 x5 x6 [/variables] [objective minimize 'obj'] x1 + 2 x2 [/objective] [constraints] [con 'c1'] 2 x3 + 4 x4 = 5 [/con] [/constraints] [bounds] # We let all variables default to the positive orthant [b] 0 <= * [/b] # ... and change those that differ from the default. [b] x5,x6 = 1 [/b] # We define two rotated quadratic cones # k1: 2 x1 * x3 >= x5^2 [cone rquad 'k1'] x1, x3, x5 [/cone] # k2: 2 x2 * x4 >= x6^2 [cone rquad 'k2'] x2, x4, x6 [/cone] [/bounds]
For details on the OPF format please consult Appendix D. Finally, the example can be solved using the
mosek cqo1.opf
command line call and the solution can be studied by inspecting the cqo1.sol file.
It is possible to modify the behavior of MOSEK by setting appropriate parameters. E.g assume that a linear optimization problem should be solved with the primal simplex optimizer rather than the default interior-point optimizer. This is done by setting the parameter MSK_IPAR_OPTIMIZER to the value MSK_OPTIMIZER_PRIMAL_SIMPLEX. To accomplish this append
-d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX.
to the command line. E.g the command
mosek -d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX lo1.opf
solves the problem specified by lo1.opf using the primal simplex optimizer. For further information on the parameters available in MOSEK please see Appendix H.
MOSEK reads and writes problem data files in the formats presented in Table 4.1.
|
The columns of Table 4.1 show:
MOSEK can read and write data files compressed with gzip
For mosek to recognize a file as a gzip compressed file it must have the extension .gz. E.g the command
mosek myfile.mps.gz
will automatically decompress the file while reading it.
It is possible to use MOSEK to convert a problem file from one format to another. For instance assume the MPS file myprob.mps should be converted to an LP file named myprob.lp. This can be achieved with the command
mosek myprob.mps -out myprob.lp -x
Converting an MPS file to a LP file and back to an MPS file permutes the rows and columns of the original problem; this has no influence on the problem, but variables and constraints may appear in a different order.
Often a sequence of closely related optimization problems has to be solved. In such a case it can be expected that a previous optimal solution can serve as a good starting point when the modified problem is optimized.
Currently, only the simplex optimizer and the mixed-integer optimizer in MOSEK can exploit a guess for the optimal solution. The simplex optimizer can exploit an arbitrary guess for the optimal solution whereas the mixed-integer optimizer requires a feasible integer solution. For both the simplex optimizer and the mixed-integer optimizer it holds that if the guess is good then the optimizer may be able to reduce the solution time significantly when exploiting the guess.
Assume that the example
![]() |
(4.5.1) |
should be solved for identical to -5 and -10. Clearly, a solution for one
value will also be feasible for another value. Therefore, it might be worthwhile to exploit the previous optimal solution when reoptimizing the problem.
Assume that two MPS files have been created each corresponding to one of the values then the commands
mosek lo1.mps -baso .\lo1.bas mosek lo1-b.mps -basi .\lo1.bas -baso .\lo1-b.bas -d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX
demonstrates how to exploit the previous optimal solution in the second optimization.
In the first line MOSEK optimizes the first version of the optimization problem where is identical to -10. The -baso .\lo1.bascommand line option makes sure that the optimal basic solution is written to the file .\lo1.bas.
In the second line the second instance of the problem is optimized. The -basi .\lo1.bascommand line option forces MOSEK to read the previous optimal solution which MOSEK will try to exploit automatically. The -baso .\lo1-b.bascommand line option makes sure that the optimal basic solution is written to the .\lo1-b.basfile. Finally, the
-d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX
command line option makes sure that the primal simplex optimizer is used for the reoptimization. This is important because the interior-point optimizer used by default does not exploit a previous optimal solution.
Additional information about the MOSEK command line tool is available in Appendix A.
The MOSEK solution files can be very space consuming for large problems. One way to cut down the solution file size is only to include variables which optimal value is in a certain interesting range i.e [0.01,0.99]. This can be done by setting the MOSEK parameters
MSK_SPAR_SOL_FILTER_XX_LOW 0.01 MSK_SPAR_SOL_FILTER_XX_UPR 0.99
For further details consult the parameters MSK_SPAR_SOL_FILTER_XC_LOW and MSK_SPAR_SOL_FILTER_XC_UPR.