Package io.jenetics
Class EnumGene<A>
- java.lang.Object
-
- io.jenetics.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
The following code shows how to create a combinatorial genotype factory which can be used when creating anPermutationChromosome
.Engine
instance.The following code shows the assurances of thefinal ISeq<Integer> alleles = ISeq.of(1, 2, 3, 4, 5, 6, 7, 8); final Factory<Genotype<EnumGene<Integer>>> gtf = Genotype.of( PermutationChromosome.of(alleles) );
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
- Author:
- Franz Wilhelmstötter
- See Also:
PermutationChromosome
,PartiallyMatchedCrossover
, Serialized Form- Implementation Note:
- This class is immutable and thread-safe.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description A
allele()
Return the allele of this gene.int
alleleIndex()
Return the index of the allele this gene is representing.int
compareTo(EnumGene<A> gene)
boolean
equals(Object obj)
int
hashCode()
boolean
isValid()
Check if this object is valid.EnumGene<A>
newInstance()
Return a new, random gene with the same type and with the same constraints than this gene.EnumGene<A>
newInstance(A value)
Create a new gene from the givenvalue
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.String
toString()
ISeq<A>
validAlleles()
Return sequence of the valid alleles where this gene is a part of.
-
-
-
Method Detail
-
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.
-
isValid
public boolean isValid()
Description copied from interface:Verifiable
Check if this object is valid.- Specified by:
isValid
in interfaceVerifiable
- 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 than this gene. For all genes returned by this method holdsgene.getClass() == gene.newInstance().getClass()
. Implementations of this method has to use theRandom
object which can be fetched from theRandomRegistry
.- Specified by:
newInstance
in interfaceFactory<A>
- Specified by:
newInstance
in interfaceGene<A,EnumGene<A>>
- Returns:
- a new instance of type T
-
newInstance
public EnumGene<A> newInstance(A value)
Create a new gene from the givenvalue
and the gene context.- Specified by:
newInstance
in interfaceGene<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 interfaceComparable<A>
-
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 allelevalidAlleles.get(alleleIndex)
- Throws:
IllegalArgumentException
- if the give valid alleles sequence is emptyNullPointerException
- if the valid alleles seq isnull
.- 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 emptyNullPointerException
- if the valid alleles seq isnull
.
-
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 allelevalidAlleles[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
-
-