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 theGenotype
. It is the structural representative of an individual. This class is the encoded problem solution with one to manyChromosome
.The code snippet above creates a genotype with the same structure as shown in the figure above. In this example thefinal 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) );
DoubleGene
has been chosen as gene type.- Since:
- 1.0
- Version:
- 6.0
- See Also:
Chromosome
,Phenotype
, 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 Chromosome<G>
chromosome()
Return the first chromosome.boolean
equals(Object obj)
G
gene()
int
geneCount()
Return the number of genes this genotype consists of.Chromosome<G>
get(int index)
Return the chromosome at the given index.int
hashCode()
boolean
isValid()
Test if this genotype is valid.int
length()
Getting the number of chromosomes of this genotype.Genotype<G>
newInstance()
Return a new, random genotype by creating new, random chromosomes (calling theFactory.newInstance()
method) from the chromosomes of this genotype.static <G extends Gene<?,G>>
Genotype<G>of(Chromosome<G> first, Chromosome<G>... rest)
Create a newGenotype
from a given array ofChromosomes
.static <G extends Gene<?,G>>
Genotype<G>of(Factory<? extends Chromosome<G>> factory, int n)
Create a newGenotype
which consists ofn
chromosomes, which are created by the givenfactory
.static <G extends Gene<?,G>>
Genotype<G>of(Iterable<? extends Chromosome<G>> chromosomes)
Create a newGenotype
from a given array ofchromosomes
.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.jenetics.util.BaseSeq
forEach, isEmpty, iterator, listIterator, nonEmpty, spliterator, stream
-
-
-
-
Method Detail
-
get
public Chromosome<G> get(int index)
Return the chromosome at the given index. It is guaranteed, that the returned chromosome is not null.
-
length
public int length()
Getting the number of chromosomes of this genotype.
-
chromosome
public Chromosome<G> chromosome()
Return the first chromosome. This is an alias forfinal Genotype<DoubleGene>; gt = ... final Chromosome<DoubleGene> chromosome = gt.get(0);
- Returns:
- The first chromosome.
- Since:
- 5.2
-
gene
public G gene()
Return the firstGene
of the firstChromosome
of thisGenotype
. This is an alias forfinal Genotype<DoubleGene> gt = ... final DoubleGene gene = gt.get(0).get(0);
- Returns:
- the first
Gene
of the firstChromosome
of thisGenotype
. - 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 itsChromosome
s are valid.- Specified by:
isValid
in interfaceVerifiable
- Returns:
- true if this genotype is valid, false otherwise.
-
newInstance
public Genotype<G> newInstance()
Return a new, random genotype by creating new, random chromosomes (calling theFactory.newInstance()
method) from the chromosomes of this genotype.- Specified by:
newInstance
in interfaceFactory<G extends Gene<?,G>>
- Returns:
- a new instance of type T
-
of
@SafeVarargs public static <G extends Gene<?,G>> Genotype<G> of(Chromosome<G> first, Chromosome<G>... rest)
Create a newGenotype
from a given array ofChromosomes
.- Type Parameters:
G
- the gene type- Parameters:
first
- the firstChromosome
of theGenotype
rest
- the rest of the genotypes chromosomes.- Returns:
- a new
Genotype
from the given chromosomes - Throws:
NullPointerException
- ifchromosomes
isnull
or one of its element.- Since:
- 3.0
-
of
public static <G extends Gene<?,G>> Genotype<G> of(Factory<? extends Chromosome<G>> factory, int n)
Create a newGenotype
which consists ofn
chromosomes, which are created by the givenfactory
. This method can be used for easily creating a gene matrix. The following example will create a 10x5DoubleGene
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 ofn
- the number of chromosomes this genotype consists of- Returns:
- new
Genotype
containingn
chromosomes - Throws:
IllegalArgumentException
- ifn < 1
.NullPointerException
- if thefactory
isnull
.- Since:
- 3.0
-
of
public static <G extends Gene<?,G>> Genotype<G> of(Iterable<? extends Chromosome<G>> chromosomes)
Create a newGenotype
from a given array ofchromosomes
.- Type Parameters:
G
- the gene type- Parameters:
chromosomes
- theChromosome
s the returned genotype consists of- Returns:
- a new
Genotype
from the given chromosomes - Throws:
NullPointerException
- ifchromosomes
isnull
or one of its element.IllegalArgumentException
- ifchromosome.length() < 1
.- Since:
- 3.0
-
-