JGAP

org.jgap.gp.impl
Class ProgramChromosome

java.lang.Object
  extended by org.jgap.gp.BaseGPChromosome
      extended by org.jgap.gp.impl.ProgramChromosome
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, IGPChromosome, IBusinessKey

public class ProgramChromosome
extends BaseGPChromosome
implements java.lang.Comparable, java.lang.Cloneable, IBusinessKey

Chromosome representing a single GP Program.

Since:
3.0
See Also:
Serialized Form

Constructor Summary
ProgramChromosome()
          Default constructor.
ProgramChromosome(GPConfiguration a_conf)
           
ProgramChromosome(GPConfiguration a_conf, CommandGene[] a_initialGenes)
           
ProgramChromosome(GPConfiguration a_conf, int a_size)
           
ProgramChromosome(GPConfiguration a_conf, int a_size, CommandGene[] a_functionSet, java.lang.Class[] a_argTypes, IGPProgram a_ind)
           
ProgramChromosome(GPConfiguration a_conf, int a_size, IGPProgram a_ind)
           
 
Method Summary
 void cleanup()
          Clean up the chromosome.
 java.lang.Object clone()
           
 int compareTo(java.lang.Object a_other)
          Compares the given chromosome to this chromosome.
 boolean contains(CommandGene[] a_functionSet, java.lang.Class a_function)
          Checks if a function or terminal of the class of the given instance exists within a_functionSet
 boolean contains(CommandGene[] a_functionSet, CommandGene a_function)
          Checks if a function or terminal of the class of the given instance exists within a_functionSet
 int contains(CommandGene[] a_functionSet, CommandGene a_function, int a_limit)
          Checks if a function or terminal of the class of the given instance exists within a_functionSet at least a_limit times
protected  CommandGene createGene(java.lang.String a_geneClassName, java.lang.String a_persistentRepresentation)
          Creates a new instance of gene.
protected  java.lang.String decode(java.lang.String a_string)
           
protected  java.lang.String encode(java.lang.String a_string)
           
 boolean equals(java.lang.Object a_other)
          Compares this chromosome against the specified object.
 boolean execute_boolean(int n, int child, java.lang.Object[] args)
          Executes this node as a boolean.
 boolean execute_boolean(java.lang.Object[] args)
          Executes this node as a boolean.
 double execute_double(int n, int child, java.lang.Object[] args)
           
 double execute_double(java.lang.Object[] args)
          Executes this node as a double.
 float execute_float(int n, int child, java.lang.Object[] args)
           
 float execute_float(java.lang.Object[] args)
          Executes this node as a float.
 int execute_int(int n, int child, java.lang.Object[] args)
           
 int execute_int(java.lang.Object[] args)
          Executes this node as an integer.
 long execute_long(int n, int child, java.lang.Object[] args)
           
 long execute_long(java.lang.Object[] args)
          Executes this node as a long.
 java.lang.Object execute_object(int n, int child, java.lang.Object[] args)
           
 java.lang.Object execute_object(java.lang.Object[] args)
          Executes this node as an object.
 void execute_void(int n, int child, java.lang.Object[] args)
           
 void execute_void(java.lang.Object[] args)
          Executes this node, returning nothing.
 java.lang.Object execute(int n, int child, java.lang.Object[] args)
           
 java.lang.Object execute(java.lang.Object[] args)
          Executes this node without knowing its return type.
 java.lang.Object getApplicationData()
          Retrieves the application-specific data that is attached to this Chromosome.
 java.lang.Class[] getArgTypes()
           
 int getArity()
           
 java.lang.String getBusinessKey()
           
 int getChild(CommandGene a_node, int a_child)
           
 int getChild(int a_index, int a_child)
          Gets the a_child'th child of the a_index'th node in this chromosome.
 int getDepth(int a_index)
          Gets the depth of the branch starting at the a_index'th node.
 CommandGene[] getFunctions()
           
 CommandGene[] getFunctionSet()
           
 CommandGene getGene(int a_locus)
          Returns the Gene at the given index (locus) within the Chromosome.
 CommandGene getNode(java.lang.Class a_type, boolean a_exactMatch)
          Checks whether a node with a given type is contained in the program.
 CommandGene getNode(java.lang.Class a_type, boolean a_exactMatch, int a_startIndex)
           
 int getParentNode(int a_child)
          Gets the node which is the parent of the given node in this chromosome.
 java.lang.String getPersistentRepresentation()
           
 int getSize(int a_index)
          Gets the number of nodes in the branch starting at the a_index'th node.
 void growOrFull(int a_num, int a_depth, java.lang.Class a_type, java.lang.Class[] a_argTypes, CommandGene[] a_functionSet, boolean a_grow, int a_tries)
          Initialize this chromosome using the grow or the full method.
