Package io.jenetics

Class Genotype<G extends Gene<?,G>>

java.lang.Object
io.jenetics.Genotype<G>
All Implemented Interfaces:
BaseSeq<Chromosome<G>>, Factory<Genotype<G>>, Verifiable, Serializable, Iterable<Chromosome<G>>, RandomAccess

public final class Genotype<G extends Gene<?,G>> extends Object implements BaseSeq<Chromosome<G>>, Factory<Genotype<G>>, Verifiable, Serializable
The central class the GA is working with, is the Genotype. It is the structural representative of an individual. This class is the encoded problem solution with one to many Chromosome.

Genotype

The chromosomes of a genotype don't have to have necessarily the same size. It is only required that all genes are from the same type and the genes within a chromosome have the same constraints; e.g., the same min- and max values for the genes value.
final Genotype<DoubleGene> genotype = Genotype.of(
    DoubleChromosome.of(0.0, 1.0, 8),
    DoubleChromosome.of(1.0, 2.0, 10),
    DoubleChromosome.of(0.0, 10.0, 9),
    DoubleChromosome.of(0.1, 0.9, 5)
);
The code snippet above creates a genotype with the same structure as shown in the figure above. In this example the DoubleGene has been chosen as a gene type.
Since:
1.0
Version:
7.2
See Also:
Implementation Note:
This class is immutable and thread-safe.
  • Method Details

    • get

      public Chromosome<G> get(int index)
      Return the chromosome at the given index. It is guaranteed that the returned chromosome is not null.
      Specified by:
      get in interface BaseSeq<G extends Gene<?,G>>
      Parameters:
      index - the chromosome index
      Returns:
      the chromosome with the given index
      Throws:
      IndexOutOfBoundsException - if (index < 0 || index >= _length).
      Since:
      4.0
    • length

      public int length()
      Getting the number of chromosomes of this genotype.
      Specified by:
      length in interface BaseSeq<G extends Gene<?,G>>
      Returns:
      number of chromosomes.
    • chromosome

      public Chromosome<G> chromosome()
      Return the first chromosome. This is an alias for
      final Genotype<DoubleGene> gt = ...;
      final Chromosome<DoubleGene> chromosome = gt.get(0);
      
      Returns:
      The first chromosome.
      Since:
      5.2
    • gene

      public G gene()
      Return the first Gene of the first Chromosome of this Genotype. This is an alias for
      final Genotype<DoubleGene> gt = ...;
      final DoubleGene gene = gt.get(0).get(0);
      
      Returns:
      the first Gene of the first Chromosome of this Genotype.
      Since:
      5.2
    • geneCount

      public int geneCount()
      Return the number of genes this genotype consists of. This is the sum of the number of genes of the genotype chromosomes.
      Returns:
      Return the number of genes this genotype consists of
    • isValid

      public boolean isValid()
      Test if this genotype is valid. A genotype is valid if all its Chromosomes are valid.
      Specified by:
      isValid in interface Verifiable
      Returns:
      true if this genotype is valid, false otherwise.
    • slice

      public Genotype<G> slice(int fromIndex, int toIndex)
      Create a new Genotype which consists of the chromosomes from the given fromIndex (inclusively) to the given toIndex (exclusively). This method creates a view of the underlying chromosomes.
      Parameters:
      fromIndex - the start chromosome index, inclusively
      toIndex - the end chromosome index, exclusively
      Returns:
      a new genotype consisting of the chromosomes within the given indexes
      Throws:
      IndexOutOfBoundsException - for an illegal end point index value (fromIndex < 0 || toIndex > length() || fromIndex > toIndex).
      Since:
      7.2
    • newInstance

      public Genotype<G> newInstance()
      Return a new, random genotype by creating new, random chromosomes (calling the Factory.newInstance() method) from the chromosomes of this genotype.
      Specified by:
      newInstance in interface Factory<G extends Gene<?,G>>
      Returns:
      a new instance of type T
    • 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
    • of

      @SafeVarargs public static <G extends Gene<?, G>> Genotype<G> of(Chromosome<G> first, Chromosome<G>... rest)
      Create a new Genotype from a given array of Chromosomes.
      Type Parameters:
      G - the gene type
      Parameters:
      first - the first Chromosome of the Genotype
      rest - the rest of the genotype chromosomes.
      Returns:
      a new Genotype from the given chromosomes
      Throws:
      NullPointerException - if chromosomes is null or one of its elements.
      Since:
      3.0
    • of

      public static <G extends Gene<?, G>> Genotype<G> of(Factory<? extends Chromosome<G>> factory, int n)
      Create a new Genotype which consists of n chromosomes, which are created by the given factory. This method can be used for easily creating a gene matrix. The following example will create a 10x5 DoubleGene matrix.
      final Genotype<DoubleGene> gt = Genotype
          .of(DoubleChromosome.of(0.0, 1.0, 10), 5);
      
      Type Parameters:
      G - the gene type
      Parameters:
      factory - the factory which creates the chromosomes this genotype consists of
      n - the number of chromosomes this genotype consists of
      Returns:
      new Genotype containing n chromosomes
      Throws:
      IllegalArgumentException - if n < 1
      NullPointerException - if the factory is null
      Since:
      3.0
    • of

      public static <G extends Gene<?, G>> Genotype<G> of(Iterable<? extends Chromosome<G>> chromosomes)
      Create a new Genotype from a given array of chromosomes.
      Type Parameters:
      G - the gene type
      Parameters:
      chromosomes - the Chromosomes the returned genotype consists of
      Returns:
      a new Genotype from the given chromosomes
      Throws:
      NullPointerException - if chromosomes is null or one of its elements.
      IllegalArgumentException - if chromosome.length() < 1
      Since:
      3.0