Skip to content
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

deepcopy to save population in default trace, instead of reference #730

Merged
merged 1 commit into from
Apr 3, 2024

Conversation

jonathanfischer97
Copy link
Contributor

Default trace! overload was saving population by reference

# Overload the trace! function to add the population to the trace prior to calling any user-defined trace! method
function Evolutionary.trace!(tr, iteration, objfun, state, population,
        method::Evolutionary.AbstractOptimizer, options, curr_time = time())
    dt = Dict{String, Any}()
    dt["time"] = curr_time

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

    # set additional trace value
    Evolutionary.trace!(dt, objfun, state, population, method, options)
    Evolutionary.update!(tr,
        state,
        iteration,
        Evolutionary.value(state),
        dt,
        options.store_trace,
        options.show_trace,
        options.show_every,
        options.callback)
end

deepcopy fixes the issue

This test fails with the old code, but passes with deepcopy:

# 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"]

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Copy link

codecov bot commented Apr 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 66.01%. Comparing base (0a417ea) to head (d4c732a).
Report is 23 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master     #730       +/-   ##
===========================================
+ Coverage   11.96%   66.01%   +54.05%     
===========================================
  Files          21       22        +1     
  Lines        1438     1477       +39     
===========================================
+ Hits          172      975      +803     
+ Misses       1266      502      -764     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Vaibhavdixit02 Vaibhavdixit02 merged commit 20e46a6 into SciML:master Apr 3, 2024
40 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants