org.omegahat.Numerics.GeneticAlgorithms
Class PopulationOrdered

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractSet
              |
              +--java.util.TreeSet
                    |
                    +--org.omegahat.Numerics.GeneticAlgorithms.PopulationOrdered
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, Population, java.io.Serializable, java.util.Set, java.util.SortedSet

public class PopulationOrdered
extends java.util.TreeSet
implements Population

A population of individuals, maintained as a vector in order of increasing fitness.

Relies on the ability to compare fitness of individuals to insert a new individual. Births and deaths are handled by inserting the newborn according to its fitness and by removing the defunct individual (which can be specified by its reference or by its index).

The class supports evolution event listeners. Births and deaths produce corresponding evolution events (the population object is the source of the event). All registered listeners are notified.

See Also:
Serialized Form

Field Summary
protected  java.util.Vector evolutionListeners
          The list of listeners for birth events.
protected  double jitter
          If non-zero, a scaling value for random jitter used to break ties in the method.
 
Constructor Summary
PopulationOrdered()
           
 
Method Summary
 boolean add(java.lang.Object obj)
          Add the object to the population.
 boolean add(java.lang.Object obj, boolean force)
          The add() method in java.util.TreeSet will not enter new objects that compare equal to any already in the set.
 void addEvolutionListener(EvolutionListener listener)
          Add this listener for all birth and death events.
 boolean birth(Individual element)
          The individual supplied as argument has been born into the population.
 void death(Individual who)
          The specified individual has died: remove it from the population and dthen notify any listeners.
 void death(int index)
          The individual at the specified position in the population has died: remove it from the population and notify any listeners.
 java.util.Vector getEvolutionListeners()
          Accessor for evolutionListeners field
 double getJitter()
          Accessor for jitter field
 void notifyEvolutionListeners(EvolutionEvent event)
          Notify all the listeners of an evolution event.
 void removeEvolutionListener(EvolutionListener listener)
          Remove this evolution event listener.
 java.util.Vector setEvolutionListeners(java.util.Vector value)
          Accessor for setting evolutionListeners field
 double setJitter(double value)
          Accessor for setting jitter field
 
Methods inherited from class java.util.TreeSet
addAll, clear, clone, comparator, contains, first, headSet, isEmpty, iterator, last, remove, size, subSet, tailSet
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.SortedSet
comparator, first, headSet, last, subSet, tailSet
 
Methods inherited from interface java.util.Set
addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Field Detail

jitter

protected double jitter
If non-zero, a scaling value for random jitter used to break ties in the method. Default 1.e-9

evolutionListeners

protected java.util.Vector evolutionListeners
The list of listeners for birth events.
Constructor Detail

PopulationOrdered

public PopulationOrdered()
Method Detail

getJitter

public double getJitter()
Accessor for jitter field

setJitter

public double setJitter(double value)
Accessor for setting jitter field

getEvolutionListeners

public java.util.Vector getEvolutionListeners()
Accessor for evolutionListeners field

setEvolutionListeners

public java.util.Vector setEvolutionListeners(java.util.Vector value)
Accessor for setting evolutionListeners field

birth

public boolean birth(Individual element)
The individual supplied as argument has been born into the population.

Insert the individual in a place consistent with its fitness, and then notify any listeners of the event.


death

public void death(Individual who)
The specified individual has died: remove it from the population and dthen notify any listeners.

death

public void death(int index)
The individual at the specified position in the population has died: remove it from the population and notify any listeners.

For example, death(0) will remove the least fit individual.


addEvolutionListener

public void addEvolutionListener(EvolutionListener listener)
Add this listener for all birth and death events.
Specified by:
addEvolutionListener in interface Population

removeEvolutionListener

public void removeEvolutionListener(EvolutionListener listener)
Remove this evolution event listener.
Specified by:
removeEvolutionListener in interface Population

notifyEvolutionListeners

public void notifyEvolutionListeners(EvolutionEvent event)
Notify all the listeners of an evolution event.
Specified by:
notifyEvolutionListeners in interface Population

add

public boolean add(java.lang.Object obj,
                   boolean force)
The add() method in java.util.TreeSet will not enter new objects that compare equal to any already in the set. For populations using fitness for comparison, this is the wrong idea. To break ties, new individuals should be added by invoking the present method with force==true.

The technique used is to alter the fitness of this object by a random uniform on the range plus or minus .5 * jitter. This won't work if the fitness attribute is not numeric (e.g., a string or a special-purpose comparator). In that case, you need to define your own compareTo method that prevents unwanted ties, and set jitter to 0.


add

public boolean add(java.lang.Object obj)
Add the object to the population. Forces an addition. To add conditionally, use add(obj, false).
Specified by:
add in interface java.util.Set
Overrides:
add in class java.util.TreeSet