<commit_before>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     <NAME> (<EMAIL>)
 *
 */
package org.jenetics;

import java.util.function.Predicate;

import org.jenetics.util.StaticObject;

/**
 * Some default GA termination strategies.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 1.0
 * @version 1.0 &mdash; <em>$Date: 2013-02-01 $</em>
 */
public final class termination extends StaticObject {
	private termination() {}

	static final class SteadyFitness<C extends Comparable<? super C>>
		implements Predicate<Statistics<?, C>>
	{
		private final int _genenerations;

		private C _fitness;
		private int _stableGenerations = 0;

		public SteadyFitness(final int generations) {
			_genenerations = generations;
		}

		@Override
		public boolean test(final Statistics<?, C> statistics) {
			boolean proceed = true;

			if (_fitness == null) {
				_fitness = statistics.getBestFitness();
				_stableGenerations = 1;
			} else {
				final Optimize opt = statistics.getOptimize();
				if (opt.compare(_fitness, statistics.getBestFitness()) >= 0) {
					proceed = ++_stableGenerations <= _genenerations;
				} else {
					_fitness = statistics.getBestFitness();
					_stableGenerations = 1;
				}
			}

			return proceed;
		}
	}

	/**
	 * Create a <i>terminator</i> which returns {@code false} if the fitness
	 * hasn't improved for a given number of generations.
	 *
	 * @param <C> the fitness type.
	 * @param generation the number of generations the fitness don't have been
	 *         improved.
	 * @return the GA terminator.
	 */
	public static <C extends Comparable<? super C>>
	Predicate<Statistics<?, C>> SteadyFitness(final int generation) {
		return new SteadyFitness<>(generation);
	}

	static final class Generation implements Predicate<Statistics<?, ?>> {
		private final int _generation;

		public Generation(final int generation) {
			_generation = generation;
		}

		@Override
		public boolean test(final Statistics<?, ?> statistics) {
			return statistics.getGeneration() < _generation;
		}
	}

	/**
	 * Return a <i>termination predicate</i> which returns {@code false} if the
	 * current GA generation is {@code >=} as the given {@code generation}.
	 *
	 * [code]
	 * final GeneticAlgortihm<Float64Gene, Float64> ga = ...
	 * ga.evolve(termination.Generation(100));
	 * [/code]
	 *
	 * @param generation the maximal GA generation.
	 * @return the termination predicate.
	 */
	public static Predicate<Statistics<?, ?>> Generation(final int generation) {
		return new Generation(generation);
	}

}
<commit_msg>Fix typo.<commit_after>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     <NAME> (<EMAIL>)
 *
 */
package org.jenetics;

import java.util.function.Predicate;

import org.jenetics.util.StaticObject;

/**
 * Some default GA termination strategies.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 1.0
 * @version 1.0 &mdash; <em>$Date: 2013-02-13 $</em>
 */
public final class termination extends StaticObject {
	private termination() {}

	static final class SteadyFitness<C extends Comparable<? super C>>
		implements Predicate<Statistics<?, C>>
	{
		private final int _generations;

		private C _fitness;
		private int _stableGenerations = 0;

		public SteadyFitness(final int generations) {
			_generations = generations;
		}

		@Override
		public boolean test(final Statistics<?, C> statistics) {
			boolean proceed = true;

			if (_fitness == null) {
				_fitness = statistics.getBestFitness();
				_stableGenerations = 1;
			} else {
				final Optimize opt = statistics.getOptimize();
				if (opt.compare(_fitness, statistics.getBestFitness()) >= 0) {
					proceed = ++_stableGenerations <= _generations;
				} else {
					_fitness = statistics.getBestFitness();
					_stableGenerations = 1;
				}
			}

			return proceed;
		}
	}

	/**
	 * Create a <i>terminator</i> which returns {@code false} if the fitness
	 * hasn't improved for a given number of generations.
	 *
	 * @param <C> the fitness type.
	 * @param generation the number of generations the fitness don't have been
	 *         improved.
	 * @return the GA terminator.
	 */
	public static <C extends Comparable<? super C>>
	Predicate<Statistics<?, C>> SteadyFitness(final int generation) {
		return new SteadyFitness<>(generation);
	}

	static final class Generation implements Predicate<Statistics<?, ?>> {
		private final int _generation;

		public Generation(final int generation) {
			_generation = generation;
		}

		@Override
		public boolean test(final Statistics<?, ?> statistics) {
			return statistics.getGeneration() < _generation;
		}
	}

	/**
	 * Return a <i>termination predicate</i> which returns {@code false} if the
	 * current GA generation is {@code >=} as the given {@code generation}.
	 *
	 * [code]
	 * final GeneticAlgortihm<Float64Gene, Float64> ga = ...
	 * ga.evolve(termination.Generation(100));
	 * [/code]
	 *
	 * @param generation the maximal GA generation.
	 * @return the termination predicate.
	 */
	public static Predicate<Statistics<?, ?>> Generation(final int generation) {
		return new Generation(generation);
	}

}

{'fraction_non_alphanumeric': 0.1071321546299071, 'fraction_numerical': 0.010788133053640995, 'mean_word_length': 3.7576621525302922, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3746682', 'n_tokens_mistral': 2150, 'n_tokens_neox': 1871, 'n_words': 697}