Skip to content

Instantly share code, notes, and snippets.

@doomhz
Created August 22, 2017 08:15
Show Gist options
  • Save doomhz/a2b0d9df94bcb9f9a943961c403cf45b to your computer and use it in GitHub Desktop.
Save doomhz/a2b0d9df94bcb9f9a943961c403cf45b to your computer and use it in GitHub Desktop.
crowding
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