Class NeatChromosomeAddConnection

java.lang.Object
net.bmahe.genetics4j.neat.mutation.chromosome.NeatChromosomeAddConnection
All Implemented Interfaces:
ChromosomeMutationHandler<NeatChromosome>

public class NeatChromosomeAddConnection extends Object implements ChromosomeMutationHandler<NeatChromosome>
Chromosome mutation handler that adds new connections to NEAT (NeuroEvolution of Augmenting Topologies) neural networks.

NeatChromosomeAddConnection implements the add-connection structural mutation for NEAT chromosomes, which increases network connectivity by creating new weighted links between existing nodes. This mutation is essential for NEAT's ability to explore different network topologies and discover optimal connectivity patterns.

Add-connection mutation process:

  1. Node selection: Randomly select source and target nodes from all available nodes
  2. Validity checking: Ensure the connection doesn't violate network constraints
  3. Duplicate prevention: Verify the connection doesn't already exist
  4. Innovation assignment: Assign unique innovation number via InnovationManager
  5. Weight initialization: Set random weight within chromosome bounds
  6. Connection creation: Add enabled connection to the chromosome

Connection constraints:

  • No self-connections: Source and target nodes must be different
  • No duplicate connections: Connection between same nodes cannot already exist
  • Feed-forward topology: Output nodes cannot be sources, input nodes cannot be targets
  • Valid node references: Both nodes must exist in the network

Node selection strategy:

  • Available nodes: All input, output, and hidden nodes are potential connection endpoints
  • Dynamic range: Node range adapts to include any hidden nodes created by add-node mutations
  • Uniform selection: All valid nodes have equal probability of being selected
  • Constraint filtering: Invalid connections are rejected and no mutation occurs

Common usage patterns:


 // Create add-connection mutation handler
 RandomGenerator randomGen = RandomGenerator.getDefault();
 InnovationManager innovationManager = new InnovationManager();
 NeatChromosomeAddConnection handler = new NeatChromosomeAddConnection(
     randomGen, innovationManager
 );
 
 // Check if handler can process mutation
 AddConnection mutationPolicy = AddConnection.of(0.1);
 NeatChromosomeSpec chromosomeSpec = NeatChromosomeSpec.of(3, 2, -1.0f, 1.0f);
 boolean canHandle = handler.canHandle(mutationPolicy, chromosomeSpec);
 
 // Apply mutation to chromosome
 NeatChromosome originalChromosome = // ... existing chromosome
 NeatChromosome mutatedChromosome = handler.mutate(mutationPolicy, originalChromosome);
 
 // Result: chromosome may have one additional connection (if valid connection found)
 

Integration with NEAT algorithm:

  • Innovation tracking: Uses InnovationManager for consistent innovation number assignment
  • Population consistency: Same connection types get same innovation numbers across population
  • Genetic alignment: Innovation numbers enable proper crossover alignment
  • Structural diversity: Increases topological diversity in the population

Weight initialization:

  • Random weights: New connections get random weights within chromosome bounds
  • Uniform distribution: Weights uniformly distributed between min and max values
  • Immediate activation: New connections are enabled and immediately affect network behavior
  • Bounded values: Weights respect chromosome's min/max weight constraints

Performance considerations:

  • Efficient validation: Fast checks for connection existence and validity
  • Innovation caching: Leverages InnovationManager's O(1) innovation lookup
  • Memory efficiency: Minimal allocation during mutation
  • Failed mutation handling: Gracefully handles cases where no valid connection can be added
See Also:
  • Field Details

    • logger

      public static final org.apache.logging.log4j.Logger logger
    • randomGenerator

      private final RandomGenerator randomGenerator
    • innovationManager

      private final InnovationManager innovationManager
  • Constructor Details

    • NeatChromosomeAddConnection

      public NeatChromosomeAddConnection(RandomGenerator _randomGenerator, InnovationManager _innovationManager)
      Constructs a new add-connection mutation handler with the specified components.

      The random generator is used for node selection and weight initialization. The innovation manager provides unique innovation numbers for new connections, ensuring consistent tracking across the population.

      Parameters:
      _randomGenerator - random number generator for stochastic operations
      _innovationManager - innovation manager for tracking structural changes
      Throws:
      IllegalArgumentException - if randomGenerator or innovationManager is null
  • Method Details

    • canHandle

      public boolean canHandle(MutationPolicy mutationPolicy, ChromosomeSpec chromosome)
      Determines whether this handler can process the given mutation policy and chromosome specification.

      This handler specifically processes AddConnection mutations applied to NeatChromosomeSpec specifications, ensuring type compatibility for NEAT neural network connection addition.

      Specified by:
      canHandle in interface ChromosomeMutationHandler<NeatChromosome>
      Parameters:
      mutationPolicy - the mutation policy to check
      chromosome - the chromosome specification to check
      Returns:
      true if policy is AddConnection and chromosome is NeatChromosomeSpec, false otherwise
      Throws:
      IllegalArgumentException - if any parameter is null
    • mutate

      public NeatChromosome mutate(MutationPolicy mutationPolicy, Chromosome chromosome)
      Applies add-connection mutation to a NEAT chromosome.

      This method attempts to add a new connection between two randomly selected nodes in the neural network. The mutation may fail if no valid connection can be found (e.g., all possible connections already exist or violate network constraints).

      Mutation algorithm:

      1. Determine the range of available nodes (inputs, outputs, and hidden nodes)
      2. Randomly select source and target nodes
      3. Validate the connection doesn't violate constraints
      4. If valid, create new connection with innovation number and random weight
      5. Add connection to chromosome and return modified chromosome
      6. If invalid, return chromosome unchanged
      Specified by:
      mutate in interface ChromosomeMutationHandler<NeatChromosome>
      Parameters:
      mutationPolicy - the add-connection mutation policy
      chromosome - the NEAT chromosome to mutate
      Returns:
      a new chromosome with potentially one additional connection
      Throws:
      IllegalArgumentException - if policy is not AddConnection or chromosome is not NeatChromosome