SolverTraces.jl
Usage example
Using SolverTraces.jl is very easy:
N = 20 # Number of iterations
tol = Tolerance(1e-3)
tols = 10 .^ range(0, stop=-3, length=N)
trace = SolverTrace(N, CurrentStep(N),
ColumnSeparator(),
Performance(100), # Could be number of points processed each iteration
ColumnSeparator(),
tol)
print_header(trace)
for i = 1:N
# Do work
# ...
# Set which tolerance was achieved in this iteration:
tol.current = tols[i]
SolverTraces.next!(trace)
end
which will give output similar to this:
Optim.jl
It is similarly very easy to add colour to your Optim.jl runs:
f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
x0 = [0.0, 0.0]
max_iter = 100
tol = 1e-8
value = ScalarColumn(Inf, "Value")
tolerance = Tolerance(tol, "|∇|")
trace = SolverTrace(max_iter,
CurrentStep(max_iter), ColumnSeparator(), value, tolerance,
print_interval=1)
trace_callback = opt_state -> begin
tolerance.current = opt_state.g_norm
value.n = opt_state.value
SolverTraces.next!(trace)
false
end
options = Optim.Options(iterations=max_iter, g_tol=tol,
callback=trace_callback)
print_header(trace)
optimize(f, x0, options)
which will result in something similar to
Reference
SolverTraces.SolverTrace
— TypeSolverTrace(num_steps, i, print_interval, io, progress, columns, callbacks)
Represents a solver trace for num_steps
iterations, i
being the current one. Each time next!
is called, i
is incremented by one, and if a multiple of print_interval
is reached, a line is printed in the trace. Optionally (default), a ProgressMeter.Progress
is shown as well, providing progress information between solver trace printouts.
SolverTraces.SolverTrace
— TypeSolverTrace(num_steps[, column=CurrentStep(num_steps), columns...;
io=stdout, num_printouts=10, progress_meter=true,
print_interval, kwargs...])
Construct a SolverTrace
for num_steps
iterations, with the columns
that are to appear in the solver trace. The solver trace is printed to io
, num_printouts
lines will appear; alternatively, the print_interval
can be specified directly. Below the solver trace, a progressbar is displayed, unless !progress_meter
. kwargs...
are passed on to ProgressMeter.Progress
.
SolverTraces.print_header
— Functionprint_header(s)
Print the associated header of the SolverTrace
s
.
SolverTraces.next!
— Functionnext!(fun, s::SolverTrace; kwargs...)
Signal to the SolverTrace
s
that the next iteration has been performed, increasing the internal counter s.i
. If a multiple of s.print_interval
is reached, a line in the solver trace is printed, formatting each and every trace column, the callback function fun
is called, as well as all the registered callbacks.
kwargs
are passed on to ProgressMeter.next!
to allow e.g. showvalues
functionality.