JGAP

org.jgap.gp.impl
Class GPConfiguration

java.lang.Object
  extended by org.jgap.Configuration
      extended by org.jgap.gp.impl.GPConfiguration
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, Configurable, ICloneable
Direct Known Subclasses:
DrawingGPConfiguration

public class GPConfiguration
extends Configuration

Configuration for a GP.

Since:
3.0
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jgap.Configuration
Configuration.ConfigurationConfigurable
 
Field Summary
 
Fields inherited from class org.jgap.Configuration
PROPERTY_BFITFNC_INST, PROPERTY_EVENT_MGR_INST, PROPERTY_FITEVAL_INST, PROPERTY_FITFUNC_INST, PROPERTY_JGAPFACTORY_CLASS, PROPERTY_SAMPLE_CHROM_INST, S_CHROMOSOME_SIZE, S_CONFIGURATION, S_CONFIGURATION_HANDLER, S_CONFIGURATION_NAME, S_EVENT_MANAGER, S_FITNESS_EVALUATOR, S_FITNESS_FUNCTION, S_GENETIC_OPERATORS, S_MINPOPSIZE, S_NATURAL_SELECTORS, S_NONE, S_POPULATION_SIZE, S_POST, S_PRE, S_RANDOM_GENERATOR, S_SAMPLE_CHROM, S_SIZE, S_TOSTRING
 
Constructor Summary
GPConfiguration()
          Constructor utilizing the FitnessProportionateSelection.
GPConfiguration(INaturalGPSelector a_selectionMethod)
          Constructor utilizing the FitnessProportionateSelection.
GPConfiguration(java.lang.String a_name)
          Constructs a configuration with an informative name but without a unique ID.
GPConfiguration(java.lang.String a_id, java.lang.String a_name)
           
 
Method Summary
 void addGeneticOperator(GeneticOperator a_operatorToAdd)
          Adds a genetic operator for use in this algorithm.
 void clearMemory()
          Clears the memory.
 void clearStack()
           
 java.lang.Object clone()
           
 int compareTo(java.lang.Object a_other)
          The compareTo-method.
 void createMatrix(java.lang.String a_name, int a_cols, int a_rows)
          Creates an instance of a matrix with a unique name.
 boolean equals(java.lang.Object a_other)
          Compares this entity against the specified object.
 void flagMaxNodeWarningPrinted()
          See ProgramChromosome.growOrFull(...) and GPGenotype.evolve().
 CrossMethod getCrossMethod()
           
 double getCrossoverProb()
           
 double getDynamizeArityProb()
           
 double getFunctionProb()
           
 IGPFitnessEvaluator getGPFitnessEvaluator()
           
 GPFitnessFunction getGPFitnessFunction()
           
 IGPInitStrategy getInitStrategy()
           
 IJGAPFactory getJGAPFactory()
           
 char[][] getMatrix(java.lang.String a_name)
          Retrieves a named matrix.
 int getMaxCrossoverDepth()
           
 int getMaxInitDepth()
           
 int getMemorySize()
           
 int getMinInitDepth()
           
 double getMutationProb()
           
 double getNewChromsPercent()
           
 INodeValidator getNodeValidator()
           
 int getProgramCreationMaxtries()
           
 IGPProgram getPrototypeProgram()
           
 double getReproductionProb()
           
 INaturalGPSelector getSelectionMethod()
           
 Variable getVariable(java.lang.String a_varName)
           
