EvolutionListeners.java

package net.bmahe.genetics4j.core.evolutionlisteners;

import java.util.Comparator;
import java.util.function.Function;

import org.apache.logging.log4j.Logger;

import net.bmahe.genetics4j.core.Genotype;

public class EvolutionListeners {

	private EvolutionListeners() {
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN) {
		return ofLogTopN(logger, topN, 0);
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN,
			final int skipN) {
		return new EvolutionListenerLogTopN<U>(logger, topN, skipN);
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN,
			final Comparator<U> comparator) {
		return new EvolutionListenerLogTopN<U>(logger, topN, 0, comparator, null);
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN,
			final Comparator<U> comparator, final Function<Genotype, String> prettyPrinter) {
		return new EvolutionListenerLogTopN<U>(logger, topN, 0, comparator, prettyPrinter);
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN,
			final Function<Genotype, String> prettyPrinter) {
		return new EvolutionListenerLogTopN<U>(logger, topN, 0, null, prettyPrinter);
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN,
			final int skipN, final Comparator<U> comparator) {
		return new EvolutionListenerLogTopN<U>(logger, topN, skipN, comparator, null);
	}

	public static <U extends Comparable<U>> EvolutionListener<U> ofLogTopN(final Logger logger, final int topN,
			final int skipN, final Comparator<U> comparator, final Function<Genotype, String> prettyPrinter) {
		return new EvolutionListenerLogTopN<U>(logger, topN, skipN, comparator, prettyPrinter);
	}

	/**
	 * Creates a default evolution listener that outputs to System.out without external logging dependencies. Shows the
	 * best individual each generation.
	 *
	 * @param <U> the fitness type, must be Comparable
	 * @return a new DefaultEvolutionListener instance
	 */
	public static <U extends Comparable<U>> EvolutionListener<U> ofDefault() {
		return new DefaultEvolutionListener<U>();
	}

	/**
	 * Creates a default evolution listener with generation skipping.
	 *
	 * @param <U>   the fitness type, must be Comparable
	 * @param skipN the number of generations to skip between outputs (0 = no skipping)
	 * @return a new DefaultEvolutionListener instance
	 */
	public static <U extends Comparable<U>> EvolutionListener<U> ofDefault(final int skipN) {
		return new DefaultEvolutionListener<U>(1, skipN);
	}

	/**
	 * Creates a default evolution listener that shows the top N individuals.
	 *
	 * @param <U>   the fitness type, must be Comparable
	 * @param topN  the number of top individuals to display
	 * @param skipN the number of generations to skip between outputs
	 * @return a new DefaultEvolutionListener instance
	 */
	public static <U extends Comparable<U>> EvolutionListener<U> ofDefault(final int topN, final int skipN) {
		return new DefaultEvolutionListener<U>(topN, skipN);
	}

	/**
	 * Creates a default evolution listener with custom genotype formatting.
	 *
	 * @param <U>           the fitness type, must be Comparable
	 * @param topN          the number of top individuals to display
	 * @param prettyPrinter function to format genotypes for display
	 * @return a new DefaultEvolutionListener instance
	 */
	public static <U extends Comparable<U>> EvolutionListener<U> ofDefault(final int topN,
			final Function<Genotype, String> prettyPrinter) {
		return new DefaultEvolutionListener<U>(topN, prettyPrinter);
	}

	/**
	 * Creates a fully configurable default evolution listener.
	 *
	 * @param <U>           the fitness type, must be Comparable
	 * @param topN          the number of top individuals to display
	 * @param skipN         the number of generations to skip between outputs
	 * @param prettyPrinter function to format genotypes for display
	 * @return a new DefaultEvolutionListener instance
	 */
	public static <U extends Comparable<U>> EvolutionListener<U> ofDefault(final int topN, final int skipN,
			final Function<Genotype, String> prettyPrinter) {
		return new DefaultEvolutionListener<U>(topN, skipN, null, prettyPrinter);
	}
}