JGAP

org.jgap.gp.impl
Class GPGenotype

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

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

Genotype for GP Programs.

Since:
3.0
See Also:
Serialized Form

Constructor Summary
GPGenotype()
          Default constructor.
GPGenotype(GPConfiguration a_configuration, GPPopulation a_population, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes)
          Preferred constructor to use, if not using the static method randomInitialGenotype.
GPGenotype(GPConfiguration a_configuration, GPPopulation a_population, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, IGPPopulationInitializer a_popCreator)
          See above constructor.
 
Method Summary
 void addFittestProgram(IGPProgram a_toAdd)
          Adds a GP program to this Genotype.
 void calcFitness()
          Calculates the fitness value of all programs, of the best solution as well as the total fitness (sum of all fitness values).
static void checkErroneousPop(GPPopulation pop, java.lang.String s)
           
static void checkErroneousPop(GPPopulation a_pop, java.lang.String a_s, boolean a_clearFitness)
           
static void checkErroneousPop(GPPopulation a_pop, java.lang.String a_s, boolean a_clearFitness, boolean a_active)
           
static void checkErroneousProg(IGPProgram prog, java.lang.String s)
           
static void checkErroneousProg(IGPProgram a_prog, java.lang.String a_s, boolean a_clearFitness)
           
static void checkErroneousProg(IGPProgram a_prog, java.lang.String s, boolean a_clearFitness, boolean a_active)
           
 int compareTo(java.lang.Object a_other)
          Compares this Genotype against the specified object.
 boolean equals(java.lang.Object a_other)
          Compares this entity against the specified object.
 void evolve()
          Evolve the population by one generation.
 void evolve(int a_evolutions)
          Evolves the population n times.
 void fillPopulation(int a_num)
          Fills up the population with random programs if necessary.
 IGPProgram getAllTimeBest()
           
 IGPProgram getFittestProgram()
          Retrieves the GPProgram in the population with the best fitness value.
 IGPProgram getFittestProgramComputed()
          Retrieves the GPProgram in the population with the highest fitness value.
 GPConfiguration getGPConfiguration()
           
 GPPopulation getGPPopulation()
           
static GPConfiguration getStaticGPConfiguration()
           
 double getTotalFitness()
           
 Variable getVariable(java.lang.String a_varName)
           
 int hashCode()
          Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population.
protected  void init()
           
protected static boolean nodeExists(CommandGene[] a_functionSet, java.lang.Class a_returnType, int a_subReturnType)
          Is there a node with the needed return type and sub return type within the given function set?
protected static void outputDepthInfo(java.util.Map<CommandGene,int[]> a_invalidDepths)
          Outputs the nodes that are never used because they would result in creating GP programs which would not satisfy the depth constraints given in configuration.
 void outputSolution(IGPProgram a_best)
          Outputs the best solution until now.
protected static void outputWarning(java.util.Map<java.lang.String,CommandGene> invalidNodes)
          Outputs the nodes that are never used.
 void putVariable(Variable a_var)
          Stores a Variable.
static GPGenotype randomInitialGenotype(GPConfiguration a_conf, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, boolean a_verboseOutput)
          Creates a genotype with initial population for the world set.
static GPGenotype randomInitialGenotype(GPConfiguration a_conf, 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, boolean a_verboseOutput)
          Creates a genotype with a randomly created initial population.
static GPGenotype randomInitialGenotype(GPConfiguration a_conf, 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, boolean a_verboseOutput, IPopulationCreator a_popCreator)
          Allows to use a custom mechanism for population creation.
static GPGenotype randomInitialGenotype(GPConfiguration a_conf, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int a_maxNodes, boolean a_verboseOutput)
          Creates a genotype with initial population for the world set.
 void run()
          Default implementation of method to run GPGenotype as a thread.
 void setGPConfiguration(GPConfiguration a_configuration)
          Sets the configuration to use with the Genetic Algorithm.
protected  void setGPPopulation(GPPopulation a_pop)
           
