7.4 Input/Output¶
The logging and I/O features are provided mainly by the MOSEK task and to some extent by the MOSEK environment objects.
7.4.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.
The log messages are partitioned in three streams:
messages,
Streamtype::MSG
warnings,
Streamtype::WRN
errors,
Streamtype::ERR
These streams are aggregated in the Streamtype::LOG
stream. A stream handler can be defined for each stream separately.
A stream handler is simply a function which accepts a string, for example:
fn my_stream(msg : &str) {
print!("{}",msg);
}
In order to attach a stream handler we must use an object with callbacks (struct TaskCB
), created with Task.with_callbacks
. In most cases it is natural just to create all tasks as tasks with callbacks, for example:
let mut task = env.task().unwrap().with_callbacks();
When a handler is attached to the stream using Task.put_stream_callback
any text that is printed to that stream will be passed to the handler:
task.put_stream_callback(Streamtype::LOG, my_stream)?;
The stream can be detached by calling
task.clear_stream_callback(Streamtype::LOG)?;
A log stream can also be redirected to a file:
task.link_file_to_stream(Streamtype::LOG, "mosek.log", 0)?;
After optimization is completed an additional short summary of the solution and optimization process can be printed to any stream using the method Task.solution_summary
.
7.4.2 Log verbosity¶
The logging verbosity can be controlled by setting the relevant parameters, as for instance
Iparam::LOG_SIM
, and
Each parameter controls the output level of a specific functionality or algorithm. The main switch is Iparam::LOG
which affect the whole output. The actual log level for a specific functionality is determined as the minimum between Iparam::LOG
and the relevant parameter. For instance, the log level for the output produce by the interior-point algorithm is tuned by the Iparam::LOG_INTPNT
; the actual log level is defined by the minimum between Iparam::LOG
and Iparam::LOG_INTPNT
.
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 Iparam::LOG
. Larger values of Iparam::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 Iparam::LOG_CUT_SECOND_OPT
to zero.
7.4.3 Saving a problem to a file¶
An optimization problem can be dumped to a file using the method Task.write_data
. The file format will be determined from the extension of the filename. Supported formats are listed in Sec. 16 (Supported File Formats) together with a table of problem types supported by each.
For instance the problem can be written to a human-readable PTF file (see Sec. 16.5 (The PTF Format)) with
task.write_data("data.ptf")?;
All formats can be compressed with gzip
by appending the .gz
extension, and with ZStandard
by appending the .zst
extension, for example
task.write_data("data.task.gz")?;
Some remarks:
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.
7.4.4 Reading a problem from a file¶
A problem saved in any of the supported file formats can be read directly into a task using Task.read_data
. The task must be created in advance. Afterwards the problem can be optimized, modified, etc. If the file contained solutions, then are also imported, but the status of any solution will be set to Solsta::UNKNOWN
(solutions can also be read separately using Task.read_solution
). If the file contains parameters, they will be set accordingly.
let mut task = env.task().unwrap();
if let Err(_) = task.read_data("file.task.gz") {
println!("Problem reading the file")
}
else if let Err(_) = task.optimize() {
println!("Problem optimizing the file")
}