Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ConstantOptimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using ..CoreModule:
AbstractOptions, Dataset, DATA_TYPE, LOSS_TYPE, specialized_options, dataset_fraction
using ..UtilsModule: get_birth_order, PerTaskCache, stable_get!
using ..LossFunctionsModule: eval_loss, loss_to_cost
using ..PopMemberModule: PopMember
using ..PopMemberModule: AbstractPopMember, PopMember

function can_optimize(::AbstractExpression{T}, options) where {T}
return can_optimize(T, options)
Expand All @@ -31,7 +31,7 @@ end
member::P,
options::AbstractOptions;
rng::AbstractRNG=default_rng(),
)::Tuple{P,Float64} where {T<:DATA_TYPE,L<:LOSS_TYPE,P<:PopMember{T,L}}
)::Tuple{P,Float64} where {T<:DATA_TYPE,L<:LOSS_TYPE,N,P<:AbstractPopMember{T,L,N}}
can_optimize(member.tree, options) || return (member, 0.0)
nconst = count_constants_for_optimization(member.tree)
nconst == 0 && return (member, 0.0)
Expand Down Expand Up @@ -63,7 +63,7 @@ count_constants_for_optimization(ex::Expression) = count_scalar_constants(ex)

function _optimize_constants(
dataset, member::P, options, algorithm, optimizer_options, rng
)::Tuple{P,Float64} where {T,L,P<:PopMember{T,L}}
)::Tuple{P,Float64} where {T,L,N,P<:AbstractPopMember{T,L,N}}
tree = member.tree
x0, refs = get_scalar_constants(tree)
@assert count_constants_for_optimization(tree) == length(x0)
Expand All @@ -76,7 +76,7 @@ function _optimize_constants(
end
function _optimize_constants_inner(
f::F, fg!::G, x0, refs, dataset, member::P, options, algorithm, optimizer_options, rng
)::Tuple{P,Float64} where {F,G,T,L,P<:PopMember{T,L}}
)::Tuple{P,Float64} where {F,G,T,L,N,P<:AbstractPopMember{T,L,N}}
obj = if algorithm isa Optim.Newton || options.autodiff_backend === nothing
f
else
Expand Down
19 changes: 10 additions & 9 deletions src/ExpressionBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ using DynamicExpressions:
using ..CoreModule: AbstractOptions, Dataset
using ..HallOfFameModule: HallOfFame
using ..PopulationModule: Population
using ..PopMemberModule: PopMember
using ..PopMemberModule: PopMember, AbstractPopMember, create_child
using ..ComplexityModule: compute_complexity

import DynamicExpressions: get_operators
import ..CoreModule: create_expression
Expand Down Expand Up @@ -107,16 +108,16 @@ end
return with_metadata(ex; init_params(options, dataset, ex, Val(true))...)
end
function embed_metadata(
member::PopMember, options::AbstractOptions, dataset::Dataset{T,L}
) where {T,L}
return PopMember(
member::PM, options::AbstractOptions, dataset::Dataset{T,L}
) where {T,L,N,PM<:AbstractPopMember{T,L,N}}
return create_child(
member,
embed_metadata(member.tree, options, dataset),
member.cost,
member.loss,
nothing;
member.ref,
member.parent,
deterministic=options.deterministic,
options;
complexity=compute_complexity(member, options),
parent_ref=member.ref,
)
end
function embed_metadata(
Expand All @@ -135,7 +136,7 @@ end
end
function embed_metadata(
vec::Vector{H}, options::AbstractOptions, dataset::Dataset{T,L}
) where {T,L,H<:Union{HallOfFame,Population,PopMember}}
) where {T,L,H<:Union{HallOfFame,Population,AbstractPopMember}}
return map(Fix{2}(Fix{3}(embed_metadata, dataset), options), vec)
end
end
Expand Down
66 changes: 46 additions & 20 deletions src/HallOfFame.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ using ..UtilsModule: split_string, AnnotatedIOBuffer, dump_buffer
using ..CoreModule:
AbstractOptions, Dataset, DATA_TYPE, LOSS_TYPE, relu, create_expression, init_value
using ..ComplexityModule: compute_complexity
using ..PopMemberModule: PopMember
using ..PopMemberModule: AbstractPopMember, PopMember
import ..PopMemberModule: popmember_type
using ..InterfaceDynamicExpressionsModule: format_dimensions, WILDCARD_UNIT_STRING
using Printf: @sprintf

