Skip to content

Commit

Permalink
add solver_specific in stats
Browse files Browse the repository at this point in the history
  • Loading branch information
aldma authored and dpo committed Jan 16, 2025
1 parent 03362a0 commit 7302327
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 7 additions & 0 deletions examples/demo-cutest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ callback =
stats = AL(nlp, h, atol = 1e-6, verbose = 1, callback = callback)
print(stats)

callback =
(regnlp, solver, stats) -> begin
@info "iter $(stats.iter), f $(stats.solver_specific[:smooth_obj]), h $(stats.solver_specific[:nonsmooth_obj])"
end
stats = AL(nlp, h, atol = 1e-6, verbose = 1, callback = callback)
print(stats)

finalize(nlp)
18 changes: 13 additions & 5 deletions src/AL_alg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ Notably, you can access, and modify, the following:
- `stats.iter`: current iteration counter;
- `stats.objective`: current objective function value;
- `stats.status`: current status of the algorithm. Should be `:unknown` unless the algorithm has attained a stopping criterion. Changing this to anything will stop the algorithm, but you should use `:user` to properly indicate the intention;
- `stats.elapsed_time`: elapsed time in seconds.
- `stats.elapsed_time`: elapsed time in seconds;
- `stats.solver_specific[:smooth_obj]`: current value of the smooth part of the objective function;
- `stats.solver_specific[:nonsmooth_obj]`: current value of the nonsmooth part of the objective function.
"""
mutable struct ALSolver{T, V, M, ST} <: AbstractOptimizationSolver
x::V
Expand Down Expand Up @@ -244,9 +246,12 @@ function SolverCore.solve!(
set_constraint_multipliers!(stats, solver.y)
subout = solver.sub_stats

objx, _ = objcons!(nlp, solver.x, solver.cx)
objx += @views h(solver.x[selected])
fx, _ = objcons!(nlp, solver.x, solver.cx)
hx = @views h(solver.x[selected])
objx = fx + hx
set_objective!(stats, objx)
set_solver_specific!(stats, :smooth_obj, fx)
set_solver_specific!(stats, :nonsmooth_obj, hx)

mu = init_penalty
solver.sub_model.y .= solver.y
Expand Down Expand Up @@ -297,9 +302,12 @@ function SolverCore.solve!(
subiters += subout.iter

# objective
objx = obj(nlp, solver.x)
objx += @views h(solver.x[selected])
fx = obj(nlp, solver.x)
hx = @views h(solver.x[selected])
objx = fx + hx
set_objective!(stats, objx)
set_solver_specific!(stats, :smooth_obj, fx)
set_solver_specific!(stats, :nonsmooth_obj, hx)

# dual estimate
update_y!(solver.sub_model)
Expand Down

0 comments on commit 7302327

Please sign in to comment.