JGAP

org.jgap
Class Population

java.lang.Object
  extended by org.jgap.Population
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, IPersistentRepresentation, ICloneable

public class Population
extends java.lang.Object
implements java.io.Serializable, ICloneable, IPersistentRepresentation

Makes up the population of a generation during evolution. Represented by a list of chromosomes held in the Genotype.

Since:
2.0
See Also:
Serialized Form

Field Summary
static java.lang.String CHROM_DELIMITER
           
static java.lang.String CHROM_DELIMITER_CLOSING
          Represents the closing delimiter that is used to separate chromosomes in the persistent representation.
static java.lang.String CHROM_DELIMITER_HEADING
          Represents the heading delimiter that is used to separate chromosomes in the persistent representation.
 
Constructor Summary
Population()
           
Population(Configuration a_config)
           
Population(Configuration a_config, IChromosome a_chromosome)
           
Population(Configuration a_config, IChromosome[] a_chromosomes)
           
Population(Configuration a_config, int a_size)
           
 
Method Summary
 void addChromosome(IChromosome a_toAdd)
          Adds a Chromosome to this Population.
 void addChromosomes(Population a_population)
          Adds all the Chromosomes in the given Population.
 void clear()
          Clears the list of chromosomes.
 java.lang.Object clone()
           
 int compareTo(java.lang.Object a_pop)
          This method is not producing symmetric results as -1 is more often returned than 1 (see description of return value).
 boolean contains(IChromosome a_chromosome)
          Determines whether the given chromosome is contained within the population.
protected  IChromosome createChromosome(java.lang.String a_chromClassName, java.lang.String a_persistentRepresentation)
          Creates a new Chromosome instance.
 IChromosome determineFittestChromosome()
          Determines the fittest Chromosome in the Population (the one with the highest fitness value) and memorizes it.
 IChromosome determineFittestChromosome(int a_startIndex, int a_endIndex)
          Determines the fittest Chromosome in the population (the one with the highest fitness value) within the given indices and memorizes it.
 java.util.List determineFittestChromosomes(int a_numberOfChromosomes)
          Sorts the Chromosome list and returns the fittest n Chromosomes in the population.
 boolean equals(java.lang.Object a_pop)
          The equals-method.
 IChromosome getChromosome(int a_index)
           
 java.util.List<IChromosome> getChromosomes()
           
 Configuration getConfiguration()
           
 java.util.List getGenome(boolean a_resolveCompositeGenes)
          Returns the genotype of the population, i.e. the list of genes in the Population.
 java.lang.String getPersistentRepresentation()
          Returns a persistent representation of this chromosome, see interface Gene for description.
 int hashCode()
          Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population.
 boolean isChanged()
           
 boolean isSorted()
           
 java.util.Iterator iterator()
           
 void keepPopSizeConstant()
          Cares that the population size does not exceed the maximum size given in the configuration.
protected  void setChanged(boolean a_changed)
          Mark that for the population the fittest chromosome may have changed.
 void setChromosome(int a_index, IChromosome a_chromosome)
          Sets in the given Chromosome on the given index in the list of chromosomes.
 void setChromosomes(java.util.List a_chromosomes)
          Replaces all chromosomes in the population with the give list of chromosomes.
protected  void setSorted(boolean a_sorted)
          Mark the population as sorted.
 void setValueFromPersistentRepresentation(java.lang.String a_representation)
          Counterpart of getPersistentRepresentation.
 int size()
           
protected  void sort(java.util.Comparator a_comparator)
          Sorts the chromosomes within the population utilzing the given comparator.
 void sortByFitness()
          Sorts the chromosomes within the population according to their fitness value using ChromosomFitnessComparator.
protected static java.util.List split(java.lang.String a_string)
          Splits the input a_string into individual chromosome representations.
 IChromosome[] toChromosomes()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHROM_DELIMITER

public static final java.lang.String CHROM_DELIMITER
See Also:
Constant Field Values

CHROM_DELIMITER_HEADING

public static final java.lang.String CHROM_DELIMITER_HEADING
Represents the heading delimiter that is used to separate chromosomes in the persistent representation.

See Also:
Constant Field Values

CHROM_DELIMITER_CLOSING

