Interface ParentComparisonHandler

All Known Implementing Classes:
FitnessComparisonHandler, FitnessThenSizeComparisonHandler

public interface ParentComparisonHandler
Interface for handling parent comparison strategies during NEAT genetic crossover.

ParentComparisonHandler defines the contract for comparing two parent chromosomes to determine which should be considered "chosen" (typically the fitter parent) and which should be considered "other" (typically the less fit parent) for inheritance decisions during NEAT crossover operations.

Purpose in NEAT crossover:

  • Inheritance bias: Determines which parent contributes disjoint and excess genes
  • Fitness-based selection: Guides gene inheritance based on parent performance
  • Equal fitness handling: Provides strategy when parents have identical fitness
  • Policy abstraction: Allows pluggable comparison strategies

Comparison result usage:

  • Chosen parent: Usually contributes disjoint and excess genes
  • Other parent: May contribute genes when inheritance is unbiased
  • Matching genes: Randomly inherited from either parent with bias
  • Gene re-enabling: Both parents considered for connection state decisions

Common implementation patterns:


 // Simple fitness-based comparison handler
 public class FitnessComparisonHandler implements ParentComparisonHandler {
     
     public boolean canHandle(ParentComparisonPolicy policy) {
         return policy instanceof FitnessComparison;
     }
     
     public ChosenOtherChromosome compare(ParentComparisonPolicy policy, 
             NeatChromosome first, NeatChromosome second, int fitnessComparison) {
         if (fitnessComparison >= 0) {
             return new ChosenOtherChromosome(first, second);  // First is fitter
         } else {
             return new ChosenOtherChromosome(second, first); // Second is fitter
         }
     }
 }
 
 // Usage in crossover
 ParentComparisonHandler handler = new FitnessComparisonHandler();
 ChosenOtherChromosome result = handler.compare(
     policy, parent1, parent2, fitnessComparator.compare(fitness1, fitness2)
 );
 NeatChromosome chosenParent = result.chosen();
 NeatChromosome otherParent = result.other();
 

Integration with crossover:

  • Gene inheritance: Chosen parent typically dominates gene contribution
  • Bias application: Inheritance threshold applied relative to chosen parent
  • Topology preservation: Chosen parent's topology forms the base structure
  • Innovation tracking: Both parents contribute to innovation alignment

Policy-based design:

  • Strategy pattern: Different policies enable different comparison strategies
  • Extensibility: New comparison strategies can be added without changing crossover code
  • Configuration: Comparison behavior controlled by policy parameters
  • Testing: Easy to test different comparison strategies in isolation
See Also:
  • Method Details

    • canHandle

      boolean canHandle(ParentComparisonPolicy parentComparisonPolicy)
      Determines whether this handler can process the given parent comparison policy.

      This method allows the handler registry to determine which handler is appropriate for a given comparison policy type, enabling polymorphic behavior in the crossover process.

      Parameters:
      parentComparisonPolicy - the comparison policy to check
      Returns:
      true if this handler can process the policy, false otherwise
    • compare

      ChosenOtherChromosome compare(ParentComparisonPolicy parentComparisonPolicy, NeatChromosome first, NeatChromosome second, int fitnessComparison)
      Compares two parent chromosomes and determines which should be chosen for preferred inheritance.

      This method analyzes the two parent chromosomes and their relative fitness to determine which parent should be considered "chosen" (typically the fitter parent) and which should be "other" for the purposes of biased gene inheritance during crossover.

      The fitness comparison parameter provides the result of comparing the parents' fitness values: positive if first is fitter, negative if second is fitter, zero if equal.

      Parameters:
      parentComparisonPolicy - the comparison policy defining the comparison strategy
      first - the first parent chromosome
      second - the second parent chromosome
      fitnessComparison - result of fitness comparison (positive: first fitter, negative: second fitter, zero: equal)
      Returns:
      ChosenOtherChromosome containing the chosen parent and other parent