Skip to content

Instantly share code, notes, and snippets.

@machkouroke
Created April 14, 2023 22:08
Show Gist options
  • Save machkouroke/320d162c9c2d6b0e8fa8525ee73249d0 to your computer and use it in GitHub Desktop.
Save machkouroke/320d162c9c2d6b0e8fa8525ee73249d0 to your computer and use it in GitHub Desktop.
function crossover(parents::Vector{Chromosome})::Chromosome
genes_limit::Vector{Int64} = [rand(1:length(parents[1])) for _ in 1:2] |> sort
child::Chromosome = Chromosome([Queen(i, 0) for i in 1:length(parents[1])])
child[genes_limit[1]:genes_limit[2]] = parents[1][genes_limit[1]:genes_limit[2]]
other_queen::Vector{Queen} = [queen for queen in parents[2] if queen.y ∉ getproperty.(child, :y)]
for index in setdiff(1:length(child), genes_limit[1]:genes_limit[2])
child[index] = Queen(index, other_queen[1].y)
other_queen = other_queen[2:end]
end
return child
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment