| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CompositeFitness |
|
| 4.5;4.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.fitness; | |
| 25 | ||
| 26 | import java.util.ArrayList; | |
| 27 | import java.util.List; | |
| 28 | ||
| 29 | import org.dishevelled.evolve.Fitness; | |
| 30 | ||
| 31 | import org.dishevelled.functor.BinaryFunction; | |
| 32 | //import cern.colt...functions.DoubleDoubleFunction; | |
| 33 | ||
| 34 | /** | |
| 35 | * Composite fitness function. | |
| 36 | * | |
| 37 | * @param <I> individual type | |
| 38 | * @author Michael Heuer | |
| 39 | * @version $Revision$ $Date$ | |
| 40 | */ | |
| 41 | public final class CompositeFitness<I> | |
| 42 | implements Fitness<I> | |
| 43 | { | |
| 44 | /** List of child fitness functions. */ | |
| 45 | private final List<Fitness<? super I>> children; | |
| 46 | ||
| 47 | /** Aggregate function. */ | |
| 48 | private final BinaryFunction<Double, Double, Double> aggregate; | |
| 49 | //private final DoubleDoubleFunction aggregate; | |
| 50 | ||
| 51 | ||
| 52 | /** | |
| 53 | * Create a new composite fitness function with the specified list | |
| 54 | * of child fitness functions and the specified aggregate function. | |
| 55 | * | |
| 56 | * @param children list of child fitness functions, must not be | |
| 57 | * null and must contain at least one fitness function | |
| 58 | * @param aggregate aggregate function, must not be null | |
| 59 | */ | |
| 60 | public CompositeFitness(final List<Fitness<? super I>> children, | |
| 61 | final BinaryFunction<Double, Double, Double> aggregate) | |
| 62 | // final DoubleDoubleFunction aggregate) | |
| 63 | 14 | { |
| 64 | 14 | if (children == null) |
| 65 | { | |
| 66 | 1 | throw new IllegalArgumentException("children must not be null"); |
| 67 | } | |
| 68 | 13 | if (children.isEmpty()) |
| 69 | { | |
| 70 | 1 | throw new IllegalArgumentException("children must contain at least one fitness function"); |
| 71 | } | |
| 72 | 12 | if (aggregate == null) |
| 73 | { | |
| 74 | 1 | throw new IllegalArgumentException("aggregate must not be null"); |
| 75 | } | |
| 76 | 11 | this.children = new ArrayList<Fitness<? super I>>(children); |
| 77 | 11 | this.aggregate = aggregate; |
| 78 | 11 | } |
| 79 | ||
| 80 | ||
| 81 | /** {@inheritDoc} */ | |
| 82 | public Double score(final I individual) | |
| 83 | { | |
| 84 | 16 | int last = children.size() - 1; |
| 85 | 16 | double rv = children.get(last).score(individual); |
| 86 | 16 | for (int i = last; --i >= 0;) |
| 87 | { | |
| 88 | 8 | rv = aggregate.evaluate(rv, children.get(i).score(individual)); |
| 89 | } | |
| 90 | 16 | return rv; |
| 91 | } | |
| 92 | } |