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:
ADF, Argument, ForLoop, ForXLoop, If, IfElse, Loop, MathCommand, Push, ReadTerminal, StoreTerminal, SubProgram, Terminal, TransferMemory, Variable

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

Abstract base class for all GP commands. A CommandGene can hold additional CommandGene's, it acts sort of like a Composite (also see CompositeGene for a smiliar characteristics, although for a GA).

Since:
3.0
See Also:
Serialized Form

Field Summary
static java.lang.Class BooleanClass
           
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
           
static java.lang.Class VoidClass
           
 
Constructor Summary
CommandGene(GPConfiguration a_conf, int a_arity, java.lang.Class a_returnType)
           
 
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)
           
 boolean equals(java.lang.Object a_other)
           
 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.
 java.lang.Class getChildType(IGPProgram a_ind, int a_chromNum)
          Gets the type of node allowed form the given child number.
 double getEnergy()
           
 GPConfiguration getGPConfiguration()
           
protected  java.lang.Object getInternalValue()
           
 java.lang.String getName()
           
 java.lang.String getPersistentRepresentation()
           
 java.lang.Class getReturnType()
           
 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.
 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)
           
 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
Constructor Detail

CommandGene

public CommandGene(GPConfiguration a_conf,
                   int a_arity,
                   java.lang.Class a_returnType)
            throws InvalidConfigurationException
Throws:
InvalidConfigurationException
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()

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
                                             throws java.lang.UnsupportedOperationException
Throws:
java.lang.UnsupportedOperationException

setValueFromPersistentRepresentation

public void setValueFromPersistentRepresentation(java.lang.String a_representation)
                                          throws java.lang.UnsupportedOperationException,
                                                 UnsupportedRepresentationException
Throws:
java.lang.UnsupportedOperationException
UnsupportedRepresentationException

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.

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

compareTo

public int compareTo(java.lang.Object a_other)
Specified by:
compareTo in interface java.lang.Comparable

equals

public boolean equals(java.lang.Object a_other)
Overrides:
equals in class java.lang.Object

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 form the given child number. Must 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
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 PushCommand 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
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

JGAP