title: Rank Selection in GA?
body: <p>I have implemented <code>Roulette wheel selection</code> in <code>GA</code>.<br></p>
  
  <pre><code>   TotalFitness=sum(Fitness);
      ProbSelection=zeros(PopLength,1);
      CumProb=zeros(PopLength,1);
  
      for i=1:PopLength
          ProbSelection(i)=Fitness(i)/TotalFitness;
          if i==1
              CumProb(i)=ProbSelection(i);
          else
              CumProb(i)=CumProb(i-1)+ProbSelection(i);
          end
      end
  
      SelectInd=rand(PopLength,1);
  
      for i=1:PopLength
          flag=0;
          for j=1:PopLength
              if(CumProb(j)&lt;SelectInd(i) &amp;&amp; CumProb(j+1)&gt;=SelectInd(i))
                  SelectedPop(i,1:IndLength)=CurrentPop(j+1,1:IndLength);
                  flag=1;
                  break;
              end
          end
          if(flag==0)
              SelectedPop(i,1:IndLength)=CurrentPop(1,1:IndLength);
          end
      end
  </code></pre>
  
  <p><strong>Now i was trying to implement <code>rank selection</code> in <code>GA</code>. I learned that:</strong></p>
  
  <ul>
  <li><p>Rank selection first ranks the population and then every chromosome receives fitness from this ranking. </p></li>
  <li><p>The worst will have fitness 1, second worst 2 etc. and the best will have fitness N (number of chromosomes in population). <br></p></li>
  </ul>
  
  <p><strong>I saw these <a href="https://stackoverflow.com/questions/9462350/differences-between-roulette-wheel-selection-and-rank-based-selection">link1</a> and <a href="https://stackoverflow.com/questions/20290831/how-to-perform-rank-based-selection-in-a-genetic-algorithm">link2</a> and what i understood is that:</strong></p>
  
  <ol>
  <li><p>First i will sort the Fitness value of the Population.</p></li>
  <li><p>Then if the Population number is 10 then i will give the probability of selection to the Population like 0.1,0.2,0.3,...,1.0 .</p></li>
  <li>Then i will calculate cumulative Fitness like roulette wheel.</li>
  <li>And the next steps is same as roulette wheel.</li>
  </ol>
  
  <p><strong>My Implementation:</strong></p>
  
  <pre><code>  NewFitness=sort(Fitness);
      NewPop=round(rand(PopLength,IndLength));
  
      for i=1:PopLength
          for j=1:PopLength
              if(NewFitness(i)==Fitness(j))
                  NewPop(i,1:IndLength)=CurrentPop(j,1:IndLength);
                  break;
              end
          end
      end
      CurrentPop=NewPop;
  
      ProbSelection=zeros(PopLength,1);
      CumProb=zeros(PopLength,1);
  
      for i=1:PopLength
          ProbSelection(i)=i/PopLength;
          if i==1
              CumProb(i)=ProbSelection(i);
          else
              CumProb(i)=CumProb(i-1)+ProbSelection(i);
          end
      end
  
      SelectInd=rand(PopLength,1);
  
      for i=1:PopLength
          flag=0;
          for j=1:PopLength
              if(CumProb(j)&lt;SelectInd(i) &amp;&amp; CumProb(j+1)&gt;=SelectInd(i))
                  SelectedPop(i,1:IndLength)=CurrentPop(j+1,1:IndLength);
                  flag=1;
                  break;
              end
          end
          if(flag==0)
              SelectedPop(i,1:IndLength)=CurrentPop(1,1:IndLength);
          end
      end
  </code></pre>
  
  <p><br><br>
  <strong>Am i understanding the algo wrong?? If it is then can anyone give me any idea how to modify my roulette wheel to rank selection??</strong></p>