001/*
002 * Java Genetic Algorithm Library (jenetics-7.1.0).
003 * Copyright (c) 2007-2022 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.engine;
021
022import io.jenetics.Gene;
023import io.jenetics.Phenotype;
024import io.jenetics.util.ISeq;
025
026/**
027 * This functional interface defines the evolution function, which takes an
028 * {@link EvolutionStart} object, evolves the population, and returns an
029 * {@link EvolutionResult} object.
030 *
031 * @param <G> the gene type
032 * @param <C> the fitness result type
033 *
034 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
035 * @version 5.1
036 * @since 5.1
037 */
038@FunctionalInterface
039public interface Evolution<
040        G extends Gene<?, G>,
041        C extends Comparable<? super C>
042> {
043
044        /**
045         * Perform one evolution step with the given evolution {@code start} object
046         * New phenotypes are created with the fitness function defined by this
047         * <em>engine</em>
048         *
049         * @apiNote
050         * The implementation of this method must be thread-safe.
051         *
052         * @since 3.1
053         * @see #evolve(ISeq, long)
054         *
055         * @param start the evolution start object
056         * @return the evolution result
057         * @throws java.lang.NullPointerException if the given evolution
058         *         {@code start} is {@code null}
059         */
060        EvolutionResult<G, C> evolve(final EvolutionStart<G, C> start);
061
062
063        /**
064         * Perform one evolution step with the given {@code population} and
065         * {@code generation}.
066         * <p>
067         * <em>This method is thread-safe.</em>
068         *
069         * @see #evolve(EvolutionStart)
070         *
071         * @param population the population to evolve
072         * @param generation the current generation; used for calculating the
073         *        phenotype age.
074         * @return the evolution result
075         * @throws java.lang.NullPointerException if the given {@code population} is
076         *         {@code null}
077         * @throws IllegalArgumentException if the given {@code generation} is
078         *         smaller then one
079         */
080        default EvolutionResult<G, C> evolve(
081                final ISeq<Phenotype<G, C>> population,
082                final long generation
083        ) {
084                return evolve(EvolutionStart.of(population, generation));
085        }
086
087}