Class NeatChromosomeCombinator<T extends Comparable<T>>
- Type Parameters:
T
- the fitness value type (typically Double)
- All Implemented Interfaces:
ChromosomeCombinator<T>
NeatChromosomeCombinator performs innovation-number-based genetic recombination between two neural network chromosomes, creating offspring that inherit network topology and connection weights from both parents while preserving the historical tracking essential to the NEAT algorithm.
NEAT crossover algorithm:
- Parent comparison: Determine which parent is "fitter" using comparison policy
- Gene alignment: Match connections by innovation number between parents
- Matching genes: Randomly inherit from either parent (biased by inheritance threshold)
- Disjoint genes: Inherit from fitter parent when innovation ranges overlap
- Excess genes: Inherit from fitter parent beyond other parent's range
- Gene re-enabling: Potentially re-enable disabled genes based on threshold
Key genetic operations:
- Innovation alignment: Uses innovation numbers to match corresponding genes
- Fitness-biased inheritance: Favors genes from fitter parent based on inheritance threshold
- Gene state management: Handles enabled/disabled connection states during crossover
- Topology preservation: Ensures offspring have valid network topology
Gene classification:
- Matching genes: Same innovation number in both parents, inherit randomly
- Disjoint genes: Innovation number exists in one parent within other's range
- Excess genes: Innovation number beyond other parent's highest innovation
- Disabled genes: May be re-enabled if other parent has enabled version
Common usage patterns:
// Create NEAT chromosome combinator
RandomGenerator randomGen = RandomGenerator.getDefault();
NeatCombination policy = NeatCombination.builder()
.inheritanceThresold(0.7) // 70% bias toward fitter parent
.reenableGeneInheritanceThresold(0.25) // 25% gene re-enabling chance
.parentComparisonPolicy(FitnessComparison.build())
.build();
ParentComparisonHandler comparisonHandler = new FitnessComparisonHandler();
NeatChromosomeCombinator<Double> combinator = new NeatChromosomeCombinator<>(
randomGen, policy, comparisonHandler
);
// Perform crossover
NeatChromosome parent1 = // ... first parent
NeatChromosome parent2 = // ... second parent
Double fitness1 = 0.85;
Double fitness2 = 0.72;
List<Chromosome> offspring = combinator.combine(
eaConfiguration, parent1, fitness1, parent2, fitness2
);
NeatChromosome child = (NeatChromosome) offspring.get(0);
Inheritance threshold effects:
- 0.5: Unbiased inheritance, equal probability from both parents
- > 0.5: Bias toward fitter parent, promotes convergence
- < 0.5: Bias toward less fit parent, increases diversity
- 1.0: Always inherit from fitter parent (when fitness differs)
Gene re-enabling mechanism:
- Preservation: Disabled genes maintain connection topology information
- Re-activation: Chance to re-enable genes that are enabled in other parent
- Exploration: Allows rediscovery of previously disabled connection patterns
- Genetic diversity: Prevents permanent loss of structural information
Duplicate connection prevention:
- Links cache: Tracks already included connections to prevent duplicates
- Topology validation: Ensures each connection appears at most once
- Cache efficiency: O(1) lookup for connection existence checking
- Memory management: Cache cleared after each crossover operation
Performance considerations:
- Linear time complexity: O(n + m) where n, m are parent connection counts
- Innovation sorting: Leverages pre-sorted connection lists for efficiency
- Memory efficiency: Minimal allocation during crossover
- Cache optimization: Efficient duplicate detection and prevention
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.apache.logging.log4j.Logger
private final NeatCombination
private final ParentComparisonHandler
private final RandomGenerator
-
Constructor Summary
ConstructorsConstructorDescriptionNeatChromosomeCombinator
(RandomGenerator _randomGenerator, NeatCombination _neatCombination, ParentComparisonHandler _parentComparisonHandler) Constructs a new NEAT chromosome combinator with the specified components. -
Method Summary
Modifier and TypeMethodDescriptioncombine
(AbstractEAConfiguration<T> eaConfiguration, Chromosome firstChromosome, T firstParentFitness, Chromosome secondChromosome, T secondParentFitness) Performs genetic crossover between two NEAT chromosomes to produce offspring.private void
insertInlinksCache
(Map<Integer, Set<Integer>> linksCache, Connection connection) Adds a connection to the links cache to prevent future duplicates.private boolean
linksCacheContainsConnection
(Map<Integer, Set<Integer>> linksCache, Connection connection) Checks whether a connection already exists in the links cache.protected boolean
shouldReEnable
(Connection chosenParent, Connection otherParent) Determines whether a disabled gene should be re-enabled during crossover.
-
Field Details
-
logger
public static final org.apache.logging.log4j.Logger logger -
randomGenerator
-
neatCombination
-
parentComparisonHandler
-
-
Constructor Details
-
NeatChromosomeCombinator
public NeatChromosomeCombinator(RandomGenerator _randomGenerator, NeatCombination _neatCombination, ParentComparisonHandler _parentComparisonHandler) Constructs a new NEAT chromosome combinator with the specified components.The combinator uses the random generator for stochastic decisions during crossover, the combination policy for inheritance parameters, and the comparison handler for determining parent fitness relationships.
- Parameters:
_randomGenerator
- random number generator for stochastic crossover decisions_neatCombination
- crossover policy defining inheritance parameters_parentComparisonHandler
- handler for comparing parent fitness and determining inheritance bias- Throws:
IllegalArgumentException
- if any parameter is null
-
-
Method Details
-
linksCacheContainsConnection
private boolean linksCacheContainsConnection(Map<Integer, Set<Integer>> linksCache, Connection connection) Checks whether a connection already exists in the links cache.The links cache prevents duplicate connections in the offspring by tracking all connections that have already been added. This ensures each connection appears at most once in the resulting chromosome.
- Parameters:
linksCache
- cache mapping from-node indices to sets of to-node indicesconnection
- connection to check for existence- Returns:
- true if connection already exists in cache, false otherwise
- Throws:
IllegalArgumentException
- if linksCache or connection is null
-
insertInlinksCache
Adds a connection to the links cache to prevent future duplicates.This method records that a connection from the specified source to target node has been added to the offspring, preventing the same connection from being added again during the crossover process.
- Parameters:
linksCache
- cache mapping from-node indices to sets of to-node indicesconnection
- connection to add to the cache- Throws:
IllegalArgumentException
- if linksCache or connection is null
-
shouldReEnable
Determines whether a disabled gene should be re-enabled during crossover.If the chosen parent has a disabled connection but the other parent has the same connection enabled, there is a configurable chance to re-enable the connection in the offspring. This mechanism prevents permanent loss of potentially useful connections.
- Parameters:
chosenParent
- the connection selected for inheritanceotherParent
- the corresponding connection from the other parent- Returns:
- true if the disabled connection should be re-enabled, false otherwise
- Throws:
IllegalArgumentException
- if either connection is null
-
combine
public List<Chromosome> combine(AbstractEAConfiguration<T> eaConfiguration, Chromosome firstChromosome, T firstParentFitness, Chromosome secondChromosome, T secondParentFitness) Performs genetic crossover between two NEAT chromosomes to produce offspring.This method implements the NEAT crossover algorithm, aligning genes by innovation number and applying inheritance rules based on parent fitness and configuration parameters. The result is a single offspring chromosome that inherits network topology and connection weights from both parents.
Crossover process:
- Compare parent fitness to determine inheritance bias
- Align genes by innovation number between parents
- Process matching genes with random inheritance (biased)
- Process disjoint genes based on fitness comparison
- Process excess genes from fitter parent
- Apply gene re-enabling rules for disabled connections
- Specified by:
combine
in interfaceChromosomeCombinator<T extends Comparable<T>>
- Parameters:
eaConfiguration
- evolutionary algorithm configuration containing fitness comparatorfirstChromosome
- first parent chromosome (must be NeatChromosome)firstParentFitness
- fitness value of first parentsecondChromosome
- second parent chromosome (must be NeatChromosome)secondParentFitness
- fitness value of second parent- Returns:
- list containing single offspring chromosome
- Throws:
IllegalArgumentException
- if chromosomes are not NeatChromosome instances or any parameter is null
-