JGAP

org.jgap.gp.impl
Class GPProgram

java.lang.Object
  extended by org.jgap.gp.GPProgramBase
      extended by org.jgap.gp.impl.GPProgram
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, IGPProgram, IBusinessKey, ICloneable

public class GPProgram
extends GPProgramBase
implements java.io.Serializable, java.lang.Comparable, ICloneable, IBusinessKey

A GP program contains 1..n ProgramChromosome's.

Since:
3.0
See Also:
Serialized Form

Constructor Summary
GPProgram()
          Default constructor, only for dynamic instantiation.
GPProgram(GPConfiguration a_conf, java.lang.Class[] a_types, java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets, int[] a_minDepths, int[] a_maxDepths, int a_maxNodes)
          Master constructor.
GPProgram(GPConfiguration a_conf, int a_numChromosomes)
          Sort of minimalistic constructor.
GPProgram(IGPProgram a_prog)
          Constructor to initialize a GPProgram with values of another GPProgram.
 
Method Summary
 java.lang.Object clone()
           
 int compareTo(java.lang.Object a_other)
          Compares the given program to this program.
protected  java.lang.String decode(java.lang.String a_string)
           
protected  java.lang.String encode(java.lang.String a_string)
           
 boolean execute_boolean(int a_chromosomeNum, java.lang.Object[] a_args)
          Executes the given chromosome as a boolean function.
 double execute_double(int a_chromosomeNum, java.lang.Object[] a_args)
          Executes the given chromosome as a double function.
 float execute_float(int a_chromosomeNum, java.lang.Object[] a_args)
          Executes the given chromosome as a float function.
 int execute_int(int a_chromosomeNum, java.lang.Object[] a_args)
          Executes the given chromosome as an integer function.
 java.lang.Object execute_object(int a_chromosomeNum, java.lang.Object[] a_args)
          Executes the given chromosome as an object function.
 void execute_void(int a_chromosomeNum, java.lang.Object[] a_args)
          Executes the given chromosome as an object function.
 java.lang.String getBusinessKey()
           
 ProgramChromosome getChromosome(int a_index)
           
 int getCommandOfClass(int a_chromosomeNum, java.lang.Class a_class)
          Searches for a chromosome that has the given class and returns its index.
 java.lang.String getPersistentRepresentation()
           
 void growOrFull(java.lang.Class[][] a_argTypes, CommandGene[][] a_nodeSets)
          Initialize this program by using given chromosomes.
 void growOrFull(int a_depth, boolean a_grow, int a_maxNodes, boolean[] a_fullModeAllowed, int a_tries)
          Initialize the chromosomes within this GP program using the grow or the full method.
protected  GPProgramInfo putToCache(GPProgram a_program)
          Put program to cache.
 void setChromosome(int a_index, ProgramChromosome a_chrom)
          Sets the given chromosome at the given index.
 int size()
           
 java.lang.String toString(int a_startNode)
          Builds a string that represents the output of the GPProgram in left-hand-notion.
 java.lang.String toStringDebug()
          Builds a string that represents the debug output of the GPProgram.
 java.lang.String toStringNorm(int a_startNode)
          Builds a string that represents the normalized output of the GPProgram.
 
Methods inherited from class org.jgap.gp.GPProgramBase
calcFitnessValue, equals, getApplicationData, getArgType, getArgTypes, getFitnessValue, getFitnessValueDirectly, getGPConfiguration, getMaxDepths, getMaxNodes, getMinDepths, getNodeSet, getNodeSets, getType, getTypes, setApplicationData, setArgTypes, setFitnessValue, setMaxDepths, setMaxNodes, setMinDepths, setNodeSets, setTypes
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GPProgram

public GPProgram()
          throws java.lang.Exception
Default constructor, only for dynamic instantiation.

Throws:
java.lang.Exception
Since:
3.3.4

GPProgram

public GPProgram(GPConfiguration a_conf,
                 java.lang.Class[] a_types,
                 java.lang.Class[][] a_argTypes,
                 CommandGene[][] a_nodeSets,
                 int[] a_minDepths,
                 int[] a_maxDepths,
                 int a_maxNodes)
          throws InvalidConfigurationException
Master constructor.

Parameters:
a_conf - the configuration to use
a_types - the type of each chromosome, the length is the number of chromosomes
a_argTypes - the types of the arguments to each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of arguments to the chromosome
a_nodeSets - the nodes which are allowed to be used by each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of nodes
a_minDepths - contains the minimum depth allowed for each chromosome
a_maxDepths - contains the maximum depth allowed for each chromosome
a_maxNodes - reserve space for a_maxNodes number of nodes
Throws:
InvalidConfigurationException
Since:
3.0

GPProgram

public GPProgram(IGPProgram a_prog)
          throws InvalidConfigurationException
Constructor to initialize a GPProgram with values of another GPProgram.

Parameters:
a_prog - the GPProgram to read the initialization values from
Throws:
InvalidConfigurationException
Since:
3.0

GPProgram

public GPProgram(GPConfiguration a_conf,
                 int a_numChromosomes)
          throws InvalidConfigurationException
Sort of minimalistic constructor. Use only if you are aware of what you do.

Parameters:
a_conf - the configuration to use
a_numChromosomes - the number of chromosomes to use with this program.
Throws:
InvalidConfigurationException
Since:
3.0
Method Detail

