Package io.jenetics

Class EnumGene<A>

  • All Implemented Interfaces:
    Gene<A,​EnumGene<A>>, Factory<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:
    PermutationChromosome, PartiallyMatchedCrossover, Serialized Form
    Implementation Note:
    This class is immutable and thread-safe.
    • Method Detail

      • validAlleles

        public ISeq<AvalidAlleles()
        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<AnewInstance()
        Description copied from interface: Gene
        Return a new, random gene with the same type and with the same constraints than this gene. For all genes returned by this method holds gene.getClass() == gene.newInstance().getClass(). Implementations of this method has 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<AnewInstance​(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
      • 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 an 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 an randomly chosen allele from the sequence of valid alleles
        Throws:
        IllegalArgumentException - if the give valid alleles array is empty