Coverage Report - org.dishevelled.evolve.select.RankBasedSelection
 
Classes in this File Line Coverage Branch Coverage Complexity
RankBasedSelection
100%
23/23
100%
12/12
3.667
 
 1  
 /*
 2  
 
 3  
     dsh-evolve  Framework for evolutionary algorithms.
 4  
     Copyright (c) 2005-2013 held jointly by the individual authors.
 5  
 
 6  
     This library is free software; you can redistribute it and/or modify it
 7  
     under the terms of the GNU Lesser General Public License as published
 8  
     by the Free Software Foundation; either version 3 of the License, or (at
 9  
     your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful, but WITHOUT
 12  
     ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 13  
     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 14  
     License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public License
 17  
     along with this library;  if not, write to the Free Software Foundation,
 18  
     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 19  
 
 20  
     > http://www.fsf.org/licensing/licenses/lgpl.html
 21  
     > http://www.opensource.org/licenses/lgpl-license.php
 22  
 
 23  
 */
 24  
 package org.dishevelled.evolve.select;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 import java.util.List;
 29  
 
 30  
 import org.dishevelled.weighted.WeightedMap;
 31  
 import org.dishevelled.weighted.HashWeightedMap;
 32  
 
 33  
 import org.dishevelled.evolve.Selection;
 34  
 
 35  
 /**
 36  
  * Rank-based selection function.
 37  
  *
 38  
  * @param <I> individual type
 39  
  * @author  Michael Heuer
 40  
  */
 41  
 public final class RankBasedSelection<I>
 42  
     implements Selection<I>
 43  
 {
 44  
     /** Rank. */
 45  
     private final int rank;
 46  
 
 47  
 
 48  
     /**
 49  
      * Create a new rank based selection function with
 50  
      * the specified rank.
 51  
      *
 52  
      * @param rank rank for this rank based selection function,
 53  
      *    must be <code>&gt;= 1</code>
 54  
      */
 55  
     public RankBasedSelection(final int rank)
 56  14
     {
 57  14
         if (rank < 1)
 58  
         {
 59  3
             throw new IllegalArgumentException("rank must be greater than or equal to one");
 60  
         }
 61  11
         this.rank = rank;
 62  11
     }
 63  
 
 64  
 
 65  
     /**
 66  
      * Return the rank for this rank based selection function.
 67  
      *
 68  
      * @return the rank for this rank based selection function
 69  
      */
 70  
     public int getRank()
 71  
     {
 72  1
         return rank;
 73  
     }
 74  
 
 75  
     /** {@inheritDoc} */
 76  
     public Collection<I> select(final Collection<I> population,
 77  
                                 final WeightedMap<I> scores)
 78  
     {
 79  6
         if (scores.totalWeight() == 0.0d)
 80  
         {
 81  1
             throw new IllegalStateException("scores total weight must be greater than zero");
 82  
         }
 83  5
         int size = population.size();
 84  5
         List<I> selected = new ArrayList<I>(size);
 85  
 
 86  
         // create intermediate map of those ranked at or above rank
 87  5
         WeightedMap<I> intermediate = new HashWeightedMap<I>();
 88  5
         for (I i : scores.keySet())
 89  
         {
 90  303
             if (scores.rank(i) <= rank)
 91  
             {
 92  
                 // temporarily set fitness score to 0.0d
 93  105
                 intermediate.put(i, Double.valueOf(0.0d));
 94  
             }
 95  303
         }
 96  
 
 97  
         // update fitness based on size of intermediate map
 98  5
         int intermediateSize = intermediate.size();
 99  5
         for (I i : intermediate.keySet())
 100  
         {
 101  105
             intermediate.put(i, Double.valueOf((double) rank / (double) intermediateSize));
 102  105
         }
 103  
 
 104  
         // fitness proportional selection on intermediate map
 105  308
         for (int i = 0; i < size; i++)
 106  
         {
 107  303
             selected.add(intermediate.sample());
 108  
         }
 109  5
         intermediate = null;
 110  5
         return selected;
 111  
     }
 112  
 }