Class EASystemFactory

java.lang.Object
net.bmahe.genetics4j.core.EASystemFactory

public class EASystemFactory extends Object
Factory class providing convenient methods for creating properly configured EASystem instances.

EASystemFactory simplifies the complex process of assembling all the components required for evolutionary algorithms by providing pre-configured factory methods that handle the initialization and wiring of selection strategies, mutation operators, combination policies, and fitness evaluators.

The factory abstracts away the complexity of manually creating and configuring:

  • Selection policy resolvers: Components that match selection strategies to implementations
  • Mutation policy resolvers: Components that handle different mutation strategies
  • Chromosome combinators: Components responsible for crossover operations
  • Replacement strategy handlers: Components managing population replacement
  • Fitness evaluators: Components handling fitness computation strategies

Factory methods are organized by evaluation strategy:

  • Synchronous evaluation: Traditional single-threaded or parallel evaluation using EAConfiguration
  • Bulk asynchronous evaluation: Batch processing for external services or GPU acceleration using EAConfigurationBulkAsync
  • Custom evaluation: User-provided FitnessEvaluator implementations

Common usage patterns:


 // Simple synchronous evaluation with default thread pool
 EAConfiguration<Double> config = EAConfigurationBuilder.<Double>builder()
     .chromosomeSpecs(chromosomeSpec)
     .parentSelectionPolicy(Tournament.of(3))
     .combinationPolicy(SinglePointCrossover.build())
     .mutationPolicies(List.of(RandomMutation.of(0.1)))
     .replacementStrategy(Elitism.builder().offspringRatio(0.8).build())
     .build();
 
 EAExecutionContext<Double> context = EAExecutionContextBuilder.<Double>builder()
     .populationSize(100)
     .fitness(genotype -> computeFitness(genotype))
     .termination(Generations.of(100))
     .build();
 
 EASystem<Double> eaSystem = EASystemFactory.from(config, context);
 
 // Asynchronous evaluation for expensive fitness functions
 EAConfigurationBulkAsync<Double> asyncConfig = EAConfigurationBulkAsyncBuilder.<Double>builder()
     .chromosomeSpecs(chromosomeSpec)
     .parentSelectionPolicy(Tournament.of(3))
     .combinationPolicy(SinglePointCrossover.build())
     .mutationPolicies(List.of(RandomMutation.of(0.1)))
     .replacementStrategy(Elitism.builder().offspringRatio(0.8).build())
     .fitnessBulkAsync(genotypes -> evaluateBatch(genotypes))
     .build();
 
 EASystem<Double> asyncSystem = EASystemFactory.from(asyncConfig, context);
 
 // Custom evaluation strategy
 FitnessEvaluator<Double> customEvaluator = new CachedFitnessEvaluator<>(baseFitness, cacheSize);
 EASystem<Double> customSystem = EASystemFactory.from(config, context, executorService, customEvaluator);
 

Factory method selection guide:

  • Fast fitness functions: Use synchronous methods with EAConfiguration
  • Expensive fitness functions: Use asynchronous methods with EAConfigurationBulkAsync
  • External fitness computation: Use custom evaluator methods
  • GPU acceleration: Use bulk async methods for batch processing

Thread pool considerations:

