Record Class ChosenOtherChromosome

java.lang.Object
java.lang.Record
net.bmahe.genetics4j.neat.combination.parentcompare.ChosenOtherChromosome
Record Components:
chosen - the chromosome designated as "chosen" (typically fitter) for preferred inheritance
other - the chromosome designated as "other" (typically less fit) for secondary inheritance

public record ChosenOtherChromosome(NeatChromosome chosen, NeatChromosome other) extends Record
Represents the result of parent comparison during NEAT genetic crossover.

ChosenOtherChromosome is a simple record that encapsulates the result of comparing two parent chromosomes for NEAT crossover operations. It designates which parent should be considered "chosen" (typically the fitter parent) and which should be "other" for the purposes of biased gene inheritance.

Usage in NEAT crossover:

  • Inheritance bias: Chosen parent typically contributes disjoint and excess genes
  • Gene selection: Inheritance threshold applied relative to chosen parent
  • Topology guidance: Chosen parent's structure often forms the offspring base
  • Equal fitness handling: Both parents may be treated equally when fitness is identical

Parent role significance:

  • Chosen parent: Usually the fitter parent, contributes more genes to offspring
  • Other parent: Usually the less fit parent, contributes fewer genes
  • Matching genes: Both parents can contribute genes with equal probability (biased)
  • Gene re-enabling: Both parents considered for connection state decisions

Common usage patterns:


 // Result of parent comparison
 ChosenOtherChromosome comparison = parentHandler.compare(
     policy, parent1, parent2, fitnessComparison
 );
 
 // Extract parents for crossover
 NeatChromosome chosenParent = comparison.chosen();
 NeatChromosome otherParent = comparison.other();
 
 // Use in gene inheritance decisions
 if (fitnessComparison != 0 || random.nextDouble() < inheritanceThreshold) {
     // Inherit from chosen parent
     inheritGene(chosenParent.getConnections().get(index));
 } else {
     // Inherit from other parent
     inheritGene(otherParent.getConnections().get(index));
 }
 
 // Access parent properties
 int numInputs = chosenParent.getNumInputs();
 float minWeight = chosenParent.getMinWeightValue();
 List<Connection> chosenConnections = chosenParent.getConnections();
 List<Connection> otherConnections = otherParent.getConnections();
 

Immutability and value semantics:

  • Immutable: Record provides immutability for safe passing between methods
  • Value equality: Two instances are equal if both chosen and other chromosomes match
  • Compact representation: Minimal memory overhead with clear semantic meaning
  • Null safety: Both chromosomes are expected to be non-null in valid comparisons

Integration with comparison handlers:

  • Return type: Standard return type for ParentComparisonHandler.compare()
  • Type safety: Ensures correct parent roles in crossover operations
  • Clear semantics: Eliminates confusion about which parent is which
  • Consistent interface: All comparison handlers return same type
See Also:
  • Field Details

  • Constructor Details

    • ChosenOtherChromosome

      public ChosenOtherChromosome(NeatChromosome chosen, NeatChromosome other)
      Creates an instance of a ChosenOtherChromosome record class.
      Parameters:
      chosen - the value for the chosen record component
      other - the value for the other record component
  • Method Details

    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • chosen

      public NeatChromosome chosen()
      Returns the value of the chosen record component.
      Returns:
      the value of the chosen record component
    • other

      public NeatChromosome other()
      Returns the value of the other record component.
      Returns:
      the value of the other record component