Package io.jenetics
Class Phenotype<G extends Gene<?,G>,C extends Comparable<? super C>>
- java.lang.Object
-
- io.jenetics.Phenotype<G,C>
-
- Type Parameters:
G
- the gene typeC
- the fitness result type
- All Implemented Interfaces:
Verifiable
,Serializable
,Comparable<Phenotype<G,C>>
public final class Phenotype<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Comparable<Phenotype<G,C>>, Verifiable, Serializable
ThePhenotype
consists of aGenotype
, the current generation and an optional fitness value. Once the fitness has been evaluated, a newPhenotype
instance, with the calculated fitness, can be created with thewithFitness(Comparable)
.- Since:
- 1.0
- Version:
- 6.0
- Author:
- Franz Wilhelmstötter
- See Also:
Genotype
, 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 long
age(long currentGeneration)
Return the age of this phenotype depending on the given current generation.int
compareTo(Phenotype<G,C> pt)
boolean
equals(Object obj)
Phenotype<G,C>
eval(Function<? super Genotype<G>,? extends C> ff)
Applies the given fitness function to the underlying genotype and return a new phenotype with the (newly) evaluated fitness function, if not already evaluated.C
fitness()
Return the fitness value of thisPhenotype
.Optional<C>
fitnessOptional()
Return the fitness value ofthis
phenotype, orOptional.empty()
if not evaluated yet.long
generation()
Return the generation thisPhenotype
was created.Genotype<G>
genotype()
This method returns a copy of theGenotype
, to guarantee a immutable class.int
hashCode()
boolean
isEvaluated()
A phenotype instance can be created with or without fitness value.boolean
isValid()
Test whether this phenotype is valid.boolean
nonEvaluated()
A phenotype instance can be created with or without fitness value.Phenotype<G,C>
nullifyFitness()
Return a phenotype, where the fitness is set tonull
.static <G extends Gene<?,G>,C extends Comparable<? super C>>
Phenotype<G,C>of(Genotype<G> genotype, long generation)
Create a new phenotype from the given arguments.static <G extends Gene<?,G>,C extends Comparable<? super C>>
Phenotype<G,C>of(Genotype<G> genotype, long generation, C fitness)
Create a new phenotype from the given arguments.String
toString()
Phenotype<G,C>
withFitness(C fitness)
Return a newPhenotype
object with the given raw fitness value.Phenotype<G,C>
withGeneration(long generation)
Return a newPhenotype
object with the given generation.
-
-
-
Method Detail
-
eval
public Phenotype<G,C> eval(Function<? super Genotype<G>,? extends C> ff)
Applies the given fitness function to the underlying genotype and return a new phenotype with the (newly) evaluated fitness function, if not already evaluated. If the fitness value is already setthis
phenotype is returned.- Parameters:
ff
- the fitness function- Returns:
- a evaluated phenotype or
this
if the fitness value is already set - Throws:
NullPointerException
- if the given fitness function isnull
- Since:
- 5.0
-
genotype
public Genotype<G> genotype()
This method returns a copy of theGenotype
, to guarantee a immutable class.- Returns:
- the cloned
Genotype
of thisPhenotype
. - Throws:
NullPointerException
- if one of the arguments isnull
.
-
isEvaluated
public boolean isEvaluated()
A phenotype instance can be created with or without fitness value. Initially, the phenotype is created without fitness value. The fitness evaluation strategy is responsible for creating phenotypes with fitness value assigned.- Returns:
true
is this phenotype has an fitness value assigned,false
otherwise- Since:
- 4.2
- See Also:
nonEvaluated()
-
nonEvaluated
public boolean nonEvaluated()
A phenotype instance can be created with or without fitness value. Initially, the phenotype is created without fitness value. The fitness evaluation strategy is responsible for creating phenotypes with fitness value assigned.- Returns:
false
is this phenotype has an fitness value assigned,true
otherwise- Since:
- 5.0
- See Also:
isEvaluated()
-
fitness
public C fitness()
Return the fitness value of thisPhenotype
.- Returns:
- The fitness value of this
Phenotype
. - Throws:
NoSuchElementException
- ifisEvaluated()
returnsfalse
- See Also:
fitnessOptional()
-
fitnessOptional
public Optional<C> fitnessOptional()
Return the fitness value ofthis
phenotype, orOptional.empty()
if not evaluated yet.- Returns:
- the fitness value
- Since:
- 5.0
- See Also:
fitness()
-
generation
public long generation()
Return the generation thisPhenotype
was created.
-
age
public long age(long currentGeneration)
Return the age of this phenotype depending on the given current generation.- Parameters:
currentGeneration
- the current generation evaluated by the GA.- Returns:
- the age of this phenotype:
currentGeneration - this.getGeneration()
. - See Also:
generation()
-
nullifyFitness
public Phenotype<G,C> nullifyFitness()
Return a phenotype, where the fitness is set tonull
. Ifthis
phenotype isn't evaluated,this
instance is returned.- Returns:
- a phenotype, where the fitness is set to
null
- Since:
- 6.0
-
isValid
public boolean isValid()
Test whether this phenotype is valid. The phenotype is valid if itsGenotype
is valid.- Specified by:
isValid
in interfaceVerifiable
- Returns:
- true if this phenotype is valid, false otherwise.
-
compareTo
public int compareTo(Phenotype<G,C> pt)
- Specified by:
compareTo
in interfaceComparable<G extends Gene<?,G>>
-
withFitness
public Phenotype<G,C> withFitness(C fitness)
Return a newPhenotype
object with the given raw fitness value. The returned phenotype is automatically evaluated:isEvaluated() == true
- Parameters:
fitness
- the phenotypes fitness value- Returns:
- a new phenotype with the given fitness value
- Throws:
NullPointerException
- if the givenfitness
value isnull
- Since:
- 4.2
-
withGeneration
public Phenotype<G,C> withGeneration(long generation)
Return a newPhenotype
object with the given generation.- Parameters:
generation
- the generation of the newly created phenotype- Returns:
- a new phenotype with the given generation
- Since:
- 5.0
-
of
public static <G extends Gene<?,G>,C extends Comparable<? super C>> Phenotype<G,C> of(Genotype<G> genotype, long generation)
Create a new phenotype from the given arguments. The phenotype is created with a non assigned fitness function and the call ofisEvaluated()
will returnfalse
.- Type Parameters:
G
- the gene type of the chromosomeC
- the fitness value type- Parameters:
genotype
- the genotype of this phenotype.generation
- the current generation of the generated phenotype.- Returns:
- a new phenotype object
- Throws:
NullPointerException
- if one of the arguments isnull
.IllegalArgumentException
- if the givengeneration
is< 0
.
-
of
public static <G extends Gene<?,G>,C extends Comparable<? super C>> Phenotype<G,C> of(Genotype<G> genotype, long generation, C fitness)
Create a new phenotype from the given arguments.- Type Parameters:
G
- the gene type of the chromosomeC
- the fitness value type- Parameters:
genotype
- the genotype of this phenotype.generation
- the current generation of the generated phenotype.fitness
- the known fitness of the phenotype.- Returns:
- a new phenotype object
- Throws:
NullPointerException
- if one of the arguments isnull
.IllegalArgumentException
- if the givengeneration
is< 0
.
-
-