|
1 | 1 | @testset "Genetic Programming" begin |
2 | | - Random.seed!(9874984737486); |
| 2 | + rng = StableRNG(42) |
| 3 | + |
3 | 4 | pop = 10 |
4 | 5 | terms = Terminal[:x, :y, rand] |
5 | 6 | funcs = Function[+,-,*,/] |
|
16 | 17 | @test_skip summary(t) == "TreeGP[P=10,Parameter[x,y],Function[*, +, /, -]]" |
17 | 18 |
|
18 | 19 | # population initialization |
19 | | - popexp = Evolutionary.initial_population(t); |
| 20 | + popexp = Evolutionary.initial_population(t, rng=rng); |
20 | 21 | @test length(popexp) == pop |
21 | 22 | popexp = Evolutionary.initial_population(t, :(x + 1)); |
22 | 23 | @test popexp[1] == :(x + 1) |
23 | 24 |
|
24 | 25 | # recursive helper functions |
25 | | - Random.seed!(9874984737482) |
26 | | - gt = rand(TreeGP(pop, terms, funcs, maxdepth=2, initialization=:grow), 3) |
| 26 | + Random.seed!(rng, 1) |
| 27 | + gt = rand(rng, TreeGP(pop, terms, funcs, maxdepth=2, initialization=:grow), 3) |
27 | 28 | @test Evolutionary.nodes(gt) < 15 |
28 | 29 | @test Evolutionary.height(gt) <= 3 |
29 | 30 | @test length(gt) < 15 |
30 | | - ft = rand(TreeGP(pop, terms, funcs, maxdepth=2, initialization=:full), 3) |
| 31 | + ft = rand(rng, TreeGP(pop, terms, funcs, maxdepth=2, initialization=:full), 3) |
31 | 32 | @test Evolutionary.nodes(ft) == 15 |
32 | 33 | @test Evolutionary.height(ft) == 3 |
33 | 34 | @test length(ft) == 15 |
|
57 | 58 |
|
58 | 59 | # evaluation |
59 | 60 | ex = Expr(:call, +, 1, :x) |> Evolutionary.Expression |
60 | | - xs = rand(10) |
| 61 | + xs = rand(rng, 10) |
61 | 62 | @test ex(xs[1]) == xs[1]+1 |
62 | 63 | @test ex.(xs) == xs.+1 |
63 | 64 | io = IOBuffer() |
|
67 | 68 | depth = 5 |
68 | 69 | fitfun(x) = x*x + x + 1.0 |
69 | 70 | function fitobj(expr) |
70 | | - rg = -5.0:0.5:5.0 |
| 71 | + rg = -5.0:0.1:5.0 |
71 | 72 | ex = Evolutionary.Expression(expr) |
72 | | - sum(v->isnan(v) ? 1.0 : v, abs2.(fitfun.(rg) - ex.(rg)) )/length(rg) |> sqrt |
| 73 | + sum(v->isnan(v) ? 1.0 : v, abs2.(fitfun.(rg) - ex.(rg)) )/length(rg) |
73 | 74 | end |
74 | 75 |
|
75 | | - Random.seed!(9874984737426); |
76 | 76 | res = Evolutionary.optimize(fitobj, |
77 | 77 | TreeGP(50, Terminal[:x, randn], Function[+,-,*,Evolutionary.pdiv], |
78 | 78 | mindepth=1, |
79 | 79 | maxdepth=depth, |
80 | 80 | optimizer = GA( |
81 | | - selection = tournament(5), |
| 81 | + selection = tournament(4), |
82 | 82 | ɛ = 0.1, |
83 | | - mutationRate = 0.85, |
| 83 | + mutationRate = 0.8, |
84 | 84 | crossoverRate = 0.2, |
85 | 85 | ), |
86 | | - ) |
| 86 | + ), Evolutionary.Options(rng=StableRNG(1)) |
87 | 87 | ) |
88 | | - @test minimum(res) < 1.1 |
| 88 | + @test minimum(res) < 1 |
89 | 89 |
|
90 | 90 | end |
0 commit comments