org.jgap.gp.impl
Class GPGenotype
- java.lang.Object
-
- 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.ComparableGenotype for GP Programs.- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description 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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddFittestProgram(IGPProgram a_toAdd)Adds a GP program to this Genotype.voidcalcFitness()Calculates the fitness value of all programs, of the best solution as well as the total fitness (sum of all fitness values).static voidcheckErroneousPop(GPPopulation pop, java.lang.String s)static voidcheckErroneousPop(GPPopulation a_pop, java.lang.String a_s, boolean a_clearFitness)static voidcheckErroneousPop(GPPopulation a_pop, java.lang.String a_s, boolean a_clearFitness, boolean a_active)static voidcheckErroneousProg(IGPProgram prog, java.lang.String s)static voidcheckErroneousProg(IGPProgram a_prog, java.lang.String a_s, boolean a_clearFitness)static voidcheckErroneousProg(IGPProgram a_prog, java.lang.String s, boolean a_clearFitness, boolean a_active)intcompareTo(java.lang.Object a_other)Compares this Genotype against the specified object.booleanequals(java.lang.Object a_other)Compares this entity against the specified object.voidevolve()Evolve the population by one generation.voidevolve(int a_evolutions)Evolves the population n times.voidfillPopulation(int a_num)Fills up the population with random programs if necessary.IGPProgramgetAllTimeBest()IGPProgramgetFittestProgram()Retrieves the GPProgram in the population with the highest fitness value.IGPProgramgetFittestProgramComputed()Retrieves the GPProgram in the population with the highest fitness value.GPConfigurationgetGPConfiguration()GPPopulationgetGPPopulation()static GPConfigurationgetStaticGPConfiguration()doublegetTotalFitness()VariablegetVariable(java.lang.String a_varName)inthashCode()Hashcode function for the genotype, tries to create a unique hashcode for the chromosomes within the population.voidoutputSolution(IGPProgram a_best)Outputs the best solution until now.voidputVariable(Variable a_var)Stores a Variable.static GPGenotyperandomInitialGenotype(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 GPGenotyperandomInitialGenotype(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 GPGenotyperandomInitialGenotype(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 GPGenotyperandomInitialGenotype(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.voidrun()Default implementation of method to run GPGenotype as a thread.voidsetGPConfiguration(GPConfiguration a_configuration)Sets the configuration to use with the Genetic Algorithm.static voidsetStaticGPConfiguration(GPConfiguration a_configuration)Sets the static configuration to use with the Genetic Programming.voidsetVerboseOutput(boolean a_verbose)static java.util.Map<java.lang.String,CommandGene>verifyChildNodes(GPConfiguration a_conf, java.lang.Class[] a_types, CommandGene[][] a_nodeSets)static java.util.Map<CommandGene,int[]>verifyDepthsForNodes(GPConfiguration a_conf, java.lang.Class[] a_types, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, CommandGene[][] a_nodeSets)
-
-
-
Constructor Detail
-
GPGenotype
public GPGenotype() throws InvalidConfigurationExceptionDefault 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 usea_population- the initialized population to usea_types- the type for each chromosome, the length of the array represents the number of chromosomesa_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 chromosomea_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 nodesa_minDepths- contains the minimum depth allowed for each chromosomea_maxDepths- contains the maximum depth allowed for each chromosomea_maxNodes- reserve space for a_maxNodes number of nodes- Throws:
InvalidConfigurationException- Since:
- 3.0
-
-
Method Detail
-
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 usea_types- the type of each chromosome, the length is the number of chromosomesa_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 chromosomea_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 automaticallya_maxNodes- reserve space for a_maxNodes number of nodesa_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 usea_types- the type of each chromosome, the length is the number of chromosomesa_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 chromosomea_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 automaticallya_minDepths- array of minimum depths to use: for each chromosome one entrya_maxDepths- array of maximum depths to use: for each chromosome one entrya_maxNodes- reserve space for a_maxNodes number of nodesa_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 usea_types- the type of each chromosome, the length is the number of chromosomesa_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 chromosomea_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 automaticallya_minDepths- array of minimum depths to use: for each chromosome one entrya_maxDepths- array of maximum depths to use: for each chromosome one entrya_maxNodes- reserve space for a_maxNodes number of nodesa_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 usea_types- the type of each chromosome, the length is the number of chromosomesa_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 chromosomea_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 automaticallya_minDepths- array of minimum depths to use: for each chromosome one entrya_maxDepths- array of maximum depths to use: for each chromosome one entrya_maxNodes- reserve space for a_maxNodes number of nodesa_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 consolea_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:
runin interfacejava.lang.Runnable- Since:
- 3.0
-
getFittestProgram
public IGPProgram getFittestProgram()
Retrieves the GPProgram in the population with the highest 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 highest fitness value, or null if there are no programs with known fitness value in this Genotype
- Since:
- 3.2
-
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:
equalsin classjava.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:
compareToin interfacejava.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:
hashCodein classjava.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 InvalidConfigurationExceptionFills 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)
- Parameters:
a_conf- GPConfigurationa_types- Class[]a_nodeSets- CommandGene[][]- Returns:
- Map
- Since:
- 3.4.4
-
verifyDepthsForNodes
public static java.util.Map<CommandGene,int[]> verifyDepthsForNodes(GPConfiguration a_conf, java.lang.Class[] a_types, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes, CommandGene[][] a_nodeSets)
-
-
DMelt 3.0 © DataMelt by jWork.ORG