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. Thepublic 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 theEngine.Builder
class and can't be changed after creation. The actual evolution is performed by theEvolutionStream
, which is created by theEngine
.- Since:
- 3.0
- Version:
- 6.0
- Author:
- Franz Wilhelmstötter
- See Also:
Engine.Builder
,EvolutionStart
,EvolutionResult
,EvolutionStream
,EvolutionStatistics
,Codec
,Constraint
- Implementation Note:
- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Alterer<G,C>
alterer()
Return the usedAlterer
of the GA.static <T,G extends Gene<?,G>,C extends Comparable<? super C>>
Engine.Builder<G,C>builder(Problem<T,G,C> problem)
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>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.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)
Create a new evolutionEngine.Builder
with the given fitness function and problemcodec
.Clock
clock()
Return theClock
the engine is using for measuring the execution time.Constraint<G,C>
constraint()
Return the constraint of the evolution problem.ISeq<Phenotype<G,C>>
eval(Seq<Phenotype<G,C>> population)
Evaluates the fitness function of the given population with the configuredEvaluator
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 evolutionstart
object New phenotypes are created with the fitness function and fitness scaler defined by this engineExecutor
executor()
Return theExecutor
the engine is using for executing the evolution steps.Factory<Genotype<G>>
genotypeFactory()
Return the used genotypeFactory
of the GA.EvolutionInterceptor<G,C>
interceptor()
Return the evolution interceptor.long
maximalPhenotypeAge()
Return the maximal allowed phenotype age.Selector<G,C>
offspringSelector()
Return the used offspringSelector
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 survivorSelector
of the GA.int
survivorsSize()
The number of selected survivors.Engine.Builder<G,C>
toBuilder()
Create a new evolutionEngine.Builder
initialized with the values of the current evolutionEngine
.
-
-
-
Method Detail
-
evolve
public EvolutionResult<G,C> evolve(EvolutionStart<G,C> start)
Description copied from interface:Evolution
Perform one evolution step with the given evolutionstart
object New phenotypes are created with the fitness function and fitness scaler defined by this engine- Specified by:
evolve
in interfaceEvolution<G extends Gene<?,G>,C extends Comparable<? super C>>
- Parameters:
start
- the evolution start object- Returns:
- the evolution result
- See Also:
Evolution.evolve(ISeq, long)
-
eval
public ISeq<Phenotype<G,C>> eval(Seq<Phenotype<G,C>> population)
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:
Evaluator
,Evaluator.eval(Seq)
-
stream
public EvolutionStream<G,C> stream(Supplier<EvolutionStart<G,C>> start)
Description copied from interface:EvolutionStreamable
Create a new, possibly infinite, evolution stream with the given evolution start. If an emptyPopulation
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.- 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
public EvolutionStream<G,C> stream(EvolutionInit<G> init)
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 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.- 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
public Factory<Genotype<G>> 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
public Constraint<G,C> constraint()
Return the constraint of the evolution problem.- Returns:
- the constraint of the evolution problem
- Since:
- 5.0
-
survivorsSelector
public Selector<G,C> survivorsSelector()
Return the used survivorSelector
of the GA.- Returns:
- the used survivor
Selector
of the GA.
-
offspringSelector
public Selector<G,C> offspringSelector()
Return the used offspringSelector
of the GA.- Returns:
- the used offspring
Selector
of the GA.
-
alterer
public Alterer<G,C> alterer()
Return the usedAlterer
of the GA.- Returns:
- the used
Alterer
of the GA.
-
offspringSize
public int offspringSize()
Return the number of selected offspring.- Returns:
- the number of selected offspring
-
survivorsSize
public int survivorsSize()
The number of selected survivors.- Returns:
- the number of selected survivors
-
populationSize
public int populationSize()
Return the number of individuals of a population.- Returns:
- the number of individuals of a population
-
maximalPhenotypeAge
public long maximalPhenotypeAge()
Return the maximal allowed phenotype age.- Returns:
- the maximal allowed phenotype age
-
optimize
public Optimize optimize()
Return the optimization strategy.- Returns:
- the optimization strategy
-
clock
public Clock clock()
Return theClock
the engine is using for measuring the execution time.- Returns:
- the clock used for measuring the execution time
-
executor
public Executor executor()
Return theExecutor
the engine is using for executing the evolution steps.- Returns:
- the executor used for performing the evolution steps
-
interceptor
public EvolutionInterceptor<G,C> interceptor()
Return the evolution interceptor.- Returns:
- the evolution result mapper
- Since:
- 6.0
-
toBuilder
public Engine.Builder<G,C> 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>,C extends Comparable<? super C>> Engine.Builder<G,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<?,G>,C extends Comparable<? super C>> Engine.Builder<G,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<?,G>,C extends Comparable<? super C>> Engine.Builder<G,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>,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.- 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
.
-
-