Package de.sfuhrm.genetic
Class GeneticAlgorithmBuilder<H>
- java.lang.Object
-
- de.sfuhrm.genetic.GeneticAlgorithmBuilder<H>
-
- Type Parameters:
H- the class the hypothesis data will be of.
public class GeneticAlgorithmBuilder<H> extends Object
A builder for GeneticAlgorithm instances. The builder has defaults that can be seen in the default static final fields:- CROSS_OVER_RATE_DEFAULT,
- MUTATION_RATE_DEFAULT,
- GENERATION_SIZE_DEFAULT,
- no executor service (only single threaded execution),
- and a generic pseudo random number generator.
- Author:
- Stephan Fuhrmann
-
-
Field Summary
Fields Modifier and Type Field Description static doubleCROSS_OVER_RATE_DEFAULTThe default cross-over rate (0.3).static intGENERATION_SIZE_DEFAULTThe default generation size (100).static doubleMUTATION_RATE_DEFAULTThe default mutation-rate (0.05).
-
Constructor Summary
Constructors Constructor Description GeneticAlgorithmBuilder(@NonNull AlgorithmDefinition<H> inAlgorithmDefinition)Creates a new builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GeneticAlgorithm<H>build()Creates a new instance of aGeneticAlgorithmwith the defined parameters.GeneticAlgorithmBuilder<H>withCrossOverRate(double inRate)Defines the cross-over rate to use.GeneticAlgorithmBuilder<H>withExecutorService(@NonNull ExecutorService inExecutorService)Defines the executor service to use.GeneticAlgorithmBuilder<H>withGenerationSize(int size)Defines the generation size to use.GeneticAlgorithmBuilder<H>withMutationRate(double rate)Defines the mutation rate to use.GeneticAlgorithmBuilder<H>withRandom(@NonNull Random inRandom)Defines the random generator to use.
-
-
-
Field Detail
-
CROSS_OVER_RATE_DEFAULT
public static final double CROSS_OVER_RATE_DEFAULT
The default cross-over rate (0.3).- See Also:
- Constant Field Values
-
MUTATION_RATE_DEFAULT
public static final double MUTATION_RATE_DEFAULT
The default mutation-rate (0.05).- See Also:
- Constant Field Values
-
GENERATION_SIZE_DEFAULT
public static final int GENERATION_SIZE_DEFAULT
The default generation size (100).- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GeneticAlgorithmBuilder
public GeneticAlgorithmBuilder(@NonNull @NonNull AlgorithmDefinition<H> inAlgorithmDefinition)Creates a new builder.- Parameters:
inAlgorithmDefinition- the non-null algorithm definition to generate a genetic algorithm with.
-
-
Method Detail
-
withCrossOverRate
public GeneticAlgorithmBuilder<H> withCrossOverRate(double inRate)
Defines the cross-over rate to use.- Parameters:
inRate- the cross-over rate between 0 and 1.- Returns:
- this instance.
- Throws:
IllegalArgumentException- if the valid parameter range is exceeded.
-
withRandom
public GeneticAlgorithmBuilder<H> withRandom(@NonNull @NonNull Random inRandom)
Defines the random generator to use.- Parameters:
inRandom- the random generator to use.- Returns:
- this instance.
- Throws:
IllegalArgumentException- if the valid parameter range is exceeded.
-
withExecutorService
public GeneticAlgorithmBuilder<H> withExecutorService(@NonNull @NonNull ExecutorService inExecutorService)
Defines the executor service to use.- Parameters:
inExecutorService- the executor service.- Returns:
- this instance.
- Throws:
NullPointerException- if the input reference isnull.
-
withMutationRate
public GeneticAlgorithmBuilder<H> withMutationRate(double rate)
Defines the mutation rate to use.- Parameters:
rate- the mutation rate between 0 and 1.- Returns:
- this instance.
- Throws:
IllegalArgumentException- if the valid parameter range is exceeded.
-
withGenerationSize
public GeneticAlgorithmBuilder<H> withGenerationSize(int size)
Defines the generation size to use.- Parameters:
size- the generation size greater or equal to 2.- Returns:
- this instance.
- Throws:
IllegalArgumentException- if the valid parameter range is exceeded.
-
build
public GeneticAlgorithm<H> build()
Creates a new instance of aGeneticAlgorithmwith the defined parameters.- Returns:
- a new GeneticAlgorithm instance.
-
-