Skip to content

Commit

Permalink
Merge pull request #8 from Nikola-Mircic/main
Browse files Browse the repository at this point in the history
Update branch
  • Loading branch information
Nikola-Mircic authored Jan 3, 2024
2 parents 8c04e6b + cba42fd commit 72f20d6
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.vscode/
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "FTN_PSI_PA"
uuid = "0afddabe-dfaf-4480-bf01-29bfefedff8e"
version = "0.1.2"
authors = ["Nikola-Mircic"]

[deps]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
48 changes: 0 additions & 48 deletions genetic_algorithm/genetic_algorithm.jl

This file was deleted.

8 changes: 8 additions & 0 deletions src/FTN_PSI_PA.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module FTN_PSI_PA

import Printf

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

end
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
include("genetic_algorithm.jl")
module GeneticAlgorithm

#=
| Example:
| Find four numbers that add up to 143.
|
| x1 + x2 + x3 + x4 = 143
| x1=?, x2=?, x3=?, x4=?
=#
include("crossover.jl")

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

elitePercent = 0.15
mutationPercent = 0.2
numOfIterations = 100
function geneticAlgorithm(population::Vector{Entity}, elitePercent::Float64, mutationPercent::Float64, crossoverFunc!::Function, iter::Int)
bestFitnes = []

updatePopulationFitness!(population, fitFunction)

population = generatePopulation(populationSize, genesLength, minGene, maxGene)
push!(bestFitnes, population[1].fitness)

num_gen, best = geneticAlgorithm(population,
elitePercent,
mutationPercent,
makeCrossoverFunc([1;3]),
numOfIterations)
while !shouldStop(iter, bestFitnes)
n = length(population)

println("$(num_gen) -> $best")
eliteNumber = Int(trunc(elitePercent*n));

eliteNumber = eliteNumber + (n-eliteNumber)%2

elite = deepcopy(population[1:eliteNumber])

population = crossover!(population[eliteNumber+1:end], crossoverFunc!)

mutatePopulation!(population, mutationPercent)

population = [population; elite]

updatePopulationFitness!(population, fitFunction)

push!(bestFitnes, population[1].fitness)
end

return length(bestFitnes), population[1]
end

function shouldStop(iter::Int, bestFitnes)
n = length(bestFitnes)

if bestFitnes[n] < 0.01
return true
elseif n > iter
return true
elseif n > 3
return abs(bestFitnes[n-3] - bestFitnes[n]) < 0.1
end

return false
end

# ANALYZATION

Expand Down Expand Up @@ -130,14 +150,13 @@ function printResult(values, results)
end
end

println("Elite percentage analysis: \n")
printResult(collect(0.0:0.05:0.5), analyzeElitePercentage(collect(0.0:0.05:0.5)))

println("\nMutation percentage analysis: \n")
printResult(collect(0.0:0.05:0.5), analyzeMutationPercentage(collect(0.0:0.05:0.5)))
export geneticAlgorithm
export getAverage
export analyzeElitePercentage
export analyzeMutationPercentage
export analyzeNumberOfIteration
export analyzePopulationSize

println("\nPopulation size analysis: \n")
printResult(collect(50:10:150), analyzePopulationSize(collect(50:10:150)))
export printResult

println("\nNumber of iterations analysis: \n")
printResult(collect(50:10:150), analyzeNumberOfIteration(collect(50:10:150)))
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ end

Base.show(io::IO, e::Entity) = print(io, "{$(e.fitness)}[$(join(e.genes, ","))]")


function fitFunction(e::Entity)
return abs(sum(e.genes) - 143)
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 72f20d6

Please sign in to comment.