Skip to content

Commit

Permalink
Add test cases for WeightVectorNeighborhood initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed Oct 3, 2024
1 parent ab3dc65 commit 8fecb1f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/MetaJul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export compare
include("util/comparator.jl")

export Neighborhood
export minFastSort
export minFastSort!
include("util/neighborhood.jl")

export NonDominatedArchive, CrowdingDistanceArchive
Expand Down
10 changes: 5 additions & 5 deletions src/component/evolutionaryAlgorithm/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ end
ARCHIVE
end

struct PopulationAndNeighborhoodSelection{T} <: Selection
struct PopulationAndNeighborhoodSelection <: Selection
matingPoolSize::Int
solutionIndexGenerator::Function
neighborhood::Neighborhood{T}
neighborhood::Neighborhood
neighborhoodSelectionProbability::Float64
selectCurrentSolution::Bool
selectionOperator::SelectionOperator
Expand All @@ -64,15 +64,15 @@ end
# Constructor for PopulationAndNeighborhoodSelection
function PopulationAndNeighborhoodSelection(matingPoolSize::Int,
solutionIndexGenerator::Function,
neighborhood::Neighborhood{T},
neighborhood::Neighborhood,
neighborhoodSelectionProbability::Float64,
selectCurrentSolution::Bool) where T
selectCurrentSolution::Bool)
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
function select(selection::PopulationAndNeighborhoodSelection, solutionList::Vector{T})::Vector{T} where T
matingPool = Vector{T}()
randomValue = rand()

Expand Down
24 changes: 13 additions & 11 deletions src/util/neighborhood.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
abstract type Neighborhood{S} end
abstract type Neighborhood end

# ChatGPT code
function minFastSort(x::Vector{Float64}, idx::Vector{Int}, numberOfWeightVectors::Int, neighborhoodSize::Int)
function minFastSort!(x::Vector{Float64}, idx::Vector{Int}, numberOfWeightVectors::Int, neighborhoodSize::Int)
for i in 1:neighborhoodSize
for j in (i+1):numberOfWeightVectors
if x[i] > x[j]
Expand All @@ -26,33 +26,35 @@ function minFastSort!(x::Vector{Float64}, idx::Vector{Int}, n::Int, m::Int)
x[i], x[j] = x[j], x[i]
idx[i], idx[j] = idx[j], idx[i]
end
end
end
end
end
=#

struct WeightVectorNeighborhood{S <: Solution} <: Neighborhood{S}
struct WeightVectorNeighborhood <: Neighborhood
numberOfWeightVectors::Int
weightVectorSize::Int
neighborhood::Array{Int,2}
weightVector::Array{Float64,2}
neighborhoodSize::Int
neighborhood::Array
weightVector::Array{Float64,2}

function WeightVectorNeighborhood(numberOfWeightVectors::Int, neighborhoodSize::Int)
weightVectorSize = 2

neighborhood = Array{Int,2}(undef, numberOfWeightVectors, neighborhoodSize)
weightVector = Array{Float64,2}(undef, numberOfWeightVectors, weightVectorSize)
neighborhood = Array{Int}(undef, numberOfWeightVectors, neighborhoodSize)
weightVector = Array{Float64}(undef, numberOfWeightVectors, weightVectorSize)

for n in 1:numberOfWeightVectors
a = 1.0 * (n - 1) / (numberOfWeightVectors - 1)
weightVector[n, 1] = a
weightVector[n, 2] = 1 - a
end

weightVectorNeighborhood = new{S}(numberOfWeightVectors, weightVectorSize, neighborhood, weightVector, neighborhoodSize)

weightVectorNeighborhood = new(numberOfWeightVectors, weightVectorSize, neighborhoodSize, neighborhood, weightVector)
initializeNeighborhood(weightVectorNeighborhood)
return obj

return weightVectorNeighborhood
end
end

Expand All @@ -79,7 +81,7 @@ function initializeNeighborhood(weightVectorNeighborhood::WeightVectorNeighborho
end
end

function getNeighbors(weightVectorNeighborhood::WeightVectorNeighborhood, solutionList::Vector{S}, solutionIndex::Int) where {S}
function getNeighbors(weightVectorNeighborhood::WeightVectorNeighborhood, solutionList::Vector{S}, solutionIndex::Int) where {S <: Solution}
neighbourSolutions = Vector{S}()
for neighborIndex in weightVectorNeighborhood.neighborhood[solutionIndex, :]
push!(neighbourSolutions, solutionList[neighborIndex])
Expand Down
30 changes: 17 additions & 13 deletions test/util/neighborhoodTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
idx = [1, 2]
n = length(x)
m = 2
minFastSort(x, idx, n, m)
minFastSort!(x, idx, n, m)
@test x == [1.0, 2.0]
@test idx == [2, 1]

Expand All @@ -14,23 +14,27 @@
idx = [1, 2, 3]
n = length(x)
m = 3
minFastSort(x, idx, n, m)
minFastSort!(x, idx, n, m)
@test x == [1.0, 2.0, 3.0]
@test idx == [1, 2, 3]
end


weightVectorNeighborhood = WeightVectorNeighborhood{ContinuousSolution{Float64}}(100, 20)

function ()
dimension = 4
point = ArrayPoint(dimension)

return values(point) == zeros(dimension)
end

numberOfWeightVectors = 100
neightborhoodSize = 20
weightVectorNeighborhood = WeightVectorNeighborhood(numberOfWeightVectors, neightborhoodSize)

@testset "WeightVectorNeighborhood initialization Tests" begin
#@test constructorCreatesAnIdealPointWithAGivenDimension()

@test weightVectorNeighborhood.numberOfWeightVectors == numberOfWeightVectors
@test weightVectorNeighborhood.neighborhoodSize == neightborhoodSize
@test weightVectorNeighborhood.weightVectorSize == 2
@test weightVectorNeighborhood.weightVector[1, 1] == 0.0
@test weightVectorNeighborhood.weightVector[1, 2] == 1.0
@test weightVectorNeighborhood.weightVector[2, 1] == 0.0101010101010101010101
@test weightVectorNeighborhood.weightVector[2, 2] == 0.989898989898989898
@test weightVectorNeighborhood.weightVector[100, 1] == 1.0
@test weightVectorNeighborhood.weightVector[100, 2] == 0.0

@test weightVectorNeighborhood.neighborhood[1,:] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
@test weightVectorNeighborhood.neighborhood[70,:] == [70, 71, 69, 72, 68, 73, 67, 74, 66, 65, 75, 76, 64, 77, 63, 78, 62, 79, 61, 80]
end

0 comments on commit 8fecb1f

Please sign in to comment.