001/* 002 * Java Genetic Algorithm Library (jenetics-8.3.0). 003 * Copyright (c) 2007-2025 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@gmail.com) 019 */ 020package io.jenetics; 021 022import static java.util.Objects.requireNonNull; 023 024import io.jenetics.util.ISeq; 025import io.jenetics.util.Seq; 026 027/** 028 * The Alterer is responsible for the changing/recombining the Population. 029 * Alterers can be chained by appending a list of alterers with the 030 * {@link io.jenetics.engine.Engine.Builder#alterers(Alterer, Alterer[])} method. 031 * {@snippet lang="java": 032 * final Engine<DoubleGene, Double> engine = Engine 033 * .builder(gtf, ff) 034 * .alterers( 035 * new Crossover<>(0.1), 036 * new Mutator<>(0.05), 037 * new MeanAlterer<>(0.2)) 038 * .build(); 039 * final EvolutionStream<DoubleGene, Double> stream = engine.stream(); 040 * } 041 * 042 * The order of the alterer calls is: Crossover, Mutation and MeanAlterer. 043 * 044 * @param <G> the gene type 045 * @param <C> the fitness function result type 046 * 047 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 048 * @since 1.0 049 * @version 4.0 050 */ 051@FunctionalInterface 052public interface Alterer< 053 G extends Gene<?, G>, 054 C extends Comparable<? super C> 055> { 056 057 /** 058 * The default alter probability: 0.2 059 */ 060 double DEFAULT_ALTER_PROBABILITY = 0.2; 061 062 /** 063 * Alters (recombine) a given population. If the {@code population} is empty, 064 * nothing is altered. The altered population is part of the returned 065 * {@code AlterResult} object. 066 * 067 * @param population The Population to be altered. If the {@code population} 068 * is {@code null} or empty, nothing is altered. 069 * @param generation the date of birth (generation) of the altered phenotypes. 070 * @return the alter-result object, which contains the altered population 071 * and the alteration counts 072 * @throws NullPointerException if the given {@code population} is 073 * {@code null}. 074 */ 075 AltererResult<G, C> alter( 076 final Seq<Phenotype<G, C>> population, 077 final long generation 078 ); 079 080 /** 081 * Returns a composed alterer that first applies the {@code before} alterer 082 * to its input, and then applies {@code this} alterer to the result. 083 * 084 * @param before the alterer to apply first 085 * @return the new composed alterer 086 */ 087 default Alterer<G, C> compose(final Alterer<G, C> before) { 088 return of(requireNonNull(before), this); 089 } 090 091 /** 092 * Returns a composed alterer that applies the {@code this} alterer 093 * to its input, and then applies the {@code after} alterer to the result. 094 * 095 * @param after the alterer to apply first 096 * @return the new composed alterer 097 */ 098 default Alterer<G, C> andThen(final Alterer<G, C> after) { 099 return of(this, requireNonNull(after)); 100 } 101 102 /** 103 * Combine the given alterers. 104 * 105 * @param <G> the gene type 106 * @param <C> the fitness function result type 107 * @param alterers the alterers to combine. 108 * @return a new alterer which consists of the given one 109 * @throws NullPointerException if one of the alterers is {@code null}. 110 */ 111 @SafeVarargs 112 static <G extends Gene<?, G>, C extends Comparable<? super C>> 113 Alterer<G, C> of(final Alterer<G, C>... alterers) { 114 return alterers.length == 0 115 ? (p, g) -> new AltererResult<>(p.asISeq()) 116 : alterers.length == 1 117 ? alterers[0] 118 : new CompositeAlterer<>(ISeq.of(alterers)); 119 } 120 121}