Class Genotype

java.lang.Object
net.bmahe.genetics4j.core.Genotype

public class Genotype extends Object
Represents a genotype in an evolutionary algorithm, which is a collection of chromosomes.

A genotype encapsulates the complete genetic representation of an individual solution, consisting of one or more chromosomes that together define the individual's characteristics. Each chromosome may represent different aspects or components of the solution space.

Genotypes are immutable once created and provide type-safe access to their constituent chromosomes.

See Also:
  • Field Details

    • chromosomes

      private final Chromosome[] chromosomes
  • Constructor Details

    • Genotype

      public Genotype(Chromosome... _chromosomes)
      Creates a new genotype with the specified chromosomes.
      Parameters:
      _chromosomes - one or more chromosomes to include in this genotype
      Throws:
      IllegalArgumentException - if chromosomes array is null or empty
    • Genotype

      public Genotype(Collection<Chromosome> _chromosomes)
      Creates a new genotype with chromosomes from the specified collection.
      Parameters:
      _chromosomes - a collection of chromosomes to include in this genotype
      Throws:
      IllegalArgumentException - if chromosomes collection is null or empty
  • Method Details

    • getSize

      public int getSize()
      Returns the number of chromosomes in this genotype.
      Returns:
      the count of chromosomes
    • getChromosomes

      public Chromosome[] getChromosomes()
      Returns all chromosomes in this genotype.
      Returns:
      an array containing all chromosomes
    • getChromosome

      public Chromosome getChromosome(int index)
      Returns the chromosome at the specified index.
      Parameters:
      index - the index of the chromosome to retrieve (0-based)
      Returns:
      the chromosome at the specified index
      Throws:
      IllegalArgumentException - if index is negative or greater than or equal to the number of chromosomes
    • getChromosome

      public <T extends Chromosome> T getChromosome(int index, Class<T> clazz)
      Returns the chromosome at the specified index, cast to the specified type.
      Type Parameters:
      T - the expected chromosome type
      Parameters:
      index - the index of the chromosome to retrieve (0-based)
      clazz - the class to cast the chromosome to
      Returns:
      the chromosome at the specified index, cast to the specified type
      Throws:
      IllegalArgumentException - if index is invalid or clazz is null
      ClassCastException - if the chromosome cannot be cast to the specified type
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object