public final class Engine<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Function<EvolutionStart<G,C>,EvolutionResult<G,C>>, EvolutionStreamable<G,C>
public class RealFunction {
// Definition of the fitness function.
private static Double eval(final Genotype<DoubleGene> gt) {
final double x = gt.getGene().doubleValue();
return cos(0.5 + sin(x))*cos(x);
}
public static void main(String[] args) {
// Create/configuring the engine via its builder.
final Engine<DoubleGene, Double> engine = Engine
.builder(
RealFunction::eval,
DoubleChromosome.of(0.0, 2.0*PI))
.populationSize(500)
.optimize(Optimize.MINIMUM)
.alterers(
new Mutator<>(0.03),
new MeanAlterer<>(0.6))
.build();
// Execute the GA (engine).
final Phenotype<DoubleGene, Double> result = engine.stream()
// Truncate the evolution stream if no better individual could
// be found after 5 consecutive generations.
.limit(bySteadyFitness(5))
// Terminate the evolution after maximal 100 generations.
.limit(100)
.collect(toBestPhenotype());
}
}
Engine
is configured via the Engine.Builder
class and can't be changed after creation. The actual evolution is
performed by the EvolutionStream
, which is created by the
Engine
.Engine.Builder
,
EvolutionStart
,
EvolutionResult
,
EvolutionStream
,
EvolutionStatistics
,
Codec
Modifier and Type | Class and Description |
---|---|
static class |
Engine.Builder<G extends Gene<?,G>,C extends Comparable<? super C>>
Builder class for building GA
Engine instances. |
static interface |
Engine.Evaluator<G extends Gene<?,G>,C extends Comparable<? super C>>
This interface allows to define different strategies for evaluating the
fitness functions of a given population.
|
static interface |
Engine.GenotypeEvaluator<G extends Gene<?,G>,C extends Comparable<? super C>>
This interface gives a different possibility in evaluating the fitness
values of a population.
|
Modifier and Type | Method and Description |
---|---|
EvolutionResult<G,C> |
apply(EvolutionStart<G,C> start)
This method is an alias for the
evolve(EvolutionStart)
method. |
Engine.Builder<G,C> |
builder()
Create a new evolution
Engine.Builder initialized with the values
of the current evolution Engine . |
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
builder(Function<? super Genotype<G>,? extends C> ff,
Chromosome<G> chromosome,
Chromosome<G>... chromosomes)
Create a new evolution
Engine.Builder with the given fitness
function and chromosome templates. |
static <G extends Gene<?,G>,C extends Comparable<? super C>> |
builder(Function<? super Genotype<G>,? extends C> ff,
Factory<Genotype<G>> genotypeFactory)
Create a new evolution
Engine.Builder with the given fitness
function and genotype factory. |
static <T,G extends Gene<?,G>,C extends Comparable<? super C>> |
builder(Function<? super T,? extends C> ff,
Codec<T,G> codec)
Create a new evolution
Engine.Builder with the given fitness
function and problem codec . |
static <T,G extends Gene<?,G>,C extends Comparable<? super C>> |
builder(Problem<T,G,C> problem)
Create a new evolution
Engine.Builder for the given
Problem . |
EvolutionResult<G,C> |
evolve(EvolutionStart<G,C> start)
Perform one evolution step with the given evolution
start object
New phenotypes are created with the fitness function and fitness scaler
defined by this engine |
EvolutionResult<G,C> |
evolve(ISeq<Phenotype<G,C>> population,
long generation)
Perform one evolution step with the given
population and
generation . |
Alterer<G,C> |
getAlterer()
Return the used
Alterer of the GA. |
Clock |
getClock()
Return the
Clock the engine is using for measuring the execution
time. |
Executor |
getExecutor()
Return the
Executor the engine is using for executing the
evolution steps. |
Function<? super Genotype<G>,? extends C> |
getFitnessFunction()
Return the fitness function of the GA engine.
|
Function<? super C,? extends C> |
getFitnessScaler()
Deprecated.
The fitness scaler will be remove in a future version.
|
Factory<Genotype<G>> |
getGenotypeFactory()
Return the used genotype
Factory of the GA. |
int |
getIndividualCreationRetries()
Return the maximal number of attempt before the
Engine gives
up creating a valid individual (Phenotype ). |
UnaryOperator<EvolutionResult<G,C>> |
getMapper()
Return the evolution result mapper.
|
long |
getMaximalPhenotypeAge()
Return the maximal allowed phenotype age.
|
int |
getOffspringCount()
Return the number of selected offsprings.
|
Selector<G,C> |
getOffspringSelector()
Return the used offspring
Selector of the GA. |
Optimize |
getOptimize()
Return the optimization strategy.
|
int |
getPopulationSize()
Return the number of individuals of a population.
|
int |
getSurvivorsCount()
The number of selected survivors.
|
Selector<G,C> |
getSurvivorsSelector()
Return the used survivor
Selector of the GA. |
default Iterator<EvolutionResult<G,C>> |
iterator()
Deprecated.
Marked for removal in the next major version.
|
Iterator<EvolutionResult<G,C>> |
iterator(EvolutionInit<G> init)
Deprecated.
|
default Iterator<EvolutionResult<G,C>> |
iterator(EvolutionResult<G,C> result)
Deprecated.
Marked for removal in the next major version.
|
default Iterator<EvolutionResult<G,C>> |
iterator(EvolutionStart<G,C> start)
Deprecated.
Marked for removal in the next major version.
|
default Iterator<EvolutionResult<G,C>> |
iterator(ISeq<Phenotype<G,C>> population)
Deprecated.
Marked for removal in the next major version.
|
default Iterator<EvolutionResult<G,C>> |
iterator(ISeq<Phenotype<G,C>> population,
long generation)
Deprecated.
Marked for removal in the next major version.
|
default Iterator<EvolutionResult<G,C>> |
iterator(Iterable<Genotype<G>> genotypes)
Deprecated.
Marked for removal in the next major version.
|
default Iterator<EvolutionResult<G,C>> |
iterator(Iterable<Genotype<G>> genotypes,
long generation)
Deprecated.
Marked for removal in the next major version.
|
Iterator<EvolutionResult<G,C>> |
iterator(Supplier<EvolutionStart<G,C>> start)
Deprecated.
|
EvolutionStream<G,C> |
stream(EvolutionInit<G> init)
Create a new, possibly infinite, evolution stream with the given
initial value.
|
EvolutionStream<G,C> |
stream(Supplier<EvolutionStart<G,C>> start)
Create a new, possibly infinite, evolution stream with the given
evolution start.
|
public EvolutionResult<G,C> evolve(ISeq<Phenotype<G,C>> population, long generation)
population
and
generation
. New phenotypes are created with the fitness function
and fitness scaler defined by this engine
This method is thread-safe.
population
- the population to evolvegeneration
- the current generation; used for calculating the
phenotype age.NullPointerException
- if the given population
is
null
IllegalArgumentException
- if the given generation
is
smaller then oneevolve(EvolutionStart)
public EvolutionResult<G,C> evolve(EvolutionStart<G,C> start)
start
object
New phenotypes are created with the fitness function and fitness scaler
defined by this engine
This method is thread-safe.
start
- the evolution start objectNullPointerException
- if the given evolution
start
is null
evolve(ISeq, long)
public EvolutionResult<G,C> apply(EvolutionStart<G,C> start)
evolve(EvolutionStart)
method.apply
in interface Function<EvolutionStart<G extends Gene<?,G>,C extends Comparable<? super C>>,EvolutionResult<G extends Gene<?,G>,C extends Comparable<? super C>>>
@Deprecated public Iterator<EvolutionResult<G,C>> iterator(Supplier<EvolutionStart<G,C>> start)
Population
is given, the engines genotype
factory is used for creating the population. The given population might
be the result of an other engine and this method allows to start the
evolution with the outcome of an different engine. The fitness function
and the fitness scaler are replaced by the one defined for this engine.start
- the data the evolution stream starts with@Deprecated public Iterator<EvolutionResult<G,C>> iterator(EvolutionInit<G> init)
Population
is given, the engines genotype
factory is used for creating the population. The given population might
be the result of an other engine and this method allows to start the
evolution with the outcome of an different engine. The fitness function
and the fitness scaler are replaced by the one defined for this engine.init
- the data the evolution iterator is initialized withpublic EvolutionStream<G,C> stream(Supplier<EvolutionStart<G,C>> start)
EvolutionStreamable
Population
is given, the engines
genotype factory is used for creating the population. The given
population might be the result of an other engine and this method allows
to start the evolution with the outcome of an different engine.
The fitness function and the fitness scaler are replaced by the one
defined for this engine.stream
in interface EvolutionStreamable<G extends Gene<?,G>,C extends Comparable<? super C>>
start
- the data the evolution stream starts withpublic EvolutionStream<G,C> stream(EvolutionInit<G> init)
EvolutionStreamable
Population
is given, the engines genotype
factory is used for creating the population. The given population might
be the result of an other engine and this method allows to start the
evolution with the outcome of an different engine. The fitness function
and the fitness scaler are replaced by the one defined for this engine.stream
in interface EvolutionStreamable<G extends Gene<?,G>,C extends Comparable<? super C>>
init
- the data the evolution stream is initialized withpublic Function<? super Genotype<G>,? extends C> getFitnessFunction()
@Deprecated public Function<? super C,? extends C> getFitnessScaler()
public Factory<Genotype<G>> getGenotypeFactory()
Factory
of the GA. The genotype factory
is used for creating the initial population and new, random individuals
when needed (as replacement for invalid and/or died genotypes).Factory
of the GA.public Selector<G,C> getSurvivorsSelector()
Selector
of the GA.Selector
of the GA.public Selector<G,C> getOffspringSelector()
Selector
of the GA.Selector
of the GA.public Alterer<G,C> getAlterer()
Alterer
of the GA.Alterer
of the GA.public int getOffspringCount()
public int getSurvivorsCount()
public int getPopulationSize()
public long getMaximalPhenotypeAge()
public Optimize getOptimize()
public Clock getClock()
Clock
the engine is using for measuring the execution
time.public Executor getExecutor()
Executor
the engine is using for executing the
evolution steps.public int getIndividualCreationRetries()
Engine
gives
up creating a valid individual (Phenotype
).Phenotype
creation attemptspublic UnaryOperator<EvolutionResult<G,C>> getMapper()
public Engine.Builder<G,C> builder()
Engine.Builder
initialized with the values
of the current evolution Engine
. With this method, the evolution
engine can serve as a template for a new one.public static <T,G extends Gene<?,G>,C extends Comparable<? super C>> Engine.Builder<G,C> builder(Problem<T,G,C> problem)
Engine.Builder
for the given
Problem
.T
- the (native) argument type of the problem fitness functionG
- the gene type the evolution engine is working withC
- the result type of the fitness functionproblem
- the problem to be solved by the evolution Engine
Engine.Builder
public static <G extends Gene<?,G>,C extends Comparable<? super C>> Engine.Builder<G,C> builder(Function<? super Genotype<G>,? extends C> ff, Factory<Genotype<G>> genotypeFactory)
Engine.Builder
with the given fitness
function and genotype factory.G
- the gene typeC
- the fitness function result typeff
- the fitness functiongenotypeFactory
- the genotype factoryNullPointerException
- if one of the arguments is
null
.@SafeVarargs public static <G extends Gene<?,G>,C extends Comparable<? super C>> Engine.Builder<G,C> builder(Function<? super Genotype<G>,? extends C> ff, Chromosome<G> chromosome, Chromosome<G>... chromosomes)
Engine.Builder
with the given fitness
function and chromosome templates.G
- the gene typeC
- the fitness function result typeff
- the fitness functionchromosome
- the first chromosomechromosomes
- the chromosome templatesNullPointerException
- if one of the arguments is
null
.public static <T,G extends Gene<?,G>,C extends Comparable<? super C>> Engine.Builder<G,C> builder(Function<? super T,? extends C> ff, Codec<T,G> codec)
Engine.Builder
with the given fitness
function and problem codec
.T
- the fitness function input typeC
- the fitness function result typeG
- the gene typeff
- the fitness functioncodec
- the problem codecNullPointerException
- if one of the arguments is
null
.@Deprecated public Iterator<EvolutionResult<G,C>> iterator()
@Deprecated public Iterator<EvolutionResult<G,C>> iterator(EvolutionStart<G,C> start)
Population
is given, the engines genotype
factory is used for creating the population. The given population might
be the result of an other engine and this method allows to start the
evolution with the outcome of an different engine. The fitness function
and the fitness scaler are replaced by the one defined for this engine.start
- the data the evolution stream starts withNullPointerException
- if the given evolution
start
is null
.@Deprecated public Iterator<EvolutionResult<G,C>> iterator(EvolutionResult<G,C> result)
EvolutionResult
. The iterator is initialized
with the population of the given result
and its total generation
EvolutionResult.getTotalGenerations()
.result
- the previously evolved EvolutionResult
NullPointerException
- if the given evolution result
is
null
@Deprecated public Iterator<EvolutionResult<G,C>> iterator(ISeq<Phenotype<G,C>> population, long generation)
Population
is given, the engines genotype
factory is used for creating the population. The given population might
be the result of an other engine and this method allows to start the
evolution with the outcome of an different engine. The fitness function
and the fitness scaler are replaced by the one defined for this engine.population
- the initial individuals used for the evolution iterator.
Missing individuals are created and individuals not needed are
skipped.generation
- the generation the iterator starts from; must be greater
than zero.NullPointerException
- if the given population
is
null
.IllegalArgumentException
- if the given generation
is smaller
then one@Deprecated public Iterator<EvolutionResult<G,C>> iterator(ISeq<Phenotype<G,C>> population)
Population
is given, the engines genotype
factory is used for creating the population. The given population might
be the result of an other engine and this method allows to start the
evolution with the outcome of an different engine. The fitness function
and the fitness scaler are replaced by the one defined for this engine.population
- the initial individuals used for the evolution iterator.
Missing individuals are created and individuals not needed are
skipped.NullPointerException
- if the given population
is
null
.@Deprecated public Iterator<EvolutionResult<G,C>> iterator(Iterable<Genotype<G>> genotypes, long generation)
Iterable
is given, the engines genotype
factory is used for creating the population.genotypes
- the initial individuals used for the evolution iterator.
Missing individuals are created and individuals not needed are
skipped.generation
- the generation the stream starts from; must be greater
than zero.NullPointerException
- if the given genotypes
is
null
.IllegalArgumentException
- if the given generation
is
smaller then one@Deprecated public Iterator<EvolutionResult<G,C>> iterator(Iterable<Genotype<G>> genotypes)
Iterable
is given, the engines genotype
factory is used for creating the population.genotypes
- the initial individuals used for the evolution iterator.
Missing individuals are created and individuals not needed are
skipped.NullPointerException
- if the given genotypes
is
null
.© 2007-2018 Franz Wilhelmstötter (2018-10-28 17:23)