public final class Phenotype<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Comparable<Phenotype<G,C>>, Verifiable, Serializable, Runnable
Phenotype
consists of a Genotype
plus a fitness
Function
, where the fitness Function
represents the
environment where the Genotype
lives.
This class implements the Comparable
interface, to define a natural
order between two Phenotype
s. The natural order of the
Phenotypes
is defined by its fitness value (given by the
fitness Function
. The Phenotype
is immutable and therefore
can't be changed after creation.
The evaluation of the fitness function is performed lazily. Either by calling
one of the fitness accessors (getFitness()
or getRawFitness()
)
of through the evaluation methods run()
or evaluate()
.
Since the Phenotype
implements the Runnable
interface, it is
easily possible to perform the fitness function evaluation concurrently, by
putting it into an ExecutorService
.
Genotype
,
Serialized FormModifier and Type | Method and Description |
---|---|
int |
compareTo(Phenotype<G,C> pt) |
boolean |
equals(Object obj) |
Phenotype<G,C> |
evaluate()
Evaluates the (raw) fitness values and caches it so the fitness calculation
is performed only once.
|
long |
getAge(long currentGeneration)
Return the age of this phenotype depending on the given current generation.
|
C |
getFitness()
Return the fitness value of this
Phenotype . |
Function<? super Genotype<G>,? extends C> |
getFitnessFunction()
Deprecated.
Will be removed in a later version
|
Function<? super C,? extends C> |
getFitnessScaler()
Deprecated.
Will be removed in a later version
|
long |
getGeneration()
Return the generation this
Phenotype was created. |
Genotype<G> |
getGenotype()
This method returns a copy of the
Genotype , to guarantee a
immutable class. |
C |
getRawFitness()
Return the raw fitness (before scaling) of the phenotype.
|
int |
hashCode() |
boolean |
isEvaluated()
The fitness value of the phenotype is evaluated lazily.
|
boolean |
isValid()
Test whether this phenotype is valid.
|
Phenotype<G,C> |
newInstance(Genotype<G> genotype)
Create a new
Phenotype with a different Genotype but the
same generation , fitness function and fitness
scaler . |
Phenotype<G,C> |
newInstance(Genotype<G> genotype,
long generation)
|
Phenotype<G,C> |
newInstance(long generation,
Function<? super Genotype<G>,? extends C> function)
Deprecated.
Will be removed in a later version
|
Phenotype<G,C> |
newInstance(long generation,
Function<? super Genotype<G>,? extends C> function,
Function<? super C,? extends C> scaler)
Deprecated.
Will be removed in a later version
|
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Genotype<G> genotype,
long generation,
Function<? super Genotype<G>,? extends C> function,
Function<? super C,? extends C> scaler)
Create a new phenotype from the given arguments.
|
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Genotype<G> genotype,
long generation,
Function<? super Genotype<G>,? extends C> function,
Function<? super C,? extends C> scaler,
C rawFitness)
Create a new phenotype from the given arguments.
|
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
of(Genotype<G> genotype,
long generation,
Function<? super Genotype<G>,C> function)
The
Genotype is copied to guarantee an immutable class. |
void |
run()
This method simply calls the
evaluate() method. |
String |
toString() |
Phenotype<G,C> |
withFitness(C fitness)
Return a new
Phenotype object with the given raw fitness
value. |
public Genotype<G> getGenotype()
Genotype
, to guarantee a
immutable class.Genotype
of this Phenotype
.NullPointerException
- if one of the arguments is null
.public Phenotype<G,C> evaluate()
public boolean isEvaluated()
true
is this phenotype has been evaluated, false
otherwisepublic void run()
evaluate()
method. The purpose of
this method is to have a simple way for concurrent fitness calculation
for expensive fitness values.@Deprecated public Function<? super Genotype<G>,? extends C> getFitnessFunction()
@Deprecated public Function<? super C,? extends C> getFitnessScaler()
public C getFitness()
Phenotype
.Phenotype
.public C getRawFitness()
public long getGeneration()
Phenotype
was created.Phenotype
was created.public long getAge(long currentGeneration)
currentGeneration
- the current generation evaluated by the GA.currentGeneration - this.getGeneration()
.public boolean isValid()
Genotype
is valid.isValid
in interface Verifiable
public int compareTo(Phenotype<G,C> pt)
compareTo
in interface Comparable<Phenotype<G extends Gene<?,G>,C extends Comparable<? super C>>>
public Phenotype<G,C> withFitness(C fitness)
Phenotype
object with the given raw fitness
value. The returned phenotype is automatically evaluated:
isEvaluated() == true
fitness
- the phenotypes fitness valueNullPointerException
- if the given fitness
value is
null
public Phenotype<G,C> newInstance(Genotype<G> genotype)
Phenotype
with a different Genotype
but the
same generation
, fitness function
and fitness
scaler
.genotype
- the new genotypephenotype
with replaced genotype
NullPointerException
- if the given genotype
is null
.public Phenotype<G,C> newInstance(Genotype<G> genotype, long generation)
genotype
- the new genotype of the new phenotype.generation
- date of birth (generation) of the new phenotype.Phenotype
with the same fitness Function
.NullPointerException
- if the genotype
is null
.@Deprecated public Phenotype<G,C> newInstance(long generation, Function<? super Genotype<G>,? extends C> function, Function<? super C,? extends C> scaler)
generation
- the generation of the new phenotype.function
- the (new) fitness scaler of the created phenotype.scaler
- the (new) fitness scaler of the created phenotypeNullPointerException
- if one of the values is null
.IllegalArgumentException
- if the given generation
is
< 0
.@Deprecated public Phenotype<G,C> newInstance(long generation, Function<? super Genotype<G>,? extends C> function)
generation
- the generation of the new phenotype.function
- the (new) fitness scaler of the created phenotype.NullPointerException
- if one of the values is null
.IllegalArgumentException
- if the given generation
is
< 0
.public static <G extends Gene<?,G>,C extends Comparable<? super C>> Phenotype<G,C> of(Genotype<G> genotype, long generation, Function<? super Genotype<G>,C> function)
Genotype
is copied to guarantee an immutable class. Only
the age of the Phenotype
can be incremented.G
- the gene type of the chromosomeC
- the fitness value typegenotype
- the genotype of this phenotype.generation
- the current generation of the generated phenotype.function
- the fitness function of this phenotype.NullPointerException
- if one of the arguments is null
.IllegalArgumentException
- if the given generation
is
< 0
.public static <G extends Gene<?,G>,C extends Comparable<? super C>> Phenotype<G,C> of(Genotype<G> genotype, long generation, Function<? super Genotype<G>,? extends C> function, Function<? super C,? extends C> scaler)
G
- the gene type of the chromosomeC
- the fitness value typegenotype
- the genotype of this phenotype.generation
- the current generation of the generated phenotype.function
- the fitness function of this phenotype.scaler
- the fitness scaler.NullPointerException
- if one of the arguments is null
.IllegalArgumentException
- if the given generation
is
< 0
.public static <G extends Gene<?,G>,C extends Comparable<? super C>> Phenotype<G,C> of(Genotype<G> genotype, long generation, Function<? super Genotype<G>,? extends C> function, Function<? super C,? extends C> scaler, C rawFitness)
Engine.GenotypeEvaluator
strategy
then the default one.G
- the gene type of the chromosomeC
- the fitness value typegenotype
- the genotype of this phenotype.generation
- the current generation of the generated phenotype.function
- the fitness function of this phenotype.scaler
- the fitness scaler.rawFitness
- the known raw-fitness of the phenotype.NullPointerException
- if one of the arguments is null
.IllegalArgumentException
- if the given generation
is
< 0
.© 2007-2018 Franz Wilhelmstötter (2018-10-28 17:23)