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.gene().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
,
Constraint
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 |
---|---|
Alterer<G,C> |
alterer()
Return the used
Alterer of the GA. |
EvolutionResult<G,C> |
apply(EvolutionStart<G,C> start)
Deprecated.
Will be removed and superseded by
evolve(EvolutionStart) |
Engine.Builder<G,C> |
builder()
Deprecated.
Use
toBuilder() instead |
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 . |
Clock |
clock()
Return the
Clock the engine is using for measuring the execution
time. |
Constraint<G,C> |
constraint()
Return the constraint of the evolution 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 |
Executor |
executor()
Return the
Executor the engine is using for executing the
evolution steps. |
Factory<Genotype<G>> |
genotypeFactory()
Return the used genotype
Factory of the GA. |
Alterer<G,C> |
getAlterer()
Deprecated.
Use
alterer() instead |
Clock |
getClock()
Deprecated.
Use
clock() instead |
Constraint<G,C> |
getConstraint()
Deprecated.
Use
constraint() instead |
Executor |
getExecutor()
Deprecated.
Use
executor() instead |
Factory<Genotype<G>> |
getGenotypeFactory()
Deprecated.
Use
genotypeFactory() instead |
UnaryOperator<EvolutionResult<G,C>> |
getMapper()
Deprecated.
Use
mapper() instead |
long |
getMaximalPhenotypeAge()
Deprecated.
Use
maximalPhenotypeAge() instead |
int |
getOffspringCount()
Deprecated.
Use
offspringSize() instead |
Selector<G,C> |
getOffspringSelector()
Deprecated.
Use
offspringSelector() instead |
Optimize |
getOptimize()
Deprecated.
Use
optimize() instead |
int |
getPopulationSize()
Deprecated.
Use
populationSize() instead |
int |
getSurvivorsCount()
Deprecated.
Use
survivorsSize() instead |
Selector<G,C> |
getSurvivorsSelector()
Deprecated.
Use
survivorsSelector() instead |
UnaryOperator<EvolutionResult<G,C>> |
mapper()
Return the evolution result mapper.
|
long |
maximalPhenotypeAge()
Return the maximal allowed phenotype age.
|
Selector<G,C> |
offspringSelector()
Return the used offspring
Selector of the GA. |
int |
offspringSize()
Return the number of selected offspring.
|
Optimize |
optimize()
Return the optimization strategy.
|
int |
populationSize()
Return the number of individuals of a population.
|
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.
|
Selector<G,C> |
survivorsSelector()
Return the used survivor
Selector of the GA. |
int |
survivorsSize()
The number of selected survivors.
|
Engine.Builder<G,C> |
toBuilder()
Create a new evolution
Engine.Builder initialized with the values
of the current evolution Engine . |
@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>> genotypeFactory()
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.@Deprecated public Factory<Genotype<G>> getGenotypeFactory()
genotypeFactory()
insteadFactory
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> constraint()
@Deprecated public Constraint<G,C> getConstraint()
constraint()
insteadpublic Selector<G,C> survivorsSelector()
Selector
of the GA.Selector
of the GA.@Deprecated public Selector<G,C> getSurvivorsSelector()
survivorsSelector()
insteadSelector
of the GA.Selector
of the GA.public Selector<G,C> offspringSelector()
Selector
of the GA.Selector
of the GA.@Deprecated public Selector<G,C> getOffspringSelector()
offspringSelector()
insteadSelector
of the GA.Selector
of the GA.public Alterer<G,C> alterer()
Alterer
of the GA.Alterer
of the GA.@Deprecated public Alterer<G,C> getAlterer()
alterer()
insteadAlterer
of the GA.Alterer
of the GA.public int offspringSize()
@Deprecated public int getOffspringCount()
offspringSize()
insteadpublic int survivorsSize()
@Deprecated public int getSurvivorsCount()
survivorsSize()
insteadpublic int populationSize()
@Deprecated public int getPopulationSize()
populationSize()
insteadpublic long maximalPhenotypeAge()
@Deprecated public long getMaximalPhenotypeAge()
maximalPhenotypeAge()
insteadpublic Optimize optimize()
@Deprecated public Optimize getOptimize()
optimize()
insteadpublic Clock clock()
Clock
the engine is using for measuring the execution
time.@Deprecated public Clock getClock()
clock()
insteadClock
the engine is using for measuring the execution
time.public Executor executor()
Executor
the engine is using for executing the
evolution steps.@Deprecated public Executor getExecutor()
executor()
insteadExecutor
the engine is using for executing the
evolution steps.public UnaryOperator<EvolutionResult<G,C>> mapper()
@Deprecated public UnaryOperator<EvolutionResult<G,C>> getMapper()
mapper()
insteadpublic Engine.Builder<G,C> toBuilder()
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.@Deprecated public Engine.Builder<G,C> builder()
toBuilder()
insteadEngine.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-2020 Franz Wilhelmstötter (2020-02-18 20:01)