Coverage Report - org.dishevelled.evolve.select.ElitistSelection
 
Classes in this File Line Coverage Branch Coverage Complexity
ElitistSelection
100%
27/27
100%
10/10
2.75
ElitistSelection$1
N/A
N/A
2.75
ElitistSelection$WeightedMapEntryComparator
100%
2/2
N/A
2.75
 
 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.Map;
 27  
 import java.util.List;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Comparator;
 30  
 import java.util.Collection;
 31  
 import java.util.Collections;
 32  
 
 33  
 import org.dishevelled.weighted.WeightedMap;
 34  
 import org.dishevelled.weighted.HashWeightedMap;
 35  
 
 36  
 import org.dishevelled.evolve.Selection;
 37  
 
 38  
 /**
 39  
  * Elitist selection function.
 40  
  *
 41  
  * @param <I> individual type
 42  
  * @author  Michael Heuer
 43  
  */
 44  
 public final class ElitistSelection<I>
 45  
     implements Selection<I>
 46  
 {
 47  
     /** Number of individuals. */
 48  
     private final int individuals;
 49  
 
 50  
     /** Default hash map load factor. */
 51  
     private static final float DEFAULT_LOAD_FACTOR = 0.75f;
 52  
 
 53  
     /** WeightedMap entry comparator. */
 54  14
     private final WeightedMapEntryComparator weightedMapEntryComparator = new WeightedMapEntryComparator();
 55  
 
 56  
 
 57  
     /**
 58  
      * Create a new elitist selection function with the specified
 59  
      * number of individuals.
 60  
      *
 61  
      * @param individuals number of individuals for this elitist
 62  
      *    selection function, must be <code>&gt;= 0</code>
 63  
      */
 64  
     public ElitistSelection(final int individuals)
 65  14
     {
 66  14
         if (individuals < 0)
 67  
         {
 68  2
             throw new IllegalArgumentException("individuals must be greater than or equal to zero");
 69  
         }
 70  12
         this.individuals = individuals;
 71  12
     }
 72  
 
 73  
 
 74  
     /**
 75  
      * Return the number of individuals for this elitist selection function.
 76  
      *
 77  
      * @return the number of individuals for this elitist selection function
 78  
      */
 79  
     public int getIndividuals()
 80  
     {
 81  1
         return individuals;
 82  
     }
 83  
 
 84  
     /** {@inheritDoc} */
 85  
     public Collection<I> select(final Collection<I> population,
 86  
                                 final WeightedMap<I> scores)
 87  
     {
 88  6
         if (scores.totalWeight() == 0.0d)
 89  
         {
 90  1
             throw new IllegalStateException("scores total weight must be greater than zero");
 91  
         }
 92  5
         int size = population.size();
 93  5
         List<I> selected = new ArrayList<I>(size);
 94  
 
 95  
         // sort keys by score
 96  5
         List<Map.Entry<I, Double>> sortedKeys = new ArrayList<Map.Entry<I, Double>>(scores.entrySet());
 97  5
         Collections.sort(sortedKeys, weightedMapEntryComparator);
 98  
 
 99  
         // take the top n individuals
 100  5
         List<Map.Entry<I, Double>> eliteKeys = sortedKeys.subList(0, Math.min(individuals, size));
 101  
 
 102  
         // create intermediate map of the top n individuals
 103  5
         WeightedMap<I> intermediate = new HashWeightedMap<I>(individuals, DEFAULT_LOAD_FACTOR);
 104  5
         for (Map.Entry<I, Double> e : eliteKeys)
 105  
         {
 106  105
             intermediate.put(e.getKey(), Double.valueOf(0.0d));
 107  105
         }
 108  
 
 109  
         // update fitness based on number of top n individuals
 110  5
         for (I i : intermediate.keySet())
 111  
         {
 112  105
             intermediate.put(i, Double.valueOf(1.0d / individuals));
 113  105
         }
 114  
 
 115  
         // fitness proportional selection on intermediate map
 116  308
         for (int i = 0; i < size; i++)
 117  
         {
 118  303
             selected.add(intermediate.sample());
 119  
         }
 120  
 
 121  5
         sortedKeys = null;
 122  5
         eliteKeys = null;
 123  5
         intermediate = null;
 124  5
         return selected;
 125  
     }
 126  
 
 127  
 
 128  
     /**
 129  
      * WeightedMap entry comparator.
 130  
      */
 131  1340
     private class WeightedMapEntryComparator
 132  
         implements Comparator<Map.Entry<I, Double>>
 133  
     {
 134  
 
 135  
         /** {@inheritDoc} */
 136  
         public int compare(final Map.Entry<I, Double> e1, final Map.Entry<I, Double> e2)
 137  
         {
 138  
             // reverse order of comparison to get decreasing sort
 139  1312
             return e2.getValue().compareTo(e1.getValue());
 140  
         }
 141  
     }
 142  
 }