public final class Engine<G extends Gene<?,G>,C extends Comparable<? super C>> extends Object implements Function<EvolutionStart<G,C>,EvolutionResult<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.
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,
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)
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(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. |
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()
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(EvolutionResult<G,C> result)
Create a new infinite evolution iterator starting with a
previously evolved
EvolutionResult. |
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(Iterable<Genotype<G>> genotypes,
long generation)
Create a new infinite evolution iterator with the given initial
individuals.
|
Iterator<EvolutionResult<G,C>> |
iterator(Population<G,C> population)
Create a new infinite evolution iterator with the given initial
population.
|
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(EvolutionResult<G,C> result)
Create a new
EvolutionStream starting with a previously evolved
EvolutionResult. |
EvolutionStream<G,C> |
stream(Iterable<Genotype<G>> genotypes)
Create a new infinite evolution stream with the given initial
individuals.
|
EvolutionStream<G,C> |
stream(Iterable<Genotype<G>> genotypes,
long generation)
Create a new infinite evolution stream with the given initial
individuals.
|
EvolutionStream<G,C> |
stream(Population<G,C> population)
Create a new infinite evolution stream with the given initial
population.
|
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
nullIllegalArgumentException - 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 nullevolve(org.jenetics.Population, 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>>>public Iterator<EvolutionResult<G,C>> iterator()
public EvolutionStream<G,C> stream()
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(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, 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 onepublic EvolutionStream<G,C> stream(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 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 genotypes is
null.IllegalArgumentException - if the given generation is
smaller then onepublic Iterator<EvolutionResult<G,C>> iterator(Population<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.public EvolutionStream<G,C> stream(Population<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 stream.
Missing individuals are created and individuals not needed are
skipped.NullPointerException - if the given population is
null.public 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 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(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 EvolutionResultNullPointerException - if the given evolution result is
nullpublic EvolutionStream<G,C> stream(EvolutionResult<G,C> result)
EvolutionStream starting with a previously evolved
EvolutionResult. The stream is initialized with the population
of the given result and its total generation
EvolutionResult.getTotalGenerations().
private static final Problem<Double, DoubleGene, Double>
PROBLEM = Problem.of(
x -> cos(0.5 + sin(x))*cos(x),
codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI))
);
private static final Engine<DoubleGene, Double>
ENGINE = Engine.builder(PROBLEM)
.optimize(Optimize.MINIMUM)
.offspringSelector(new RouletteWheelSelector<>())
.build();
public static void main(final String[] args) throws IOException {
// Result of the first evolution run.
final EvolutionResult<DoubleGene, Double> rescue = ENGINE.stream()
.limit(limit.bySteadyFitness(10))
.collect(EvolutionResult.toBestEvolutionResult());
// Save the result of the first run into a file.
final Path path = Paths.get("result.bin");
IO.object.write(rescue, path);
// Load the previous result and continue evolution.
@SuppressWarnings("unchecked")
final EvolutionResult<DoubleGene, Double> result = ENGINE
.stream((EvolutionResult<DoubleGene, Double>)IO.object.read(path))
.limit(limit.bySteadyFitness(20))
.collect(EvolutionResult.toBestEvolutionResult());
System.out.println(result.getBestPhenotype());
}EvolutionResult from a
first run, save it to disk and continue the evolution.result - the previously evolved EvolutionResultNullPointerException - if the given evolution result is
nullpublic 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 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 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 EngineEngine.Builderpublic 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.© 2007-2017 Franz Wilhelmstötter (2017-04-28 16:50)