Skip to content

Commit

Permalink
deepcopy of population in default trace, so gets values instead of re…
Browse files Browse the repository at this point in the history
…ferences
  • Loading branch information
jonathanfischer97 committed Apr 3, 2024
1 parent 8310517 commit d4c732a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Evolutionary.trace!(tr, iteration, objfun, state, population,
dt["time"] = curr_time

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

Check warning on line 21 in lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl

View check run for this annotation

Codecov / codecov/patch

lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl#L21

Added line #L21 was not covered by tests

# set additional trace value
Evolutionary.trace!(dt, objfun, state, population, method, options)
Expand Down
5 changes: 4 additions & 1 deletion lib/OptimizationEvolutionary/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ Random.seed!(1234)
record["TESTVAL"] = state.fittest
end

#test that `store_trace=true` works now. Threw ""type Array has no field value" before.
# 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.
@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"]
end

0 comments on commit d4c732a

Please sign in to comment.