Skip to content

Commit

Permalink
More refactoring of main search loop
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Dec 24, 2023
1 parent 27dcc04 commit 4461adc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
22 changes: 21 additions & 1 deletion src/SearchUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
44 changes: 12 additions & 32 deletions src/SymbolicRegression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand Down

0 comments on commit 4461adc

Please sign in to comment.