A - the type of the alphabet used in the representation of the
individuals in the population (this is to provide flexibility in
terms of how a problem can be encoded).public class GeneticAlgorithm<A>
extends java.lang.Object
function GENETIC-ALGORITHM(population, FITNESS-FN) returns an individual
inputs: population, a set of individuals
FITNESS-FN, a function that measures the fitness of an individual
repeat
new_population <- empty set
for i = 1 to SIZE(population) do
x <- RANDOM-SELECTION(population, FITNESS-FN)
y <- RANDOM-SELECTION(population, FITNESS-FN)
child <- REPRODUCE(x, y)
if (small random probability) then child <- MUTATE(child)
add child to new_population
population <- new_population
until some individual is fit enough, or enough time has elapsed
return the best individual in population, according to FITNESS-FN
--------------------------------------------------------------------------------
function REPRODUCE(x, y) returns an individual
inputs: x, y, parent individuals
n <- LENGTH(x); c <- random number from 1 to n
return APPEND(SUBSTRING(x, 1, c), SUBSTRING(y, c+1, n))
Figure 4.8 A genetic algorithm. The algorithm is the same as the one
diagrammed in Figure 4.6, with one variation: in this more popular version,
each mating of two parents produces only one offspring, not two.| Modifier and Type | Class and Description |
|---|---|
static interface |
GeneticAlgorithm.ProgressTracer<A>
Interface for progress tracers.
|
| Modifier and Type | Field and Description |
|---|---|
protected java.util.List<A> |
finiteAlphabet |
protected int |
individualLength |
protected static java.lang.String |
ITERATIONS |
protected Metrics |
metrics |
protected double |
mutationProbability |
protected static java.lang.String |
POPULATION_SIZE |
protected java.util.Random |
random |
protected static java.lang.String |
TIME_IN_MILLISECONDS |
| Constructor and Description |
|---|
GeneticAlgorithm(int individualLength,
java.util.Collection<A> finiteAlphabet,
double mutationProbability) |
GeneticAlgorithm(int individualLength,
java.util.Collection<A> finiteAlphabet,
double mutationProbability,
java.util.Random random) |
| Modifier and Type | Method and Description |
|---|---|
void |
addProgressTracer(GeneticAlgorithm.ProgressTracer<A> pTracer)
Progress tracers can be used to display progress information.
|
void |
clearInstrumentation()
Sets the population size and number of iterations to zero.
|
Individual<A> |
geneticAlgorithm(java.util.Collection<Individual<A>> initPopulation,
FitnessFunction<A> fitnessFn,
GoalTest goalTest,
long maxTimeMilliseconds)
Template method controlling search.
|
Individual<A> |
geneticAlgorithm(java.util.Collection<Individual<A>> initPopulation,
FitnessFunction<A> fitnessFn,
int maxIterations)
Starts the genetic algorithm and stops after a specified number of
iterations.
|
int |
getIterations()
Returns the number of iterations of the genetic algorithm.
|
Metrics |
getMetrics()
Returns all the metrics of the genetic algorithm.
|
int |
getPopulationSize()
Returns the population size.
|
long |
getTimeInMilliseconds() |
protected Individual<A> |
mutate(Individual<A> child) |
protected java.util.List<Individual<A>> |
nextGeneration(java.util.List<Individual<A>> population,
FitnessFunction<A> fitnessFn)
Primitive operation which is responsible for creating the next
generation.
|
protected int |
randomOffset(int length) |
protected Individual<A> |
randomSelection(java.util.List<Individual<A>> population,
FitnessFunction<A> fitnessFn) |
protected Individual<A> |
reproduce(Individual<A> x,
Individual<A> y) |
Individual<A> |
retrieveBestIndividual(java.util.Collection<Individual<A>> population,
FitnessFunction<A> fitnessFn) |
protected void |
updateMetrics(java.util.Collection<Individual<A>> population,
int itCount,
long time)
Updates statistic data collected during search.
|
protected void |
validatePopulation(java.util.Collection<Individual<A>> population) |
protected static final java.lang.String POPULATION_SIZE
protected static final java.lang.String ITERATIONS
protected static final java.lang.String TIME_IN_MILLISECONDS
protected Metrics metrics
protected int individualLength
protected java.util.List<A> finiteAlphabet
protected double mutationProbability
protected java.util.Random random
public GeneticAlgorithm(int individualLength,
java.util.Collection<A> finiteAlphabet,
double mutationProbability)
public GeneticAlgorithm(int individualLength,
java.util.Collection<A> finiteAlphabet,
double mutationProbability,
java.util.Random random)
public void addProgressTracer(GeneticAlgorithm.ProgressTracer<A> pTracer)
public Individual<A> geneticAlgorithm(java.util.Collection<Individual<A>> initPopulation, FitnessFunction<A> fitnessFn, int maxIterations)
public Individual<A> geneticAlgorithm(java.util.Collection<Individual<A>> initPopulation, FitnessFunction<A> fitnessFn, GoalTest goalTest, long maxTimeMilliseconds)
population - a set of individualsfitnessFn - a function that measures the fitness of an individualgoalTest - test determines whether a given individual is fit enough to
return. Can be used in subclasses to implement additional
termination criteria, e.g. maximum number of iterations.maxTimeMilliseconds - the maximum time in milliseconds that the algorithm is to run
for (approximate). Only used if > 0L.public Individual<A> retrieveBestIndividual(java.util.Collection<Individual<A>> population, FitnessFunction<A> fitnessFn)
public void clearInstrumentation()
public Metrics getMetrics()
public int getPopulationSize()
public int getIterations()
public long getTimeInMilliseconds()
protected void updateMetrics(java.util.Collection<Individual<A>> population, int itCount, long time)
itCount - the number of iterations.time - the time in milliseconds that the genetic algorithm took.protected java.util.List<Individual<A>> nextGeneration(java.util.List<Individual<A>> population, FitnessFunction<A> fitnessFn)
protected Individual<A> randomSelection(java.util.List<Individual<A>> population, FitnessFunction<A> fitnessFn)
protected Individual<A> reproduce(Individual<A> x, Individual<A> y)
protected Individual<A> mutate(Individual<A> child)
protected int randomOffset(int length)
protected void validatePopulation(java.util.Collection<Individual<A>> population)