public final class Engine<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Evolution<G,C>, 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. |
Modifier and Type | Method and Description |
---|---|
EvolutionResult<G,C> |
apply(EvolutionStart<G,C> start)
Deprecated.
Will be removed and superseded by
evolve(EvolutionStart) |
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>> gtf)
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 . |
ISeq<Phenotype<G,C>> |
evaluate(Seq<Phenotype<G,C>> population)
Evaluates the fitness function of the given population with the configured
Evaluator of this engine and returns a new population
with its fitness value assigned. |
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 |
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. |
Constraint<G,C> |
getConstraint()
Return the constraint of the evolution problem.
|
Executor |
getExecutor()
Return the
Executor the engine is using for executing the
evolution steps. |
Factory<Genotype<G>> |
getGenotypeFactory()
Return the used genotype
Factory of the GA. |
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. |
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.
|
@Deprecated public EvolutionResult<G,C> apply(EvolutionStart<G,C> start)
evolve(EvolutionStart)
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>>>
Evolution
public EvolutionResult<G,C> evolve(EvolutionStart<G,C> start)
Evolution
start
object
New phenotypes are created with the fitness function and fitness scaler
defined by this engineevolve
in interface Evolution<G extends Gene<?,G>,C extends Comparable<? super C>>
start
- the evolution start objectEvolution.evolve(ISeq, long)
public ISeq<Phenotype<G,C>> evaluate(Seq<Phenotype<G,C>> population)
Evaluator
of this engine and returns a new population
with its fitness value assigned.population
- the population to evaluateIllegalStateException
- if the configured fitness function doesn't
return a population with the same size as the input population.
This exception is also thrown if one of the populations
phenotype has no fitness value assigned.Evaluator
,
Evaluator.eval(Seq)
public 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 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 Constraint<G,C> getConstraint()
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 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 <G extends Gene<?,G>,C extends Comparable<? super C>> Engine.Builder<G,C> builder(Function<? super Genotype<G>,? extends C> ff, Factory<Genotype<G>> gtf)
Engine.Builder
with the given fitness
function and genotype factory.G
- the gene typeC
- the fitness function result typeff
- the fitness functiongtf
- the genotype factoryNullPointerException
- 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 evaluatorcodec
- the problem codecNullPointerException
- if one of the arguments is
null
.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
@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
.© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)