protected  CommandGene[] growOrFullNode(int a_num, int a_depth, java.lang.Class a_returnType, int a_subReturnType, CommandGene[] a_functionSet, CommandGene a_rootNode, int a_recurseLevel, boolean a_grow, int a_childNum, boolean a_validateNode)
          Create a tree of nodes using the grow or the full method.
 boolean isCompareApplicationData()
           
 boolean isPossible(java.lang.Class a_returnType, int a_subReturnType, CommandGene[] a_nodeSet, boolean a_function, boolean a_growing)
          Determines whether there exists a function or terminal in the given node set with the given return and sub return type.
 void redepth()
          Recalculate the depth of each node.
protected  int redepth(int a_index)
          Calculate the depth of the next node and the indices of the children of the current node.
protected  CommandGene selectNode(int a_chromIndex, java.lang.Class a_returnType, int a_subReturnType, CommandGene[] a_functionSet, boolean a_function, boolean a_growing)
          Randomly chooses a valid node from the functions set.
 void setArgTypes(java.lang.Class[] a_argTypes)
           
 void setCompareApplicationData(boolean a_doCompare)
          Should we also consider the application data when comparing?
 void setFunctions(CommandGene[] a_functions)
           
 void setFunctionSet(CommandGene[] a_functionSet)
           
 void setGene(int index, CommandGene a_gene)
           
 void setValueFromPersistentRepresentation(java.lang.String a_representation)
           
 int size()
           
protected static java.util.List split(java.lang.String a_string)
          Splits a_string into individual gene representations.
 java.lang.String toString(int a_startNode)
          Output program in left-hand notion (e.g.: "+ X Y" for "X + Y").
 java.lang.String toStringDebug()
           
 java.lang.String toStringNorm(int a_startNode)
          Output program in "natural" notion (e.g.: "X + Y" for "X + Y").
 
Methods inherited from class org.jgap.gp.BaseGPChromosome
getAssignableFromClass, getCommandOfClass, getFunction, getFunction, getGPConfiguration, getIndividual, getNode, getTerminal, getTerminal, getVariableWithReturnType, numFunctions, numFunctions, numTerminals, numTerminals, setIndividual
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProgramChromosome

public ProgramChromosome(GPConfiguration a_conf,
                         int a_size)
                  throws InvalidConfigurationException
Throws:
InvalidConfigurationException

ProgramChromosome

public ProgramChromosome(GPConfiguration a_conf,
                         int a_size,
                         IGPProgram a_ind)
                  throws InvalidConfigurationException
Throws:
InvalidConfigurationException

ProgramChromosome

public ProgramChromosome(GPConfiguration a_conf,
                         int a_size,
                         CommandGene[] a_functionSet,
                         java.lang.Class[] a_argTypes,
                         IGPProgram a_ind)
                  throws InvalidConfigurationException
Throws:
InvalidConfigurationException

ProgramChromosome

public ProgramChromosome(GPConfiguration a_conf,
                         CommandGene[] a_initialGenes)
                  throws InvalidConfigurationException
Throws:
InvalidConfigurationException

ProgramChromosome

public ProgramChromosome(GPConfiguration a_conf)
                  throws InvalidConfigurationException
Throws:
InvalidConfigurationException

ProgramChromosome

public ProgramChromosome()
                  throws InvalidConfigurationException
Default constructor. Only use for dynamic instantiation.

Throws:
InvalidConfigurationException
Since:
3.0
Method Detail

setArgTypes

public void setArgTypes(java.lang.Class[] a_argTypes)

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

cleanup

public void cleanup()
Clean up the chromosome.

Specified by:
cleanup in interface IGPChromosome
Since:
3.0

growOrFull

