JGAP

org.jgap.gp
Class CommandGene

java.lang.Object
  extended by org.jgap.gp.CommandGene
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable
Direct Known Subclasses:
AddAndStoreTerminal, ADF, AntCommand, AntCommand, Argument, CharacterProvider, CommandDynamicArity, CountMatrix, CountStones, EvaluateBoard, ExchangeMemory, ForLoop, ForXLoop, If, IfColor, IfElse, IfIsFree, IfIsOccupied, IfLessThanOrEqual, IfLessThanZero, IsOwnColor, Loop, LoopUntil, MathCommand, Push, PutStone, PutStone1, ReadBoard, ReadFromMatrix, ReadTerminal, ReadTerminalIndexed, ReplaceInMatrix, ResetMatrix, StoreTerminal, StoreTerminalIndexed, SubProgram, Terminal, TransferBoardToMemory, TransferMemory, Tupel, Variable, WriteToMatrix

public abstract class CommandGene
extends java.lang.Object
implements java.lang.Comparable, java.io.Serializable

Abstract 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
static class CommandGene.COMMAND_TYPE
           
 
Field Summary
static java.lang.Class BooleanClass
           
static java.lang.Class CharacterClass
           
static double DELTA
          Delta, useful for comparing doubles and floats.
static java.lang.Class DoubleClass
           
static java.lang.Class FloatClass
           
static java.lang.Class IntegerClass
           
static java.lang.Class LongClass
           
 int nodeIndex
           
static java.lang.Class VoidClass
           
 
Constructor Summary
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
protected  void check(ProgramChromosome a_program)
           
