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
Option Explicit | |
Option Base 1 | |
Function Diag(matrix() As Variant) As Variant | |
Dim i As Integer, n As Integer | |
Dim D() As Variant | |
n = UBound(matrix, 1) | |
ReDim D(n, 1) | |
For i = 1 To n | |
D(i, 1) = matrix(i, i) |
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 ProgressBars | |
using Plots | |
using Random | |
Random.seed!(1245) | |
include("utilities.jl") | |
include("plotter.jl") | |
include("solver.jl") | |
function main() | |
n::Int64 = 8 |
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
struct NqueenSolver | |
n::Int | |
pop_size::Int64 | |
n_gen::Int64 | |
n_parents::Int64 | |
n_mates::Int64 | |
selection_method::String | |
function NqueenSolver(; n::Int, pop_size::Int64, n_gen::Int64, n_mates::Int64, selection_method::String, n_parents::Int64=2) | |
new(n, pop_size, n_gen, n_parents, n_mates, selection_method) | |
end |
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
function survival(n_survivors::Int64, population::Vector{Chromosome})::Vector{Chromosome} | |
evaluations::Vector{Float64} = [fitness(x) for x in population] | |
indices_tries = sortperm(vec(evaluations), rev=true) | |
return population[indices_tries[1:n_survivors]] | |
end |
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
function eliminate_duplicates(population::Vector{Chromosome})::Vector{Chromosome} | |
return unique(population) | |
end |
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
function mutation(child::Chromosome, n::Int64)::Chromosome | |
mutated_child::Chromosome = deepcopy(child) | |
probability::Float64 = 0.8 | |
for i in 1:2 | |
if (rand()) < probability | |
swap_index::Int64 = rand(setdiff(1:n, i)) | |
mutated_child[i], mutated_child[swap_index] = Queen(i, mutated_child[swap_index].y), Queen(swap_index, mutated_child[i].y) | |
end | |
end |
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
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 |
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
function make_mates(population::Vector{Chromosome}, n_parents::Int64, n_mates::Int64; method::String="random")::Tuple | |
mates::Tuple = () | |
for _ in 1:n_mates | |
indices::Vector{Int64} = select(size(population)[1], n_parents, population; method=method) | |
mates = (mates..., (population[indices[1]], population[indices[2]])) | |
end | |
return mates | |
end |
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) |
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
function generate_solution(n::Int)::Chromosome | |
return [Queen(i, j) for (i, j) in enumerate(randperm(n))] |> Chromosome | |
end | |
function randpop(popsize::Int64, n::Int64)::Vector{Chromosome} | |
return [generate_solution(n) for _ in 1:popsize] | |
end |
NewerOlder