JGAP

org.jgap.gp.impl
Class GPPopulation

java.lang.Object
  extended by org.jgap.gp.impl.GPPopulation
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable

public class GPPopulation
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable

Population for GP programs.

Since:
3.0
See Also:
Serialized Form

Field Summary
static double DELTA
           
 
Constructor Summary
GPPopulation()
          Default constructor, only for dynamic instantiation.
GPPopulation(GPConfiguration a_config, int a_size)
           
GPPopulation(GPPopulation a_pop)
           
GPPopulation(GPPopulation a_pop, boolean a_keepPrograms)
           
 
Method Summary
 void addFittestProgram(IGPProgram a_toAdd)
          Adds a GP program to this Population.
 void clear()
          Clears the list of programs.
 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).
protected  boolean containedInArray(IGPProgram[] a_progs, IGPProgram a_prog)
          Checks if a program is contained within an array of programs.
 void copyGPPrograms(GPPopulation a_pop)
          Sets the GPPrograms of the given population to this population.
 void create(java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, boolean[] a_fullModeAllowed)
          Creates a population.
 void create(java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, boolean[] a_fullModeAllowed, IProgramCreator a_programCreator)
          Creates a population.
 void create(java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, boolean[] a_fullModeAllowed, IProgramCreator a_programCreator, int a_offset)
          Creates a population.
 IGPProgram create(java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_depth, boolean a_grow, int a_maxNodes, boolean[] a_fullModeAllowed, int a_tries)
          Creates a population.
 IGPProgram create(int a_programIndex, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_depth, boolean a_grow, int a_maxNodes, boolean[] a_fullModeAllowed, int a_tries)
           
 IGPProgram create(int a_programIndex, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_depth, boolean a_grow, int a_maxNodes, boolean[] a_fullModeAllowed, int a_tries, IProgramCreator a_programCreator)
          Creates a valid IGPProgram.
protected  java.lang.String decode(java.lang.String a_string)
           
 java.util.List determineFittestChromosomes(int a_numberOfPrograms)
          Sorts the GPPrograms list and returns the fittest n GPPrograms in the population.
 IGPProgram determineFittestProgram()
          Determines the fittest GPProgram in the population (the one with the highest fitness value) and memorizes it.
 IGPProgram determineFittestProgramComputed()
          Determines the fittest GPProgram in the population, but only considers programs with already computed fitness value.
protected  java.lang.String encode(java.lang.String a_string)
           
 boolean equals(java.lang.Object a_pop)
          The equals-method.
 float getFitnessRank(int a_index)
           
 float[] getFitnessRanks()
           
 GPConfiguration getGPConfiguration()
           
 IGPProgram getGPProgram(int a_index)
           
 IGPProgram[] getGPPrograms()
           
 java.lang.String getPersistentRepresentation()
           
 int getPopSize()
           
 boolean isChanged()
           
 boolean isFirstEmpty()
           
protected  void setChanged(boolean a_changed)
          Mark that for the population the fittest program may have changed.
 void setGPProgram(int a_index, IGPProgram a_program)
          Sets the given GPProgram at the given index in the list of GPProgram's.
 void setGPPrograms(GPPopulation a_pop)
          Sets the GPPrograms of the given population to this population.
 void setGPPrograms(IGPProgram[] a_progs)
          Sets the GPPrograms of the given array to this population.
 void setGPPrograms(java.util.List a_pop)
          Sets the GPPrograms of the given population to this population.
protected  void setSorted(boolean a_sorted)
          Mark the population as sorted.
 int size()
           
 void sort(java.util.Comparator c)
          Sorts the population into "ascending" order using some criterion for "ascending".
 void sortByFitness()
          Sorts the programs within the population according to their fitness value using GPProgramFitnessComparator.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELTA

public static final double DELTA
See Also:
Constant Field Values
Constructor Detail

GPPopulation

public GPPopulation()
             throws java.lang.Exception
Default constructor, only for dynamic instantiation.

Throws:
java.lang.Exception
Since:
3.3.4

GPPopulation

public GPPopulation(GPConfiguration a_config,
                    int a_size)
             throws InvalidConfigurationException
Throws:
InvalidConfigurationException

GPPopulation

public GPPopulation(GPPopulation a_pop)
             throws InvalidConfigurationException
Throws:
InvalidConfigurationException

GPPopulation

public GPPopulation(GPPopulation a_pop,
                    boolean a_keepPrograms)
             throws InvalidConfigurationException
Parameters:
a_pop - the population to retrieve the parameters from
a_keepPrograms - true copy programs of given population to this one
Throws:
InvalidConfigurationException
Method Detail

sort

public void sort(java.util.Comparator c)
Sorts the population into "ascending" order using some criterion for "ascending". A Comparator is given which will compare two individuals, and if one individual compares lower than another individual, the first individual will appear in the population before the second individual.

Parameters:
c - the Comparator to use
Since:
3.0

create