public void growOrFull(int a_num,
                       int a_depth,
                       java.lang.Class a_type,
                       java.lang.Class[] a_argTypes,
                       CommandGene[] a_functionSet,
                       boolean a_grow,
                       int a_tries)
Initialize this chromosome using the grow or the full method.

Parameters:
a_num - the chromosome's index in the individual of this chromosome
a_depth - the maximum depth of the chromosome to create
a_type - the type of the chromosome to create
a_argTypes - the array of argument types for this chromosome
a_functionSet - the set of nodes valid to pick from
a_grow - true: use grow method; false: use full method
a_tries - maximum number of tries for creating a valid program
Since:
3.0

toString

public java.lang.String toString(int a_startNode)
Output program in left-hand notion (e.g.: "+ X Y" for "X + Y").

Parameters:
a_startNode - node to start with
Returns:
output in left-hand notion
Since:
3.0

toStringNorm

public java.lang.String toStringNorm(int a_startNode)
Output program in "natural" notion (e.g.: "X + Y" for "X + Y").

Specified by:
toStringNorm in interface IGPChromosome
Parameters:
a_startNode - the node to start with, e.g. 0 for a complete dump of the program
Returns:
output in normalized notion
Since:
3.0

getBusinessKey

public java.lang.String getBusinessKey()
Specified by:
getBusinessKey in interface IBusinessKey
Returns:
business key of the chromosome
Since:
3.4

toStringDebug

public java.lang.String toStringDebug()
Returns:
debug representation of progrm chromosome, containing class names of all children
Since:
3.4

isPossible

public boolean isPossible(java.lang.Class a_returnType,
                          int a_subReturnType,
                          CommandGene[] a_nodeSet,
                          boolean a_function,
                          boolean a_growing)
Determines whether there exists a function or terminal in the given node set with the given return and sub return type.

Parameters:
a_returnType - the return type to look for
a_subReturnType - the sub return type to look for
a_nodeSet - the array of nodes to look through
a_function - true to look for a function, false to look for a terminal
a_growing - true: grow mode, false: full mode
Returns:
true if such a node exists, false otherwise
Since:
3.0

selectNode

protected CommandGene selectNode(int a_chromIndex,
                                 java.lang.Class a_returnType,
                                 int a_subReturnType,
                                 CommandGene[] a_functionSet,
                                 boolean a_function,
                                 boolean a_growing)
Randomly chooses a valid node from the functions set.

Parameters:
a_chromIndex - index of the chromosome in the individual (0..n-1)
a_returnType - the return type of node to choose
a_subReturnType - the sub return type to look for
a_functionSet - the functions to use
a_function - true to choose a function, false to choose a terminal
a_growing - true to ignore the function parameter, false otherwise
Returns:
the node chosen
Since:
3.0

growOrFullNode

protected CommandGene[] growOrFullNode(int a_num,
                                       int a_depth,
                                       java.lang.Class a_returnType,
                                       int a_subReturnType,
                                       CommandGene[] a_functionSet,
                                       CommandGene a_rootNode,
                                       int a_recurseLevel,
                                       boolean a_grow,
                                       int a_childNum,
                                       boolean a_validateNode)
Create a tree of nodes using the grow or the full method.

Parameters:
a_num - the chromosome's index in the individual of this chromosome
a_depth - the maximum depth of the tree to create
a_returnType - the return type the lastly evaluated node must have
a_subReturnType - the sub return type to look for
a_functionSet - the set of function valid to pick from
a_rootNode - null, or parent node of the node to develop
a_recurseLevel - 0 for first call
a_grow - true: use grow method; false: use full method
a_childNum - index of the child in the parent node to which it belongs (-1 if node is root node)
a_validateNode - true: check if node selected is valid (when called recursively a_validateNode is set to true)
Returns:
possible modified set of functions (e.g. to avoid having a unique command more than once)
Since:
3.0

redepth

public void redepth()
Recalculate the depth of each node.

Specified by:
redepth in interface IGPChromosome
Since:
3.0

redepth

protected int redepth(int a_index)
Calculate the depth of the next node and the indices of the children of the current node. The depth of the next node is just one plus the depth of the current node. The index of the first child is always the next node. The index of the second child is found by recursively calling this method on the tree starting with the first child.