static void setStaticGPConfiguration(GPConfiguration a_configuration)
          Sets the static configuration to use with the Genetic Programming.
 void setVerboseOutput(boolean a_verbose)
           
static java.util.Map<java.lang.String,CommandGene> verifyChildNodes(GPConfiguration a_conf, java.lang.Class[] a_types, CommandGene[][] a_nodeSets)
          Determine impossible functions and terminals to obtimize creation of GP programs.
static java.util.Map<CommandGene,int[]> verifyDepthsForNodes(GPPopulation a_pop, GPConfiguration a_conf, java.lang.Class[] a_types, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, CommandGene[][] a_nodeSets)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GPGenotype

public GPGenotype()
           throws InvalidConfigurationException
Default constructor. Ony use with dynamic instantiation.

Throws:
InvalidConfigurationException
Since:
3.0

GPGenotype

public GPGenotype(GPConfiguration a_configuration,
                  GPPopulation a_population,
                  java.lang.Class[] a_types,
                  java.lang.Class[][] a_argTypes,
                  CommandGene[][] a_nodeSets,
                  int[] a_minDepths,
                  int[] a_maxDepths,
                  int a_maxNodes)
           throws InvalidConfigurationException
Preferred constructor to use, if not using the static method randomInitialGenotype.

Parameters:
a_configuration - the configuration to use
a_population - the initialized population to use
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
Throws:
InvalidConfigurationException
Since:
3.0

GPGenotype

public GPGenotype(GPConfiguration a_configuration,
                  GPPopulation a_population,
                  java.lang.Class[] a_types,
                  java.lang.Class[][] a_argTypes,
                  CommandGene[][] a_nodeSets,
                  int[] a_minDepths,
                  int[] a_maxDepths,
                  int a_maxNodes,
                  IGPPopulationInitializer a_popCreator)
           throws InvalidConfigurationException
See above constructor. Parameter a_popCreator is used in grid context only.

Parameters:
a_configuration - GPConfiguration
a_population - GPPopulation
a_types - Class[]
a_argTypes - Class[][]
a_nodeSets - CommandGene[][]
a_minDepths - int[]
a_maxDepths - int[]
a_maxNodes - int
a_popCreator - IPopulationCreator
Throws:
InvalidConfigurationException
Since:
3.6
Method Detail

init

protected void init()

randomInitialGenotype

public static GPGenotype randomInitialGenotype(GPConfiguration a_conf,
                                               java.lang.Class[] a_types,
                                               java.lang.Class[][] a_argTypes,
                                               CommandGene[][] a_nodeSets,
                                               int a_maxNodes,
                                               boolean a_verboseOutput)
                                        throws InvalidConfigurationException
Creates a genotype with initial population for the world set.

Parameters:
a_conf - the configuration to use
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. Note that it is not necessary to include the arguments of a chromosome as terminals in the chromosome's node set. This is done automatically
a_maxNodes - reserve space for a_maxNodes number of nodes
a_verboseOutput - true: output status information to console
Returns:
created genotype with initialized population
Throws:
InvalidConfigurationException
Since:
3.0

randomInitialGenotype

public static GPGenotype randomInitialGenotype(GPConfiguration a_conf,
                                               java.lang.Class[] a_types,
                                               java.lang.Class[][] a_argTypes,
                                               CommandGene[][] a_nodeSets,
                                               int[] a_minDepths,
                                               int[] a_maxDepths,
                                               int a_maxNodes,
                                               boolean a_verboseOutput)
                                        throws InvalidConfigurationException
Creates a genotype with initial population for the world set.

Parameters:
a_conf - the configuration to use
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. Note that it is not necessary to include the arguments of a chromosome as terminals in the chromosome's node set. This is done automatically
a_minDepths - array of minimum depths to use: for each chromosome one entry
a_maxDepths - array of maximum depths to use: for each chromosome one entry
a_maxNodes - reserve space for a_maxNodes number of nodes
a_verboseOutput - true: output status information to console
Returns:
created genotype with initialized population
Throws:
InvalidConfigurationException
Since:
3.0

randomInitialGenotype

