Skip to content

Commit 880fc54

Browse files
Merge pull request #652 from SciML/Vaibhavdixit02-patch-4
[WIP]Stats and State
2 parents 80d8465 + abfa548 commit 880fc54

File tree

45 files changed

+210
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+210
-126
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Optimization"
22
uuid = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
3-
version = "3.20.2"
3+
version = "3.21.0"
44

55
[deps]
66
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
@@ -57,7 +57,7 @@ Printf = "1.9"
5757
ProgressLogging = "0.1"
5858
Reexport = "1.2"
5959
ReverseDiff = "1.14"
60-
SciMLBase = "2.11"
60+
SciMLBase = "2.16.3"
6161
SparseArrays = "1.9, 1.10"
6262
SparseDiffTools = "2.14"
6363
SymbolicIndexingInterface = "0.3"

lib/OptimizationBBO/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OptimizationBBO"
22
uuid = "3e6eede4-6085-4f62-9a71-46d9bc1eb92b"
33
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
4-
version = "0.1.5"
4+
version = "0.2.0"
55

66
[deps]
77
BlackBoxOptim = "a134a8b2-14d6-55f6-9291-3336d3ab0209"
@@ -10,7 +10,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1010

1111
[compat]
1212
BlackBoxOptim = "0.6"
13-
Optimization = "3.15"
13+
Optimization = "3.21"
1414
Reexport = "1.2"
1515
julia = "1"
1616

lib/OptimizationBBO/src/OptimizationBBO.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ function SciMLBase.__solve(cache::Optimization.OptimizationCache{
115115
if cache.callback === Optimization.DEFAULT_CALLBACK
116116
cb_call = false
117117
else
118-
cb_call = cache.callback(decompose_trace(trace, cache.progress), x...)
118+
n_steps = BlackBoxOptim.num_steps(trace)
119+
curr_u = decompose_trace(trace, cache.progress)
120+
opt_state = Optimization.OptimizationState(iteration = n_steps, u = curr_u, objective = x[1], solver_state = trace)
121+
cb_call = cache.callback(opt_state, x...)
119122
end
120123

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

177180
opt_ret = Symbol(opt_res.stop_reason)
178-
181+
stats = Optimization.OptimizationStats(; iterations = opt_res.iterations, time = t1 - t0, fevals = opt_res.f_calls)
179182
SciMLBase.build_solution(cache, cache.opt,
180183
BlackBoxOptim.best_candidate(opt_res),
181-
BlackBoxOptim.best_fitness(opt_res); original = opt_res,
182-
retcode = opt_ret, solve_time = t1 - t0)
184+
BlackBoxOptim.best_fitness(opt_res);
185+
original = opt_res,
186+
retcode = opt_ret,
187+
stats = stats)
183188
end
184189

185190
end

lib/OptimizationBBO/test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ using Test
2525
@test 10 * sol.objective < l1
2626

2727
fitness_progress_history = []
28-
function cb(best_candidate, fitness)
29-
push!(fitness_progress_history, [best_candidate, fitness])
28+
function cb(state, fitness)
29+
push!(fitness_progress_history, [state.u, fitness])
3030
return false
3131
end
3232
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited(), callback = cb)

lib/OptimizationCMAEvolutionStrategy/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OptimizationCMAEvolutionStrategy"
22
uuid = "bd407f91-200f-4536-9381-e4ba712f53f8"
33
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
4-
version = "0.1.4"
4+
version = "0.2.0"
55

66
[deps]
77
CMAEvolutionStrategy = "8d3b24bd-414e-49e0-94fb-163cc3a3e411"
@@ -11,7 +11,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1111
[compat]
1212
julia = "1"
1313
CMAEvolutionStrategy = "0.2"
14-
Optimization = "3.15"
14+
Optimization = "3.21"
1515
Reexport = "1.2"
1616

1717
[extras]

lib/OptimizationCMAEvolutionStrategy/src/OptimizationCMAEvolutionStrategy.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export CMAEvolutionStrategyOpt
99
struct CMAEvolutionStrategyOpt end
1010

1111
SciMLBase.allowsbounds(::CMAEvolutionStrategyOpt) = true
12-
SciMLBase.allowscallback(::CMAEvolutionStrategyOpt) = false #looks like `logger` kwarg can be used to pass it, so should be implemented
1312
SciMLBase.supports_opt_cache_interface(opt::CMAEvolutionStrategyOpt) = true
1413