Parameters:
a_index - the index of the reference depth
Returns:
the index of the next node of the same depth as the current node (i.e. the next sibling node)
Since:
3.0

getChild

public int getChild(int a_index,
                    int a_child)
Gets the a_child'th child of the a_index'th node in this chromosome. This is the same as the a_child'th node whose depth is one more than the depth of the a_index'th node.

Specified by:
getChild in interface IGPChromosome
Parameters:
a_index - the node number of the parent
a_child - the child number (starting from 0) of the parent
Returns:
the node number of the child, or -1 if not found
Since:
3.01

getChild

public int getChild(CommandGene a_node,
                    int a_child)

getFunctionSet

public CommandGene[] getFunctionSet()
Specified by:
getFunctionSet in interface IGPChromosome
Returns:
set of CommandGene instances allowed

setFunctionSet

public void setFunctionSet(CommandGene[] a_functionSet)

getFunctions

public CommandGene[] getFunctions()
Specified by:
getFunctions in interface IGPChromosome

setFunctions

public void setFunctions(CommandGene[] a_functions)
                  throws InvalidConfigurationException
Throws:
InvalidConfigurationException

getSize

public int getSize(int a_index)
Gets the number of nodes in the branch starting at the a_index'th node.

Parameters:
a_index - the index of the node at which to start counting
Returns:
the number of nodes in the branch starting at the a_index'th node
Since:
3.0

getDepth

public int getDepth(int a_index)
Gets the depth of the branch starting at the a_index'th node.

Parameters:
a_index - the index of the node at which to check the depth
Returns:
the depth of the branch starting at the a_index'th node
Since:
3.0

getParentNode

public int getParentNode(int a_child)
Gets the node which is the parent of the given node in this chromosome. If the child is at depth d then the parent is the first function at depth d-1 when iterating backwards through the function list starting from the child.

Parameters:
a_child - the child node
Returns:
the parent node, or null if the child is the root node
Since:
3.0

getNode

public CommandGene getNode(java.lang.Class a_type,
                           boolean a_exactMatch)
Checks whether a node with a given type is contained in the program.

Parameters:
a_type - the type to look for
a_exactMatch - true: look for exactly the given type: false: also look for sub types
Returns:
true specific node found
Since:
3.2.1

getNode

public CommandGene getNode(java.lang.Class a_type,
                           boolean a_exactMatch,
                           int a_startIndex)

execute_boolean

public boolean execute_boolean(java.lang.Object[] args)
Executes this node as a boolean.

Parameters:
args - the arguments for execution
Returns:
the boolean return value of this node
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not boolean
Since:
3.0

execute_boolean

public boolean execute_boolean(int n,
                               int child,
                               java.lang.Object[] args)
Executes this node as a boolean.

Parameters:
n - the index of the parent node
child - the child number of the node to execute
args - the arguments for execution
Returns:
the boolean return value of this node
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not boolean
Since:
3.0

execute_void

public void execute_void(java.lang.Object[] args)
Executes this node, returning nothing.

Parameters:
args - the arguments for execution
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not void
Since:
3.0

execute_void

public void execute_void(int n,
                         int child,
                         java.lang.Object[] args)

execute_int

public int execute_int(java.lang.Object[] args)
Executes this node as an integer.

Parameters:
args - the arguments for execution
Returns:
the integer return value of this node
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not integer
Since:
3.0

execute_int

public int execute_int(int n,
                       int child,
                       java.lang.Object[] args)

execute_long

public long execute_long(java.lang.Object[] args)
Executes this node as a long.

Parameters:
args - the arguments for execution
Returns:
the long return value of this node
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not long
Since:
3.0

execute_long

public long execute_long(int n,
                         int child,
                         java.lang.Object[] args)

execute_float

public float execute_float(java.lang.Object[] args)
Executes this node as a float.

Parameters:
args - the arguments for execution
Returns:
the float return value of this node
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not float
Since:
3.0

execute_float

public float execute_float(int n,
                           int child,
                           java.lang.Object[] args)

execute_double

public double execute_double(java.lang.Object[] args)
Executes this node as a double.

Parameters:
args - the arguments for execution
Returns:
the double return value of this node
Throws:
java.lang.UnsupportedOperationException - if this node's type is not double
Since:
3.0

