JGAP

org.jgap.gp.function
Class SubProgram

java.lang.Object
  extended by org.jgap.gp.CommandGene
      extended by org.jgap.gp.function.SubProgram
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, IMutateable, ICloneable

public class SubProgram
extends CommandGene
implements ICloneable, IMutateable

A connector for independent subprograms (subtrees). Each subtree except the last one must have a memory- or stack-modifying command (such as push or store), otherwise there is no connection between the subtrees (which would be useless bloating).

Since:
3.0
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jgap.gp.CommandGene
CommandGene.COMMAND_TYPE
 
Field Summary
 
Fields inherited from class org.jgap.gp.CommandGene
BooleanClass, CharacterClass, DELTA, DoubleClass, FloatClass, IntegerClass, LongClass, nodeIndex, VoidClass
 
Constructor Summary
SubProgram(GPConfiguration a_conf, java.lang.Class[] a_types)
           
SubProgram(GPConfiguration a_conf, java.lang.Class[] a_types, boolean a_mutateable)
           
SubProgram(GPConfiguration a_conf, java.lang.Class[] a_types, int a_subReturnType, int[] a_subChildTypes)
           
SubProgram(GPConfiguration a_conf, java.lang.Class[] a_types, int a_subReturnType, int[] a_subChildTypes, boolean a_mutateable)
           
SubProgram(GPConfiguration a_conf, int a_arity, java.lang.Class a_types)
          Collage constructor: Create a sub program that has a_arity elements of the same type a_types.
SubProgram(GPConfiguration a_conf, int a_arity, java.lang.Class a_types, boolean a_mutateable)
          Collage constructor: Create a sub program that has a_arity elements of the same type a_types.
SubProgram(GPConfiguration a_conf, int a_arity, java.lang.Class a_types, int a_minArity, int a_maxArity, boolean a_mutateable)
           
 
Method Summary
 CommandGene applyMutation()
           
 CommandGene applyMutation(int index, double a_percentage)
          Mutates a CommandGene.
 java.lang.Object clone()
           
 int compareTo(java.lang.Object a_other)
          The compareTo-method.
 boolean equals(java.lang.Object a_other)
          The equals-method.
 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.Class getChildType(IGPProgram a_ind, int a_chromNum)
          Gets the type of node allowed from the given child number.
 java.lang.String getName()
           
 boolean isValid(ProgramChromosome a_program)
          Subclasses capable of validating programs should overwrite this method.
 java.lang.String toString()
           
 
Methods inherited from class org.jgap.gp.CommandGene
check, check, cleanup, dynamizeArity, ensureUniqueness, ensureUniqueness, ensureUniqueness2, execute_boolean, execute, getAllele, getApplicationData, getArity, getArityMax, getArityMin, getCommandType, getEnergy, getGPConfiguration, getInternalValue, getPersistentRepresentation, getPersistentRepresentationExt, getReturnType, getSubChildType, getSubChildTypes, getSubReturnType, hashCode, init, isAffectGlobalState, isCompareApplicationData, isFloatType, isIntegerType, isValid, setAllele, setApplicationData, setArity, setArityMax, setArityMin, setCompareApplicationData, setEnergy, setNoValidation, setReturnType, setToRandomValue, setValueFromPersistentRepresentation, setValueFromString, size
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SubProgram

public SubProgram(GPConfiguration a_conf,
                  java.lang.Class[] a_types)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

SubProgram

public SubProgram(GPConfiguration a_conf,
                  int a_arity,
                  java.lang.Class a_types)
           throws InvalidConfigurationException
Collage constructor: Create a sub program that has a_arity elements of the same type a_types.

Parameters:
a_conf - the configuration to use
a_arity - number of children in the collage
a_types - uniform type of all children
Throws:
InvalidConfigurationException
Since:
3.4

SubProgram

public SubProgram(GPConfiguration a_conf,
                  int a_arity,
                  java.lang.Class a_types,
                  boolean a_mutateable)
           throws InvalidConfigurationException
Collage constructor: Create a sub program that has a_arity elements of the same type a_types.

Parameters:
a_conf - the configuration to use
a_arity - number of children in the collage
a_types - uniform type of all children
a_mutateable - true: allow mutation of the sub program, i.e., the number of children (=arity) may be varied automatically during evolution
Throws:
InvalidConfigurationException
Since:
3.4

SubProgram

public SubProgram(GPConfiguration a_conf,
                  int a_arity,
                  java.lang.Class a_types,
                  int a_minArity,
                  int a_maxArity,
                  boolean a_mutateable)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

SubProgram

