# Evolutionary TreeGP distribution version falls over

**URL:** <https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769>\
**Category:** New to Julia\
**Created:** [February 4, 2022, 1:13am UTC](https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769 "2022-02-04T01:13:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabel](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sabel](https://discourse.julialang.org/u/sabel)\
**Post date:** [February 4, 2022, 1:13am UTC](https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769/1 "2022-02-04T01:13:56Z")

</div>

Hi folks - I am just learning Julia as I want to use TreeGP in Evolutionary. The code at **SymbolicRegression.ipynb** works fine right up to this bit ==\>

Random.seed!(987498737423);  
res = Evolutionary.optimize(fitobj,  
TreeGP(50, Terminal[syms…], funcs,  
mindepth=1,  
maxdepth=4,  
optimizer = GA(  
selection = uniformranking(5),  
ɛ = 0.1,  
mutationRate = 0.95,  
crossoverRate = 0.05,  
),  
)  
)

which it pukes up as below. I don’t understand. How does this not work? - it’s the basic example code. It tells me “optimizer” is not an argument. Thanks for any help.

here’s the errors ==\>

MethodError: no method matching TreeGP(; populationSize=50, terminals=Dict(:x =\> 1), functions=Dict{Function, Int64}((-) =\> 2, (/) =\> 2, (\*) =\> 2, (+) =\> 2), mindepth=1, maxdepth=4, optimizer=GA[P=50,x=0.05,μ=0.95,ɛ=0.1])  
Closest candidates are:  
TreeGP(; populationSize, terminals, functions, mindepth, maxdepth, crossover, mutation, selection, crossoverRate, mutationRate, initialization, simplify, metrics) at util.jl:453 got unsupported keyword argument “optimizer”  
TreeGP(::Integer, ::Vector{Union{Function, Real, Symbol}}, ::Vector{Function}; kwargs…) at /Users/sabel/.julia/packages/Evolutionary/65hL6/src/gp.jl:40  
TreeGP(::Integer, ::Dict{Union{Function, Real, Symbol}, Int64}, ::Dict{Function, Int64}, ::Int64, ::Int64, ::Function, ::Function, ::Function, ::Real, ::Real, ::Symbol, ::Union{Nothing, Function}, ::Vector{ConvergenceMetric}) at /Users/sabel/.julia/packages/Evolutionary/65hL6/src/gp.jl:26 got unsupported keyword arguments “populationSize”, “terminals”, “functions”, “mindepth”, “maxdepth”, “optimizer”  
…

Stacktrace:  
[1] kwerr(kw::NamedTuple{(:populationSize, :terminals, :functions, :mindepth, :maxdepth, :optimizer), Tuple{Int64, Dict{Symbol, Int64}, Dict{Function, Int64}, Int64, Int64, GA{Evolutionary.var"#uniformrank#246"{Evolutionary.var"#uniformrank#245#247"{Int64}}, typeof(Evolutionary.genop), typeof(Evolutionary.genop)}}}, args::Type)  
@ Base ./error.jl:157  
[2] TreeGP(pop::Int64, term::Vector{Union{Function, Real, Symbol}}, func::Vector{Function}; kwargs::Base.Iterators.Pairs{Symbol, Any, Tuple{Symbol, Symbol, Symbol}, NamedTuple{(:mindepth, :maxdepth, :optimizer), Tuple{Int64, Int64, GA{Evolutionary.var"#uniformrank#246"{Evolutionary.var"#uniformrank#245#247"{Int64}}, typeof(Evolutionary.genop), typeof(Evolutionary.genop)}}}})  
@ Evolutionary ~/.julia/packages/Evolutionary/65hL6/src/gp.jl:43  
[3] top-level scope  
@ In[15]:2  
[4] eval  
@ ./boot.jl:360 [inlined]  
[5] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base ./loading.jl:1116

---

<div class="post-metadata">

**Author:** ![wildart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wildart/32/57_2.png) [@wildart](https://discourse.julialang.org/u/wildart)\
**Post date:** [February 5, 2022, 10:51pm UTC](https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769/2 "2022-02-05T22:51:31Z")

</div>

The example is outdated, `TreeGP` constructor has different parameters now. There is no `optimizer` parameter anymore. Mutation operators are specified as `TreeGP` parameters, [see here](https://wildart.github.io/Evolutionary.jl/dev/gp/).

You should run

```julia
julia> res = Evolutionary.optimize(fitobj,
           TreeGP(25, Terminal[syms...], funcs,
               mindepth=1,
               maxdepth=3,
               selection = tournament(3),
               mutationRate = 0.5,
               crossoverRate = 0.05
           )
       )

 * Status: success

 * Candidate solution
    Minimizer: (+)(x, (+)((*)(x, x), x))
    Minimum: 0.27305007890318983
    Iterations: 117

 * Found with
    Algorithm: TreeGP[P=25,Parameter[x],Function[-, *, /, +]]

 * Convergence measures
    |f(x) - f(x')| = 0.0 ≤ 1.0e-5

 * Work counters
    Seconds run: 0.5655 (vs limit Inf)
    Iterations: 117
    f(x) calls: 2950

```

---

<div class="post-metadata">

**Author:** ![sabel](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sabel](https://discourse.julialang.org/u/sabel)\
**Post date:** [February 6, 2022, 6:38pm UTC](https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769/3 "2022-02-06T18:38:36Z")

</div>

Fantastic - thanks so much that worked perfectly. I am inspired to use this.!

---

<div class="post-metadata">

**Author:** ![sabel](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sabel](https://discourse.julialang.org/u/sabel)\
**Post date:** [February 7, 2022, 10:26am UTC](https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769/4 "2022-02-07T10:26:25Z")

</div>

Hi again - hmmm, it runs but doesn’t work. It never finds for example x_x_x+x but declares an early success with a function x (I pasted the output below). I have not yet got it to output x^3+x even if I increase maximum tree depth.

I am using Julia 1.6.5 - I assume this is OK? If so, is there code for your result so I can see if I am doing something wrong. Or a repository of other examples would be great!

By the way I defined data in like this …  
Random.seed!(42);  
d, n = 1, 20  
Nguyen1(x) = x_x_x + x  
xs = sort!(2\*rand(n).-1)

Cheers  
Steve

here’s my output ==\>

- Status: success

- Candidate solution  
Minimizer: (-)(x, (\*)((-)(x, x), (/)(x, x)))  
Minimum: 0.41449217882225364  
Iterations: 700

- Found with  
Algorithm: TreeGP[P=25,Parameter,Function[-, /, \*, +]]

- Convergence measures  
|f(x) - f(x’)| = 0.0 ≤ 1.0e-5

- Work counters  
Seconds run: 1.83 (vs limit Inf)  
Iterations: 700  
f(x) calls: 17525

---

<div class="post-metadata">

**Author:** ![wildart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wildart/32/57_2.png) [@wildart](https://discourse.julialang.org/u/wildart)\
**Post date:** [February 9, 2022, 6:37am UTC](https://discourse.julialang.org/t/evolutionary-treegp-distribution-version-falls-over/75769/5 "2022-02-09T06:37:33Z")

</div>

> [@sabel](#):
>
> Hi again - hmmm, it runs but doesn’t work. It never finds for example x _x_ x+x but declares an early success with a function x (I pasted the output below). I have not yet got it to output x^3+x even if I increase maximum tree depth.

You need to play around with mutation operations rates. Usually, for GP, you would use a high crossover rate and a low mutation rate with a relatively sized random population. If the solution is still subpar, and optimization terminates earlier, try to increase mutation rate.

There is one caveat, the default (and only) crossover operation constantly increases depth of the expression tree (the tree depth parameter is not applied). You might want to use `simplify` parameter to counteract unchecked growth, but no guaranties. The better crossovers are needed.

> <https://github.com/wildart/Evolutionary.jl/blob/81d8a7257624a8329424a9678845f4e72c1b3ed1/test/gp.jl#L125-L133>
