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 TypeMethodDescriptionlongage(long currentGeneration) Return the age of this phenotype depending on the given current generation.intbooleanApplies 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 ofthisphenotype, orOptional.empty()if not evaluated yet.longReturn the generation thisPhenotypewas created.genotype()This method returns a copy of theGenotype, to guarantee a immutable class.inthashCode()booleanA phenotype instance can be created with or without fitness value.booleanisValid()Test whether this phenotype is valid.booleanA 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 newPhenotypeobject with the given raw fitness value.withGeneration(long generation) Return a newPhenotypeobject 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 setthisphenotype is returned.- Parameters:
ff- the fitness function- Returns:
- an evaluated phenotype or
thisif 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
Genotypeof 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:
trueis this phenotype has a fitness value assigned,falseotherwise- 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:
falseis this phenotype has a fitness value assigned,trueotherwise- 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 ofthisphenotype, orOptional.empty()if not evaluated yet.- Returns:
- the fitness value
- Since:
- 5.0
- See Also:
-
generation
Return the generation thisPhenotypewas created.- Returns:
- The generation this
Phenotypewas 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. Ifthisphenotype isn't evaluated,thisinstance 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 itsGenotypeis valid.- Specified by:
isValidin interfaceVerifiable- Returns:
- true if this phenotype is valid, false otherwise.
-
compareTo
- Specified by:
compareToin interfaceComparable<G extends Gene<?,G>>
-
hashCode
-
equals
-
toString
-
withFitness
Return a newPhenotypeobject 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 givenfitnessvalue isnull- Since:
- 4.2
-
withGeneration
Return a newPhenotypeobject 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 givengenerationis< 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 givengenerationis< 0.
-