Coverage Report - org.dishevelled.evolve.mutate.ProportionalMutation
 
Classes in this File Line Coverage Branch Coverage Complexity
ProportionalMutation
100%
16/16
100%
6/6
2.333
 
 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.mutate;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 
 29  
 import org.dishevelled.weighted.WeightedMap;
 30  
 import org.dishevelled.weighted.HashWeightedMap;
 31  
 
 32  
 import org.dishevelled.evolve.Mutation;
 33  
 
 34  
 /**
 35  
  * Proportional mutation function.
 36  
  *
 37  
  * @param <I> individual type
 38  
  * @author  Michael Heuer
 39  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 40  
  */
 41  
 public final class ProportionalMutation<I>
 42  
     implements Mutation<I>
 43  
 {
 44  
     /** Weighted map of individual-wise mutation functions. */
 45  
     private WeightedMap<IndividualWiseMutation<I>> mutations;
 46  
 
 47  
     /** Default null individual-wise mutation function, in case the map is empty. */
 48  3
     private final IndividualWiseMutation<I> nullMutation = new NullIndividualWiseMutation<I>();
 49  
 
 50  
 
 51  
     /**
 52  
      * Create a new proportional mutation function.
 53  
      */
 54  
     public ProportionalMutation()
 55  3
     {
 56  3
         mutations = new HashWeightedMap<IndividualWiseMutation<I>>();
 57  3
     }
 58  
 
 59  
 
 60  
     /**
 61  
      * Add the specified individual-wise mutation function to this
 62  
      * proportional mutation function at the specified weight.
 63  
      *
 64  
      * @param mutation individual-wise mutation function, must not be null
 65  
      * @param weight weight
 66  
      */
 67  
     public void add(final IndividualWiseMutation<I> mutation, final double weight)
 68  
     {
 69  2
         if (mutation == null)
 70  
         {
 71  1
             throw new IllegalArgumentException("mutation must not be null");
 72  
         }
 73  1
         mutations.put(mutation, Double.valueOf(weight));
 74  1
     }
 75  
 
 76  
     /** {@inheritDoc} */
 77  
     public Collection<I> mutate(final Collection<I> recombined)
 78  
     {
 79  2
         Collection<I> mutated = new ArrayList<I>(recombined.size());
 80  
 
 81  2
         for (I i : recombined)
 82  
         {
 83  4
             IndividualWiseMutation<I> mutation = mutations.sample();
 84  
 
 85  4
             if (mutation == null)
 86  
             {
 87  3
                 mutation = nullMutation;
 88  
             }
 89  4
             mutated.add(mutation.mutate(i));
 90  4
         }
 91  2
         return mutated;
 92  
     }
 93  
 }