| 
001 /*002  * Java Genetic Algorithm Library (jenetics-3.0.0).
 003  * Copyright (c) 2007-2014 Franz Wilhelmstötter
 004  *
 005  * Licensed under the Apache License, Version 2.0 (the "License");
 006  * you may not use this file except in compliance with the License.
 007  * You may obtain a copy of the License at
 008  *
 009  *      http://www.apache.org/licenses/LICENSE-2.0
 010  *
 011  * Unless required by applicable law or agreed to in writing, software
 012  * distributed under the License is distributed on an "AS IS" BASIS,
 013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 014  * See the License for the specific language governing permissions and
 015  * limitations under the License.
 016  *
 017  * Author:
 018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 019  */
 020 package org.jenetics;
 021
 022 import org.jenetics.util.ISeq;
 023
 024 /**
 025  * The Alterer is responsible for the changing/recombining the Population.
 026  * Alterers can be chained by appending a list of alterers with the
 027  * {@link org.jenetics.engine.Engine.Builder#alterers(Alterer, Alterer[])} method.
 028  *
 029  * [code]
 030  * final Engine<DoubleGene, Double> engine = Engine
 031  *     .builder(gtf, ff)
 032  *     .alterers(
 033  *         new Crossover<>(0.1),
 034  *         new Mutator<>(0.05),
 035  *         new MeanAlterer<>(0.2))
 036  *     .build();
 037  * final EvolutionStream<DoubleGene, Double> stream = engine.stream();
 038  * [/code]
 039  *
 040  * The order of the alterer calls is: Crossover, Mutation and MeanAlterer.
 041  *
 042  * @param <G> the gene type
 043  * @param <C> the fitness function result type
 044  *
 045  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 046  * @since 1.0
 047  * @version 3.0 — <em>$Date: 2014-12-28 $</em>
 048  */
 049 @FunctionalInterface
 050 public interface Alterer<
 051     G extends Gene<?, G>,
 052     C extends Comparable<? super C>
 053 >
 054 {
 055
 056     public static final double DEFAULT_ALTER_PROBABILITY = 0.2;
 057
 058     /**
 059      * Alters (recombine) a given population. If the {@code population} is empty,
 060      * nothing is altered. The altering of the population is done in place; the
 061      * given <i>population</i> is altered.
 062      *
 063      * @param population The Population to be altered. If the
 064      *        {@code population} is {@code null} or empty, nothing is altered.
 065      * @param generation the date of birth (generation) of the altered phenotypes.
 066      * @return the number of genes that has been altered.
 067      * @throws NullPointerException if the given {@code population} is
 068      *        {@code null}.
 069      */
 070     public int alter(final Population<G, C> population, final long generation);
 071
 072     /**
 073      * Returns a composed alterer that first applies the {@code before} alterer
 074      * to its input, and then applies {@code this} alterer to the result.
 075      *
 076      * @param before the alterer to apply first
 077      * @return the new composed alterer
 078      */
 079     public default Alterer<G, C> compose(final Alterer<G, C> before) {
 080         return of(before, this);
 081     }
 082
 083     /**
 084      * Returns a composed alterer that applies the {@code this} alterer
 085      * to its input, and then applies the {@code after} alterer to the result.
 086      *
 087      * @param after the alterer to apply first
 088      * @return the new composed alterer
 089      */
 090     public default Alterer<G, C> andThen(final Alterer<G, C> after) {
 091         return of(this, after);
 092     }
 093
 094     /**
 095      * Combine the given alterers.
 096      *
 097      * @param <G> the gene type
 098      * @param <C> the fitness function result type
 099      * @param alterers the alterers to combine.
 100      * @return a new alterer which consists of the given one
 101      * @throws NullPointerException if one of the alterers is {@code null}.
 102      */
 103     @SafeVarargs
 104     public static <G extends Gene<?, G>, C extends Comparable<? super C>>
 105     Alterer<G, C> of(final Alterer<G, C>... alterers) {
 106         return alterers.length == 0 ?
 107             (p, g) -> 0 :
 108             alterers.length == 1 ?
 109                 alterers[0] :
 110                 new CompositeAlterer<G, C>(ISeq.of(alterers));
 111     }
 112
 113 }
 |