public final class Engine<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object
public class RealFunction {
// Definition of the fitness function.
private static Double evaluate(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::evaluate,
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
.
This class is thread safe: No mutable state is maintained by the engine. Therefore it is save to create multiple evolution streams with one engine, which may be actually used in different threads.
Engine.Builder
,
EvolutionResult
,
EvolutionStream
,
EvolutionStatistics
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 |
---|---|
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> fitnessFunction,
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> fitnessFunction,
Factory<Genotype<G>> genotypeFactory)
Create a new evolution
Engine.Builder with the given fitness
function and genotype factory. |
EvolutionResult<G,C> |
evolve(Population<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. |
Function<? super Genotype<G>,? extends C> |
getFitnessFunction()
Return the fitness function of the GA engine.
|
Function<? super C,? extends C> |
getFitnessScaler()
Return the fitness scaler of the GA engine.
|
Factory<Genotype<G>> |
getGenotypeFactory()
Return the used genotype
Factory of the GA. |
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. |
Iterator<EvolutionResult<G,C>> |
iterator()
Create a new infinite evolution iterator with a newly created
population.
|
Iterator<EvolutionResult<G,C>> |
iterator(Iterable<Genotype<G>> genotypes)
Create a new infinite evolution iterator with the given initial
individuals.
|
Iterator<EvolutionResult<G,C>> |
iterator(Population<G,C> population,
long generation)
Create a new infinite evolution iterator with the given initial
population.
|
EvolutionStream<G,C> |
stream()
Create a new infinite evolution stream with a newly created
population.
|
EvolutionStream<G,C> |
stream(Iterable<Genotype<G>> genotypes)
Create a new infinite evolution stream with the given initial
individuals.
|
EvolutionStream<G,C> |
stream(Population<G,C> population,
long generation)
Create a new infinite evolution stream with the given initial
population.
|
public EvolutionResult<G,C> evolve(Population<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
public Iterator<EvolutionResult<G,C>> iterator()
public EvolutionStream<G,C> stream()
public EvolutionStream<G,C> stream(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 stream.
Missing individuals are created and individuals not needed are
skipped.NullPointerException
- if the given genotypes
is
null
.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
.public EvolutionStream<G,C> stream(Population<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 stream.
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 population
is
null
.IllegalArgumentException
- if the given generation
is smaller
then onepublic Iterator<EvolutionResult<G,C>> iterator(Population<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 onepublic Function<? super Genotype<G>,? extends C> getFitnessFunction()
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 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 an new one.public static <G extends Gene<?,G>,C extends Comparable<? super C>> Engine.Builder<G,C> builder(Function<? super Genotype<G>,? extends C> fitnessFunction, Factory<Genotype<G>> genotypeFactory)
Engine.Builder
with the given fitness
function and genotype factory.G
- the gene typeC
- the fitness function result typefitnessFunction
- 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> fitnessFunction, Chromosome<G> chromosome, Chromosome<G>... chromosomes)
Engine.Builder
with the given fitness
function and chromosome templates.G
- the gene typeC
- the fitness function result typefitnessFunction
- the fitness functionchromosome
- the first chromosomechromosomes
- the chromosome templatesNullPointerException
- if one of the arguments is
null
.© 2007-2014 Franz Wilhelmstötter (2014-12-28 10:45)