Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix equation splitting for unicode variables #223

Merged
merged 2 commits into from
Jun 19, 2023
Merged
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
17 changes: 1 addition & 16 deletions src/HallOfFame.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module HallOfFameModule

import DynamicExpressions: Node, string_tree
import ..UtilsModule: split_string
import ..CoreModule: MAX_DEGREE, Options, Dataset, DATA_TYPE, LOSS_TYPE
import ..ComplexityModule: compute_complexity
import ..PopMemberModule: PopMember, copy_pop_member
Expand Down Expand Up @@ -160,20 +161,4 @@ function string_dominating_pareto_curve(
return output
end

"""
split_string(s::String, n::Integer)

```jldoctest
split_string("abcdefgh", 3)

# output

["abc", "def", "gh"]
```
"""
function split_string(s::String, n::Integer)
length(s) <= n && return [s]
return [s[i:min(i + n - 1, end)] for i in 1:n:length(s)]
end

end
18 changes: 18 additions & 0 deletions src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ function recursive_merge(x...)
return x[end]
end

"""
split_string(s::String, n::Integer)

```jldoctest
split_string("abcdefgh", 3)

# output

["abc", "def", "gh"]
```
"""
function split_string(s::String, n::Integer)
length(s) <= n && return [s]
# Due to unicode characters, need to split only at valid indices:
I = eachindex(s) |> collect
return [s[I[i]:I[min(i + n - 1, end)]] for i in 1:n:length(s)]
end

"""
Tiny equivalent to StaticArrays.MVector

Expand Down
10 changes: 10 additions & 0 deletions test/test_print.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using SymbolicRegression
using SymbolicRegression.UtilsModule: split_string

include("test_params.jl")

Expand Down Expand Up @@ -45,3 +46,12 @@ for binop in [safe_pow, ^]
minitree = Node(5, Node("x1"), Node("x2"))
@test string_tree(minitree, opts) == "(x1 ^ x2)"
end

@testset "Test splitting of strings" begin
split_string("abcdefgh", 3) == ["abc", "def", "gh"]
split_string("abcdefgh", 100) == ["abcdefgh"]
split_string("⋅", 1) == ["⋅"]
split_string("⋅⋅", 1) == ["⋅", "⋅"]
split_string("⋅⋅⋅⋅", 2) == ["⋅⋅", "⋅⋅"]
split_string("ραβγ", 2) == ["ρα", "βγ"]
end