replace_chromosome.m
Upload User: hjzhipin
Upload Date: 2020-09-23
Package Size: 376k
Code Size: 3k
Development Platform:

Others

  1. function f  = replace_chromosome(intermediate_chromosome,pro,pop)
  2. %% replace_chromosome(intermediate_chromosome,pro,pop)
  3. % This function replaces the chromosomes based on rank and crowding
  4. % distance. Initially until the population size is reached each front is
  5. % added one by one until addition of a complete front which results in
  6. % exceeding the population size. At this point the chromosomes in that
  7. % front is added subsequently to the population based on crowding distance.
  8. % %     Copyright (C) 2009 Aravind Seshadri % %     This program is free software: you can redistribute it and/or modify %     it under the terms of the GNU General Public License as published by %     the Free Software Foundation, either version 3 of the License, or %     (at your option) any later version. %  %     This program is distributed in the hope that it will be useful, %     but WITHOUT ANY WARRANTY; without even the implied warranty of %     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the %     GNU General Public License for more details. %  %     You should have received a copy of the GNU General Public License %     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  9. [N,V] = size(intermediate_chromosome);
  10. switch pro
  11.         case 1
  12.         M = 2;
  13.         V = 6;
  14.     case 2
  15.         M = 3;
  16.         V = 12;
  17. end
  18. % Get the index for the population sort based on the rank
  19. [temp,index] = sort(intermediate_chromosome(:,M + V + 1));
  20. % Now sort the individuals based on the index
  21. for i = 1 : N
  22.     sorted_chromosome(i,:) = intermediate_chromosome(index(i),:);
  23. end
  24. % Find the maximum rank in the current population
  25. max_rank = max(intermediate_chromosome(:,M + V + 1));
  26. % Start adding each front based on rank and crowing distance until the
  27. % whole population is filled.
  28. previous_index = 0;
  29. for i = 1 : max_rank
  30.     current_index = max(find(sorted_chromosome(:,M + V + 1) == i));
  31.     if current_index > pop
  32.         remaining = pop - previous_index;
  33.         temp_pop = ...
  34.             sorted_chromosome(previous_index + 1 : current_index, :);
  35.         [temp_sort,temp_sort_index] = ...
  36.             sort(temp_pop(:, M + V + 2),'descend');
  37.         for j = 1 : remaining
  38.             f(previous_index + j,:) = temp_pop(temp_sort_index(j),:);
  39.         end
  40.         return;
  41.     elseif current_index < pop
  42.         f(previous_index + 1 : current_index, :) = ...
  43.             sorted_chromosome(previous_index + 1 : current_index, :);
  44.     else
  45.         f(previous_index + 1 : current_index, :) = ...
  46.             sorted_chromosome(previous_index + 1 : current_index, :);
  47.         return;
  48.     end
  49.     previous_index = current_index;
  50. end