public static final java.lang.String CHROM_DELIMITER_CLOSING
Represents the closing delimiter that is used to separate chromosomes in the persistent representation.

See Also:
Constant Field Values
Constructor Detail

Population

public Population(Configuration a_config)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

Population

public Population(Configuration a_config,
                  IChromosome[] a_chromosomes)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

Population

public Population(Configuration a_config,
                  IChromosome a_chromosome)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

Population

public Population(Configuration a_config,
                  int a_size)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

Population

public Population()
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException
Method Detail

getConfiguration

public Configuration getConfiguration()

addChromosome

public void addChromosome(IChromosome a_toAdd)
Adds a Chromosome to this Population. Does nothing when given null.

Parameters:
a_toAdd - the Chromosome to add
Since:
2.0

addChromosomes

public void addChromosomes(Population a_population)
Adds all the Chromosomes in the given Population. Does nothing on null or an empty Population.

Parameters:
a_population - the Population to add
Since:
2.0

setChromosomes

public void setChromosomes(java.util.List a_chromosomes)
Replaces all chromosomes in the population with the give list of chromosomes.

Parameters:
a_chromosomes - the chromosomes to make the population up from

setChromosome

public void setChromosome(int a_index,
                          IChromosome a_chromosome)
Sets in the given Chromosome on the given index in the list of chromosomes. If the given index is exceeding the list by one, the chromosome is appended.

Parameters:
a_index - the index to set the Chromosome in
a_chromosome - the Chromosome to be set
Since:
2.0

getChromosomes

public java.util.List<IChromosome> getChromosomes()
Returns:
the list of Chromosome's in the Population. Don't modify the retrieved list by using clear(), remove(int) etc. If you do so, you need to call setChanged(true)
Since:
2.0

getChromosome

public IChromosome getChromosome(int a_index)
Parameters:
a_index - the index of the Chromosome to be returned
Returns:
Chromosome at given index in the Population
Since:
2.0

size

public int size()
Returns:
number of Chromosome's in the Population
Since:
2.0

iterator

public java.util.Iterator iterator()
Returns:
Iterator for the Chromosome list in the Population. Please be aware that using remove() forces you to call setChanged(true)
Since:
2.0

toChromosomes

public IChromosome[] toChromosomes()
Returns:
the Population converted into a list of Chromosome's
Since:
2.0

determineFittestChromosome

public IChromosome determineFittestChromosome()
Determines the fittest Chromosome in the Population (the one with the highest fitness value) and memorizes it. This is an optimized version compared to calling determineFittesChromosomes(1).

Returns:
the fittest Chromosome of the Population
Since:
2.0

determineFittestChromosome

public IChromosome determineFittestChromosome(int a_startIndex,
                                              int a_endIndex)
Determines the fittest Chromosome in the population (the one with the highest fitness value) within the given indices and memorizes it. This is an optimized version compared to calling determineFittesChromosomes(1).

Parameters:
a_startIndex - index to begin the evaluation with
a_endIndex - index to end the evaluation with
Returns:
the fittest Chromosome of the population within the given indices
Since:
3.0

setChanged

protected void setChanged(boolean a_changed)
Mark that for the population the fittest chromosome may have changed.

Parameters:
a_changed - true: population's fittest chromosome may have changed, false: fittest chromosome evaluated earlier is still valid
Since:
2.2

isChanged

public boolean isChanged()
Returns:
true: population's chromosomes (maybe) were changed, false: not changed for sure
Since:
2.6

setSorted

protected void setSorted(boolean a_sorted)
Mark the population as sorted.

Parameters:
a_sorted - true: mark population as sorted
Since:
2.6

contains

public boolean contains(IChromosome a_chromosome)
Determines whether the given chromosome is contained within the population.

Parameters:
a_chromosome - the chromosome to check
Returns:
true: chromosome contained within population
Since:
2.1

keepPopSizeConstant

public void keepPopSizeConstant()
                         throws InvalidConfigurationException
Cares that the population size does not exceed the maximum size given in the configuration.

Throws:
InvalidConfigurationException
Since:
3.2

determineFittestChromosomes

