Skip to content

Commit

Permalink
Switching acosh_abs to acosh_nan as was done for the logs
Browse files Browse the repository at this point in the history
This also removes the regularizing '1' that was present in the old
definition. Instead the test domain is shifted.
  • Loading branch information
johanbluecreek committed Sep 6, 2022
1 parent 3836617 commit 325d681
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import .OperatorsModule:
log10_nan,
log1p_nan,
sqrt_abs,
acosh_abs,
acosh_nan,
neg,
greater,
greater,
Expand Down
7 changes: 4 additions & 3 deletions src/Operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ function log1p_nan(x::T)::T where {T<:Real}
x <= T(-1) && return T(NaN)
return log1p(x)
end
function acosh_abs(x::T)::T where {T<:Real}
return acosh(abs(x) + convert(T, 1))
function acosh_nan(x::T)::T where {T<:Real}
x < T(1) && return T(NaN)
return acosh(x)
end

# Generics:
Expand All @@ -75,7 +76,7 @@ log_nan(x) = log(x)
log2_nan(x) = log2(x)
log10_nan(x) = log10(x)
log1p_nan(x) = log1p(x)
acosh_abs(x) = acosh(abs(x) + 1)
acosh_nan(x) = acosh(x)

function sqrt_abs(x::T)::T where {T}
return sqrt(abs(x))
Expand Down
4 changes: 2 additions & 2 deletions src/Options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ..OperatorsModule:
log2_nan,
log1p_nan,
sqrt_abs,
acosh_abs,
acosh_nan,
atanh_clip
import ..EquationModule: Node, string_tree
import ..OptionsStructModule: Options, ComplexityMapping
Expand Down Expand Up @@ -112,7 +112,7 @@ function unaopmap(op)
elseif op == sqrt
return sqrt_abs
elseif op == acosh
return acosh_abs
return acosh_nan
elseif op == atanh
return atanh_clip
end
Expand Down
4 changes: 2 additions & 2 deletions src/SymbolicRegression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export Population,
log2_nan,
log10_nan,
log1p_nan,
acosh_abs,
acosh_nan,
sqrt_abs,
neg,
greater,
Expand Down Expand Up @@ -130,7 +130,7 @@ import .CoreModule:
log10_nan,
log1p_nan,
sqrt_abs,
acosh_abs,
acosh_nan,
neg,
greater,
greater,
Expand Down
4 changes: 3 additions & 1 deletion test/test_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using SymbolicRegression:
log2_nan,
log10_nan,
sqrt_abs,
acosh_abs,
acosh_nan,
neg,
greater,
greater,
Expand All @@ -33,6 +33,8 @@ for T in types_to_test
@test isnan(log2_nan(-val))
@test abs(log10_nan(val) - log10(val)) < 1e-6
@test isnan(log10_nan(-val))
@test abs(acosh_nan(val2) - acosh(val2)) < 1e-6
@test isnan(acosh_nan(-val2))
@test neg(-val) == val
@test sqrt_abs(val) == sqrt(val)
@test mult(val, val2) == val * val2
Expand Down
8 changes: 6 additions & 2 deletions test/test_tree_construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include("test_params.jl")
x1 = 2.0

# Initialize functions in Base....
for unaop in [cos, exp, log_nan, log2_nan, log10_nan, relu, gamma, acosh_abs]
for unaop in [cos, exp, log_nan, log2_nan, log10_nan, relu, gamma, acosh_nan]
for binop in [sub]
function make_options(; kw...)
return Options(;
Expand Down Expand Up @@ -56,12 +56,16 @@ for unaop in [cos, exp, log_nan, log2_nan, log10_nan, relu, gamma, acosh_abs]

Random.seed!(0)
N = 100
if unaop in [log_nan, log2_nan, log10_nan]
if unaop in [log_nan, log2_nan, log10_nan, acosh_nan]
X = T.(rand(MersenneTwister(0), 5, N) / 3)
else
X = T.(randn(MersenneTwister(0), 5, N) / 3)
end
X = X + sign.(X) * T(0.1)
if unaop == acosh_nan
X = X .+ T(1.0)
end

y = T.(f_true.(X[1, :]))
dataset = Dataset(X, y)
test_y, complete = eval_tree_array(tree, X, make_options())
Expand Down

0 comments on commit 325d681

Please sign in to comment.