wildart / Evolutionary.jl

Evolutionary & genetic algorithms for Julia
Other
327 stars 59 forks source link

Cannot modify trace #104

Closed Thyra closed 1 year ago

Thyra commented 1 year ago

I've been trying to add additional information to the trace because I have a quite computationally heavy optimization and would like to save the best minimizer every X iterations rather than just at the end (and risk losing everything if the optimization does not finish). I've tried following the example in the documentation but it seems my new trace! function is not even being called! Neither when I define it as Evolutionary.trace! rather than just trace!. I am using Julia version 1.8.3 and Evolutionary v0.11.1. Might also just me being very new to Julia, but here is a minimal (non-)working example:

using Evolutionary

function trace!(record::Dict{String,Any}, objfun, state, population, method::CMAES, options)
    record["σ"] = state.σ
    record["test"] = 47
    println("I am being called!")
end

function cb(trace)
    println(trace.metadata)
    return(false)
end

result = Evolutionary.optimize(
             x -> sum(x.^2), ones(3),
             GA(populationSize = 100, selection = susinv,
                crossover = DC, mutation = PLM()), Evolutionary.Options(show_trace=true, callback=cb))
wildart commented 1 year ago

Set store_trace to true in Options. See, here https://wildart.github.io/Evolutionary.jl/stable/tutorial/#General-options

Thyra commented 1 year ago

Ha! I'm surprised I didn't think of that myself, thank you. Would you consider adding that to the end of this paragraph to make it more clear? Something like "In order to extend trace record, you need to override trace! function providing specialize function behavior on one of specific parameters and set store_trace to true"

wildart commented 1 year ago

Done. See https://wildart.github.io/Evolutionary.jl/dev/tutorial/#Trace