public class GeneticAlgorithm<T extends Chromosome> extends Object
In a genetic algorithm, a population of strings (called chromosomes), which encode candidate solutions (called individuals) to an optimization problem, evolves toward better solutions. Traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible. The evolution usually starts from a population of randomly generated individuals and happens in generations. In each generation, the fitness of every individual in the population is evaluated, multiple individuals are stochastically selected from the current population (based on their fitness), and modified (recombined and possibly randomly mutated) to form a new population. The new population is then used in the next iteration of the algorithm. Commonly, the algorithm terminates when either a maximum number of generations has been produced, or a satisfactory fitness level has been reached for the population. If the algorithm has terminated due to a maximum number of generations, a satisfactory solution may or may not have been reached.
A typical genetic algorithm requires:
The fitness function is defined over the genetic representation and measures the quality of the represented solution. The fitness function is always problem dependent.
Once we have the genetic representation and the fitness function defined, GA proceeds to initialize a population of solutions randomly, then improve it through repetitive application of selection, crossover and mutation operators.
Here are some basic recommendations of typical parameter values on binary encoding based on empiric studies.
The number of iterations t during each fitness assessment is a knob that adjusts the degree of exploitation in the algorithm. If t is very large, then we're doing more hill-climbing and thus more exploiting; whereas if t is very small, then we're spending more time in the outer algorithm and thus doing more exploring.
Modifier and Type | Class and Description |
---|---|
static class |
GeneticAlgorithm.Selection
The way to select chromosomes from the population as parents to crossover.
|
Constructor and Description |
---|
GeneticAlgorithm(T[] seeds)
Constructor.
|
GeneticAlgorithm(T[] seeds,
GeneticAlgorithm.Selection selection)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
T |
evolve(int generation)
Performs genetic algorithm for a given number of generations.
|
T |
evolve(int generation,
double threshold)
Performs genetic algorithm until the given number of generations is reached
or the best fitness is larger than the given threshold.
|
int |
getElitism()
Returns the number of best chromosomes to copy to new population.
|
int |
getLocalSearchSteps()
Gets the number of iterations of local search for Lamarckian algorithm.
|
double |
getTournamentProbability()
Returns the best-player-wins probability in tournament selection.
|
int |
getTournamentSize()
Returns the tournament size in tournament selection.
|
T[] |
population()
Returns the population of current generation.
|
void |
setElitism(int elitism)
Sets the number of best chromosomes to copy to new population.
|
void |
setLocalSearchSteps(int t)
Sets the number of iterations of local search for Lamarckian algorithm.
|
void |
setTournament(int size,
double p)
Set the tournament size and the best-player-wins probability in
tournament selection.
|
public GeneticAlgorithm(T[] seeds)
seeds
- the initial population, which is usually randomly generated.public GeneticAlgorithm(T[] seeds, GeneticAlgorithm.Selection selection)
selection
- the selection strategy.seeds
- the initial population, which is usually randomly generated.public T[] population()
public void setElitism(int elitism)
public int getElitism()
public void setTournament(int size, double p)
size
- the size of tournament pool.p
- the best-player-wins probability.public int getTournamentSize()
public double getTournamentProbability()
public void setLocalSearchSteps(int t)
t
- the number of iterations of local search.public int getLocalSearchSteps()
public T evolve(int generation)
generation
- the number of iterations.public T evolve(int generation, double threshold)
generation
- the maximum number of iterations.threshold
- the fitness threshold. The algorithm stops when a
solution is found that satisfies minimum criteria.Copyright © 2015. All rights reserved.