Module io.jenetics.base
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
The
Phenotype
consists of a Genotype
, the current generation
and an optional fitness value. Once the fitness has been evaluated, a new
Phenotype
instance, with the calculated fitness, can be created with
the withFitness(Comparable)
.- Since:
- 1.0
- Version:
- 6.0
- See Also:
- Implementation Note:
- This class is immutable and thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionlong
age
(long currentGeneration) Return the age of this phenotype depending on the given current generation.int
boolean
Applies the given fitness function to the underlying genotype and return a new phenotype with the (newly) evaluated fitness function, if not already evaluated.fitness()
Return the fitness value of thisPhenotype
.Return the fitness value ofthis
phenotype, orOptional.empty()
if not evaluated yet.long
Return the generation thisPhenotype
was created.genotype()
This method returns a copy of theGenotype
, to guarantee a immutable class.int
hashCode()
boolean
A phenotype instance can be created with or without fitness value.boolean
isValid()
Test whether this phenotype is valid.boolean
A phenotype instance can be created with or without fitness value.Return a phenotype, where the fitness is set tonull
.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
Phenotype<G, C> Create a new phenotype from the given arguments.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
Phenotype<G, C> Create a new phenotype from the given arguments.toString()
withFitness
(C fitness) Return a newPhenotype
object with the given raw fitness value.withGeneration
(long generation) Return a newPhenotype
object with the given generation.
-
Method Details
-
eval
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:
- an evaluated phenotype or
this
if the fitness value is already set - Throws:
NullPointerException
- if the given fitness function isnull
- Since:
- 5.0
-
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
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 a fitness value assigned,false
otherwise- Since:
- 4.2
- See Also:
-
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 a fitness value assigned,true
otherwise- Since:
- 5.0
- See Also:
-
fitness
Return the fitness value of thisPhenotype
.- Returns:
- The fitness value of this
Phenotype
. - Throws:
NoSuchElementException
- ifisEvaluated()
returnsfalse
- See Also:
-
fitnessOptional
Return the fitness value ofthis
phenotype, orOptional.empty()
if not evaluated yet.- Returns:
- the fitness value
- Since:
- 5.0
- See Also:
-
generation
Return the generation thisPhenotype
was created.- Returns:
- The generation this
Phenotype
was created. - See Also:
-
age
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:
-
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
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
- Specified by:
compareTo
in interfaceComparable<G extends Gene<?,
G>>
-
hashCode
-
equals
-
toString
-
withFitness
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
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>, Phenotype<G,C extends Comparable<? super C>> 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>, Phenotype<G,C extends Comparable<? super C>> 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
.
-