Skip to content

Commit

Permalink
Add OptimizationStats and OptimizationState
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Dec 28, 2023
1 parent 7f2f357 commit 7aef715
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/OptimizationBBO/src/OptimizationBBO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ function SciMLBase.__solve(cache::Optimization.OptimizationCache{
if cache.callback === Optimization.DEFAULT_CALLBACK
cb_call = false
else
cb_call = cache.callback(decompose_trace(trace, cache.progress), x...)
n_steps = BlackBoxOptim.num_steps(trace)
curr_u = decompose_trace(trace, cache.progress)
state = Optimization.OptimizationState(iterations = n_steps, u = curr_u, objective = x[1], solver_state = trace)
cb_call = cache.callback(curr_u, x..., state)

Check warning on line 121 in lib/OptimizationBBO/src/OptimizationBBO.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationBBO/src/OptimizationBBO.jl#L118-L121

Added lines #L118 - L121 were not covered by tests
end

if !(cb_call isa Bool)
Expand Down Expand Up @@ -175,11 +178,13 @@ function SciMLBase.__solve(cache::Optimization.OptimizationCache{
t1 = time()

opt_ret = Symbol(opt_res.stop_reason)

stats = Optimization.OptimizationStats(opt_res.num_steps, t1 - t0, BlackBoxOptim.num_func_evals(opt_res))

Check warning on line 181 in lib/OptimizationBBO/src/OptimizationBBO.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationBBO/src/OptimizationBBO.jl#L181

Added line #L181 was not covered by tests
SciMLBase.build_solution(cache, cache.opt,
BlackBoxOptim.best_candidate(opt_res),
BlackBoxOptim.best_fitness(opt_res); original = opt_res,
retcode = opt_ret, solve_time = t1 - t0)
BlackBoxOptim.best_fitness(opt_res);
original = opt_res,
retcode = opt_ret,
stats = stats)
end

end
24 changes: 24 additions & 0 deletions src/stats_state.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

struct OptimizationStats
iterations::Int
time::Float64
fevals::Int
gevals::Int
hevals::Int
end

OptimizationStats(; iterations = 0, time = 0.0, fevals = 0, gevals = 0, hevals = 0) =

Check warning on line 10 in src/stats_state.jl

View check run for this annotation

Codecov / codecov/patch

src/stats_state.jl#L10

Added line #L10 was not covered by tests
OptimizationStats(iterations, time, fevals, gevals, hevals)

struct OptimizationState{X, O, G, H, S}
iteration::Int
u::X
objective::O
gradient::G
hessian::H
solver_state::S
end

OptimizationState(; iterations = 0, u = nothing, objective = nothing,

Check warning on line 22 in src/stats_state.jl

View check run for this annotation

Codecov / codecov/patch

src/stats_state.jl#L22

Added line #L22 was not covered by tests
gradient = nothing, hessian = nothing, solver_state = nothing) =
OptimizationState(iterations, u, objective, gradient, hessian, solver_state)

0 comments on commit 7aef715

Please sign in to comment.