Coverage Report - org.dishevelled.evolve.select.RankBasedSelection
 
Classes in this File Line Coverage Branch Coverage Complexity
RankBasedSelection
100%
21/21
100%
12/12
3.667
 
 1  
 /*
 2  
 
 3  
     dsh-evolve  Framework for evolutionary algorithms.
 4  
     Copyright (c) 2005-2012 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  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 41  
  */
 42  
 public final class RankBasedSelection<I>
 43  
     implements Selection<I>
 44  
 {
 45  
     /** Rank. */
 46  
     private final int rank;
 47  
 
 48  
 
 49  
     /**
 50  
      * Create a new rank based selection function with
 51  
      * the specified rank.
 52  
      *
 53  
      * @param rank rank for this rank based selection function,
 54  
      *    must be <code>&gt;= 1</code>
 55  
      */
 56  
     public RankBasedSelection(final int rank)
 57  14
     {
 58  14
         if (rank < 1)
 59  
         {
 60  3
             throw new IllegalArgumentException("rank must be greater than or equal to one");
 61  
         }
 62  11
         this.rank = rank;
 63  11
     }
 64  
 
 65  
 
 66  
     /**
 67  
      * Return the rank for this rank based selection function.
 68  
      *
 69  
      * @return the rank for this rank based selection function
 70  
      */
 71  
     public int getRank()
 72  
     {
 73  1
         return rank;
 74  
     }
 75  
 
 76  
     /** {@inheritDoc} */
 77  
     public Collection<I> select(final Collection<I> population,
 78  
                                 final WeightedMap<I> scores)
 79  
     {
 80  6
         if (scores.totalWeight() == 0.0d)
 81  
         {
 82  1
             throw new IllegalStateException("scores total weight must be greater than zero");
 83  
         }
 84  5
         int size = population.size();
 85  5
         List<I> selected = new ArrayList<I>(size);
 86  
 
 87  
         // create intermediate map of those ranked at or above rank
 88  5
         WeightedMap<I> intermediate = new HashWeightedMap<I>();
 89  5
         for (I i : scores.keySet())
 90  
         {
 91  303
             if (scores.rank(i) <= rank)
 92  
             {
 93  
                 // temporarily set fitness score to 0.0d
 94  105
                 intermediate.put(i, Double.valueOf(0.0d));
 95  
             }
 96  
         }
 97  
 
 98  
         // update fitness based on size of intermediate map
 99  5
         int intermediateSize = intermediate.size();
 100  5
         for (I i : intermediate.keySet())
 101  
         {
 102  105
             intermediate.put(i, Double.valueOf((double) rank / (double) intermediateSize));
 103  
         }
 104  
 
 105  
         // fitness proportional selection on intermediate map
 106  308
         for (int i = 0; i < size; i++)
 107  
         {
 108  303
             selected.add(intermediate.sample());
 109  
         }
 110  5
         intermediate = null;
 111  5
         return selected;
 112  
     }
 113  
 }