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-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.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  
  */
 40  
 public final class ProportionalMutation<I>
 41  
     implements Mutation<I>
 42  
 {
 43  
     /** Weighted map of individual-wise mutation functions. */
 44  
     private WeightedMap<IndividualWiseMutation<I>> mutations;
 45  
 
 46  
     /** Default null individual-wise mutation function, in case the map is empty. */
 47  3
     private final IndividualWiseMutation<I> nullMutation = new NullIndividualWiseMutation<I>();
 48  
 
 49  
 
 50  
     /**
 51  
      * Create a new proportional mutation function.
 52  
      */
 53  
     public ProportionalMutation()
 54  3
     {
 55  3
         mutations = new HashWeightedMap<IndividualWiseMutation<I>>();
 56  3
     }
 57  
 
 58  
 
 59  
     /**
 60  
      * Add the specified individual-wise mutation function to this
 61  
      * proportional mutation function at the specified weight.
 62  
      *
 63  
      * @param mutation individual-wise mutation function, must not be null
 64  
      * @param weight weight
 65  
      */
 66  
     public void add(final IndividualWiseMutation<I> mutation, final double weight)
 67  
     {
 68  2
         if (mutation == null)
 69  
         {
 70  1
             throw new IllegalArgumentException("mutation must not be null");
 71  
         }
 72  1
         mutations.put(mutation, Double.valueOf(weight));
 73  1
     }
 74  
 
 75  
     /** {@inheritDoc} */
 76  
     public Collection<I> mutate(final Collection<I> recombined)
 77  
     {
 78  2
         Collection<I> mutated = new ArrayList<I>(recombined.size());
 79  
 
 80  2
         for (I i : recombined)
 81  
         {
 82  4
             IndividualWiseMutation<I> mutation = mutations.sample();
 83  
 
 84  4
             if (mutation == null)
 85  
             {
 86  3
                 mutation = nullMutation;
 87  
             }
 88  4
             mutated.add(mutation.mutate(i));
 89  4
         }
 90  2
         return mutated;
 91  
     }
 92  
 }