public static GPGenotype randomInitialGenotype(GPConfiguration a_conf,
                                               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,
                                               boolean a_verboseOutput)
                                        throws InvalidConfigurationException
Creates a genotype with a randomly created initial population.

Parameters:
a_conf - the configuration to use
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. Note that it is not necessary to include the arguments of a chromosome as terminals in the chromosome's node set. This is done automatically
a_minDepths - array of minimum depths to use: for each chromosome one entry
a_maxDepths - array of maximum depths to use: for each chromosome one entry
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_verboseOutput - true: output status information to console
Returns:
created genotype with initialized population
Throws:
InvalidConfigurationException
Since:
3.0

randomInitialGenotype

public static GPGenotype randomInitialGenotype(GPConfiguration a_conf,
                                               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,
                                               boolean a_verboseOutput,
                                               IPopulationCreator a_popCreator)
                                        throws InvalidConfigurationException
Allows to use a custom mechanism for population creation.

Parameters:
a_conf - the configuration to use
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. Note that it is not necessary to include the arguments of a chromosome as terminals in the chromosome's node set. This is done automatically
a_minDepths - array of minimum depths to use: for each chromosome one entry
a_maxDepths - array of maximum depths to use: for each chromosome one entry
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_verboseOutput - true: output status information to console
a_popCreator - mechanism fior creating the population
Returns:
GPGenotype
Throws:
InvalidConfigurationException
Since:
3.2.2

getGPConfiguration

public GPConfiguration getGPConfiguration()

getStaticGPConfiguration

public static GPConfiguration getStaticGPConfiguration()
Returns:
the static configuration to use with Genetic Programming
Since:
3.2

setStaticGPConfiguration

public static void setStaticGPConfiguration(GPConfiguration a_configuration)
Sets the static configuration to use with the Genetic Programming.

Parameters:
a_configuration - the static configuration to use
Since:
3.2

evolve

public void evolve(int a_evolutions)
Evolves the population n times.

Parameters:
a_evolutions - number of evolution
Since:
3.0

calcFitness

public void calcFitness()
Calculates the fitness value of all programs, of the best solution as well as the total fitness (sum of all fitness values).

Since:
3.0

getAllTimeBest

public IGPProgram getAllTimeBest()
Returns:
the all-time best solution found
Since:
3.0

outputSolution

public void outputSolution(IGPProgram a_best)
Outputs the best solution until now.

Parameters:
a_best - the fittest ProgramChromosome
Since:
3.0

evolve

public void evolve()
Evolve the population by one generation. Probabilistically reproduces and crosses individuals into a new population which then overwrites the original population.

Since:
3.0

getGPPopulation

public GPPopulation getGPPopulation()

getTotalFitness

public double getTotalFitness()
Returns:
the total fitness, that is the fitness over all chromosomes
Since:
3.0

run

public void run()
Default implementation of method to run GPGenotype as a thread.

Specified by:
run in interface java.lang.Runnable
Since:
3.0

getFittestProgram

public IGPProgram getFittestProgram()
Retrieves the GPProgram in the population with the best fitness value.

Returns:
the GPProgram with the highest fitness value, or null if there are no programs in this Genotype
Since:
3.0

getFittestProgramComputed

public IGPProgram getFittestProgramComputed()
Retrieves the GPProgram in the population with the highest fitness value. Only considers programs for which the fitness value has already been computed.

Returns:
the GPProgram with the best fitness value, or null if there are no programs with known fitness value in this Genotype
Since:
3.2

setGPPopulation

protected void setGPPopulation(GPPopulation a_pop)

setGPConfiguration

public void setGPConfiguration(GPConfiguration a_configuration)
Sets the configuration to use with the Genetic Algorithm.

Parameters:
a_configuration - the configuration to use
Since:
3.0

equals

public boolean equals(java.lang.Object a_other)
Compares this entity against the specified object.

Overrides:
equals in class java.lang.Object
Parameters:
a_other - the object to compare against
Returns:
true: if the objects are the same, false otherwise
Since:
3.0

compareTo

