| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ElitistSelection |
|
| 2.75;2.75 | ||||
| ElitistSelection$1 |
|
| 2.75;2.75 | ||||
| ElitistSelection$WeightedMapEntryComparator |
|
| 2.75;2.75 |
| 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.Map; | |
| 27 | import java.util.List; | |
| 28 | import java.util.ArrayList; | |
| 29 | import java.util.Comparator; | |
| 30 | import java.util.Collection; | |
| 31 | import java.util.Collections; | |
| 32 | ||
| 33 | import org.dishevelled.weighted.WeightedMap; | |
| 34 | import org.dishevelled.weighted.HashWeightedMap; | |
| 35 | ||
| 36 | import org.dishevelled.evolve.Selection; | |
| 37 | ||
| 38 | /** | |
| 39 | * Elitist selection function. | |
| 40 | * | |
| 41 | * @param <I> individual type | |
| 42 | * @author Michael Heuer | |
| 43 | * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $ | |
| 44 | */ | |
| 45 | public final class ElitistSelection<I> | |
| 46 | implements Selection<I> | |
| 47 | { | |
| 48 | /** Number of individuals. */ | |
| 49 | private final int individuals; | |
| 50 | ||
| 51 | /** Default hash map load factor. */ | |
| 52 | private static final float DEFAULT_LOAD_FACTOR = 0.75f; | |
| 53 | ||
| 54 | /** WeightedMap entry comparator. */ | |
| 55 | 14 | private final WeightedMapEntryComparator weightedMapEntryComparator = new WeightedMapEntryComparator(); |
| 56 | ||
| 57 | ||
| 58 | /** | |
| 59 | * Create a new elitist selection function with the specified | |
| 60 | * number of individuals. | |
| 61 | * | |
| 62 | * @param individuals number of individuals for this elitist | |
| 63 | * selection function, must be <code>>= 0</code> | |
| 64 | */ | |
| 65 | public ElitistSelection(final int individuals) | |
| 66 | 14 | { |
| 67 | 14 | if (individuals < 0) |
| 68 | { | |
| 69 | 2 | throw new IllegalArgumentException("individuals must be greater than or equal to zero"); |
| 70 | } | |
| 71 | 12 | this.individuals = individuals; |
| 72 | 12 | } |
| 73 | ||
| 74 | ||
| 75 | /** | |
| 76 | * Return the number of individuals for this elitist selection function. | |
| 77 | * | |
| 78 | * @return the number of individuals for this elitist selection function | |
| 79 | */ | |
| 80 | public int getIndividuals() | |
| 81 | { | |
| 82 | 1 | return individuals; |
| 83 | } | |
| 84 | ||
| 85 | /** {@inheritDoc} */ | |
| 86 | public Collection<I> select(final Collection<I> population, | |
| 87 | final WeightedMap<I> scores) | |
| 88 | { | |
| 89 | 6 | if (scores.totalWeight() == 0.0d) |
| 90 | { | |
| 91 | 1 | throw new IllegalStateException("scores total weight must be greater than zero"); |
| 92 | } | |
| 93 | 5 | int size = population.size(); |
| 94 | 5 | List<I> selected = new ArrayList<I>(size); |
| 95 | ||
| 96 | // sort keys by score | |
| 97 | 5 | List<Map.Entry<I, Double>> sortedKeys = new ArrayList<Map.Entry<I, Double>>(scores.entrySet()); |
| 98 | 5 | Collections.sort(sortedKeys, weightedMapEntryComparator); |
| 99 | ||
| 100 | // take the top n individuals | |
| 101 | 5 | List<Map.Entry<I, Double>> eliteKeys = sortedKeys.subList(0, Math.min(individuals, size)); |
| 102 | ||
| 103 | // create intermediate map of the top n individuals | |
| 104 | 5 | WeightedMap<I> intermediate = new HashWeightedMap<I>(individuals, DEFAULT_LOAD_FACTOR); |
| 105 | 5 | for (Map.Entry<I, Double> e : eliteKeys) |
| 106 | { | |
| 107 | 105 | intermediate.put(e.getKey(), Double.valueOf(0.0d)); |
| 108 | } | |
| 109 | ||
| 110 | // update fitness based on number of top n individuals | |
| 111 | 5 | for (I i : intermediate.keySet()) |
| 112 | { | |
| 113 | 105 | intermediate.put(i, Double.valueOf(1.0d / individuals)); |
| 114 | } | |
| 115 | ||
| 116 | // fitness proportional selection on intermediate map | |
| 117 | 308 | for (int i = 0; i < size; i++) |
| 118 | { | |
| 119 | 303 | selected.add(intermediate.sample()); |
| 120 | } | |
| 121 | ||
| 122 | 5 | sortedKeys = null; |
| 123 | 5 | eliteKeys = null; |
| 124 | 5 | intermediate = null; |
| 125 | 5 | return selected; |
| 126 | } | |
| 127 | ||
| 128 | ||
| 129 | /** | |
| 130 | * WeightedMap entry comparator. | |
| 131 | */ | |
| 132 | 1433 | private class WeightedMapEntryComparator |
| 133 | implements Comparator<Map.Entry<I, Double>> | |
| 134 | { | |
| 135 | ||
| 136 | /** {@inheritDoc} */ | |
| 137 | public int compare(final Map.Entry<I, Double> e1, final Map.Entry<I, Double> e2) | |
| 138 | { | |
| 139 | // reverse order of comparison to get decreasing sort | |
| 140 | 1405 | return e2.getValue().compareTo(e1.getValue()); |
| 141 | } | |
| 142 | } | |
| 143 | } |