From 6c5075b4dcaebc657fed511524796f7a3273c011 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Sun, 24 Dec 2023 02:18:17 +0000 Subject: [PATCH] More refactoring of main search loop --- src/Configure.jl | 14 ++++++------- src/SearchUtils.jl | 22 +++++++++++++++++++- src/SymbolicRegression.jl | 44 +++++++++++---------------------------- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/Configure.jl b/src/Configure.jl index a2405a698..559122552 100644 --- a/src/Configure.jl +++ b/src/Configure.jl @@ -307,14 +307,14 @@ function test_entire_pipeline( end function configure_workers(; - procs, - numprocs, - addprocs_function, - options, - exeflags, + procs::Union{Vector{Int},Nothing}, + numprocs::Int, + addprocs_function::Function, + options::Options, + exeflags::Cmd, verbosity, - example_dataset, - runtests, + example_dataset::Dataset, + runtests::Bool, ) (procs, we_created_procs) = if procs === nothing (addprocs_function(numprocs; lazy=false, exeflags), true) diff --git a/src/SearchUtils.jl b/src/SearchUtils.jl index 3074eb624..223f06ca4 100644 --- a/src/SearchUtils.jl +++ b/src/SearchUtils.jl @@ -8,12 +8,14 @@ using Distributed import StatsBase: mean import ..UtilsModule: subscriptify -import ..CoreModule: Dataset, Options +import ..CoreModule: Dataset, Options, MAX_DEGREE import ..ComplexityModule: compute_complexity import ..PopulationModule: Population +import ..PopMemberModule: PopMember import ..HallOfFameModule: HallOfFame, calculate_pareto_frontier, string_dominating_pareto_curve import ..ProgressBarsModule: WrappedProgressBar, set_multiline_postfix!, manually_iterate! +import ..AdaptiveParsimonyModule: update_frequencies! function next_worker(worker_assignment::Dict{Tuple{Int,Int},Int}, procs::Vector{Int})::Int job_counts = Dict(proc => 0 for proc in procs) @@ -365,4 +367,22 @@ function construct_datasets( ] end +function update_hall_of_fame!( + hall_of_fame::HallOfFame, members::Vector{PM}, options::Options +) where {PM<:PopMember} + for member in members + size = compute_complexity(member, options) + valid_size = 0 < size < options.maxsize + MAX_DEGREE + if !valid_size + continue + end + not_filled = !hall_of_fame.exists[size] + better_than_current = member.score < hall_of_fame.members[size].score + if not_filled || better_than_current + hall_of_fame.members[size] = copy(member) + hall_of_fame.exists[size] = true + end + end +end + end diff --git a/src/SymbolicRegression.jl b/src/SymbolicRegression.jl index c018508f4..e033661c0 100644 --- a/src/SymbolicRegression.jl +++ b/src/SymbolicRegression.jl @@ -232,7 +232,8 @@ import .SearchUtilsModule: load_saved_hall_of_fame, load_saved_population, construct_datasets, - get_cur_maxsize + get_cur_maxsize, + update_hall_of_fame! include("deprecates.jl") include("Configure.jl") @@ -627,8 +628,6 @@ function _equation_search( # (Unused for :serial) end - actualMaxsize = options.maxsize + MAX_DEGREE - # TODO: Should really be one per population too. all_running_search_statistics = [ RunningSearchStatistics(; options=options) for j in 1:nout @@ -839,39 +838,17 @@ function _equation_search( bestSubPops[j][i] = best_sub_pop(cur_pop; topn=options.topn) @recorder record = recursive_merge(record, cur_record) num_evals[j][i] += cur_num_evals - dataset = datasets[j] curmaxsize = curmaxsizes[j] - #Try normal copy... - bestPops = Population([ - member for pop in bestSubPops[j] for member in pop.members - ]) - - ################################################################### - # Hall Of Fame updating ########################################### - for (i_member, member) in enumerate( - Iterators.flatten((cur_pop.members, best_seen.members[best_seen.exists])) - ) - part_of_cur_pop = i_member <= length(cur_pop.members) + for member in cur_pop.members size = compute_complexity(member, options) - - if part_of_cur_pop - update_frequencies!(all_running_search_statistics[j]; size=size) - end - actualMaxsize = options.maxsize + MAX_DEGREE - - valid_size = 0 < size < actualMaxsize - if valid_size - already_filled = hallOfFame[j].exists[size] - better_than_current = member.score < hallOfFame[j].members[size].score - if !already_filled || better_than_current - hallOfFame[j].members[size] = copy(member) - hallOfFame[j].exists[size] = true - end - end + update_frequencies!(all_running_search_statistics[j]; size) end - ################################################################### + #! format: off + update_hall_of_fame!(hallOfFame[j], cur_pop.members, options) + update_hall_of_fame!(hallOfFame[j], best_seen.members[best_seen.exists], options) + #! format: on # Dominating pareto curve - must be better than all simpler equations dominating = calculate_pareto_frontier(hallOfFame[j]) @@ -898,8 +875,11 @@ function _equation_search( ################################################################### # Migration ####################################################### if options.migration + best_of_each = Population([ + member for pop in bestSubPops[j] for member in pop.members + ]) migrate!( - bestPops.members => cur_pop, options; frac=options.fraction_replaced + best_of_each.members => cur_pop, options; frac=options.fraction_replaced ) end if options.hof_migration && length(dominating) > 0