org.jgap.gp.impl
Class ProgramChromosome
- java.lang.Object
-
- org.jgap.gp.BaseGPChromosome
-
- 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
Constructors Constructor and Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidcleanup()Clean up the chromosome.java.lang.Objectclone()intcompareTo(java.lang.Object a_other)Compares the given chromosome to this chromosome.booleanequals(java.lang.Object a_other)Compares this chromosome against the specified object.booleanexecute_boolean(int n, int child, java.lang.Object[] args)Executes this node as a boolean.booleanexecute_boolean(java.lang.Object[] args)Executes this node as a boolean.doubleexecute_double(int n, int child, java.lang.Object[] args)doubleexecute_double(java.lang.Object[] args)Executes this node as a double.floatexecute_float(int n, int child, java.lang.Object[] args)floatexecute_float(java.lang.Object[] args)Executes this node as a float.intexecute_int(int n, int child, java.lang.Object[] args)intexecute_int(java.lang.Object[] args)Executes this node as an integer.longexecute_long(int n, int child, java.lang.Object[] args)longexecute_long(java.lang.Object[] args)Executes this node as a long.java.lang.Objectexecute_object(int n, int child, java.lang.Object[] args)java.lang.Objectexecute_object(java.lang.Object[] args)Executes this node as an object.voidexecute_void(int n, int child, java.lang.Object[] args)voidexecute_void(java.lang.Object[] args)Executes this node, returning nothing.java.lang.Objectexecute(int n, int child, java.lang.Object[] args)java.lang.Objectexecute(java.lang.Object[] args)Executes this node without knowing its return type.java.lang.ObjectgetApplicationData()Retrieves the application-specific data that is attached to this Chromosome.java.lang.Class[]getArgTypes()intgetArity()java.lang.StringgetBusinessKey()intgetChild(CommandGene a_node, int a_child)intgetChild(int a_index, int a_child)Gets the a_child'th child of the a_index'th node in this chromosome.intgetDepth(int a_index)Gets the depth of the branch starting at the a_index'th node.CommandGene[]getFunctions()CommandGene[]getFunctionSet()CommandGenegetGene(int a_locus)Returns the Gene at the given index (locus) within the Chromosome.CommandGenegetNode(java.lang.Class a_type, boolean a_exactMatch)Checks whether a node with a given type is contained in the program.CommandGenegetNode(java.lang.Class a_type, boolean a_exactMatch, int a_startIndex)intgetParentNode(int a_child)Gets the node which is the parent of the given node in this chromosome.java.lang.StringgetPersistentRepresentation()intgetSize(int a_index)Gets the number of nodes in the branch starting at the a_index'th node.voidgrowOrFull(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.booleanisCompareApplicationData()booleanisPossible(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.voidredepth()Recalculate the depth of each node.voidsetArgTypes(java.lang.Class[] a_argTypes)voidsetCompareApplicationData(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!voidsetFunctions(CommandGene[] a_functions)voidsetFunctionSet(CommandGene[] a_functionSet)voidsetGene(int index, CommandGene a_gene)voidsetValueFromPersistentRepresentation(java.lang.String a_representation)intsize()java.lang.StringtoString(int a_startNode)Output program in left-hand notion (e.g.: "+ X Y" for "X + Y").java.lang.StringtoStringDebug()java.lang.StringtoStringNorm(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
-
-
-
-
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 InvalidConfigurationExceptionDefault 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:
clonein classjava.lang.Object
-
cleanup
public void cleanup()
Clean up the chromosome.- Specified by:
cleanupin interfaceIGPChromosome- 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 chromosomea_depth- the maximum depth of the chromosome to createa_type- the type of the chromosome to createa_argTypes- the array of argument types for this chromosomea_functionSet- the set of nodes valid to pick froma_grow- true: use grow method; false: use full methoda_tries- maximum number of tries to create 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:
toStringNormin interfaceIGPChromosome- 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:
getBusinessKeyin interfaceIBusinessKey- 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 fora_subReturnType- the sub return type to look fora_nodeSet- the array of nodes to look througha_function- true to look for a function, false to look for a terminala_growing- true: grow mode, false: full mode- Returns:
- true if such a node exists, false otherwise
- Since:
- 3.0
-
redepth
public void redepth()
Recalculate the depth of each node.- Specified by:
redepthin interfaceIGPChromosome- 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:
getChildin interfaceIGPChromosome- Parameters:
a_index- the node number of the parenta_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:
getFunctionSetin interfaceIGPChromosome- Returns:
- set of CommandGene instances allowed
-
setFunctionSet
public void setFunctionSet(CommandGene[] a_functionSet)
-
getFunctions
public CommandGene[] getFunctions()
- Specified by:
getFunctionsin interfaceIGPChromosome
-
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 fora_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 nodechild- the child number of the node to executeargs- 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:
compareToin interfacejava.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:
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
-
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
-
getPersistentRepresentation
public java.lang.String getPersistentRepresentation()
- Specified by:
getPersistentRepresentationin interfaceIGPChromosome- 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
-
-
DMelt 3.0 © DataMelt by jWork.ORG