Interface ChromosomeFactory<T extends Chromosome>
- Type Parameters:
T
- the type of chromosome this factory creates
- All Known Implementing Classes:
BitChromosomeFactory
,DoubleChromosomeFactory
,FloatChromosomeFactory
,IntChromosomeFactory
,NeatConnectedChromosomeFactory
,NeatEmptyChromosomeFactory
,ProgramTreeChromosomeFactory
ChromosomeFactory implements the Factory pattern to decouple chromosome creation from the specific implementation details. Each factory is responsible for creating chromosomes of a specific type (bit, integer, double, etc.) based on the provided specifications.
The factory pattern provides several benefits:
- Type safety: Compile-time guarantees about chromosome types
- Extensibility: Easy addition of new chromosome types
- Configuration-driven: Chromosome creation based on specifications
- Encapsulation: Hides complex chromosome initialization logic
Factory implementations typically handle:
- Random initialization: Creating chromosomes with random values within constraints
- Constraint validation: Ensuring generated chromosomes meet specification requirements
- Value range handling: Respecting minimum/maximum bounds for numeric types
- Size management: Creating chromosomes with the correct number of alleles
The framework provides concrete factories for common chromosome types:
BitChromosomeFactory
: Creates binary string chromosomesIntChromosomeFactory
: Creates integer array chromosomesDoubleChromosomeFactory
: Creates double-precision floating-point chromosomesFloatChromosomeFactory
: Creates single-precision floating-point chromosomes
Factory selection is typically handled automatically by ChromosomeFactoryProvider
which maintains a registry of available factories and selects the appropriate one based
on the chromosome specification type.
Example custom factory implementation:
public class CustomChromosomeFactory implements ChromosomeFactory<CustomChromosome> {
@Override
public boolean canHandle(ChromosomeSpec chromosomeSpec) {
return chromosomeSpec instanceof CustomChromosomeSpec;
}
@Override
public CustomChromosome generate(ChromosomeSpec chromosomeSpec) {
CustomChromosomeSpec spec = (CustomChromosomeSpec) chromosomeSpec;
// Create chromosome based on specification
return new CustomChromosome(spec.getParameters());
}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionboolean
canHandle
(ChromosomeSpec chromosomeSpec) Determines if this factory can create chromosomes for the given specification.generate
(ChromosomeSpec chromosomeSpec) Creates a new chromosome instance based on the provided specification.
-
Method Details
-
canHandle
Determines if this factory can create chromosomes for the given specification.This method implements the type-checking logic that allows the factory system to automatically select the appropriate factory for each chromosome specification. Implementations typically check if the specification is of the expected type.
The method should be:
- Fast: Called frequently during factory selection
- Accurate: Return true only if generation will succeed
- Safe: Handle null and unexpected specification types gracefully
- Parameters:
chromosomeSpec
- the chromosome specification to evaluate- Returns:
true
if this factory can generate chromosomes for the given specification,false
otherwise- Throws:
IllegalArgumentException
- if chromosomeSpec is null (optional, implementation-dependent)
-
generate
Creates a new chromosome instance based on the provided specification.This method implements the core factory logic, creating and initializing a new chromosome according to the parameters defined in the specification. The generated chromosome should be ready for use in genetic operations.
The generation process typically involves:
- Extracting parameters from the specification (size, bounds, constraints)
- Generating random values within the specified constraints
- Creating and initializing the chromosome instance
- Validating that the result meets all requirements
Implementation requirements:
- Specification compatibility: Only call after
canHandle(net.bmahe.genetics4j.core.spec.chromosome.ChromosomeSpec)
returns true - Randomization: Use appropriate random number generation for initialization
- Constraint compliance: Ensure generated values respect specification bounds
- Thread safety: Support concurrent calls in multi-threaded environments
- Parameters:
chromosomeSpec
- the specification defining the chromosome to create- Returns:
- a newly created chromosome instance conforming to the specification
- Throws:
IllegalArgumentException
- if the specification is null, invalid, or not supported by this factoryRuntimeException
- if chromosome generation fails due to constraint conflicts or other errors
-