Skip to content

Commit

Permalink
Added PopulationAndNeighborhoodSelection.
Browse files Browse the repository at this point in the history
  • Loading branch information
arlym7b committed Sep 10, 2024
1 parent af55f52 commit c7d186e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/component/evolutionaryAlgorithm/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,57 @@ function select(selection::BinaryTournamentSelection, solutions::Vector{S})::Vec
return [select(solutions, selectionOperator) for _ in range(1, matingPoolSize)]
end

# Enum for NeighborType
@enum NeighborType begin
NEIGHBOR
POPULATION
ARCHIVE
end

abstract type Neighborhood{S} end

struct PopulationAndNeighborhoodSelection{T} <: Selection
matingPoolSize::Int
solutionIndexGenerator::Function
neighborhood::Neighborhood{T}
neighborhoodSelectionProbability::Float64
selectCurrentSolution::Bool
selectionOperator::SelectionOperator
end

# Constructor for PopulationAndNeighborhoodSelection
function PopulationAndNeighborhoodSelection(matingPoolSize::Int,
solutionIndexGenerator::Function,
neighborhood::Neighborhood{T},
neighborhoodSelectionProbability::Float64,
selectCurrentSolution::Bool) where T
selectionOperator = NaryRandomSelection(selectCurrentSolution ? matingPoolSize - 1 : matingPoolSize)
return PopulationAndNeighborhoodSelection(matingPoolSize, solutionIndexGenerator, neighborhood, neighborhoodSelectionProbability, selectCurrentSolution, selectionOperator)
end

# Selection method for PopulationAndNeighborhoodSelection
function select(selection::PopulationAndNeighborhoodSelection{T}, solutionList::Vector{T})::Vector{T} where T
matingPool = Vector{T}()
randomValue = rand()

if randomValue < selection.neighborhoodSelectionProbability
# Select from neighborhood
neighborType = NEIGHBOR
neighbors = selection.neighborhood.getNeighbors(solutionList, selection.solutionIndexGenerator())
matingPool = selection.selectionOperator.execute(neighbors, selectionOperator)
else
# Select from population
neighborType = POPULATION
matingPool = selection.selectionOperator.execute(solutionList, selection.selectionOperator)
end

if selection.selectCurrentSolution
# Add the current solution to the mating pool
currentSolution = solutionList[selection.solutionIndexGenerator()]
push!(matingPool, currentSolution)
end

@assert length(matingPool) == selection.matingPoolSize string("The mating pool size ", length(matingPool), " is not equal to the required size ", selection.matingPoolSize)

return matingPool
end

0 comments on commit c7d186e

Please sign in to comment.