protected  void init(boolean a_fullInit)
          Helper for construction.
 boolean isMaxNodeWarningPrinted()
           
 boolean isNoCommandGeneCloning()
           
 boolean isStrictProgramCreation()
           
 boolean isUseProgramCache()
           
 boolean isVerifyPrograms()
           
 GPConfiguration newInstanceGP(java.lang.String a_id, java.lang.String a_name)
          Creates a new GPConfiguration instance by cloning.
 java.lang.Object peekStack()
           
 java.lang.Object popFromStack()
           
 void pushToStack(java.lang.Object a_value)
           
 GPProgramInfo putToProgramCache(GPProgram a_prog)
           
 void putVariable(Variable a_var)
          Stores a Variable.
 java.lang.Object readFromMemory(java.lang.String a_name)
          Reads a value from the internal memory.
 java.lang.Object readFromMemoryIfExists(java.lang.String a_name)
           
 java.lang.Object readIndexedMemory(int a_index)
          Reads a value from the internal indexed memory.
 char readMatrix(java.lang.String a_name, int a_col, int a_row)
          Reads a matrix cell and returns the value.
 java.lang.Object readMatrixMemory(int a_x, int a_y)
          Reads a value from the internal matrix memory.
 GPProgramInfo readProgramCache(GPProgram a_prog)
           
 void resetMatrix(java.lang.String a_name, char a_filler)
          Resets the matrix by filling it with a given character.
 void setCrossoverMethod(CrossMethod a_method)
          Sets the crossover method to use.
 void setCrossoverProb(float a_crossoverProb)
           
 void setDynamizeArityProb(float a_dynArityProb)
           
 void setFitnessEvaluator(IGPFitnessEvaluator a_fitnessEvaluator)
          Set the fitness evaluator (deciding if a given fitness value is better when it's higher or better when it's lower).
 void setFitnessFunction(GPFitnessFunction a_functionToSet)
          Sets the fitness function to be used for this genetic algorithm.
 void setFunctionProb(double a_functionProb)
           
 void setGPFitnessEvaluator(IGPFitnessEvaluator a_evaluator)
          Sets a GP fitness evaluator, such as org.jgap.gp.impl.DefaultGPFitnessEvaluator.
 void setInitStrategy(IGPInitStrategy a_strategy)
           
 void setMatrix(java.lang.String a_name, int a_col, int a_row, char a_value)
          Sets a matrix field with a value.
 void setMaxCrossoverDepth(int a_maxCrossoverDepth)
           
 void setMaxInitDepth(int a_maxDepth)
           
 void setMinInitDepth(int a_minDepth)
           
 void setMutationProb(float a_mutationProb)
           
 void setNewChromsPercent(double a_newChromsPercent)
           
 void setNoCommandGeneCloning(boolean a_noCommandGeneCloning)
          Decide whether to clone command genes when creating a new GP program in ProgramChromosome.
 void setNodeValidator(INodeValidator a_nodeValidator)
          Sets the node validator.
 void setProgramCreationMaxTries(int a_maxtries)
           
 void setPrototypeProgram(IGPProgram a_program)
           
 void setReproductionProb(float a_reproductionProb)
           
 void setSelectionMethod(INaturalGPSelector a_method)
          Sets the selection method to use.
 void setStrictProgramCreation(boolean a_strict)
           
 void setUseProgramCache(boolean a_useCache)
           
 void setVerifyPrograms(boolean a_verify)
           
 int stackSize()
           
 CultureMemoryCell storeIndexedMemory(int a_index, java.lang.Object a_value)
          Stores a value in the internal indexed memory.
 void storeInMemory(java.lang.String a_name, java.lang.Object a_value)
          Stores a value in the internal memory.
 CultureMemoryCell storeMatrixMemory(int a_x, int a_y, java.lang.Object a_value)
          Stores a value in the internal matrix memory.
 boolean validateNode(ProgramChromosome a_chrom, CommandGene a_node, CommandGene a_rootNode, int a_tries, int a_num, int a_recurseLevel, java.lang.Class a_type, CommandGene[] a_functionSet, int a_depth, boolean a_grow, int a_childIndex, boolean a_fullProgram)
          Validates a_node in the context of a_chrom.
 void verifyStateIsValid()
          Tests the state of this Configuration object to make sure it's valid.
 