protected  void check(ProgramChromosome a_program, int a_index)
           
 void cleanup()
           
 int compareTo(java.lang.Object a_other)
          The compareTo-method.
 void dynamizeArity()
          Adaptation of the arity so that it represents a value within the interval [m_arityMin, m_arityMax].
 void ensureUniqueness(ProgramChromosome a_program)
          Ensures that the calling command is unique within the program.
 void ensureUniqueness(ProgramChromosome a_program, int a_maxCount)
          Ensures that the calling command is unique within the program.
 boolean ensureUniqueness2(ProgramChromosome a_program, int a_maxCount)
          Ensures that the calling command is unique within the program.
 boolean equals(java.lang.Object a_other)
          The equals-method.
 boolean execute_boolean(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node as a boolean.
 double execute_double(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node as a double.
 float execute_float(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node as a float.
 int execute_int(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node as an integer.
 long execute_long(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node as a long.
 java.lang.Object execute_object(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node as an object.
 void execute_void(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node, returning nothing.
 java.lang.Object execute(ProgramChromosome c, int n, java.lang.Object[] args)
          Executes this node without knowing its return type.
 java.lang.Object getAllele()
           
 java.lang.Object getApplicationData()
          Retrieves the application-specific data that is attached to this Gene.
 int getArity(IGPProgram a_indvividual)
          Arity of the command.
protected  int getArityMax()
           
protected  int getArityMin()
           
 java.lang.Class getChildType(IGPProgram a_ind, int a_chromNum)
          Gets the type of node allowed from the given child number.
protected  CommandGene.COMMAND_TYPE getCommandType()
          The type of the command this gene represents.
 double getEnergy()
           
 GPConfiguration getGPConfiguration()
           
protected  java.lang.Object getInternalValue()
           
 java.lang.String getName()
           
 java.lang.String getPersistentRepresentation()
           
protected  java.lang.String getPersistentRepresentationExt()
          Override in your sub classes of CommandGene if you have to add additional information to be persisted.
 java.lang.Class getReturnType()
           
 int getSubChildType(int a_childNum)
           
 int[] getSubChildTypes()
           
 int getSubReturnType()
           
 int hashCode()
          Retrieves the hash code value for a CommandGene.
protected  void init()
          Initializations, called from each Constructor.
 boolean isAffectGlobalState()
           
 boolean isCompareApplicationData()
           
 boolean isFloatType()
           
 boolean isIntegerType()
           
 boolean isValid(ProgramChromosome a_program)
          Subclasses capable of validating programs should overwrite this method.
 boolean isValid(ProgramChromosome a_program, int a_index)
           
 void setAllele(java.lang.Object a_newValue)
           
 void setApplicationData(java.lang.Object a_newData)
          This sets the application-specific data that is attached to this Gene.
protected  void setArity(int a_arity)
          Should only be used by class CommandDynamicArity or a similar class.
protected  void setArityMax(int a_arityMax)
          Should only be used by class CommandDynamicArity or a similar class.
protected  void setArityMin(int a_arityMin)
          Should only be used by class CommandDynamicArity or a similar class.
 void setCompareApplicationData(boolean a_doCompare)
          Should we also consider the application data when comparing?
 void setEnergy(double a_energy)
          Sets the energy of the gene.
 void setNoValidation(boolean a_noValidation)
           
 void setReturnType(java.lang.Class a_type)
          Sets the return type of this node.
 void setToRandomValue(RandomGenerator a_numberGenerator)
           
 void setValueFromPersistentRepresentation(java.lang.String a_representation)
           
protected  void setValueFromString(int a_index, java.lang.String a_value)
          Override in your sub classes of CommandGene if you have to add additional information to be persisted.
 int size()
           
abstract  java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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.Exception
Default 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 use
a_arity - the number of children of the node
a_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 use
a_arity - the number of children of the node
a_returnType - type of the return value of the node
a_subReturnType - sub type of the return type, optional usage
a_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 use
a_arity - the number of children of the node
a_returnType - type of the return value of the node
a_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 use
a_arity - the number of children of the node
a_returnType - type of the return value of the node
a_subReturnType - sub type of the return type, optional usage
a_childSubType - sub type of a child, optional usage
Throws:
InvalidConfigurationException
Since:
3.2
Method Detail

init

protected void init()
Initializations, called from each Constructor.


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

setArity

protected void setArity(int a_arity)
Should only be used by class CommandDynamicArity or a similar class.

Parameters:
a_arity - the arity to set, overrides the current arity.
Since:
3.4

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

setArityMin

protected void setArityMin(int a_arityMin)
Should only be used by class CommandDynamicArity or a similar class.

Parameters:
a_arityMin - the minimal arity possible
Since:
3.4

setArityMax

protected void setArityMax(int a_arityMax)
Should only be used by class CommandDynamicArity or a similar class.

Parameters:
a_arityMax - the maximal arity possible
Since:
3.4

getArityMin

protected int getArityMin()

getArityMax

protected int getArityMax()

compareTo

public int compareTo(java.lang.Object a_other)
The compareTo-method. Considers application data when the configuration asks for this.

Specified by:
compareTo in interface java.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:
equals in class java.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:
toString in class java.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 executing
n - the index of the Function in the Chromosome's Function array which is executing
args - 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 here
n - ignored here
args - 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 here
n - ignored here
args - 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 here
n - ignored here
args - 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 here
n - ignored here
args - 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 here
n - ignored here
args - 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 here
n - ignored here
args - 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 here
n - ignored here
args - 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 to
a_chromNum - the chromosome number
Returns:
the type of node allowed for that child, or null of no child exists
Since:
3.0

getInternalValue

protected java.lang.Object getInternalValue()

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:
hashCode in class java.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)

check

protected void check(ProgramChromosome a_program)

check

protected void check(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 validate
a_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 validate
a_maxCount - maximum number of allowed occurences
Returns:
false: uniqueness constraint violated
Since:
3.4

getCommandType

protected CommandGene.COMMAND_TYPE getCommandType()
The type of the command this gene represents. Overwrite in sub classes. This is optional and allows fine-tuning of GP program creating. For example, you could have a function as a part of a desired GP program that only does some analysis stuff and not any execution. E.g. take a game like Noughts and Crosses (Tic Tac Toe). There, a first part could be analysing the current state of the board and a second part could cope with exploiting the analysis results to execute a command (like put stone onto board at specific position).

Returns:
type of the command this gene represents
Since:
3.4.3

getPersistentRepresentation

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

getPersistentRepresentationExt

protected java.lang.String getPersistentRepresentationExt()
Override in your sub classes of CommandGene if you have to add additional information to be persisted.

Returns:
additional infos
Since:
3.3

setValueFromPersistentRepresentation

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

setValueFromString

protected void setValueFromString(int a_index,
                                  java.lang.String a_value)
Override in your sub classes of CommandGene if you have to add additional information to be persisted.

Parameters:
a_index - index of the parameter in the range 0..n-1 (n=number of parameters)
a_value - string value of the parameter
Since:
3.3

JGAP