Alterer.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.9.0).
003  * Copyright (c) 2007-2017 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  <pre>{@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  * }</pre>
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
048  */
049 @FunctionalInterface
050 public interface Alterer<
051     extends Gene<?, G>,
052     extends Comparable<? super C>
053 >
054 {
055 
056     /**
057      * The default alter probability: 0.2
058      */
059     public static final double DEFAULT_ALTER_PROBABILITY = 0.2;
060 
061     /**
062      * Alters (recombine) a given population. If the {@code population} is empty,
063      * nothing is altered. The altering of the population is done in place; the
064      * given <i>population</i> is altered.
065      *
066      @param population The Population to be altered. If the
067      *        {@code population} is {@code null} or empty, nothing is altered.
068      @param generation the date of birth (generation) of the altered phenotypes.
069      @return the number of genes that has been altered.
070      @throws NullPointerException if the given {@code population} is
071      *        {@code null}.
072      */
073     public int alter(final Population<G, C> population, final long generation);
074 
075     /**
076      * Returns a composed alterer that first applies the {@code before} alterer
077      * to its input, and then applies {@code this} alterer to the result.
078      *
079      @param before the alterer to apply first
080      @return the new composed alterer
081      */
082     public default Alterer<G, C> compose(final Alterer<G, C> before) {
083         return of(before, this);
084     }
085 
086     /**
087      * Returns a composed alterer that applies the {@code this} alterer
088      * to its input, and then applies the {@code after} alterer to the result.
089      *
090      @param after the alterer to apply first
091      @return the new composed alterer
092      */
093     public default Alterer<G, C> andThen(final Alterer<G, C> after) {
094         return of(this, after);
095     }
096 
097     /**
098      * Combine the given alterers.
099      *
100      @param <G> the gene type
101      @param <C> the fitness function result type
102      @param alterers the alterers to combine.
103      @return a new alterer which consists of the given one
104      @throws NullPointerException if one of the alterers is {@code null}.
105      */
106     @SafeVarargs
107     public static <G extends Gene<?, G>, C extends Comparable<? super C>>
108     Alterer<G, C> of(final Alterer<G, C>... alterers) {
109         return alterers.length == 0
110             (p, g-> 0
111             : alterers.length == 1
112                 ? alterers[0]
113                 new CompositeAlterer<>(ISeq.of(alterers));
114     }
115 
116 }