Methods inherited from class org.jgap.Configuration
addNaturalSelector, checkProperty, doClone, getBreeder, getBulkFitnessFunction, getChromosomePool, getChromosomeSize, getConfigurationHandler, getEventManager, getFitnessEvaluator, getFitnessFunction, getGenerationNr, getGeneticOperators, getId, getMinimumPopSizePercent, getMonitor, getName, getNaturalSelector, getNaturalSelector, getNaturalSelectors, getNaturalSelectorsSize, getPopulationSize, getRandomGenerator, getSampleChromosome, getSelectFromPrevGen, getThreadKey, incrementGenerationNr, isAlwaysCalculateFitness, isKeepPopulationSizeConstant, isLocked, isPreserveFittestIndividual, isUniqueKeysActive, lockSettings, makeKey, makeThreadKey, newInstance, removeNaturalSelectors, reset, reset, resetProperty, resetProperty, setAlwaysCaculateFitness, setBreeder, setBulkFitnessFunction, setChromosomePool, setEventManager, setFitnessEvaluator, setFitnessFunction, setId, setJGAPFactory, setKeepPopulationSizeConstant, setMinimumPopSizePercent, setMonitor, setName, setNaturalSelector, setPopulationSize, setPreservFittestIndividual, setRandomGenerator, setSampleChromosome, setSelectFromPrevGen, setUniqueKeysActive, toString, verifyChangesAllowed
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GPConfiguration

public GPConfiguration()
                throws InvalidConfigurationException
Constructor utilizing the FitnessProportionateSelection.

Throws:
InvalidConfigurationException
Since:
3.0

GPConfiguration

public GPConfiguration(java.lang.String a_id,
                       java.lang.String a_name)
                throws InvalidConfigurationException
Throws:
InvalidConfigurationException

GPConfiguration

public GPConfiguration(java.lang.String a_name)
                throws InvalidConfigurationException
Constructs a configuration with an informative name but without a unique ID. This practically prevents more than one configurations to be instantiated within the same thread.

Parameters:
a_name - informative name of the configuration, may be null
Throws:
InvalidConfigurationException

GPConfiguration

public GPConfiguration(INaturalGPSelector a_selectionMethod)
                throws InvalidConfigurationException
Constructor utilizing the FitnessProportionateSelection.

Parameters:
a_selectionMethod - the selection method to use
Throws:
InvalidConfigurationException
Since:
3.1
Method Detail

setGPFitnessEvaluator

public void setGPFitnessEvaluator(IGPFitnessEvaluator a_evaluator)
Sets a GP fitness evaluator, such as org.jgap.gp.impl.DefaultGPFitnessEvaluator.

Parameters:
a_evaluator - the fitness evaluator to set
Since:
3.1

init

protected void init(boolean a_fullInit)
             throws InvalidConfigurationException
Helper for construction.

Parameters:
a_fullInit - true set event manager, random generator and fitness evaluator to defauklt
Throws:
InvalidConfigurationException
Since:
3.1

setSelectionMethod

public void setSelectionMethod(INaturalGPSelector a_method)
Sets the selection method to use.

Parameters:
a_method - the selection method to use
Since:
3.1

setCrossoverMethod

public void setCrossoverMethod(CrossMethod a_method)
Sets the crossover method to use.

Parameters:
a_method - the crossover method to use
Since:
3.1

verifyStateIsValid

public void verifyStateIsValid()
                        throws InvalidConfigurationException
Description copied from class: Configuration
Tests the state of this Configuration object to make sure it's valid. This generally consists of verifying that required settings have, in fact, been set. If this object is not in a valid state, then an exception will be thrown detailing the reason the state is not valid.

Overrides:
verifyStateIsValid in class Configuration
Throws:
InvalidConfigurationException - if the state of this Configuration is not valid. The error message in the exception will detail the reason for invalidity

addGeneticOperator

public void addGeneticOperator(GeneticOperator a_operatorToAdd)
                        throws InvalidConfigurationException
Description copied from class: Configuration
Adds a genetic operator for use in this algorithm. Genetic operators represent evolutionary steps that, when combined, make up the evolutionary process. Examples of genetic operators are reproduction, crossover, and mutation. During the evolution process, all of the genetic operators added via this method are invoked in the order they were added. At least one genetic operator must be provided.

Overrides:
addGeneticOperator in class Configuration
Parameters:
a_operatorToAdd - the genetic operator to add.
Throws:
InvalidConfigurationException - if the genetic operator is null or if this Configuration object is locked

getCrossoverProb

public double getCrossoverProb()

setCrossoverProb

