org.jgap.gp
Class CommandGene
- java.lang.Object
-
- org.jgap.gp.CommandGene
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable
- Direct Known Subclasses:
- AddAndStoreTerminal, ADF, Argument, CharacterProvider, CommandDynamicArity, CountMatrix, ExchangeMemory, ForLoop, ForLoopD, ForXLoop, If, IfElse, IfElseD, IfLessThanOrEqualD, IfLessThanOrEqualZeroD, Loop, LoopD, MathCommand, Push, ReadFromMatrix, ReadTerminal, ReadTerminalIndexed, ReplaceInMatrix, ResetMatrix, StoreTerminal, StoreTerminalIndexed, SubProgram, Terminal, TransferMemory, Tupel, Variable, WriteToMatrix
public abstract class CommandGene extends java.lang.Object implements java.lang.Comparable, java.io.SerializableAbstract base class for all GP commands. A CommandGene represents a node within a GP program. A node either is a terminal (like a constant), or a function (having input parameters). A CommandGene can hold additional CommandGene's, it acts sort of like a * Composite (also see CompositeGene for a comparable concept, although for a GA).- Since:
- 3.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classCommandGene.COMMAND_TYPE
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.ClassBooleanClassstatic java.lang.ClassCharacterClassstatic doubleDELTADelta, useful for comparing doubles and floats.static java.lang.ClassDoubleClassstatic java.lang.ClassFloatClassstatic java.lang.ClassIntegerClassstatic java.lang.ClassLongClassintnodeIndexstatic java.lang.ClassVoidClass
-
Constructor Summary
Constructors Constructor and Description CommandGene()Default constructor, only for dynamic instantiation.CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType)CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType, int a_subReturnType)Allows specifying a sub return type.CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType, int a_subReturnType, int a_childSubType)Command with one child: Allows specifying a sub return type and a sub child type.CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType, int a_subReturnType, int[] a_childSubTypes)Allows specifying a sub return type and sub child types.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description voidcleanup()intcompareTo(java.lang.Object a_other)The compareTo-method.voiddynamizeArity()Adaptation of the arity so that it represents a value within the interval [m_arityMin, m_arityMax].voidensureUniqueness(ProgramChromosome a_program)Ensures that the calling command is unique within the program.voidensureUniqueness(ProgramChromosome a_program, int a_maxCount)Ensures that the calling command is unique within the program.booleanensureUniqueness2(ProgramChromosome a_program, int a_maxCount)Ensures that the calling command is unique within the program.booleanequals(java.lang.Object a_other)The equals-method.booleanexecute_boolean(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node as a boolean.doubleexecute_double(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node as a double.floatexecute_float(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node as a float.intexecute_int(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node as an integer.longexecute_long(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node as a long.java.lang.Objectexecute_object(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node as an object.voidexecute_void(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node, returning nothing.java.lang.Objectexecute(ProgramChromosome c, int n, java.lang.Object[] args)Executes this node without knowing its return type.java.lang.ObjectgetAllele()java.lang.ObjectgetApplicationData()Retrieves the application-specific data that is attached to this Gene.intgetArity(IGPProgram a_indvividual)Arity of the command.java.lang.ClassgetChildType(IGPProgram a_ind, int a_chromNum)Gets the type of node allowed from the given child number.doublegetEnergy()GPConfigurationgetGPConfiguration()java.lang.StringgetName()java.lang.StringgetPersistentRepresentation()java.lang.ClassgetReturnType()intgetSubChildType(int a_childNum)int[]getSubChildTypes()intgetSubReturnType()inthashCode()Retrieves the hash code value for a CommandGene.booleanisAffectGlobalState()booleanisCompareApplicationData()booleanisFloatType()booleanisIntegerType()booleanisValid(ProgramChromosome a_program)Subclasses capable of validating programs should overwrite this method.booleanisValid(ProgramChromosome a_program, int a_index)voidsetAllele(java.lang.Object a_newValue)voidsetApplicationData(java.lang.Object a_newData)This sets the application-specific data that is attached to this Gene.voidsetCompareApplicationData(boolean a_doCompare)Should we also consider the application data when comparing? Default is "false" as "true" means a Gene is losing its identity when application data is set differently!voidsetEnergy(double a_energy)Sets the energy of the gene.voidsetNoValidation(boolean a_noValidation)voidsetReturnType(java.lang.Class a_type)Sets the return type of this node.voidsetToRandomValue(RandomGenerator a_numberGenerator)voidsetValueFromPersistentRepresentation(java.lang.String a_representation)intsize()abstract java.lang.StringtoString()
-
-
-
Field Detail
-
DELTA
public static final double DELTA
Delta, useful for comparing doubles and floats.- See Also:
- Constant Field Values
-
BooleanClass
public static final java.lang.Class BooleanClass
-
IntegerClass
public static final java.lang.Class IntegerClass
-
LongClass
public static final java.lang.Class LongClass
-
FloatClass
public static final java.lang.Class FloatClass
-
DoubleClass
public static final java.lang.Class DoubleClass
-
VoidClass
public static final java.lang.Class VoidClass
-
CharacterClass
public static final java.lang.Class CharacterClass
-
nodeIndex
public int nodeIndex
-
-
Constructor Detail
-
CommandGene
public CommandGene() throws java.lang.ExceptionDefault constructor, only for dynamic instantiation.- Throws:
java.lang.Exception- Since:
- 3.3.4
-
CommandGene
public CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType) throws InvalidConfigurationException
- Parameters:
a_conf- the configuration to usea_arity- the number of children of the nodea_returnType- type of the return value of the node- Throws:
InvalidConfigurationException
-
CommandGene
public CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType, int a_subReturnType, int[] a_childSubTypes) throws InvalidConfigurationException
Allows specifying a sub return type and sub child types.- Parameters:
a_conf- the configuration to usea_arity- the number of children of the nodea_returnType- type of the return value of the nodea_subReturnType- sub type of the return type, optional usagea_childSubTypes- sub types of the childs, optional usage- Throws:
InvalidConfigurationException- Since:
- 3.2
-
CommandGene
public CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType, int a_subReturnType) throws InvalidConfigurationException
Allows specifying a sub return type.- Parameters:
a_conf- the configuration to usea_arity- the number of children of the nodea_returnType- type of the return value of the nodea_subReturnType- sub type of the return type, optional usage- Throws:
InvalidConfigurationException- Since:
- 3.2
-
CommandGene
public CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType, int a_subReturnType, int a_childSubType) throws InvalidConfigurationException
Command with one child: Allows specifying a sub return type and a sub child type. Convenience version of the called constructor.- Parameters:
a_conf- the configuration to usea_arity- the number of children of the nodea_returnType- type of the return value of the nodea_subReturnType- sub type of the return type, optional usagea_childSubType- sub type of a child, optional usage- Throws:
InvalidConfigurationException- Since:
- 3.2
-
-
Method Detail
-
setAllele
public void setAllele(java.lang.Object a_newValue)
-
getAllele
public java.lang.Object getAllele()
-
setToRandomValue
public void setToRandomValue(RandomGenerator a_numberGenerator)
-
cleanup
public void cleanup()
-
size
public int size()
-
getArity
public int getArity(IGPProgram a_indvividual)
Arity of the command. Override if necessary. The arity is the number of children a node has.- Parameters:
a_indvividual- the invididual the command's arity may depend on (in most cases the arity will not depend on the individual)- Returns:
- arity of the command
- Since:
- 3.0
-
dynamizeArity
public void dynamizeArity()
Adaptation of the arity so that it represents a value within the interval [m_arityMin, m_arityMax]. Override if necessary. See CommandDynamicArity for an implementation.- Since:
- 3.4
-
compareTo
public int compareTo(java.lang.Object a_other)
The compareTo-method. Considers application data when the configuration asks for this.- Specified by:
compareToin interfacejava.lang.Comparable- Parameters:
a_other- the other object to compare- Returns:
- -1, 0, 1
- Since:
- 3.0
-
equals
public boolean equals(java.lang.Object a_other)
The equals-method. Considers application data when the configuration asks for this.- Overrides:
equalsin classjava.lang.Object- Parameters:
a_other- the other object to compare- Returns:
- true if the objects are seen as equal
- Since:
- 3.0
-
toString
public abstract java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- the string representation of the command. Especially usefull to output a resulting formula in human-readable form.
-
execute
public java.lang.Object execute(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node without knowing its return type.- Parameters:
c- the current Chromosome which is executingn- the index of the Function in the Chromosome's Function array which is executingargs- the arguments to the current Chromosome which is executing- Returns:
- the object which wraps the return value of this node, or null if the return type is null or unknown
- Throws:
java.lang.UnsupportedOperationException- if the type of this node is not boolean- Since:
- 3.0
-
getReturnType
public java.lang.Class getReturnType()
- Returns:
- the return type of this node
- Since:
- 3.0
-
setReturnType
public void setReturnType(java.lang.Class a_type)
Sets the return type of this node.- Parameters:
a_type- the type to set the return type to- Since:
- 3.0
-
execute_boolean
public boolean execute_boolean(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node as a boolean. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Returns:
- nothing but exception
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
execute_void
public void execute_void(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node, returning nothing. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
execute_int
public int execute_int(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node as an integer. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Returns:
- nothing but exception
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
execute_long
public long execute_long(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node as a long. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Returns:
- nothing but exception
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
execute_float
public float execute_float(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node as a float. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Returns:
- nothing but exception
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
execute_double
public double execute_double(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node as a double. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Returns:
- nothing but exception
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
execute_object
public java.lang.Object execute_object(ProgramChromosome c, int n, java.lang.Object[] args)
Executes this node as an object. Override to implement.- Parameters:
c- ignored heren- ignored hereargs- ignored here- Returns:
- nothing but exception
- Throws:
java.lang.UnsupportedOperationException- Since:
- 3.0
-
getName
public java.lang.String getName()
-
getChildType
public java.lang.Class getChildType(IGPProgram a_ind, int a_chromNum)
Gets the type of node allowed from the given child number. Should be overridden in subclasses.- Parameters:
a_ind- the individual the child belongs toa_chromNum- the chromosome number- Returns:
- the type of node allowed for that child, or null of no child exists
- Since:
- 3.0
-
hashCode
public int hashCode()
Retrieves the hash code value for a CommandGene. Override if another hashCode() implementation is necessary or more appropriate than this default implementation.- Overrides:
hashCodein classjava.lang.Object- Returns:
- this Gene's hash code
- Since:
- 3.0
-
isIntegerType
public boolean isIntegerType()
-
isFloatType
public boolean isFloatType()
-
isAffectGlobalState
public boolean isAffectGlobalState()
- Returns:
- true: command affects global state (i.e. stack or memory)
- Since:
- 3.0
-
isValid
public boolean isValid(ProgramChromosome a_program)
Subclasses capable of validating programs should overwrite this method. See class Push as a sample.- Parameters:
a_program- the ProgramChromosome to validate- Returns:
- true: a_program is (superficially) valid with the current Command
- Since:
- 3.0
-
isValid
public boolean isValid(ProgramChromosome a_program, int a_index)
-
setNoValidation
public void setNoValidation(boolean a_noValidation)
-
getGPConfiguration
public GPConfiguration getGPConfiguration()
- Returns:
- the configuration set
- Since:
- 3.0
-
setApplicationData
public void setApplicationData(java.lang.Object a_newData)
This sets the application-specific data that is attached to this Gene. Attaching application-specific data may be useful for some applications when it comes time to distinguish a Gene from another. JGAP ignores this data functionally.- Parameters:
a_newData- the new application-specific data to attach to this Gene- Since:
- 3.0
-
getApplicationData
public java.lang.Object getApplicationData()
Retrieves the application-specific data that is attached to this Gene. Attaching application-specific data may be useful for some applications when it comes time to distinguish a Gene from another. JGAP ignores this data functionally.- Returns:
- the application-specific data previously attached to this Gene, or null if there is no data attached
- 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 Gene is losing its identity when application data is set differently!- Parameters:
a_doCompare- true: consider application data in method compareTo and equals- Since:
- 2.4
-
isCompareApplicationData
public boolean isCompareApplicationData()
-
getEnergy
public double getEnergy()
- Returns:
- energy of the gene
- Since:
- 2.3
-
setEnergy
public void setEnergy(double a_energy)
Sets the energy of the gene.- Parameters:
a_energy- the energy to set- Since:
- 2.3
-
getSubReturnType
public int getSubReturnType()
- Returns:
- sub return type
- Since:
- 3.2
-
getSubChildType
public int getSubChildType(int a_childNum)
- Parameters:
a_childNum- the index of the child- Returns:
- the sub type of the given child
- Since:
- 3.2
-
getSubChildTypes
public int[] getSubChildTypes()
- Returns:
- sub type for all children
- Since:
- 3.2
-
ensureUniqueness
public void ensureUniqueness(ProgramChromosome a_program)
Ensures that the calling command is unique within the program. Call it on first place from the execute method.- Parameters:
a_program- the program to validate- Since:
- 3.3
-
ensureUniqueness
public void ensureUniqueness(ProgramChromosome a_program, int a_maxCount)
Ensures that the calling command is unique within the program. Throws an exception if uniqueness is violated. Call it on first place from the execute method.- Parameters:
a_program- the program to validatea_maxCount- maximum number of allowed occurences- Since:
- 3.4
-
ensureUniqueness2
public boolean ensureUniqueness2(ProgramChromosome a_program, int a_maxCount)
Ensures that the calling command is unique within the program. Returns false if uniqueness is violated. Call it on first place from the execute method.- Parameters:
a_program- the program to validatea_maxCount- maximum number of allowed occurences- Returns:
- false: uniqueness constraint violated
- Since:
- 3.4
-
getPersistentRepresentation
public java.lang.String getPersistentRepresentation()
- 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