Class GenotypeGenerator<T extends Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.core.util.GenotypeGenerator<T>
Type Parameters:
T - the type of fitness values in the evolutionary algorithm

public class GenotypeGenerator<T extends Comparable<T>> extends Object
Utility class for generating initial populations of genotypes in evolutionary algorithms.

GenotypeGenerator provides the infrastructure for creating random initial populations based on chromosome specifications defined in the EA configuration. It supports both automatic generation using chromosome factories and custom generation through user-provided suppliers, enabling flexible population initialization strategies.

The generator operates in two modes:

  • Automatic generation: Uses registered chromosome factories to create random individuals
  • Custom generation: Uses a user-provided supplier for specialized initialization

Key responsibilities include:

  • Population initialization: Creating diverse initial populations for algorithm startup
  • Factory coordination: Managing chromosome factories for different chromosome types
  • Configuration compliance: Ensuring generated genotypes match EA configuration specifications
  • Logging and monitoring: Providing visibility into population generation process

Automatic generation workflow:

  1. Resolve appropriate chromosome factories for each chromosome specification
  2. Generate random chromosomes using the resolved factories
  3. Assemble chromosomes into complete genotypes
  4. Repeat until the required population size is reached

Common usage patterns:


 // Standard automatic generation
 ChromosomeFactoryProvider factoryProvider = new ChromosomeFactoryProvider();
 EAConfiguration<Double> config = createConfiguration();
 GenotypeGenerator<Double> generator = new GenotypeGenerator<>(factoryProvider, config);
 
 List<Genotype> initialPopulation = generator.generateGenotypes(100);
 
 // With custom generation logic
 EAConfiguration<Double> configWithCustomGen = EAConfigurationBuilder.<Double>builder()
     .chromosomeSpecs(chromosomeSpec)
     .genotypeGenerator(() -> createSpecializedGenotype())
     .build();
 
 GenotypeGenerator<Double> customGenerator = new GenotypeGenerator<>(factoryProvider, configWithCustomGen);
 List<Genotype> customPopulation = customGenerator.generateGenotypes(50);
 

Performance considerations:

  • Factory reuse: Resolves chromosome factories once per generation session
  • Memory efficiency: Generates individuals on-demand without intermediate storage
  • Logging overhead: Uses efficient logging for population generation tracking
  • Validation: Performs minimal validation to maintain generation performance
See Also:
  • Field Details

  • Constructor Details

    • GenotypeGenerator

      public GenotypeGenerator(ChromosomeFactoryProvider _chromosomeFactoryProvider, AbstractEAConfiguration<T> _eaConfiguration)
      Constructs a new genotype generator with the specified factory provider and EA configuration.

      The generator will use the provided chromosome factory provider to resolve appropriate factories for each chromosome type specified in the configuration. If the configuration includes a custom genotype generator, it will be used instead of automatic generation.

      Parameters:
      _chromosomeFactoryProvider - the provider for chromosome factories
      _eaConfiguration - the EA configuration containing chromosome specifications
      Throws:
      NullPointerException - if any parameter is null
  • Method Details

    • generateGenotypes

      public List<Genotype> generateGenotypes(int numPopulation)
      Generates a specified number of random genotypes for initial population creation.

      This method creates a diverse initial population by either using a custom genotype generator (if specified in the configuration) or by automatically generating individuals using chromosome factories. Each generated genotype conforms to the chromosome specifications defined in the EA configuration.

      Generation process:

      1. Check if custom genotype generator is available
      2. If custom generator exists, use it to create all individuals
      3. Otherwise, resolve chromosome factories and generate chromosomes automatically
      4. Assemble chromosomes into complete genotypes
      Parameters:
      numPopulation - the number of genotypes to generate (must be positive)
      Returns:
      a list of randomly generated genotypes ready for evolution
      Throws:
      IllegalArgumentException - if numPopulation is not positive
      RuntimeException - if chromosome factory resolution or generation fails