Consistent way to suppress solver output

Just came across this old problem. If you want to use parts of the output you can redirect stdout to a string:

function capture_stdout(f)
    stdout_orig = stdout
    (rd, wr) = redirect_stdout()
    f()
    close(wr)
    redirect_stdout(stdout_orig)
    read(rd, String)
end

s = capture_stdout() do
    optimize!(model)
end

#print the summary (which starts with "Number of Iterations")
try
    s[findlast("Number of Iterations", s)[1]:end] |> print
catch
    print(s)
end

EDIT:
Digged a bit further and found that most solvers support some kind of loglevel setting, e.g.:

  • Ipopt: “print_level”
  • Cbc: “LogLevel”
  • Clp: “logLevel”

These options can be set either when initialising the model, e.g.

model = Model(with_optimizer(Ipopt.Optimizer, tol = 1e-8, max_iter = 1000, print_level = 1))`

or afterwards using the MOI Api (which is currently not so nicely documented …)

MOI.set(model, MOI.RawParameter("print_level"), 1)
2 Likes