Package io.jenetics

Class EnumGene<A>

java.lang.Object
io.jenetics.EnumGene<A>
All Implemented Interfaces:
Gene<A,EnumGene<A>>, Factory<EnumGene<A>>, Self<EnumGene<A>>, Verifiable, Serializable, Comparable<EnumGene<A>>

public final class EnumGene<A> extends Object implements Gene<A,EnumGene<A>>, Comparable<EnumGene<A>>, Serializable

Gene which holds enumerable (countable) genes. Will be used for combinatorial problems in combination with the PermutationChromosome.

The following code shows how to create a combinatorial genotype factory which can be used when creating an Engine instance.
final ISeq<Integer> alleles = ISeq.of(1, 2, 3, 4, 5, 6, 7, 8); final Factory<Genotype<EnumGene<Integer>>> gtf = Genotype.of( PermutationChromosome.of(alleles) );
The following code shows the assurances of the EnumGene.
final ISeq<Integer> alleles = ISeq.of(1, 2, 3, 4, 5, 6, 7, 8); final EnumGene<Integer> gene = new EnumGene<>(5, alleles); assert(gene.alleleIndex() == 5); assert(gene.allele() == gene.validAlleles().get(5)); assert(gene.validAlleles() == alleles);
Since:
1.0
Version:
5.2
See Also:
Implementation Note:
This class is immutable and thread-safe.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return the allele of this gene.
    int
    Return the index of the allele this gene is representing.
    int
     
    boolean
     
    int
     
    boolean
    Check if this object is valid.
    Return a new, random gene with the same type and with the same constraints as this gene.
    newInstance(A value)
    Create a new gene from the given value and the gene context.
    static <A> EnumGene<A>
    of(int alleleIndex, A... validAlleles)
    Create a new enum gene from the given valid genes and the chosen allele index.
    static <A> EnumGene<A>
    of(int alleleIndex, ISeq<? extends A> validAlleles)
    Create a new enum gene from the given valid genes and the chosen allele index.
    static <A> EnumGene<A>
    of(A... validAlleles)
    Return a new enum gene with an allele randomly chosen from the given valid alleles.
    static <A> EnumGene<A>
    of(ISeq<? extends A> validAlleles)
    Return a new enum gene with an allele randomly chosen from the given valid alleles.
     
    Return sequence of the valid alleles where this gene is a part of.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface io.jenetics.util.Factory

    instances

    Methods inherited from interface io.jenetics.util.Self

    self
  • Method Details

    • validAlleles

      public ISeq<A> validAlleles()
      Return sequence of the valid alleles where this gene is a part of.
      Returns:
      the sequence of the valid alleles.
    • alleleIndex

      public int alleleIndex()
      Return the index of the allele this gene is representing.
      Returns:
      the index of the allele this gene is representing.
    • allele

      public A allele()
      Description copied from interface: Gene
      Return the allele of this gene.
      Specified by:
      allele in interface Gene<A,EnumGene<A>>
      Returns:
      the allele of this gene.
    • isValid

      public boolean isValid()
      Description copied from interface: Verifiable
      Check if this object is valid.
      Specified by:
      isValid in interface Verifiable
      Returns:
      true if this object is valid, false otherwise.
    • newInstance

      public EnumGene<A> newInstance()
      Description copied from interface: Gene
      Return a new, random gene with the same type and with the same constraints as this gene. For all genes returned by this method holds gene.getClass() == gene.newInstance().getClass(). Implementations of this method have to use the Random object which can be fetched from the RandomRegistry.
      Specified by:
      newInstance in interface Factory<A>
      Specified by:
      newInstance in interface Gene<A,EnumGene<A>>
      Returns:
      a new instance of type T
    • newInstance

      public EnumGene<A> newInstance(A value)
      Create a new gene from the given value and the gene context.
      Specified by:
      newInstance in interface Gene<A,EnumGene<A>>
      Parameters:
      value - the value of the new gene.
      Returns:
      a new gene with the given value.
      Since:
      1.6
    • compareTo

      public int compareTo(EnumGene<A> gene)
      Specified by:
      compareTo in interface Comparable<A>
    • 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

      public static <A> EnumGene<A> of(int alleleIndex, ISeq<? extends A> validAlleles)
      Create a new enum gene from the given valid genes and the chosen allele index.
      Type Parameters:
      A - the allele type
      Parameters:
      alleleIndex - the index of the allele for this gene.
      validAlleles - the sequence of valid alleles.
      Returns:
      a new EnumGene with the given with the allele validAlleles.get(alleleIndex)
      Throws:
      IllegalArgumentException - if the give valid alleles sequence is empty
      NullPointerException - if the valid alleles seq is null.
      Since:
      3.4
    • of

      public static <A> EnumGene<A> of(ISeq<? extends A> validAlleles)
      Return a new enum gene with an allele randomly chosen from the given valid alleles.
      Type Parameters:
      A - the allele type
      Parameters:
      validAlleles - the sequence of valid alleles.
      Returns:
      a new EnumGene with a randomly chosen allele from the sequence of valid alleles
      Throws:
      IllegalArgumentException - if the give valid alleles sequence is empty
      NullPointerException - if the valid alleles seq is null.
    • of

      @SafeVarargs public static <A> EnumGene<A> of(int alleleIndex, A... validAlleles)
      Create a new enum gene from the given valid genes and the chosen allele index.
      Type Parameters:
      A - the allele type
      Parameters:
      alleleIndex - the index of the allele for this gene.
      validAlleles - the array of valid alleles.
      Returns:
      a new EnumGene with the given with the allele validAlleles[alleleIndex]
      Throws:
      IllegalArgumentException - if the give valid alleles array is empty of the allele index is out of range.
    • of

      @SafeVarargs public static <A> EnumGene<A> of(A... validAlleles)
      Return a new enum gene with an allele randomly chosen from the given valid alleles.
      Type Parameters:
      A - the allele type
      Parameters:
      validAlleles - the array of valid alleles.
      Returns:
      a new EnumGene with a randomly chosen allele from the sequence of valid alleles
      Throws:
      IllegalArgumentException - if the give valid alleles array is empty