Skip to content

Instantly share code, notes, and snippets.

@machkouroke
Last active April 14, 2023 21:52
Show Gist options
  • Save machkouroke/9cd49367b993af2527d95c4e9528cad1 to your computer and use it in GitHub Desktop.
Save machkouroke/9cd49367b993af2527d95c4e9528cad1 to your computer and use it in GitHub Desktop.
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