getChromosome

public ProgramChromosome getChromosome(int a_index)
Specified by:
getChromosome in interface IGPProgram
Parameters:
a_index - the chromosome to get
Returns:
the ProgramChromosome with the given index
Since:
3.0

setChromosome

public void setChromosome(int a_index,
                          ProgramChromosome a_chrom)
Sets the given chromosome at the given index.

Specified by:
setChromosome in interface IGPProgram
Parameters:
a_index - sic
a_chrom - sic
Since:
3.0

growOrFull

public void growOrFull(int a_depth,
                       boolean a_grow,
                       int a_maxNodes,
                       boolean[] a_fullModeAllowed,
                       int a_tries)
Initialize the chromosomes within this GP program using the grow or the full method.

Parameters:
a_depth - the maximum depth of the chromosome to create
a_grow - true: use grow method; false: use full method
a_maxNodes - maximum number of nodes allowed
a_fullModeAllowed - for each chromosome: true means full mode allowed, otherwise use grow mode
a_tries - maximum number of tries to create a valid program
Since:
3.0

putToCache

protected GPProgramInfo putToCache(GPProgram a_program)
Put program to cache.

Parameters:
a_program - the program to put into the cache
Returns:
GPProgramInfo info about the program
Since:
3.4

growOrFull

public void growOrFull(java.lang.Class[][] a_argTypes,
                       CommandGene[][] a_nodeSets)
Initialize this program by using given chromosomes.

Parameters:
a_argTypes - the types of the arguments to each chromosome, must be an array of arrays, the first dimension of which is the number of chromosomes and the second dimension of which is the number of arguments to the chromosome
a_nodeSets - the nodes which are allowed to be used by each chromosome,
Since:
3.2.2

size

public int size()
Specified by:
size in interface IGPProgram
Returns:
the number of chromosomes in the program
Since:
3.0

toString

public java.lang.String toString(int a_startNode)
Builds a string that represents the output of the GPProgram in left-hand-notion.

Parameters:
a_startNode - the node to start with
Returns:
output in left-hand notion
Since:
3.0

toStringNorm

public java.lang.String toStringNorm(int a_startNode)
Builds a string that represents the normalized output of the GPProgram.

Specified by:
toStringNorm in interface IGPProgram
Parameters:
a_startNode - the node to start with
Returns:
output in normalized notion
Since:
3.0

toStringDebug

public java.lang.String toStringDebug()
Builds a string that represents the debug output of the GPProgram.

Returns:
class names of all program chromosomes
Since:
3.0

execute_int

public int execute_int(int a_chromosomeNum,
                       java.lang.Object[] a_args)
Executes the given chromosome as an integer function.

Specified by:
execute_int in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to execute
a_args - the arguments to use
Returns:
the integer return value
Since:
3.0

execute_float

public float execute_float(int a_chromosomeNum,
                           java.lang.Object[] a_args)
Executes the given chromosome as a float function.

Specified by:
execute_float in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to execute
a_args - the arguments to use
Returns:
the floar return value
Since:
3.0

execute_double

public double execute_double(int a_chromosomeNum,
                             java.lang.Object[] a_args)
Executes the given chromosome as a double function.

Specified by:
execute_double in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to execute
a_args - the arguments to use
Returns:
the double return value
Since:
3.0

execute_boolean

public boolean execute_boolean(int a_chromosomeNum,
                               java.lang.Object[] a_args)
Executes the given chromosome as a boolean function.

Specified by:
execute_boolean in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to execute
a_args - the arguments to use
Returns:
the boolean return value
Since:
3.2

execute_object

public java.lang.Object execute_object(int a_chromosomeNum,
                                       java.lang.Object[] a_args)
Executes the given chromosome as an object function.

Specified by:
execute_object in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to execute
a_args - the arguments to use
Returns:
the object return value
Since:
3.0

execute_void

public void execute_void(int a_chromosomeNum,
                         java.lang.Object[] a_args)
Executes the given chromosome as an object function.

Specified by:
execute_void in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to execute
a_args - the arguments to use
Since:
3.0

getCommandOfClass

public int getCommandOfClass(int a_chromosomeNum,
                             java.lang.Class a_class)
Searches for a chromosome that has the given class and returns its index.

Specified by:
getCommandOfClass in interface IGPProgram
Parameters:
a_chromosomeNum - the index of the chromosome to start the search with
a_class - the class to find
Returns:
the index of the first chromosome found that is of a_class, or -1
Since:
3.0

compareTo

public int compareTo(java.lang.Object a_other)
Compares the given program to this program.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
a_other - the program against which to compare this program
Returns:
a negative number if this program is "less than" the given program, zero if they are equal to each other, and a positive number if this program is "greater than" the given program
Since:
3.0

clone

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

getPersistentRepresentation

public java.lang.String getPersistentRepresentation()
Specified by:
getPersistentRepresentation in interface IGPProgram
Returns:
the persistent representation of the GP program, including all chromosomes
Since:
3.3

encode

protected java.lang.String encode(java.lang.String a_string)

decode

protected java.lang.String decode(java.lang.String a_string)

getBusinessKey

public java.lang.String getBusinessKey()
Specified by:
getBusinessKey in interface IBusinessKey
Returns:
hopefully unique key representing the state of the GPProgram
Since:
3.4

JGAP