Skip to content

Commit

Permalink
Merge pull request #11 from Nikola-Mircic/genetic_algorithm
Browse files Browse the repository at this point in the history
Genetic algorithm
  • Loading branch information
Nikola-Mircic authored Jan 4, 2024
2 parents 289e709 + f9727dc commit 539f0d6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/FTN_PSI_PA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module FTN_PSI_PA

import Printf

export GeneticAlgorithm

include("genetic_algorithm/genetic_algorithm.jl")
include("pso/pso.jl")

Expand Down
25 changes: 15 additions & 10 deletions src/genetic_algorithm/genetic_algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ module GeneticAlgorithm

include("crossover.jl")

export makeCrossoverFunc
export generatePopulation

export fitFunction

export geneticAlgorithm

export getAverage
export analyzeElitePercentage
export analyzeMutationPercentage
export analyzeNumberOfIteration
export analyzePopulationSize

export printResult

function geneticAlgorithm(population::Vector{Entity}, elitePercent::Float64, mutationPercent::Float64, crossoverFunc!::Function, iter::Int)
bestFitnes = []
Expand Down Expand Up @@ -58,7 +72,7 @@ function getAverage(population::Vector{Entity}, elitePercent::Float64, mutationP
elitePercent,
mutationPercent,
crossoverFunc!,
numOfIterations)
iter)
avg_gen += gen_i
avg_best += best_i.fitness
end
Expand Down Expand Up @@ -150,13 +164,4 @@ function printResult(values, results)
end
end

export geneticAlgorithm
export getAverage
export analyzeElitePercentage
export analyzeMutationPercentage
export analyzeNumberOfIteration
export analyzePopulationSize

export printResult

end
29 changes: 29 additions & 0 deletions test/genetic_algorithm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FTN_PSI_PA.GeneticAlgorithm

@testset "GeneticAlgorithm" begin
#=
| Find four numbers that add up to 143.
|
| x1 + x2 + x3 + x4 = 143
| x1=?, x2=?, x3=?, x4=?
=#

populationSize = 100
genesLength = 4
minGene = 0
maxGene = 143

elitePercent = 0.15
mutationPercent = 0.2
numOfIterations = 100

population = generatePopulation(populationSize, genesLength, minGene, maxGene)

num_gen, best = getAverage(population,
elitePercent,
mutationPercent,
makeCrossoverFunc([1;3]),
numOfIterations)

@test best < 0.1 # Test if the absolute error is smaller than 0.1
end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FTN_PSI_PA
using Test

@testset "Tests" begin
a = 5
@test a == 5
include("genetic_algorithm.jl")
end

0 comments on commit 539f0d6

Please sign in to comment.