1514
function __map_optimizer_args(prob::OptimizationCache, opt::CMAEvolutionStrategyOpt;
@@ -23,7 +22,7 @@ function __map_optimizer_args(prob::OptimizationCache, opt::CMAEvolutionStrategy
2322
end
2423

2524
mapped_args = (; lower = prob.lb,
26-
upper = prob.ub)
25+
upper = prob.ub, logger = CMAEvolutionStrategy.BasicLogger(prob.u0; verbosity = 0, callback = callback))
2726

2827
if !isnothing(maxiters)
2928
mapped_args = (; mapped_args..., maxiter = maxiters)
@@ -74,12 +73,18 @@ function SciMLBase.__solve(cache::OptimizationCache{
7473

7574
cur, state = iterate(cache.data)
7675

77-
function _cb(trace)
78-
cb_call = cache.callback(decompose_trace(trace).metadata["x"], trace.value...)
76+
function _cb(opt, y, fvals, perm)
77+
curr_u = opt.logger.xbest[end]
78+
opt_state = Optimization.OptimizationState(; iteration = length(opt.logger.fmedian),
79+
u = curr_u,
80+
objective = opt.logger.fbest[end],
81+
solver_state = opt.logger)
82+
83+
cb_call = cache.callback(opt_state, x...)
7984
if !(cb_call isa Bool)
8085
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
8186
end
82-
cur, state = iterate(data, state)
87+
cur, state = iterate(cache.data, state)
8388
cb_call
8489
end
8590

@@ -100,11 +105,12 @@ function SciMLBase.__solve(cache::OptimizationCache{
100105
t1 = time()
101106

102107
opt_ret = opt_res.stop.reason
103-
108+
stats = Optimization.OptimizationStats(; iterations = length(opt_res.logger.fmedian), time = t1 - t0, fevals = length(opt_res.logger.fmedian))
104109
SciMLBase.build_solution(cache, cache.opt,
105110
opt_res.logger.xbest[end],
106111
opt_res.logger.fbest[end]; original = opt_res,
107-
retcode = opt_ret, solve_time = t1 - t0)
112+
retcode = opt_ret,
113+
stats = stats)
108114
end
109115

110116
end

lib/OptimizationCMAEvolutionStrategy/test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ using Test
1010
prob = OptimizationProblem(f, x0, _p, lb = [-1.0, -1.0], ub = [0.8, 0.8])
1111
sol = solve(prob, CMAEvolutionStrategyOpt())
1212
@test 10 * sol.objective < l1
13+
14+
function cb(state, args...)
15+
if state.iteration %10 == 0
16+
println(state.u)
17+
end
18+
return false
19+
end
20+
sol = solve(prob, CMAEvolutionStrategyOpt(), callback = cb, maxiters = 100)
1321
end

lib/OptimizationEvolutionary/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OptimizationEvolutionary"
22
uuid = "cb963754-43f6-435e-8d4b-99009ff27753"
33
authors = ["Vaibhav Dixit <[email protected]> and contributors"]
4-
version = "0.1.3"
4+
version = "0.2.0"
55

66
[deps]
77
Evolutionary = "86b6b26d-c046-49b6-aa0b-5f0f74682bd6"
@@ -11,7 +11,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1111
[compat]
1212
julia = "1"
1313
Evolutionary = "0.11"
14-
Optimization = "3.15"
14+
Optimization = "3.21"
1515
Reexport = "1.2"
1616

1717
[extras]

lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ function SciMLBase.__solve(cache::OptimizationCache{
8484
cur, state = iterate(cache.data)
8585

8686
function _cb(trace)
87-
cb_call = cache.callback(decompose_trace(trace).metadata["x"], trace.value...)
87+
curr_u = decompose_trace(trace).metadata["x"][end]
88+
opt_state = Optimization.OptimizationState(; iteration = decompose_trace(trace).iteration,
89+
u = curr_u,
90+
objective = x[1],
91+
solver_state = trace)
92+
cb_call = cache.callback(opt_state, trace.value...)
8893
if !(cb_call isa Bool)
8994
error("The callback should return a boolean `halt` for whether to stop the optimization process.")
9095
end
@@ -127,11 +132,13 @@ function SciMLBase.__solve(cache::OptimizationCache{
127132
end
128133
t1 = time()
129134
opt_ret = Symbol(Evolutionary.converged(opt_res))
130-
135+
stats = Optimization.OptimizationStats(; iterations = opt_res.iterations
136+
, time = t1 - t0, fevals = opt_res.f_calls)
131137
SciMLBase.build_solution(cache, cache.opt,
132138
Evolutionary.minimizer(opt_res),
133139
Evolutionary.minimum(opt_res); original = opt_res,
134-
retcode = opt_ret, solve_time = t1 - t0)
140+
retcode = opt_ret,
141+
stats = stats)
135142
end
136143

137144
end

lib/OptimizationEvolutionary/test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ Random.seed!(1234)
3434
res = zeros(1)
3535
cons_circ(res, sol.u, nothing)
3636
@test sol.objective < l1
37+
38+
function cb(state, args...)
39+
if state.iteration %10 == 0
40+
println(state.u)
41+
end
42+
return false
43+
end
44+
sol = solve(prob, CMAES= 40, λ = 100), callback = cb, maxiters = 100)
3745
end

0 commit comments

Comments
 (0)