Coverage Report - org.dishevelled.evolve.recombine.SexualRecombination
 
Classes in this File Line Coverage Branch Coverage Complexity
SexualRecombination
100%
10/10
100%
2/2
1.5
 
 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.recombine;
 25  
 
 26  
 import java.util.Collection;
 27  
 import java.util.List;
 28  
 import java.util.ArrayList;
 29  
 
 30  
 import org.dishevelled.evolve.Recombination;
 31  
 
 32  
 /**
 33  
  * Abstract sexual recombination function implementation.
 34  
  * Subclasses need only to implement the abstract individual-wise method
 35  
  * <code>I recombine(I individual0, I individual1)</code>.
 36  
  *
 37  
  * @param <I> individual type
 38  
  * @author  Michael Heuer
 39  
  */
 40  5
 public abstract class SexualRecombination<I>
 41  
     implements Recombination<I>
 42  
 {
 43  
 
 44  
     /** {@inheritDoc} */
 45  
     public final Collection<I> recombine(final Collection<I> population)
 46  
     {
 47  4
         int size = population.size();
 48  4
         List<I> populationAsList = new ArrayList<I>(population);
 49  4
         Collection<I> recombined = new ArrayList<I>(population.size());
 50  
 
 51  10
         for (int i = 1; i < size; i++)
 52  
         {
 53  6
             I parent0 = populationAsList.get(i - 1);
 54  6
             I parent1 = populationAsList.get(i);
 55  6
             recombined.add(recombine(parent0, parent1));
 56  
         }
 57  4
         recombined.add(recombine(populationAsList.get(size - 1), populationAsList.get(size - 1)));
 58  4
         return recombined;
 59  
     }
 60  
 
 61  
     /**
 62  
      * Recombine the specified individuals sexually, returning a
 63  
      * new individual.
 64  
      *
 65  
      * @param individual0 individual to recombine sexually
 66  
      * @param individual1 individual to recombine sexually
 67  
      * @return a new individual recombined from the specified individuals
 68  
      */
 69  
     protected abstract I recombine(I individual0, I individual1);
 70  
 }