public void setCrossoverProb(float a_crossoverProb)

getReproductionProb

public double getReproductionProb()

setReproductionProb

public void setReproductionProb(float a_reproductionProb)

getMutationProb

public double getMutationProb()
Returns:
probability for mutation of a node during growing a program
Since:
3.3.1

setMutationProb

public void setMutationProb(float a_mutationProb)
Parameters:
a_mutationProb - probability for mutation of a node during growing a program
Since:
3.3.1

getDynamizeArityProb

public double getDynamizeArityProb()
Returns:
probability for dynamizing the arity of a node during growing a program
Since:
3.4

setDynamizeArityProb

public void setDynamizeArityProb(float a_dynArityProb)
Parameters:
a_dynArityProb - probability for dynamizing the arity of a node during growing a program
Since:
3.4

setFunctionProb

public void setFunctionProb(double a_functionProb)
Parameters:
a_functionProb - probability that a function instead of a terminal is chosen in crossing over (between 0 and 1)
Since:
3.2

getFunctionProb

public double getFunctionProb()
Returns:
probability that a function instead of a terminal is chosen in crossing over
Since:
3.2

setNewChromsPercent

public void setNewChromsPercent(double a_newChromsPercent)

getNewChromsPercent

public double getNewChromsPercent()

getMaxCrossoverDepth

public int getMaxCrossoverDepth()

setMaxCrossoverDepth

public void setMaxCrossoverDepth(int a_maxCrossoverDepth)

getSelectionMethod

public INaturalGPSelector getSelectionMethod()

getCrossMethod

public CrossMethod getCrossMethod()

getMaxInitDepth

public int getMaxInitDepth()

setMaxInitDepth

public void setMaxInitDepth(int a_maxDepth)

getMinInitDepth

public int getMinInitDepth()

setMinInitDepth

public void setMinInitDepth(int a_minDepth)

pushToStack

public void pushToStack(java.lang.Object a_value)

popFromStack

public java.lang.Object popFromStack()

peekStack

public java.lang.Object peekStack()

stackSize

public int stackSize()

clearStack

public void clearStack()

storeInMemory

public void storeInMemory(java.lang.String a_name,
                          java.lang.Object a_value)
Stores a value in the internal memory.

Parameters:
a_name - named index of the memory cell
a_value - the value to store
Since:
3.0

createMatrix

public void createMatrix(java.lang.String a_name,
                         int a_cols,
                         int a_rows)
Creates an instance of a matrix with a unique name.

Parameters:
a_name - the name of the matrix
a_cols - number of columns the matrix should have
a_rows - number of rows the matrix should have
Since:
3.4.3

setMatrix

public void setMatrix(java.lang.String a_name,
                      int a_col,
                      int a_row,
                      char a_value)
Sets a matrix field with a value.

Parameters:
a_name - the name of the matrix
a_col - column in the matrix
a_row - row in the matrix
a_value - the value to set in the matrix at given column and row
Since:
3.4.3

resetMatrix

public void resetMatrix(java.lang.String a_name,
                        char a_filler)
Resets the matrix by filling it with a given character.

Parameters:
a_name - the name of the matrix
a_filler - the character to fill the whole matrix with
Since:
3.4.3

readMatrix

public char readMatrix(java.lang.String a_name,
                       int a_col,
                       int a_row)
Reads a matrix cell and returns the value.

Parameters:
a_name - the name of the matrix
a_col - the column to read
a_row - the row to read
Returns:
the value in the matrix
Since:
3.4.3

getMatrix

public char[][] getMatrix(java.lang.String a_name)
Retrieves a named matrix.

Parameters:
a_name - the name of the matrix
Returns:
the matrix itself
Since:
3.4.3

storeMatrixMemory

public CultureMemoryCell storeMatrixMemory(int a_x,
                                           int a_y,
                                           java.lang.Object a_value)
Stores a value in the internal matrix memory.

Parameters:
a_x - the first coordinate of the matrix (width)
a_y - the second coordinate of the matrix (height)
a_value - the value to store
Returns:
created or used memory cell
Since:
3.2

readMatrixMemory