public void create(java.lang.Class[] a_types,
                   java.lang.Class[][] a_argTypes,
                   CommandGene[][] a_nodeSets,
                   int[] a_minDepths,
                   int[] a_maxDepths,
                   int a_maxNodes,
                   boolean[] a_fullModeAllowed)
            throws InvalidConfigurationException
Creates a population.

Parameters:
a_types - the type for each chromosome, the length of the array represents the number of chromosomes
a_argTypes - the types of the arguments to each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of arguments to the chromosome
a_nodeSets - the nodes which are allowed to be used by each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of nodes
a_minDepths - contains the minimum depth allowed for each chromosome
a_maxDepths - contains the maximum depth allowed for each chromosome
a_maxNodes - reserve space for a_maxNodes number of nodes
a_fullModeAllowed - array of boolean values. For each chromosome there is one value indicating whether the full mode for creating chromosome generations during evolution is allowed (true) or not (false)
Throws:
InvalidConfigurationException
Since:
3.0

create

public void create(java.lang.Class[] a_types,
                   java.lang.Class[][] a_argTypes,
                   CommandGene[][] a_nodeSets,
                   int[] a_minDepths,
                   int[] a_maxDepths,
                   int a_maxNodes,
                   boolean[] a_fullModeAllowed,
                   IProgramCreator a_programCreator)
            throws InvalidConfigurationException
Creates a population.

Parameters:
a_types - the type for each chromosome, the length of the array represents the number of chromosomes
a_argTypes - the types of the arguments to each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of arguments to the chromosome
a_nodeSets - the nodes which are allowed to be used by each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of nodes
a_minDepths - contains the minimum depth allowed for each chromosome
a_maxDepths - contains the maximum depth allowed for each chromosome
a_maxNodes - reserve space for a_maxNodes number of nodes
a_fullModeAllowed - array of boolean values. For each chromosome there is one value indicating whether the full mode for creating chromosome generations during evolution is allowed (true) or not (false)
a_programCreator - service to create new programs with
Throws:
InvalidConfigurationException
Since:
3.2.2

create

public void create(java.lang.Class[] a_types,
                   java.lang.Class[][] a_argTypes,
                   CommandGene[][] a_nodeSets,
                   int[] a_minDepths,
                   int[] a_maxDepths,
                   int a_maxNodes,
                   boolean[] a_fullModeAllowed,
                   IProgramCreator a_programCreator,
                   int a_offset)
            throws InvalidConfigurationException
Creates a population.

Parameters:
a_types - the type for each chromosome, the length of the array represents the number of chromosomes
a_argTypes - the types of the arguments to each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of arguments to the chromosome
a_nodeSets - the nodes which are allowed to be used by each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of nodes
a_minDepths - contains the minimum depth allowed for each chromosome
a_maxDepths - contains the maximum depth allowed for each chromosome
a_maxNodes - reserve space for a_maxNodes number of nodes
a_fullModeAllowed - array of boolean values. For each chromosome there is one value indicating whether the full mode for creating chromosome generations during evolution is allowed (true) or not (false)
a_programCreator - service to create new programs with
a_offset - start index for new programs to put into the configuration
Throws:
InvalidConfigurationException
Since:
3.3.3

create

public IGPProgram create(java.lang.Class[] a_types,
                         java.lang.Class[][] a_argTypes,
                         CommandGene[][] a_nodeSets,
                         int[] a_minDepths,
                         int[] a_maxDepths,
                         int a_depth,
                         boolean a_grow,
                         int a_maxNodes,
                         boolean[] a_fullModeAllowed,
                         int a_tries)
                  throws InvalidConfigurationException
Creates a population.

Parameters:
a_types - Class[]
a_argTypes - Class[][]
a_nodeSets - CommandGene[][]
a_minDepths - int[]
a_maxDepths - int[]
a_depth - int
a_grow - boolean
a_maxNodes - int
a_fullModeAllowed - boolean[]
a_tries - int
Returns:
IGPProgram
Throws:
InvalidConfigurationException
Since:
3.2.2

create

public IGPProgram create(int a_programIndex,
                         java.lang.Class[] a_types,
                         java.lang.Class[][] a_argTypes,
                         CommandGene[][] a_nodeSets,
                         int[] a_minDepths,
                         int[] a_maxDepths,
                         int a_depth,
                         boolean a_grow,
                         int a_maxNodes,
                         boolean[] a_fullModeAllowed,
                         int a_tries)
                  throws InvalidConfigurationException
Parameters:
a_programIndex - int
a_types - Class[]
a_argTypes - Class[][]
a_nodeSets - CommandGene[][]
a_minDepths - int[]
a_maxDepths - int[]
a_depth - int
a_grow - boolean
a_maxNodes - int
a_fullModeAllowed - boolean[]
a_tries - int
Returns:
IGPProgram
Throws:
InvalidConfigurationException
Since:
3.3

create

public IGPProgram create(int a_programIndex,
                         java.lang.Class[] a_types,
                         java.lang.Class[][] a_argTypes,
                         CommandGene[][] a_nodeSets,
                         int[] a_minDepths,
                         int[] a_maxDepths,
                         int a_depth,
                         boolean a_grow,
                         int a_maxNodes,
                         boolean[] a_fullModeAllowed,
                         int a_tries,
                         IProgramCreator a_programCreator)
                  throws InvalidConfigurationException