public java.util.List determineFittestChromosomes(int a_numberOfChromosomes)
Sorts the Chromosome list and returns the fittest n Chromosomes in the population.

Parameters:
a_numberOfChromosomes - number of top performer chromosomes to be returned
Returns:
list of the fittest n Chromosomes of the population, or the fittest x Chromosomes with x = number of chromosomes in case n > x.
Since:
2.4

sortByFitness

public void sortByFitness()
Sorts the chromosomes within the population according to their fitness value using ChromosomFitnessComparator. The fittest chromosome is then at index 0.

Since:
2.6

sort

protected void sort(java.util.Comparator a_comparator)
Sorts the chromosomes within the population utilzing the given comparator.

Parameters:
a_comparator - the comparator to utilize for sorting
Since:
2.6

getGenome

public java.util.List getGenome(boolean a_resolveCompositeGenes)
Returns the genotype of the population, i.e. the list of genes in the Population.

Parameters:
a_resolveCompositeGenes - true: split encountered CompositeGenes into their single (atomic) genes
Returns:
genotype of the population
Since:
2.3

isSorted

public boolean isSorted()

equals

public boolean equals(java.lang.Object a_pop)
The equals-method.

Overrides:
equals in class java.lang.Object
Parameters:
a_pop - the population instance to compare with
Returns:
true: given object equal to comparing one
Since:
2.6

compareTo

public int compareTo(java.lang.Object a_pop)
This method is not producing symmetric results as -1 is more often returned than 1 (see description of return value).

Parameters:
a_pop - the other population to compare
Returns:
1: a_pop is null or having fewer chromosomes or equal number of chromosomes but at least one not contained. 0: both populations containing exactly the same chromosomes. -1: this population contains fewer chromosomes than a_pop
Since:
2.6

clone

public java.lang.Object clone()
Specified by:
clone in interface ICloneable
Overrides:
clone in class java.lang.Object
Returns:
deeply cloned population instance
Since:
3.2

clear

public void clear()
Clears the list of chromosomes. Normally, this should not be necessary. But especially in distributed computing, a fresh population has to be provided sometimes.

Since:
3.2

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
Returns a persistent representation of this chromosome, see interface Gene for description. Similar to CompositeGene's routine. But does not include all information of the chromosome (yet).

Specified by:
getPersistentRepresentation in interface IPersistentRepresentation
Returns:
string representation of this Chromosome's relevant parts of its current state
Throws:
java.lang.UnsupportedOperationException
Since:
3.2

setValueFromPersistentRepresentation

public void setValueFromPersistentRepresentation(java.lang.String a_representation)
                                          throws UnsupportedRepresentationException
Counterpart of getPersistentRepresentation.

Specified by:
setValueFromPersistentRepresentation in interface IPersistentRepresentation
Parameters:
a_representation - the string representation retrieved from a prior call to the getPersistentRepresentation() method
Throws:
UnsupportedRepresentationException
Since:
3.2

createChromosome

protected IChromosome createChromosome(java.lang.String a_chromClassName,
                                       java.lang.String a_persistentRepresentation)
                                throws java.lang.Exception
Creates a new Chromosome instance.

Taken and adapted from CompositeGene.

Parameters:
a_chromClassName - name of the Chromosome class
a_persistentRepresentation - persistent representation of the Chromosome to create (could be obtained via getPersistentRepresentation)
Returns:
newly created Chromosome
Throws:
java.lang.Exception
Since:
3.2

split

protected static final java.util.List split(java.lang.String a_string)
                                     throws UnsupportedRepresentationException
Splits the input a_string into individual chromosome representations.

Taken and adapted from CompositeGene.

Parameters:
a_string - the string to split
Returns:
the elements of the returned array are the persistent representation strings of the population's components
Throws:
UnsupportedRepresentationException
Since:
3.2

hashCode

public int hashCode()
Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population. The logic for the hashcode is Step Result ---- ------ 1 31*0 + hashcode_0 = y(1) 2 31*y(1) + hashcode_1 = y(2) 3 31*y(2) + hashcode_2 = y(3) n 31*y(n-1) + hashcode_n-1 = y(n)

Overrides:
hashCode in class java.lang.Object
Returns:
the computed hashcode
Since:
3.5

JGAP