public java.lang.Object readMatrixMemory(int a_x,
                                         int a_y)
Reads a value from the internal matrix memory.

Parameters:
a_x - the first coordinate of the matrix (width)
a_y - the second coordinate of the matrix (height)
Returns:
read value
Since:
3.2

readFromMemory

public java.lang.Object readFromMemory(java.lang.String a_name)
Reads a value from the internal memory.

Parameters:
a_name - named index of the memory cell to read out
Returns:
read value
Since:
3.0

readFromMemoryIfExists

public java.lang.Object readFromMemoryIfExists(java.lang.String a_name)
Parameters:
a_name - the name of the cell to evaluate
Returns:
the value of a memory cell, if it exsists. Otherwise returns null.
Since:
3.2

storeIndexedMemory

public CultureMemoryCell storeIndexedMemory(int a_index,
                                            java.lang.Object a_value)
Stores a value in the internal indexed memory.

Parameters:
a_index - index of the cell
a_value - the value to store
Returns:
created or used memory cell
Since:
3.2

readIndexedMemory

public java.lang.Object readIndexedMemory(int a_index)
Reads a value from the internal indexed memory.

Parameters:
a_index - index of the cell
Returns:
read value (maybe null )
Since:
3.2

clearMemory

public void clearMemory()
Clears the memory.

Since:
3.0

getGPFitnessFunction

public GPFitnessFunction getGPFitnessFunction()

setFitnessEvaluator

public void setFitnessEvaluator(IGPFitnessEvaluator a_fitnessEvaluator)
Set the fitness evaluator (deciding if a given fitness value is better when it's higher or better when it's lower).

Parameters:
a_fitnessEvaluator - the FitnessEvaluator to be used
Since:
3.3.3

setFitnessFunction

public void setFitnessFunction(GPFitnessFunction a_functionToSet)
                        throws InvalidConfigurationException
Sets the fitness function to be used for this genetic algorithm. The fitness function is responsible for evaluating a given Chromosome and returning a positive integer that represents its worth as a candidate solution. These values are used as a guide by the natural to determine which Chromosome instances will be allowed to move on to the next round of evolution, and which will instead be eliminated.

Parameters:
a_functionToSet - fitness function to be used
Throws:
InvalidConfigurationException - if the fitness function is null, or if this Configuration object is locked.
Since:
1.1

isStrictProgramCreation

public boolean isStrictProgramCreation()
Returns:
true: throw an error during evolution in case a situation is detected where no function or terminal of a required type is declared in the GPConfiguration; false: don't throw an error but try a completely different combination of functions and terminals

setStrictProgramCreation

public void setStrictProgramCreation(boolean a_strict)
Parameters:
a_strict - true: throw an error during evolution in case a situation is detected where no function or terminal of a required type is declared in the GPConfiguration; false: don't throw an error but try a completely different combination of functions and terminals

getProgramCreationMaxtries

public int getProgramCreationMaxtries()

setProgramCreationMaxTries

public void setProgramCreationMaxTries(int a_maxtries)

getGPFitnessEvaluator

public IGPFitnessEvaluator getGPFitnessEvaluator()
Returns:
the fitness evaluator set
Since:
3.0

validateNode

public boolean validateNode(ProgramChromosome a_chrom,
                            CommandGene a_node,
                            CommandGene a_rootNode,
                            int a_tries,
                            int a_num,
                            int a_recurseLevel,
                            java.lang.Class a_type,
                            CommandGene[] a_functionSet,
                            int a_depth,
                            boolean a_grow,
                            int a_childIndex,
                            boolean a_fullProgram)
Validates a_node in the context of a_chrom. Considers the recursion level (a_recursLevel), the type needed (a_type) for the node, the functions available (a_functionSet) and the depth of the whole chromosome needed (a_depth), and whether grow mode is used (a_grow is true) or not.

