org.artoflife.gp
Class Population

java.lang.Object
  |
  +--org.artoflife.gp.Population
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
GenerationalPopulation

public abstract class Population
extends java.lang.Object
implements java.io.Serializable

A Population contains a number of Individuals, has an ideal size that will be maintained during a breeding cycle, can add and remove Individuals and populates new generations using Reproducers. An interested listener can register to receive notification of changes to the population.

A Population initialises itself with new "Adams and Eves" in the first generation (see newIndividual()), and then creates new generations (see breed()) using a Selector to choose Individuals and a random choice from its set of Reproducers to create the child Individuals (see breedNewIndividual(Selector)).

Instances fire PopulationEvents to PopulationListeners to notify of changes including addition and deletion of Individuals: this enables the creation of decoupled views to watch the population as it develops.

Population implementations need to be thread-safe unfortunately.

Author:
Steve Meyfroidt
See Also:
Serialized Form
To do:
make this thread-safe

Field Summary
protected  java.util.Set individuals
           
 
Constructor Summary
Population(java.util.SortedMap reproducers)
           
 
Method Summary
 void addIndividual(Individual newIndividual)
          Add a new Individual to the Population, culling one if necessary to maintain the ideal population size.
protected  void addNewIndividuals(int number)
          Make a number of new Individuals and add them to this Population.
 void addPopulationListener(PopulationListener listener)
           
protected abstract  void breed()
          Override to implement breeding policy, returning the population size to the getIdealPopulationSize().
 Individual breedNewIndividual(Selector selector)
          Using a passed Selector, apply configured breeding strategies (Reproducers) to breed a new Individual from parents selected from the population, or possibly a brand new Individual if reproducer probabilities don't end at 1.0.
protected abstract  void cullOneWeakest()
           
protected abstract  void cullWeakest()
          Override to implement a cull policy, for example a fixed number culled every generation or just the Individuals weaker than some threshold.
 void firePopulationEvent(PopulationEvent event)
           
abstract  int getIdealPopulationSize()
           
 java.util.Set getIndividuals()
           
protected  java.util.SortedMap getReproducers()
          Provides a SortedMap of Reproducers with their probabilities (Doubles between 0.0 and 1.0) so that breedNewIndividual(Selector) can choose one to do a reproduction.
 void newGeneration()
          Breed the population through one generation, returning its size back to getIdealPopulationSize(): first cull the weaklings and then breed new children to make up the full population size.
abstract  Individual newIndividual()
          Override to create a new Individual.
 void removeIndividual(Individual individual)
          Remove an Individual from the Population (without replacing with a newly bred one), and notify listeners of the change.
 void removePopulationListener(PopulationListener listener)
           
protected abstract  void setIdealPopulationSize(int idealSize)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

individuals

protected java.util.Set individuals
Constructor Detail

Population

public Population(java.util.SortedMap reproducers)
Parameters:
reproducers - to use when breeding new generations
Method Detail

getIndividuals

public java.util.Set getIndividuals()
Returns:
Set of Individuals in this Population, never null

setIdealPopulationSize

protected abstract void setIdealPopulationSize(int idealSize)
Parameters:
idealSize - the number of Individuals that the population aims to maintain in newGeneration().

getIdealPopulationSize

public abstract int getIdealPopulationSize()
Returns:
the number of Individuals that the population aims to maintain in newGeneration().

addNewIndividuals

protected void addNewIndividuals(int number)
Make a number of new Individuals and add them to this Population. Note: this does not involve selection or reproduction: it is used to seed the initial population. Note: no error checking on the population size is done here.

Parameters:
number - of new Individuals to add to the population

newIndividual

public abstract Individual newIndividual()
Override to create a new Individual. Used to fill the Population upon construction. Can be used by the outside world to create a new instance compatible with this Population.

Returns:
a newly bred Individual

newGeneration

public void newGeneration()
Breed the population through one generation, returning its size back to getIdealPopulationSize(): first cull the weaklings and then breed new children to make up the full population size. If the population contains no Individuals then initialise it by creating an initial population using addNewIndividuals(int).


cullWeakest

protected abstract void cullWeakest()
Override to implement a cull policy, for example a fixed number culled every generation or just the Individuals weaker than some threshold. This is called before breed() on each new generation and should ideally result in a population size less than or equal to getIdealPopulationSize().


cullOneWeakest

protected abstract void cullOneWeakest()

breed

protected abstract void breed()
Override to implement breeding policy, returning the population size to the getIdealPopulationSize(). For example you could fill a nursery by mating strongest Individuals in Population then move strongest from nursery into main population. This is called after cullWeakest() on each new generation.

See Also:
#getPopulationSize(), getReproducers()

breedNewIndividual

public Individual breedNewIndividual(Selector selector)
                              throws IncompatibleGenotypeException
Using a passed Selector, apply configured breeding strategies (Reproducers) to breed a new Individual from parents selected from the population, or possibly a brand new Individual if reproducer probabilities don't end at 1.0.

Parameters:
selector - to use for selection of parents of the breeding
Returns:
a new Individual, never null
Throws:
IncompatibleGenotypeException - when breeding between individuals with incompatible genotypes

removeIndividual

public void removeIndividual(Individual individual)
Remove an Individual from the Population (without replacing with a newly bred one), and notify listeners of the change.

Parameters:
individual - the Individual to remove (does nothing if the Individual can't be found in the Population)

addIndividual

public void addIndividual(Individual newIndividual)
                   throws IncompatibleGenotypeException
Add a new Individual to the Population, culling one if necessary to maintain the ideal population size.

Parameters:
newIndividual - the Individual to be added into the Population, does nothing if the individual is already in the population
Throws:
IncompatibleGenotypeException - if the individual to be added has a genotype that is incompatible with the rest of this population

getReproducers

protected java.util.SortedMap getReproducers()
Provides a SortedMap of Reproducers with their probabilities (Doubles between 0.0 and 1.0) so that breedNewIndividual(Selector) can choose one to do a reproduction.

See Also:
Configuration.getInstancesProbabilities(String)

addPopulationListener

public void addPopulationListener(PopulationListener listener)

removePopulationListener

public void removePopulationListener(PopulationListener listener)

firePopulationEvent

public void firePopulationEvent(PopulationEvent event)

artoflife pre0.1.0-dev

Copyright © 2002 the Art Of Life team. All Rights Reserved.