Last active
April 14, 2023 21:52
-
-
Save machkouroke/9cd49367b993af2527d95c4e9528cad1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Match | |
function roulette(popsize::Int64, n_parents::Int64, population::Vector{Chromosome})::Vector{Int64} | |
evaluations::Vector{Float64} = [fitness(x) for x in population] | |
evaluations = evaluations ./ sum(evaluations) | |
return sample(1:popsize, Weights(vec(evaluations)), n_parents) | |
end | |
function select(popsize::Int64, n_parents::Int64, population::Vector{Chromosome}; method::String="random")::Vector{Int64} | |
@match method begin | |
"random" => randperm(popsize)[1:n_parents] | |
"roulette" => roulette(popsize, n_parents, population) | |
_ => randperm(popsize)[1:n_parents] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment