java.lang.Object
org.cicirello.search.evo.GenerationalEvolutionaryAlgorithm<BitVector>
org.cicirello.search.evo.GeneticAlgorithm
- All Implemented Interfaces:
- Splittable<TrackableSearch<BitVector>>,- Metaheuristic<BitVector>,- ReoptimizableMetaheuristic<BitVector>,- TrackableSearch<BitVector>
- Direct Known Subclasses:
- SimpleGeneticAlgorithm
This class is an implementation of a genetic algorithm (GA) with the common bit vector
 representation of solutions to optimization problems, and the generational model where children
 replace their parents each generation. It uses the usual bit flip mutation, where each bit of
 each member of the population is mutated (flipped) with some probability, known as the mutation
 rate, each generation. All other genetic operators, such as crossover and selection are
 configurable. This GeneticAlgorithm class can also be configured with or without the use of
 elitism. With elitism, a specified number of the most fit members of the population survive into
 the next generation unaltered.
 
The library also includes other classes for evolutionary algorithms that may be more relevant
 depending upon your use-case. For example, see the SimpleGeneticAlgorithm class for the
 form of GA known as the Simple GA, the MutationOnlyGeneticAlgorithm class if all you want
 to use is mutation and no crossover, and the GenerationalEvolutionaryAlgorithm class if
 you want to optimize something other than BitVectors or if you want even greater flexibility in
 configuring your evolutionary search.
- 
Constructor SummaryConstructorsConstructorDescriptionGeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation.
- 
Method SummaryModifier and TypeMethodDescriptionGets a reference to the problem that this search is solving.final ProgressTracker<BitVector>Gets theProgressTrackerobject that is in use for tracking search progress.longGets the total run length in number of fitness evaluations.final SolutionCostPair<BitVector>optimize(int numGenerations) Runs the evolutionary algorithm beginning from a randomly generated population.final SolutionCostPair<BitVector>reoptimize(int numGenerations) Runs the evolutionary algorithm continuing from the final population from the most recent call to eitherMetaheuristic.optimize(int)orReoptimizableMetaheuristic.reoptimize(int), or from a random population if this is the first call to either method.final voidsetProgressTracker(ProgressTracker<BitVector> tracker) Sets theProgressTrackerobject that is in use for tracking search progress.split()Generates a functionally identical copy of this object, for use in multithreaded implementations of search algorithms.
- 
Constructor Details- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, initializer, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, initializer, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- NullPointerException- if any of crossover, initializer, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- NullPointerException- if any of crossover, initializer, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, initializer, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, initializer, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- NullPointerException- if any of crossover, initializer, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, Initializer<BitVector> initializer, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- initializer- An initializer for generating random initial population members.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- NullPointerException- if any of crossover, initializer, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative
- NullPointerException- if any of crossover, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, ProgressTracker<BitVector> tracker) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- tracker- A ProgressTracker.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative.
- NullPointerException- if any of crossover, f, selection, or tracker are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative.
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection, int eliteCount) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- eliteCount- The number of elite population members. Pass 0 for no elitism. eliteCount must be less than n.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative.
- IllegalArgumentException- if eliteCount is greater than or equal to n.
- NullPointerException- if any of crossover, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Double<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type double, theFitnessFunction.Doubleinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative
- NullPointerException- if any of crossover, f, or selection are null.
 
- 
GeneticAlgorithmpublic GeneticAlgorithm(int n, int bitLength, FitnessFunction.Integer<BitVector> f, double mutationRate, CrossoverOperator<BitVector> crossover, double crossoverRate, SelectionOperator selection) Initializes a genetic algorithm with a generational model where children replace the parents, using the standard bit flip mutation. All other characteristics, such as crossover operator and selection operator are configurable. This constructor supports fitness functions with fitnesses of type int, theFitnessFunction.Integerinterface.- Parameters:
- n- The population size.
- bitLength- The length of each bit vector.
- f- The fitness function.
- mutationRate- The per-bit probability of flipping a bit. Each bit of each member of the population is flipped with this probability, and the decisions to flip bits are independent.
- crossover- The crossover operator.
- crossoverRate- The probability that a pair of parents undergo crossover.
- selection- The selection operator.
- Throws:
- IllegalArgumentException- if n is less than 1.
- IllegalArgumentException- if mutationRate ≤ 0 or if mutationRate ≥ 1.
- IllegalArgumentException- if crossoverRate is less than 0.
- IllegalArgumentException- if bitLength is negative.
- NullPointerException- if any of crossover, f, or selection are null.
 
 
- 
- 
Method Details- 
splitDescription copied from interface:SplittableGenerates a functionally identical copy of this object, for use in multithreaded implementations of search algorithms. The state of the object that is returned may or may not be identical to that of the original. Thus, this is a distinct concept from the functionality of theCopyableinterface. Classes that implement this interface must ensure that the object returned performs the same functionality, and that it does not share any state data that would be either unsafe or inefficient for concurrent access by multiple threads. The split method is allowed to simply return the this reference, provided that it is both safe and efficient for multiple threads to share a single copy of the Splittable object. The intention is to provide a multithreaded search with the capability to provide spawned threads with their own distinct search operators. Such multithreaded algorithms can call the split method for each thread it spawns to generate a functionally identical copy of the operator, but with independent state.- Specified by:
- splitin interface- Metaheuristic<BitVector>
- Specified by:
- splitin interface- ReoptimizableMetaheuristic<BitVector>
- Specified by:
- splitin interface- Splittable<TrackableSearch<BitVector>>
- Overrides:
- splitin class- GenerationalEvolutionaryAlgorithm<BitVector>
- Returns:
- A functionally identical copy of the object, or a reference to this if it is both safe and efficient for multiple threads to share a single instance of this Splittable object.
 
- 
optimizeRuns the evolutionary algorithm beginning from a randomly generated population. If this method is called multiple times, each call begins at a new randomly generated population.- Specified by:
- optimizein interface- Metaheuristic<T extends Copyable<T>>
- Parameters:
- numGenerations- The number of generations to run.
- Returns:
- The best solution found during this set of generations, which may or may not be the
     same as the solution contained in the ProgressTracker, which contains the best across all calls to optimize as well asReoptimizableMetaheuristic.reoptimize(int). Returns null if the run did not execute, such as if the ProgressTracker already contains the theoretical best solution.
 
- 
reoptimizeRuns the evolutionary algorithm continuing from the final population from the most recent call to eitherMetaheuristic.optimize(int)orReoptimizableMetaheuristic.reoptimize(int), or from a random population if this is the first call to either method.- Specified by:
- reoptimizein interface- ReoptimizableMetaheuristic<T extends Copyable<T>>
- Parameters:
- numGenerations- The number of generations to run.
- Returns:
- The best solution found during this set of generations, which may or may not be the
     same as the solution contained in the ProgressTracker, which contains the best across all calls to optimize as well asMetaheuristic.optimize(int). Returns null if the run did not execute, such as if the ProgressTracker already contains the theoretical best solution.
 
- 
getProgressTrackerDescription copied from interface:TrackableSearchGets theProgressTrackerobject that is in use for tracking search progress. The object returned by this method contains the best solution found during the search (including across multiple concurrent runs if the search is multithreaded, or across multiple restarts if the run methods were called multiple times), as well as cost of that solution, among other information. See theProgressTrackerdocumentation for more information about the search data tracked by this object.- Specified by:
- getProgressTrackerin interface- TrackableSearch<T extends Copyable<T>>
- Returns:
- the ProgressTrackerin use by this metaheuristic.
 
- 
setProgressTrackerDescription copied from interface:TrackableSearchSets theProgressTrackerobject that is in use for tracking search progress. Any previously set ProgressTracker is replaced by this one.- Specified by:
- setProgressTrackerin interface- TrackableSearch<T extends Copyable<T>>
- Parameters:
- tracker- The new ProgressTracker to set. The tracker must not be null. This method does nothing if tracker is null.
 
- 
getProblemDescription copied from interface:TrackableSearchGets a reference to the problem that this search is solving.- Specified by:
- getProblemin interface- TrackableSearch<T extends Copyable<T>>
- Returns:
- a reference to the problem.
 
- 
getTotalRunLengthpublic long getTotalRunLength()Gets the total run length in number of fitness evaluations. This is the total run length across all calls toMetaheuristic.optimize(int)andReoptimizableMetaheuristic.reoptimize(int). This may differ from what may be expected based on run lengths. For example, the search terminates if it finds the theoretical best solution, and also immediately returns if a prior call found the theoretical best. In such cases, the total run length may be less than the requested run length.- Specified by:
- getTotalRunLengthin interface- TrackableSearch<T extends Copyable<T>>
- Returns:
- The total number of generations completed across all calls to Metaheuristic.optimize(int)andReoptimizableMetaheuristic.reoptimize(int).
 
 
-