See Also:
  • Constructor Details

    • EASystemFactory

      private EASystemFactory()
      Prevents instantiation since it's a bunch of static methods
  • Method Details

    • from

      public static <T extends Comparable<T>> EASystem<T> from(AbstractEAConfiguration<T> eaConfiguration, AbstractEAExecutionContext<T> eaExecutionContext, ExecutorService executorService, FitnessEvaluator<T> fitnessEvaluator)
      Creates an EASystem with a custom fitness evaluator and explicit thread pool.

      This is the most flexible factory method that allows complete control over all components of the evolutionary algorithm system. It assembles and wires all necessary components including selection strategies, mutation operators, chromosome combinators, and replacement strategies.

      This method is primarily used internally by other factory methods, but can be used directly when you need to provide a custom FitnessEvaluator implementation such as cached, distributed, or specialized evaluation strategies.

      Type Parameters:
      T - the type of fitness values, must be comparable for selection operations
      Parameters:
      eaConfiguration - the evolutionary algorithm configuration specifying genetic operators and strategies
      eaExecutionContext - the execution context containing population parameters and fitness functions
      executorService - the thread pool for parallel operations (caller responsible for shutdown)
      fitnessEvaluator - the fitness evaluator implementation to use for population evaluation
      Returns:
      a fully configured EASystem ready for evolution execution
      Throws:
      IllegalArgumentException - if any parameter is null
      IllegalStateException - if no suitable replacement strategy handler can be found
    • from

      public static <T extends Comparable<T>> EASystem<T> from(EAConfiguration<T> eaConfigurationSync, EAExecutionContext<T> eaExecutionContext, ExecutorService executorService)
      Creates an EASystem with synchronous fitness evaluation and explicit thread pool.

      This method is ideal for scenarios where fitness computation is relatively fast and can benefit from parallel evaluation across multiple threads. The fitness function defined in the configuration will be called synchronously for each individual or in parallel depending on the evaluator implementation.

      Use this method when:

      • Fitness computation is CPU-bound and relatively fast (< 100ms per evaluation)
      • You want control over the thread pool used for parallel evaluation
      • You need deterministic thread pool behavior for testing or resource management
      Type Parameters:
      T - the type of fitness values, must be comparable for selection operations
      Parameters:
      eaConfigurationSync - the synchronous EA configuration with a simple fitness function
      eaExecutionContext - the execution context containing population and termination parameters
      executorService - the thread pool for parallel fitness evaluation (caller responsible for shutdown)
      Returns:
      a configured EASystem using synchronous fitness evaluation
      Throws:
      IllegalArgumentException - if any parameter is null
    • from

      public static <T extends Comparable<T>> EASystem<T> from(EAConfiguration<T> eaConfigurationSync, EAExecutionContext<T> eaExecutionContext)
      Creates an EASystem with synchronous fitness evaluation using the common thread pool.

      This is the most convenient method for creating evolutionary algorithms with simple fitness functions. It uses ForkJoinPool.commonPool() for automatic parallelization without requiring explicit thread pool management.

      This method is ideal for:

      • Quick prototyping and experimentation
      • Applications where thread pool management is not critical
      • Fast fitness functions that can benefit from automatic parallelization
      • Educational purposes and simple examples
      Type Parameters:
      T - the type of fitness values, must be comparable for selection operations
      Parameters:
      eaConfigurationSync - the synchronous EA configuration with a simple fitness function
      eaExecutionContext - the execution context containing population and termination parameters
      Returns:
      a configured EASystem using synchronous fitness evaluation with common thread pool
      Throws:
      IllegalArgumentException - if any parameter is null
    • from

      public static <T extends Comparable<T>> EASystem<T> from(EAConfigurationBulkAsync<T> eaConfigurationBulkAsync, EAExecutionContext<T> eaExecutionContext, ExecutorService executorService)
      Creates an EASystem with bulk asynchronous fitness evaluation and explicit thread pool.

      This method is designed for expensive fitness functions that can benefit from batch processing or asynchronous evaluation. The bulk async evaluator can process entire populations at once, enabling optimization strategies like GPU acceleration, external service calls, or database batch operations.

      Use this method when:

      • Fitness computation involves external resources (databases, web services, files)
      • Evaluation can be accelerated through batch processing (GPU, vectorized operations)
      • Fitness computation is expensive (> 100ms per evaluation)
      • You need to optimize I/O operations through batching
      Type Parameters:
      T - the type of fitness values, must be comparable for selection operations
      Parameters:
      eaConfigurationBulkAsync - the bulk async EA configuration with batch fitness evaluation
      eaExecutionContext - the execution context containing population and termination parameters
      executorService - the thread pool for managing asynchronous operations (caller responsible for shutdown)
      Returns:
      a configured EASystem using bulk asynchronous fitness evaluation
      Throws:
      IllegalArgumentException - if any parameter is null
    • from

      public static <T extends Comparable<T>> EASystem<T> from(EAConfigurationBulkAsync<T> eaConfigurationBulkAsync, EAExecutionContext<T> eaExecutionContext)
      Creates an EASystem with bulk asynchronous fitness evaluation using the common thread pool.

      This convenience method provides the benefits of bulk asynchronous evaluation without requiring explicit thread pool management. It automatically uses ForkJoinPool.commonPool() for managing asynchronous operations.

      This method combines the convenience of automatic thread pool management with the power of bulk asynchronous evaluation, making it ideal for:

      • Expensive fitness functions in research and experimentation
      • Applications requiring external resource access without complex thread management
      • GPU-accelerated fitness evaluation with simple setup
      • Prototype development with advanced evaluation strategies
      Type Parameters:
      T - the type of fitness values, must be comparable for selection operations
      Parameters:
      eaConfigurationBulkAsync - the bulk async EA configuration with batch fitness evaluation
      eaExecutionContext - the execution context containing population and termination parameters
      Returns:
      a configured EASystem using bulk asynchronous fitness evaluation with common thread pool
      Throws:
      IllegalArgumentException - if any parameter is null