| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FitnessProportionalSelection |
|
| 4.0;4 |
| 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.ArrayList; | |
| 27 | import java.util.Collection; | |
| 28 | ||
| 29 | import org.dishevelled.weighted.WeightedMap; | |
| 30 | ||
| 31 | import org.dishevelled.evolve.Selection; | |
| 32 | ||
| 33 | /** | |
| 34 | * Fitness proportional selection function. | |
| 35 | * | |
| 36 | * @param <I> individual type | |
| 37 | * @author Michael Heuer | |
| 38 | */ | |
| 39 | 5 | public final class FitnessProportionalSelection<I> |
| 40 | implements Selection<I> | |
| 41 | { | |
| 42 | ||
| 43 | /** {@inheritDoc} */ | |
| 44 | public Collection<I> select(final Collection<I> population, | |
| 45 | final WeightedMap<I> scores) | |
| 46 | { | |
| 47 | 4 | if (scores.totalWeight() == 0.0d) |
| 48 | { | |
| 49 | 1 | throw new IllegalStateException("scores total weight must be greater than zero"); |
| 50 | } | |
| 51 | 3 | int size = population.size(); |
| 52 | 3 | Collection<I> selected = new ArrayList<I>(size); |
| 53 | 1006 | for (int i = 0; i < size; i++) |
| 54 | { | |
| 55 | 1003 | selected.add(scores.sample()); |
| 56 | } | |
| 57 | 3 | return selected; |
| 58 | } | |
| 59 | } |