"""
HallOfFame{T<:DATA_TYPE,L<:LOSS_TYPE,N<:AbstractExpression{T}}
HallOfFame{T<:DATA_TYPE,L<:LOSS_TYPE,N<:AbstractExpression{T},PM<:AbstractPopMember{T,L,N}}

List of the best members seen all time in `.members`, with `.members[c]` being
the best member seen at complexity c. Including only the members which actually
have been set, you can run `.members[exists]`.

# Fields

- `members::Array{PopMember{T,L,N},1}`: List of the best members seen all time.
- `members::Array{PM,1}`: List of the best members seen all time.
These are ordered by complexity, with `.members[1]` the member with complexity 1.
- `exists::Array{Bool,1}`: Whether the member at the given complexity has been set.
"""
struct HallOfFame{T<:DATA_TYPE,L<:LOSS_TYPE,N<:AbstractExpression{T}}
members::Array{PopMember{T,L,N},1}
struct HallOfFame{
T<:DATA_TYPE,L<:LOSS_TYPE,N<:AbstractExpression{T},PM<:AbstractPopMember{T,L,N}
}
members::Array{PM,1}
exists::Array{Bool,1} #Whether it has been set
end
function Base.show(io::IO, mime::MIME"text/plain", hof::HallOfFame{T,L,N}) where {T,L,N}
function Base.show(
io::IO, mime::MIME"text/plain", hof::HallOfFame{T,L,N,PM}
) where {T,L,N,PM}
println(io, "HallOfFame{...}:")
for i in eachindex(hof.members, hof.exists)
s_member, s_exists = if hof.exists[i]
Expand All @@ -47,8 +52,8 @@ function Base.show(io::IO, mime::MIME"text/plain", hof::HallOfFame{T,L,N}) where
end
return nothing
end
function Base.eltype(::Union{HOF,Type{HOF}}) where {T,L,N,HOF<:HallOfFame{T,L,N}}
return PopMember{T,L,N}
function Base.eltype(::Union{HOF,Type{HOF}}) where {T,L,N,PM,HOF<:HallOfFame{T,L,N,PM}}
return PM
end

"""
Expand All @@ -68,17 +73,36 @@ function HallOfFame(
options::AbstractOptions, dataset::Dataset{T,L}
) where {T<:DATA_TYPE,L<:LOSS_TYPE}
base_tree = create_expression(init_value(T), options, dataset)
PM = options.popmember_type

return HallOfFame{T,L,typeof(base_tree)}(
# Create a prototype member to get the concrete type
prototype = PM(
copy(base_tree),
L(0),
L(Inf),
options,
1; # complexity
parent=-1,
deterministic=options.deterministic,
)

PMtype = typeof(prototype)

return HallOfFame{T,L,typeof(base_tree),PMtype}(
[
PopMember(
copy(base_tree),
L(0),
L(Inf),
options;
parent=-1,
deterministic=options.deterministic,
) for i in 1:(options.maxsize)
if i == 1
prototype
else
PM(
copy(base_tree),
L(0),
L(Inf),
options,
1; # complexity
parent=-1,
deterministic=options.deterministic,
)
end for i in 1:(options.maxsize)
],
[false for i in 1:(options.maxsize)],
)
Expand All @@ -93,11 +117,10 @@ end
"""
calculate_pareto_frontier(hallOfFame::HallOfFame{T,L,P}) where {T<:DATA_TYPE,L<:LOSS_TYPE}
"""
function calculate_pareto_frontier(hallOfFame::HallOfFame{T,L,N}) where {T,L,N}
function calculate_pareto_frontier(hallOfFame::HallOfFame{T,L,N,PM}) where {T,L,N,PM}
# TODO - remove dataset from args.
P = PopMember{T,L,N}
# Dominating pareto curve - must be better than all simpler equations
dominating = P[]
dominating = PM[]
for size in eachindex(hallOfFame.members)
if !hallOfFame.exists[size]
continue
Expand Down Expand Up @@ -276,4 +299,7 @@ function format_hall_of_fame(hof::AbstractVector{<:HallOfFame}, options)
end
# TODO: Re-use this in `string_dominating_pareto_curve`

# Type accessor for HallOfFame
popmember_type(::Type{<:HallOfFame{T,L,N,PM}}) where {T,L,N,PM} = PM

end
1 change: 1 addition & 0 deletions src/MLJInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ using ..CoreModule:
get_expression_type,
check_warm_start_compatibility
using ..CoreModule.OptionsModule: DEFAULT_OPTIONS, OPTION_DESCRIPTIONS
using ..PopMemberModule: default_popmember_type
using ..ComplexityModule: compute_complexity
using ..HallOfFameModule: HallOfFame, format_hall_of_fame
using ..UtilsModule: subscriptify, @ignore
Expand Down
4 changes: 2 additions & 2 deletions src/Migration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MigrationModule

using ..CoreModule: AbstractOptions
using ..PopulationModule: Population
using ..PopMemberModule: PopMember, reset_birth!
using ..PopMemberModule: AbstractPopMember, PopMember, reset_birth!
using ..UtilsModule: poisson_sample

"""
Expand All @@ -14,7 +14,7 @@ to do so. The original migrant population is not modified. Pass with, e.g.,
"""
function migrate!(
migration::Pair{Vector{PM},P}, options::AbstractOptions; frac::AbstractFloat
) where {T,L,N,PM<:PopMember{T,L,N},P<:Population{T,L,N}}
) where {T,L,N,PM<:AbstractPopMember{T,L,N},P<:Population{T,L,N,PM}}
base_pop = migration.second
population_size = length(base_pop.members)
mean_number_replaced = population_size * frac
Expand Down
Loading
Loading