Class Terminations

java.lang.Object
net.bmahe.genetics4j.core.termination.Terminations

public class Terminations extends Object
Utility class providing factory methods for creating common termination conditions in evolutionary algorithms.

Terminations provides a comprehensive set of pre-built termination criteria that can be used individually or combined to create complex stopping conditions for evolutionary algorithms. Each method returns a Termination instance that encapsulates the specific logic for determining when evolution should stop.

Available termination criteria include:

  • Generation-based: Stop after a maximum number of generations
  • Time-based: Stop after a specified duration has elapsed
  • Fitness-based: Stop when fitness reaches certain thresholds
  • Convergence-based: Stop when fitness stops improving for a period
  • Logical combinations: Combine multiple criteria with AND/OR logic

Termination criteria can be combined to create sophisticated stopping conditions:


 // Stop after 100 generations OR when fitness reaches 0.95 OR after 5 minutes
 Termination<Double> complexTermination = Terminations.or(
     Terminations.ofMaxGeneration(100),
     Terminations.ofFitnessAtLeast(0.95),
     Terminations.ofMaxTime(Duration.ofMinutes(5))
 );
 
 // Stop only when BOTH conditions are met: good fitness AND stable evolution
 Termination<Double> conservativeTermination = Terminations.and(
     Terminations.ofFitnessAtLeast(0.9),
     Terminations.ofStableFitness(20)
 );
 
 // Simple generation limit for quick experiments
 Termination<Double> simpleTermination = Terminations.ofMaxGeneration(50);
 

Common usage patterns:

  • Development and testing: Use generation limits for quick experimentation
  • Production systems: Combine time limits with fitness criteria for reliability
  • Research applications: Use convergence detection to study algorithm behavior
  • Resource-constrained environments: Use time-based limits for predictable execution

Design considerations:

  • Performance: Termination checks are called frequently; implementations are optimized
  • Thread safety: Some termination criteria maintain internal state safely
  • Flexibility: All criteria can be combined using logical operators
  • Reliability: Include fallback termination criteria to prevent infinite loops
See Also:
  • Constructor Details

    • Terminations

      public Terminations()
  • Method Details

    • ofMaxGeneration

      public static <T extends Comparable<T>> Termination<T> ofMaxGeneration(long maxGeneration)
      Creates a termination condition that stops evolution after a specified number of generations.

      This is the most common termination criterion, providing a simple upper bound on the number of evolutionary cycles. The algorithm will terminate when the generation counter reaches or exceeds the specified maximum.

      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      maxGeneration - the maximum number of generations to run (must be positive)
      Returns:
      a termination condition that stops after the specified number of generations
      Throws:
      IllegalArgumentException - if maxGeneration is not positive
    • ofMaxTime

      public static <T extends Comparable<T>> Termination<T> ofMaxTime(Duration duration)
      Creates a termination condition that stops evolution after a specified time duration.

      This time-based termination is useful for ensuring predictable execution times, especially in production environments or when computational resources are limited. The timer starts on the first evaluation and stops when the elapsed time exceeds the specified duration.

      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      duration - the maximum time to run the algorithm
      Returns:
      a termination condition that stops after the specified duration
      Throws:
      IllegalArgumentException - if duration is null
    • and

      @SafeVarargs public static <T extends Comparable<T>> Termination<T> and(Termination<T>... terminations)
      Creates a termination condition that requires ALL specified conditions to be met.

      This logical AND operation creates a conservative termination strategy where evolution continues until every provided termination criterion is satisfied. Useful for ensuring multiple quality conditions are met before stopping.

      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      terminations - the termination conditions that must all be satisfied
      Returns:
      a termination condition that stops only when all conditions are met
      Throws:
      IllegalArgumentException - if terminations is null or empty
    • or

      @SafeVarargs public static <T extends Comparable<T>> Termination<T> or(Termination<T>... terminations)
      Creates a termination condition that stops when ANY of the specified conditions is met.

      This logical OR operation creates a flexible termination strategy where evolution stops as soon as any one of the provided criteria is satisfied. Commonly used to provide multiple stopping conditions like time limits, generation limits, or fitness thresholds.

      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      terminations - the termination conditions, any of which can trigger stopping
      Returns:
      a termination condition that stops when any condition is met
      Throws:
      IllegalArgumentException - if terminations is null or empty
    • ofFitnessAtLeast

      public static <T extends Comparable<T>> Termination<T> ofFitnessAtLeast(T threshold)
      Creates a termination condition that stops when any individual reaches a minimum fitness threshold.

      This fitness-based termination is useful for maximization problems where you want to stop as soon as a solution of acceptable quality is found. The condition is satisfied when any individual in the population has a fitness value greater than or equal to the threshold.

      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      threshold - the minimum fitness value required to trigger termination
      Returns:
      a termination condition that stops when fitness reaches the threshold
      Throws:
      IllegalArgumentException - if threshold is null
    • ofFitnessAtMost

      public static <T extends Comparable<T>> Termination<T> ofFitnessAtMost(T threshold)
      Creates a termination condition that stops when any individual reaches a maximum fitness threshold.

      This fitness-based termination is useful for minimization problems where you want to stop as soon as a solution of acceptable quality is found. The condition is satisfied when any individual in the population has a fitness value less than or equal to the threshold.

      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      threshold - the maximum fitness value required to trigger termination
      Returns:
      a termination condition that stops when fitness reaches the threshold
      Throws:
      IllegalArgumentException - if threshold is null
    • ofStableFitness

      public static <T extends Comparable<T>> Termination<T> ofStableFitness(int stableGenerationsCount)
      Creates a termination condition that stops when fitness stops improving for a specified number of generations.

      This convergence-based termination detects when the evolutionary algorithm has reached a stable state where further evolution is unlikely to yield significant improvements. It tracks the best fitness value and stops evolution if no improvement is observed for the specified number of consecutive generations.

      This termination criterion is particularly useful for:

      • Preventing unnecessary computation when the algorithm has converged
      • Automatically adapting to problem difficulty
      • Research applications studying convergence behavior
      Type Parameters:
      T - the type of fitness values in the evolutionary algorithm
      Parameters:
      stableGenerationsCount - the number of generations without improvement required to trigger termination
      Returns:
      a termination condition that stops when fitness plateaus
      Throws:
      IllegalArgumentException - if stableGenerationsCount is not positive