Coverage Report - org.dishevelled.evolve.select.RandomSelection
 
Classes in this File Line Coverage Branch Coverage Complexity
RandomSelection
100%
19/19
100%
6/6
2.25
 
 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.List;
 27  
 import java.util.Random;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 
 31  
 import org.dishevelled.weighted.WeightedMap;
 32  
 
 33  
 import org.dishevelled.evolve.Selection;
 34  
 
 35  
 /**
 36  
  * Random 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 RandomSelection<I>
 43  
     implements Selection<I>
 44  
 {
 45  
     /** Source of randomness. */
 46  
     private final Random random;
 47  
 
 48  
 
 49  
     /**
 50  
      * Create a new random selection function with the default
 51  
      * source of randomness.
 52  
      */
 53  
     public RandomSelection()
 54  7
     {
 55  7
         random = new Random();
 56  7
     }
 57  
 
 58  
     /**
 59  
      * Create a new random selection function with the specified
 60  
      * source of randomness.
 61  
      *
 62  
      * @param random source of randomness, must not be null
 63  
      */
 64  
     public RandomSelection(final Random random)
 65  3
     {
 66  3
         if (random == null)
 67  
         {
 68  1
             throw new IllegalArgumentException("random must not be null");
 69  
         }
 70  2
         this.random = random;
 71  2
     }
 72  
 
 73  
 
 74  
     /**
 75  
      * Return the source of randomness for this random selection function.
 76  
      * The source of randomness will not be null.
 77  
      *
 78  
      * @return the source of randomness for this random selection function
 79  
      */
 80  
     public Random getRandom()
 81  
     {
 82  3
         return random;
 83  
     }
 84  
 
 85  
     /** {@inheritDoc} */
 86  
     public Collection<I> select(final Collection<I> population,
 87  
                                 final WeightedMap<I> scores)
 88  
     {
 89  4
         if (scores.totalWeight() == 0.0d)
 90  
         {
 91  1
             throw new IllegalStateException("scores total weight must be greater than zero");
 92  
         }
 93  3
         int size = population.size();
 94  3
         List<I> populationAsList = new ArrayList<I>(population);
 95  3
         List<I> selected = new ArrayList<I>(size);
 96  1006
         for (int i = 0; i < size; i++)
 97  
         {
 98  1003
             I individual = populationAsList.get(random.nextInt(size));
 99  1003
             selected.add(individual);
 100  
         }
 101  3
         populationAsList = null;
 102  3
         return selected;
 103  
     }
 104  
 }