Skip to content

Reversion: stop deep copying population, just save curr_u for OptimizationState construction #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function Evolutionary.trace!(tr, iteration, objfun, state, population,
dt = Dict{String, Any}()
dt["time"] = curr_time

# record `x` to store the population. Needed for constructing OptimizationState.
dt["x"] = deepcopy(population)
# record current u0. Needed for constructing OptimizationState.
dt["curr_u"] = population[end]

# set additional trace value
Evolutionary.trace!(dt, objfun, state, population, method, options)
Expand Down Expand Up @@ -105,7 +105,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
cur, state = iterate(cache.data)

function _cb(trace)
curr_u = decompose_trace(trace).metadata["x"][end]
curr_u = decompose_trace(trace).metadata["curr_u"]
opt_state = Optimization.OptimizationState(;
iter = decompose_trace(trace).iteration,
u = curr_u,
Expand Down
7 changes: 2 additions & 5 deletions lib/OptimizationEvolutionary/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ Random.seed!(1234)
# Test that `store_trace=true` works now. Threw ""type Array has no field value" before.
sol = solve(prob, CMAES(μ = 40, λ = 100), store_trace = true)

# Make sure that both the user's trace record value, as well as `x` are stored in the trace.
# Make sure that both the user's trace record value, as well as `curr_u` are stored in the trace.
@test haskey(sol.original.trace[end].metadata, "TESTVAL") &&
haskey(sol.original.trace[end].metadata, "x")

# Test the the values of x are saved, not the reference
@test !(sol.original.trace[end].metadata["x"] === sol.original.trace[end-1].metadata["x"])
haskey(sol.original.trace[end].metadata, "curr_u")
end