public int compareTo(java.lang.Object a_other)
Compares this Genotype against the specified object. The result is true if the argument is an instance of the Genotype class, has exactly the same number of programs as the given Genotype, and, for each GPProgram in this Genotype, there is an equal program in the given Genotype. The programs do not need to appear in the same order within the populations.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
a_other - the object to compare against
Returns:
a negative number if this genotype is "less than" the given genotype, zero if they are equal to each other, and a positive number if this genotype is "greater than" the given genotype
Since:
3.0

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) Each hashcode is a number and the binary equivalent is computed and returned.

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

setVerboseOutput

public void setVerboseOutput(boolean a_verbose)
Parameters:
a_verbose - true: output status information to console
Since:
3.0

putVariable

public void putVariable(Variable a_var)
Stores a Variable.

Parameters:
a_var - the Variable to store
Since:
3.2

getVariable

public Variable getVariable(java.lang.String a_varName)
Parameters:
a_varName - name of variable to retriebe
Returns:
Variable instance or null, if not found
Since:
3.2

addFittestProgram

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

Parameters:
a_toAdd - the program to add
Since:
3.2

fillPopulation

public void fillPopulation(int a_num)
                    throws InvalidConfigurationException
Fills up the population with random programs if necessary.

Parameters:
a_num - the number of programs to add
Throws:
InvalidConfigurationException
Since:
3.2

checkErroneousPop

public static void checkErroneousPop(GPPopulation pop,
                                     java.lang.String s)

checkErroneousPop

public static void checkErroneousPop(GPPopulation a_pop,
                                     java.lang.String a_s,
                                     boolean a_clearFitness)

checkErroneousPop

public static void checkErroneousPop(GPPopulation a_pop,
                                     java.lang.String a_s,
                                     boolean a_clearFitness,
                                     boolean a_active)

checkErroneousProg

public static void checkErroneousProg(IGPProgram prog,
                                      java.lang.String s)

checkErroneousProg

public static void checkErroneousProg(IGPProgram a_prog,
                                      java.lang.String a_s,
                                      boolean a_clearFitness)

checkErroneousProg

public static void checkErroneousProg(IGPProgram a_prog,
                                      java.lang.String s,
                                      boolean a_clearFitness,
                                      boolean a_active)

verifyChildNodes

public static java.util.Map<java.lang.String,CommandGene> verifyChildNodes(GPConfiguration a_conf,
                                                                           java.lang.Class[] a_types,
                                                                           CommandGene[][] a_nodeSets)
Determine impossible functions and terminals to obtimize creation of GP programs.

Parameters:
a_conf - GP configuration
a_types - Class[]
a_nodeSets - CommandGene[][]
Returns:
Map
Since:
3.4.4

nodeExists

protected static boolean nodeExists(CommandGene[] a_functionSet,
                                    java.lang.Class a_returnType,
                                    int a_subReturnType)
Is there a node with the needed return type and sub return type within the given function set?

Parameters:
a_functionSet - collection of available nodes
a_returnType - needed return type
a_subReturnType - needed sub return type
Returns:
true: node found
Since:
3.4.4

outputWarning

protected static void outputWarning(java.util.Map<java.lang.String,CommandGene> invalidNodes)
Outputs the nodes that are never used.

Parameters:
invalidNodes - never used nodes
Since:
3.4.4

outputDepthInfo

protected static void outputDepthInfo(java.util.Map<CommandGene,int[]> a_invalidDepths)
Outputs the nodes that are never used because they would result in creating GP programs which would not satisfy the depth constraints given in configuration.

Parameters:
a_invalidDepths - impossible depths for nodes
Since:
3.4.4

verifyDepthsForNodes

public static java.util.Map<CommandGene,int[]> verifyDepthsForNodes(GPPopulation a_pop,
                                                                    GPConfiguration a_conf,
                                                                    java.lang.Class[] a_types,
                                                                    int[] a_minDepths,
                                                                    int[] a_maxDepths,
                                                                    int a_maxNodes,
                                                                    CommandGene[][] a_nodeSets)

JGAP