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 type
C - 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 Details

    • 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 set this 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 is null
      Since:
      5.0
    • genotype

      public Genotype<G> genotype()
      This method returns a copy of the Genotype, to guarantee a immutable class.
      Returns:
      the cloned Genotype of this Phenotype.
      Throws:
      NullPointerException - if one of the arguments is null.
    • 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 a fitness value assigned, false otherwise
      Since:
      4.2
      See Also:
    • 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 a fitness value assigned, true otherwise
      Since:
      5.0
      See Also:
    • fitness

      public C fitness()
      Return the fitness value of this Phenotype.
      Returns:
      The fitness value of this Phenotype.
      Throws:
      NoSuchElementException - if isEvaluated() returns false
      See Also:
    • fitnessOptional

      Return the fitness value of this phenotype, or Optional.empty() if not evaluated yet.
      Returns:
      the fitness value
      Since:
      5.0
      See Also:
    • generation

      public long generation()
      Return the generation this Phenotype was created.
      Returns:
      The generation this Phenotype was created.
      See Also:
    • 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:
    • nullifyFitness

      Return a phenotype, where the fitness is set to null. If this 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 its Genotype is valid.
      Specified by:
      isValid in interface Verifiable
      Returns:
      true if this phenotype is valid, false otherwise.
    • compareTo

      public int compareTo(Phenotype<G,C> pt)
      Specified by:
      compareTo in interface Comparable<G extends Gene<?,G>>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • withFitness

      public Phenotype<G,C> withFitness(C fitness)
      Return a new Phenotype 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 given fitness value is null
      Since:
      4.2
    • withGeneration

      public Phenotype<G,C> withGeneration(long generation)
      Return a new Phenotype 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 of isEvaluated() will return false.
      Type Parameters:
      G - the gene type of the chromosome
      C - 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 is null.
      IllegalArgumentException - if the given generation 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 chromosome
      C - 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 is null.
      IllegalArgumentException - if the given generation is < 0.