8.3 Input/Output¶

The Model class is also a proxy for input/output operations related to an optimization model.

8.3.1 Stream logging¶

By default the solver runs silently and does not produce any output to the console or otherwise. However, the log output can be redirected to a user-defined output stream or stream callback function. The log output is analogous to the one produced by the command-line version of MOSEK.

To redirect all log messages use the method Model.setLogHandler. For instance, we can use the standard output:

          M.setLogHandler(new PrintWriter(System.out));


A log stream can be detached by passing NULL.

8.3.2 Log verbosity¶

The logging verbosity can be controlled by setting the relevant parameters, as for instance

Each parameter controls the output level of a specific functionality or algorithm. The main switch is log which affect the whole output. The actual log level for a specific functionality is determined as the minimum between log and the relevant parameter. For instance, the log level for the output produce by the interior-point algorithm is tuned by the logIntpnt; the actual log level is defined by the minimum between log and logIntpnt.

Tuning the solver verbosity may require adjusting several parameters. It must be noticed that verbose logging is supposed to be of interest during debugging and tuning. When output is no more of interest, the user can easily disable it globally with log. Larger values of log do not necessarily result in increased output.

By default MOSEK will reduce the amount of log information after the first optimization on a given problem. To get full log output on subsequent re-optimizations set logCutSecondOpt to zero.

8.3.3 Saving a problem to a file¶

An optimization model defined in Fusion can be dumped to a file using the method Model.writeTask. The file format will be determined from the filename’s extension. Supported formats are listed in Section 15 (together with a specification of problem types various formats support).

For instance the problem can be written to an MPS file with

          M.writeTask("dump.mps");


All formats can be compressed with gzip by appending the .gz extension, for example

          M.writeTask("dump.mps.gz");


Some remarks:

• The problem is written to the file as it is represented in the underlying optimizer task, that is including auxiliary variables introduced by Fusion if necessary.
• Unnamed variables are given generic names. It is therefore recommended to use meaningful variable names if the problem file is meant to be human-readable.
• The task format is MOSEK‘s native file format which contains all the problem data as well as solver settings.

8.3.4 Reading a problem from a file¶

It is not possible to read a file saved with Model.writeTask back into Fusion because the structure of the high-level optimization model is not saved. However, such problem files can be solved with the command-line tool or read by the low-level Optimizer API if necessary. See the documentation of those interfaces for details.

8.3.5 Pretty printing¶

Most Fusion objects (variables, matrices, expressions, constraints) provide a toString() method which returns a plain text string representation of the object. This can be useful for inspecting and debugging the contents of small models. In general, the string will contain

• object type,
• size and dimension,
• the contents of the object, usually using a sparse representation.

For example, consider the element-wise product of the identity matrix with a square variable:

        Variable x = M.variable("x",new int[]{4,4},Domain.unbounded());
Expression ee = Expr.mulElm(Matrix.eye(4), x);
System.out.println(ee.toString());


This will be formatted as

Expr(ndim=(4,4),
[ ([0 0]) ->  + 1.0 X[0,0],
([1 1]) ->  + 1.0 X[1,1],
([2 2]) ->  + 1.0 X[2,2],
([3 3]) ->  + 1.0 X[3,3] ])


As expected, only the nonzeros are printed.