execute_double

public double execute_double(int n,
                             int child,
                             java.lang.Object[] args)

execute_object

public java.lang.Object execute_object(java.lang.Object[] args)
Executes this node as an object.

Parameters:
args - the arguments for execution
Returns:
the object return value of this node
Throws:
java.lang.UnsupportedOperationException - if the type of this node is not of type Object
Since:
3.0

execute_object

public java.lang.Object execute_object(int n,
                                       int child,
                                       java.lang.Object[] args)

execute

public java.lang.Object execute(java.lang.Object[] args)
Executes this node without knowing its return type.

Parameters:
args - the arguments for execution
Returns:
the Object which wraps the return value of this node, or null if the return type is null or unknown
Since:
3.0

execute

public java.lang.Object execute(int n,
                                int child,
                                java.lang.Object[] args)

setGene

public void setGene(int index,
                    CommandGene a_gene)

getArgTypes

public java.lang.Class[] getArgTypes()

getArity

public int getArity()

size

public int size()
Returns:
number of functions and terminals present
Since:
3.0

compareTo

public int compareTo(java.lang.Object a_other)
Compares the given chromosome to this chromosome. This chromosome is considered to be "less than" the given chromosome if it has a fewer number of genes or if any of its gene values (alleles) are less than their corresponding gene values in the other chromosome.

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

equals

public boolean equals(java.lang.Object a_other)
Compares this chromosome 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

setCompareApplicationData

public void setCompareApplicationData(boolean a_doCompare)
Should we also consider the application data when comparing? Default is "false" as "true" means a Chromosome is losing its identity when application data is set differently!

Parameters:
a_doCompare - true: consider application data in method compareTo
Since:
3.0

isCompareApplicationData

public boolean isCompareApplicationData()

getApplicationData

public java.lang.Object getApplicationData()
Retrieves the application-specific data that is attached to this Chromosome. Attaching application-specific data may be useful for some applications when it comes time to evaluate this Chromosome in the fitness function. JGAP ignores this data functionally.

Returns:
the application-specific data previously attached to this Chromosome, or null if there is no data attached
Since:
3.0

getGene

public CommandGene getGene(int a_locus)
Returns the Gene at the given index (locus) within the Chromosome. The first gene is at index zero and the last gene is at the index equal to the size of this Chromosome - 1.

Parameters:
a_locus - index of the gene value to be returned
Returns:
Gene at the given index
Since:
3.0

encode

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

decode

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

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
Specified by:
getPersistentRepresentation in interface IGPChromosome
Returns:
the persistent representation of the chromosome, including all genes
Since:
3.3

setValueFromPersistentRepresentation

public void setValueFromPersistentRepresentation(java.lang.String a_representation)
                                          throws UnsupportedRepresentationException
Parameters:
a_representation - String
Throws:
UnsupportedRepresentationException
Since:
3.3

createGene

protected CommandGene createGene(java.lang.String a_geneClassName,
                                 java.lang.String a_persistentRepresentation)
                          throws java.lang.Exception
Creates a new instance of gene.

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

split

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

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

contains

public boolean contains(CommandGene[] a_functionSet,
                        CommandGene a_function)
Checks if a function or terminal of the class of the given instance exists within a_functionSet

Parameters:
a_functionSet - the set of functions to check
a_function - the commandgene to check for via its class
Returns:
true if a_function's class exists within a_functionSet
Since:
3.6

contains

public boolean contains(CommandGene[] a_functionSet,
                        java.lang.Class a_function)
Checks if a function or terminal of the class of the given instance exists within a_functionSet

Parameters:
a_functionSet - the set of functions to check
a_function - the commandgene to check for via its class
Returns:
true if a_function's class exists within a_functionSet
Since:
3.6

contains

public int contains(CommandGene[] a_functionSet,
                    CommandGene a_function,
                    int a_limit)
Checks if a function or terminal of the class of the given instance exists within a_functionSet at least a_limit times

Parameters:
a_functionSet - the set of functions to check
a_function - the commandgene to check for via its class
a_limit - if a_limit occurrences of a_function were detected, exit
Returns:
true if a_function's class exists at least a_limit times within a_functionSet
Since:
3.6

JGAP