Created
August 22, 2017 08:15
-
-
Save doomhz/a2b0d9df94bcb9f9a943961c403cf45b to your computer and use it in GitHub Desktop.
crowding
This file contains 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
def calculate_crowding_distance(pop) | |
pop.each {|p| p[:dist] = 0.0} | |
num_obs = pop.first[:objectives].size | |
num_obs.times do |i| | |
min = pop.min{|x,y| x[:objectives][i]<=>y[:objectives][i]} | |
max = pop.max{|x,y| x[:objectives][i]<=>y[:objectives][i]} | |
rge = max[:objectives][i] - min[:objectives][i] | |
pop.first[:dist], pop.last[:dist] = 1.0/0.0, 1.0/0.0 | |
next if rge == 0.0 | |
(1...(pop.size-1)).each do |j| | |
pop[j][:dist]+=(pop[j+1][:objectives][i]-pop[j-1][:objectives][i])/rge | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment