Module io.jenetics.base
Package io.jenetics.engine
Class Engine<G extends Gene<?,G>,C extends Comparable<? super C>>
java.lang.Object
io.jenetics.engine.Engine<G,C>
- Type Parameters:
G
- the gene typeC
- the fitness result type
- All Implemented Interfaces:
Evaluator<G,
,C> Evolution<G,
,C> EvolutionStreamable<G,
C>
public final class Engine<G extends Gene<?,G>,C extends Comparable<? super C>>
extends Object
implements Evolution<G,C>, EvolutionStreamable<G,C>, Evaluator<G,C>
Genetic algorithm engine which is the main class. The following
example shows the main steps in initializing and executing the GA.
The architecture allows to decouple the configuration of the engine from the
execution. The
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
.- Since:
- 3.0
- Version:
- 7.0
- See Also:
- Implementation Note:
- This class is thread safe: The engine maintains no mutable state. Therefore, it is safe to create multiple evolution streams with one engine, which may be actually used in different threads.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Engine.Builder<G extends Gene<?,
G>, C extends Comparable<? super C>> Builder class for building GAEngine
instances.static interface
Engine.Setup<G extends Gene<?,
G>, C extends Comparable<? super C>> This interface represents a recipe for configuring (setup) a givenEngine.Builder
. -
Method Summary
Modifier and TypeMethodDescriptionalterer()
Return the usedAlterer
of the GA.static <T,
G extends Gene<?, G>, C extends Comparable<? super C>>
Engine.Builder<G,C> Create a new evolutionEngine.Builder
for the givenProblem
.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) Create a new evolutionEngine.Builder
with the given fitness function and chromosome templates.static <G extends Gene<?,
G>, C extends Comparable<? super C>>
Engine.Builder<G,C> Create a new evolutionEngine.Builder
with the given fitness function and genotype factory.static <T,
G extends Gene<?, G>, C extends Comparable<? super C>>
Engine.Builder<G,C> Create a new evolutionEngine.Builder
with the given fitness function and problemcodec
.clock()
Return theInstantSource
the engine is using for measuring the execution time.Constraint<G,
C> Return the constraint of the evolution problem.Evaluates the fitness function of the given population with the configuredEvaluator
of this engine and returns a new population with its fitness value assigned.evolve
(EvolutionStart<G, C> start) Perform one evolution step with the given evolutionstart
object New phenotypes are created with the fitness function defined by this engineexecutor()
Return theExecutor
the engine is using for executing the evolution steps.Return the used genotypeFactory
of the GA.Return the evolution interceptor.long
Return the maximal allowed phenotype age.Return the used offspringSelector
of the GA.int
Return the number of selected offspring.optimize()
Return the optimization strategy.int
Return the number of individuals of a population.stream
(EvolutionInit<G> init) Create a new, possibly infinite, evolution stream with the given initial value.stream
(Supplier<EvolutionStart<G, C>> start) Create a new, possibly infinite, evolution stream with the given evolution start.Return the used survivorSelector
of the GA.int
The number of selected survivors.Create a new evolutionEngine.Builder
initialized with the values of the current evolutionEngine
.
-
Method Details
-
evolve
Description copied from interface:Evolution
Perform one evolution step with the given evolutionstart
object New phenotypes are created with the fitness function defined by this engine -
eval
Evaluates the fitness function of the given population with the configuredEvaluator
of this engine and returns a new population with its fitness value assigned.- Specified by:
eval
in interfaceEvaluator<G extends Gene<?,
G>, C extends Comparable<? super C>> - Parameters:
population
- the population to evaluate- Returns:
- a new population with assigned fitness values
- Throws:
IllegalStateException
- 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.- Since:
- 5.0
- See Also:
-
stream
Description copied from interface:EvolutionStreamable
Create a new, possibly infinite, evolution stream with the given evolution start. If an emptyPopulation
is given, the engine's genotype factory is used for creating the population. The given population might be the result of another engine, and this method allows to start the evolution with the outcome of a different engine. The fitness function is replaced by the one defined for this engine.- Specified by:
stream
in interfaceEvolutionStreamable<G extends Gene<?,
G>, C extends Comparable<? super C>> - Parameters:
start
- the data the evolution stream starts with- Returns:
- a new infinite evolution stream
-
stream
Description copied from interface:EvolutionStreamable
Create a new, possibly infinite, evolution stream with the given initial value. If an emptyPopulation
is given, the engines genotype factory is used for creating the population. The given population might be the result of another engine, and this method allows to start the evolution with the outcome of a different engine. The fitness function is replaced by the one defined for this engine.- Specified by:
stream
in interfaceEvolutionStreamable<G extends Gene<?,
G>, C extends Comparable<? super C>> - Parameters:
init
- the data the evolution stream is initialized with- Returns:
- a new infinite evolution stream
-
genotypeFactory
Return the used genotypeFactory
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).- Returns:
- the used genotype
Factory
of the GA.
-
constraint
Return the constraint of the evolution problem.- Returns:
- the constraint of the evolution problem
- Since:
- 5.0
-
survivorsSelector
Return the used survivorSelector
of the GA.- Returns:
- the used survivor
Selector
of the GA.
-
offspringSelector
Return the used offspringSelector
of the GA.- Returns:
- the used offspring
Selector
of the GA.
-
alterer
Return the usedAlterer
of the GA.- Returns:
- the used
Alterer
of the GA.
-
offspringSize
Return the number of selected offspring.- Returns:
- the number of selected offspring
-
survivorsSize
The number of selected survivors.- Returns:
- the number of selected survivors
-
populationSize
Return the number of individuals of a population.- Returns:
- the number of individuals of a population
-
maximalPhenotypeAge
Return the maximal allowed phenotype age.- Returns:
- the maximal allowed phenotype age
-
optimize
Return the optimization strategy.- Returns:
- the optimization strategy
-
clock
Return theInstantSource
the engine is using for measuring the execution time.- Returns:
- the clock used for measuring the execution time
-
executor
Return theExecutor
the engine is using for executing the evolution steps.- Returns:
- the executor used for performing the evolution steps
-
interceptor
Return the evolution interceptor.- Returns:
- the evolution result mapper
- Since:
- 6.0
-
toBuilder
Create a new evolutionEngine.Builder
initialized with the values of the current evolutionEngine
. With this method, the evolution engine can serve as a template for a new one.- Returns:
- a new engine builder
-
builder
public static <G extends Gene<?,G>, Engine.Builder<G,C extends Comparable<? super C>> C> builder(Function<? super Genotype<G>, ? extends C> ff, Factory<Genotype<G>> gtf) Create a new evolutionEngine.Builder
with the given fitness function and genotype factory.- Type Parameters:
G
- the gene typeC
- the fitness function result type- Parameters:
ff
- the fitness functiongtf
- the genotype factory- Returns:
- a new engine builder
- Throws:
NullPointerException
- if one of the arguments isnull
.
-
builder
public static <T,G extends Gene<?, Engine.Builder<G,G>, C extends Comparable<? super C>> C> builder(Function<? super T, ? extends C> ff, Codec<T, G> codec) Create a new evolutionEngine.Builder
with the given fitness function and problemcodec
.- Type Parameters:
T
- the fitness function input typeC
- the fitness function result typeG
- the gene type- Parameters:
ff
- the fitness evaluatorcodec
- the problem codec- Returns:
- a new engine builder
- Throws:
NullPointerException
- if one of the arguments isnull
.- Since:
- 3.2
-
builder
public static <T,G extends Gene<?, Engine.Builder<G,G>, C extends Comparable<? super C>> C> builder(Problem<T, G, C> problem) Create a new evolutionEngine.Builder
for the givenProblem
.- Type Parameters:
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 function- Parameters:
problem
- the problem to be solved by the evolutionEngine
- Returns:
- Create a new evolution
Engine.Builder
- Since:
- 3.4
-
builder
@SafeVarargs public static <G extends Gene<?,G>, Engine.Builder<G,C extends Comparable<? super C>> C> builder(Function<? super Genotype<G>, ? extends C> ff, Chromosome<G> chromosome, Chromosome<G>... chromosomes) Create a new evolutionEngine.Builder
with the given fitness function and chromosome templates.- Type Parameters:
G
- the gene typeC
- the fitness function result type- Parameters:
ff
- the fitness functionchromosome
- the first chromosomechromosomes
- the chromosome templates- Returns:
- a new engine builder
- Throws:
NullPointerException
- if one of the arguments isnull
.
-