Skip to content

Commit aa957c9

Browse files
committed
add test
1 parent 412af81 commit aa957c9

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

test/Entity_test.jl

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
include("../src/Entities.jl")
2+
3+
using .GepEntities
4+
using DynamicExpressions
5+
using Test
6+
using OrderedCollections
7+
using Random
8+
operators = OperatorEnum(; binary_operators=[+, -, *, /], unary_operators=[sqrt])
9+
10+
# Helper function to create test toolbox
11+
function create_test_toolbox()
12+
13+
14+
symbols = OrderedDict{Int8,Int8}(
15+
1 => 2, # Addition (+)
16+
2 => 2, # Multiplication (*)
17+
3 => 1, # SQRT
18+
4 => 0, # Variable x
19+
5 => 0 # Constant 1.0
20+
)
21+
22+
callbacks = Dict{Int8,Function}(
23+
1 => +,
24+
2 => *,
25+
3 => sqrt
26+
)
27+
28+
nodes = OrderedDict{Int8,Any}(
29+
4 => Node{Float64}(feature=1),
30+
5 => 1.0
31+
)
32+
33+
gep_probs = Dict{String,AbstractFloat}(
34+
"mutation_prob" => 0.2,
35+
"mutation_rate" => 0.1,
36+
"inversion_prob" => 0.1,
37+
"one_point_cross_over_prob" => 0.3,
38+
"two_point_cross_over_prob" => 0.3,
39+
"dominant_fusion_prob" => 0.2,
40+
"rezessiv_fusion_prob" => 0.2,
41+
"fusion_prob" => 0.2,
42+
"fusion_rate" => 0.1,
43+
"rezessiv_fusion_rate" => 0.1
44+
)
45+
46+
return Toolbox(2, 3, symbols, Int8[1], callbacks, nodes, gep_probs)
47+
end
48+
49+
@testset "SymbolicEntities Tests" begin
50+
@testset "Basic Setup" begin
51+
toolbox = create_test_toolbox()
52+
53+
@test toolbox.gene_count == 2
54+
@test toolbox.head_len == 3
55+
@test length(toolbox.headsyms) == 2 # + and *
56+
@test length(toolbox.unary_syms) == 1 # square
57+
@test length(toolbox.tailsyms) == 2 # x and 1.0
58+
end
59+
60+
@testset "Chromosome Creation" begin
61+
toolbox = create_test_toolbox()
62+
Random.seed!(42) # For reproducibility
63+
chromosome = generate_chromosome(toolbox)
64+
65+
@test chromosome isa Chromosome
66+
@test length(chromosome.genes) == (toolbox.gene_count - 1 +
67+
toolbox.gene_count * (2 * toolbox.head_len + 1))
68+
@test chromosome.compiled == true
69+
@test chromosome.dimension_homogene == false
70+
@test isnan(chromosome.fitness)
71+
end
72+
73+
@testset "Function Compilation" begin
74+
toolbox = create_test_toolbox()
75+
Random.seed!(42)
76+
chromosome = generate_chromosome(toolbox)
77+
compile_expression!(chromosome, force_compile=true)
78+
79+
@test chromosome.compiled == true
80+
@test chromosome.compiled_function !== nothing
81+
82+
# Test function evaluation
83+
if !isnothing(chromosome.compiled_function)
84+
result = chromosome.compiled_function([1.0], operators)
85+
@test typeof(result[1]) <: Real
86+
end
87+
end
88+
89+
@testset "Genetic Operators" begin
90+
toolbox = create_test_toolbox()
91+
Random.seed!(42)
92+
chromosome1 = generate_chromosome(toolbox)
93+
chromosome2 = generate_chromosome(toolbox)
94+
95+
genes1_original = copy(chromosome1.genes)
96+
genes2_original = copy(chromosome2.genes)
97+
98+
@testset "One Point Crossover" begin
99+
c1, c2 = replicate(chromosome1, chromosome2, toolbox)
100+
gene_one_point_cross_over!(c1, c2)
101+
@test c1.genes != genes1_original || c2.genes != genes2_original
102+
end
103+
104+
@testset "Mutation" begin
105+
c1 = Chromosome(copy(genes1_original), toolbox)
106+
gene_mutation!(c1, 1.0) # Force mutation
107+
@test c1.genes != genes1_original
108+
end
109+
110+
@testset "Gene Fusion" begin
111+
c1, c2 = replicate(chromosome1, chromosome2, toolbox)
112+
gene_fussion!(c1, c2, 1.0) # Force fusion
113+
@test c1.genes != genes1_original || c2.genes != genes2_original
114+
end
115+
116+
@testset "Inversion" begin
117+
pos_11 = toolbox.gene_count
118+
pos_12 = toolbox.gene_count+toolbox.head_len-1
119+
pos_21 = pos_12 + toolbox.head_len+2
120+
pos_22 = pos_21 + toolbox.head_len-1
121+
122+
c1 = Chromosome(copy(genes1_original), toolbox)
123+
124+
125+
initial_head = copy(c1.genes[pos_11:pos_12])
126+
initial_head2 = copy(c1.genes[pos_21:pos_22])
127+
128+
@show initial_head
129+
@show initial_head2
130+
131+
gene_inversion!(c1)
132+
@test c1.genes[toolbox.gene_count+1:toolbox.head_len+toolbox.gene_count] != initial_head || initial_head2 != c1.genes[9:11]
133+
end
134+
end
135+
136+
@testset "Population Generation" begin
137+
toolbox = create_test_toolbox()
138+
population_size = 10
139+
population = generate_population(population_size, toolbox)
140+
141+
@test length(population) == population_size
142+
@test all(x -> x isa Chromosome, population)
143+
@test length(unique([p.genes for p in population])) == population_size
144+
end
145+
146+
@testset "History Recording" begin
147+
toolbox = create_test_toolbox()
148+
population_size = 10
149+
population = generate_population(population_size, toolbox)
150+
151+
for (i, chromo) in enumerate(population)
152+
set_fitness!(chromo, Float64(i))
153+
@test fitness(chromo) == Float64(i)
154+
end
155+
156+
sorted_pop = sort(population, by=fitness)
157+
@test issorted([fitness(c) for c in sorted_pop])
158+
end
159+
end

0 commit comments

Comments
 (0)