Creates a valid IGPProgram. No fitness computation is initiated here!

Parameters:
a_programIndex - index of the program in the population
a_types - the type of each chromosome, the length is the number of chromosomes
a_argTypes - the types of the arguments to each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of arguments to the chromosome
a_nodeSets - the nodes which are allowed to be used by each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of nodes
a_minDepths - contains the minimum depth allowed for each chromosome
a_maxDepths - contains the maximum depth allowed for each chromosome
a_depth - the maximum depth of the program to create
a_grow - true: grow mode, false: full mode
a_maxNodes - reserve space for a_maxNodes number of nodes
a_fullModeAllowed - array of boolean values. For each chromosome there is one value indicating whether the full mode for creating chromosomes during evolution is allowed (true) or not (false)
a_tries - maximum number of tries to get a valid program
a_programCreator - strategy class to create programs for the population
Returns:
valid program
Throws:
InvalidConfigurationException
Since:
3.0

getPopSize

public int getPopSize()
Returns:
fixed size of the population
Since:
3.0

getGPConfiguration

public GPConfiguration getGPConfiguration()
Returns:
the GPConfiguration set
Since:
3.0

setGPProgram

public void setGPProgram(int a_index,
                         IGPProgram a_program)
Sets the given GPProgram at the given index in the list of GPProgram's. If the given index is exceeding the list by one, the chromosome is appended.

Parameters:
a_index - the index to set the GPProgram in
a_program - the GPProgram to be set
Since:
3.0

getGPProgram

public IGPProgram getGPProgram(int a_index)

setGPPrograms

public void setGPPrograms(GPPopulation a_pop)
Sets the GPPrograms of the given population to this population.

Parameters:
a_pop - the population to use as template
Since:
3.3.3

setGPPrograms

public void setGPPrograms(IGPProgram[] a_progs)
Sets the GPPrograms of the given array to this population.

Parameters:
a_progs - the programs to set
Since:
3.3.3

copyGPPrograms

public void copyGPPrograms(GPPopulation a_pop)
Sets the GPPrograms of the given population to this population.

Parameters:
a_pop - the population to use as template
Since:
3.3.3

setGPPrograms

public void setGPPrograms(java.util.List a_pop)
Sets the GPPrograms of the given population to this population.

Parameters:
a_pop - the population to use as template
Since:
3.3.3

getGPPrograms

public IGPProgram[] getGPPrograms()

size

public int size()

determineFittestProgram

public IGPProgram determineFittestProgram()
Determines the fittest GPProgram in the population (the one with the highest fitness value) and memorizes it. This is an optimized version compared to calling determineFittesPrograms(1).

Returns:
the fittest GPProgram of the population
Since:
3.0

determineFittestProgramComputed

public IGPProgram determineFittestProgramComputed()
Determines the fittest GPProgram in the population, but only considers programs with already computed fitness value.

Returns:
the fittest GPProgram of the population
Since:
3.2

determineFittestChromosomes

public java.util.List determineFittestChromosomes(int a_numberOfPrograms)
Sorts the GPPrograms list and returns the fittest n GPPrograms in the population.

Parameters:
a_numberOfPrograms - number of top performer GPPrograms to be returned
Returns:
list of the fittest n GPPrograms of the population, or the fittest x GPPrograms with x = number of GPPrograms in case n > x.
Since:
3.0

sortByFitness

public void sortByFitness()
Sorts the programs within the population according to their fitness value using GPProgramFitnessComparator.

Since:
3.0

getFitnessRanks

public float[] getFitnessRanks()

getFitnessRank

public float getFitnessRank(int a_index)

setChanged

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

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

isChanged

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

setSorted

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

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

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).

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
a_pop - the other population to compare
Returns:
1: a_pop is null or having fewer programs or equal number of programs but at least one not contained. 0: both populations containing exactly the same programs. -1: this population contains fewer programs than a_pop
Since:
2.6

containedInArray

protected boolean containedInArray(IGPProgram[] a_progs,
                                   IGPProgram a_prog)
Checks if a program is contained within an array of programs. Assumes that in the array no element will follow after the first null element.

Parameters:
a_progs - the array to search thru
a_prog - the program to find
Returns:
true: program found in array via equals-method
Since:
3.0

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:
3.0

addFittestProgram

public void addFittestProgram(IGPProgram a_toAdd)
Adds a GP program to this Population. Does nothing when given null. The injection is actually executed in method create(..)

Parameters:
a_toAdd - the program to add
Since:
3.2

clear

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

Since:
3.2

isFirstEmpty

public boolean isFirstEmpty()

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
Returns:
the persistent representation of the population, including all GP programs
Since:
3.2.3

encode

protected java.lang.String encode(java.lang.String a_string)

decode

protected java.lang.String decode(java.lang.String a_string)

JGAP