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