public SubProgram(GPConfiguration a_conf,
                  java.lang.Class[] a_types,
                  boolean a_mutateable)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

SubProgram

public SubProgram(GPConfiguration a_conf,
                  java.lang.Class[] a_types,
                  int a_subReturnType,
                  int[] a_subChildTypes)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException

SubProgram

public SubProgram(GPConfiguration a_conf,
                  java.lang.Class[] a_types,
                  int a_subReturnType,
                  int[] a_subChildTypes,
                  boolean a_mutateable)
           throws InvalidConfigurationException
Throws:
InvalidConfigurationException
Method Detail

toString

public java.lang.String toString()
Specified by:
toString in class CommandGene
Returns:
the string representation of the command. Especially usefull to output a resulting formula in human-readable form.

getName

public java.lang.String getName()
Overrides:
getName in class CommandGene
Returns:
textual name of this command
Since:
3.2

execute_int

public int execute_int(ProgramChromosome c,
                       int n,
                       java.lang.Object[] args)
Description copied from class: CommandGene
Executes this node as an integer. Override to implement.

Overrides:
execute_int in class CommandGene
Parameters:
c - ignored here
n - ignored here
args - ignored here
Returns:
nothing but exception

execute_void

public void execute_void(ProgramChromosome c,
                         int n,
                         java.lang.Object[] args)
Description copied from class: CommandGene
Executes this node, returning nothing. Override to implement.

Overrides:
execute_void in class CommandGene
Parameters:
c - ignored here
n - ignored here
args - ignored here

execute_long

public long execute_long(ProgramChromosome c,
                         int n,
                         java.lang.Object[] args)
Description copied from class: CommandGene
Executes this node as a long. Override to implement.

Overrides:
execute_long in class CommandGene
Parameters:
c - ignored here
n - ignored here
args - ignored here
Returns:
nothing but exception

execute_float

public float execute_float(ProgramChromosome c,
                           int n,
                           java.lang.Object[] args)
Description copied from class: CommandGene
Executes this node as a float. Override to implement.

Overrides:
execute_float in class CommandGene
Parameters:
c - ignored here
n - ignored here
args - ignored here
Returns:
nothing but exception

execute_double

public double execute_double(ProgramChromosome c,
                             int n,
                             java.lang.Object[] args)
Description copied from class: CommandGene
Executes this node as a double. Override to implement.

Overrides:
execute_double in class CommandGene
Parameters:
c - ignored here
n - ignored here
args - ignored here
Returns:
nothing but exception

execute_object

public java.lang.Object execute_object(ProgramChromosome c,
                                       int n,
                                       java.lang.Object[] args)
Description copied from class: CommandGene
Executes this node as an object. Override to implement.

Overrides:
execute_object in class CommandGene
Parameters:
c - ignored here
n - ignored here
args - ignored here
Returns:
nothing but exception

isValid

public boolean isValid(ProgramChromosome a_program)
Description copied from class: CommandGene
Subclasses capable of validating programs should overwrite this method. See class Push as a sample.

Overrides:
isValid in class CommandGene
Parameters:
a_program - the ProgramChromosome to validate
Returns:
true: a_program is (superficially) valid with the current Command

getChildType

public java.lang.Class getChildType(IGPProgram a_ind,
                                    int a_chromNum)
Description copied from class: CommandGene
Gets the type of node allowed from the given child number. Should be overridden in subclasses.

Overrides:
getChildType in class CommandGene
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

compareTo

public int compareTo(java.lang.Object a_other)
The compareTo-method.

Specified by:
compareTo in interface java.lang.Comparable
Overrides:
compareTo in class CommandGene
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.

Overrides:
equals in class CommandGene
Parameters:
a_other - the other object to compare
Returns:
true if the objects are seen as equal
Since:
3.0

clone

public java.lang.Object clone()
Specified by:
clone in interface ICloneable
Overrides:
clone in class java.lang.Object
Returns:
deep clone of this instance
Since:
3.2

applyMutation

public CommandGene applyMutation(int index,
                                 double a_percentage)
                          throws InvalidConfigurationException
Description copied from interface: IMutateable
Mutates a CommandGene.

Specified by:
applyMutation in interface IMutateable
Parameters:
index - references the part of a multipart object, normally not relevant
a_percentage - the mutation rate (0.0 to 1.0)
Returns:
the mutant
Throws:
InvalidConfigurationException

applyMutation

public CommandGene applyMutation()
                          throws InvalidConfigurationException
Returns:
mutated command gene
Throws:
InvalidConfigurationException

JGAP