Parameters:
a_chrom - the chromosome that will contain the node, if valid
a_node - the node selected and to be validated
a_rootNode - root node of the node to be validated (may be null)
a_tries - number of times the validator has been called, useful for stopping by returning true if the number exceeds a limit
a_num - the chromosome's index in the individual of this chromosome
a_recurseLevel - level of recursion
a_type - the return type of the node needed
a_functionSet - the array of available functions
a_depth - the needed depth of the program chromosome
a_grow - true: use grow mode, false: use full mode
a_childIndex - index of the child in the parent node to which it belongs (-1 if node is root node)
a_fullProgram - true: whole program is available in a_chrom
Returns:
true: node is valid; false: node is invalid
Since:
3.0

setNodeValidator

public void setNodeValidator(INodeValidator a_nodeValidator)
Sets the node validator. Also see method validateNode.

Parameters:
a_nodeValidator - sic
Since:
3.0

getNodeValidator

public INodeValidator getNodeValidator()
Returns:
the node validator set
Since:
3.0

equals

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

Overrides:
equals in class Configuration
Parameters:
a_other - the object to compare against
Returns:
true: if the objects are the same, false otherwise
Since:
3.1

compareTo

public int compareTo(java.lang.Object a_other)
Description copied from class: Configuration
The compareTo-method.

Specified by:
compareTo in interface java.lang.Comparable
Overrides:
compareTo in class Configuration
Parameters:
a_other - the other object to compare
Returns:
-1, 0, 1

isMaxNodeWarningPrinted

public boolean isMaxNodeWarningPrinted()
Returns:
see ProgramChromosome.growOrFull(...) and GPGenotype.evolve()
Since:
3.2

flagMaxNodeWarningPrinted

public void flagMaxNodeWarningPrinted()
See ProgramChromosome.growOrFull(...) and GPGenotype.evolve().

Since:
3.2

setPrototypeProgram

public void setPrototypeProgram(IGPProgram a_program)
Parameters:
a_program - IGPProgram
Since:
3.2

getPrototypeProgram

public IGPProgram getPrototypeProgram()
Returns:
prototype program set (maybe null if not setted previously)
Since:
3.2

getMemorySize

public int getMemorySize()
Returns:
capacity of the memory in cells
Since:
3.2

readProgramCache

public GPProgramInfo readProgramCache(GPProgram a_prog)

putToProgramCache

public GPProgramInfo putToProgramCache(GPProgram a_prog)

isUseProgramCache

public boolean isUseProgramCache()

setUseProgramCache

public void setUseProgramCache(boolean a_useCache)

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

clone

public java.lang.Object clone()
Specified by:
clone in interface ICloneable
Overrides:
clone in class Configuration
Returns:
deep clone of this instance
Since:
3.2

newInstanceGP

public GPConfiguration newInstanceGP(java.lang.String a_id,
                                     java.lang.String a_name)
Creates a new GPConfiguration instance by cloning. Allows to preset the ID and the name.

Parameters:
a_id - new ID for clone
a_name - new name for clone
Returns:
deep clone of this instance
Since:
3.2

getJGAPFactory

public IJGAPFactory getJGAPFactory()
Overrides:
getJGAPFactory in class Configuration
Returns:
the JGAP factory registered
Since:
3.2

setInitStrategy

public void setInitStrategy(IGPInitStrategy a_strategy)
Parameters:
a_strategy - IGPInitStrategy
Since:
3.2.2

getInitStrategy

public IGPInitStrategy getInitStrategy()
Returns:
IGPInitStrategy
Since:
3.2.2

setVerifyPrograms

public void setVerifyPrograms(boolean a_verify)
Parameters:
a_verify - true: verify GP programs for correctness (i.e. is fitness computation possible without exception?)
Since:
3.3.4

isVerifyPrograms

public boolean isVerifyPrograms()
Returns:
true: verify GP programs for correctness (i.e. is fitness computation possible without exception?)
Since:
3.3.4

setNoCommandGeneCloning

public void setNoCommandGeneCloning(boolean a_noCommandGeneCloning)
Decide whether to clone command genes when creating a new GP program in ProgramChromosome.

Parameters:
a_noCommandGeneCloning - boolean
Since:
3.4.3

isNoCommandGeneCloning

public boolean isNoCommandGeneCloning()
Returns:
true: do not clone command genes when creating a new GP program in ProgramChromosome
Since:
3.4.3

JGAP