Class gpjpp.GPGene
All Packages Class Hierarchy This Package Previous Next Index
Class gpjpp.GPGene
java.lang.Object
|
+----gpjpp.GPObject
|
+----gpjpp.GPContainer
|
+----gpjpp.GPGene
- public class GPGene
- extends GPContainer
GP trees are composed of GPGene objects, which represent the
function or terminal of each element of an s-expression. Each
tree is rooted by a GPGene object, which in turn contains GPGene
objects for each of its function arguments.
GPGene contains a reference to a
GPNode object that describes the nature of the node. The
arguments to a GPGene are stored in its container, of which GPGene
is a subclass.
GPGene is abstract in the sense that it does not contain a method
used to evaluate the fitness of a tree. It is not formally abstract
in Java terminology because gpjpp does not know the data type
embodied by the tree. A gene might represent and return a boolean,
a real, an integer, a vector, nothing at all, or something
completely different. Only at the GP
level (a collection of tree branches forming a complete genetic
program) does gpjpp enforce the requirement that the GP's fitness
evaluate to a real number.
The user program typically must create a subclass of GPGene that
defines a GPGene fitness function called by GP.evaluate(). The
example programs show how to do so in several different situations.
- Version:
- 1.0
-
allNodes
- An array of all node types in all branches used temporarily
while loading the array from a stream.
-
node
- A reference to the node type for this gene.
-
GPGene()
- Public null constructor used during stream loading only.
-
GPGene(GPGene)
- A constructor that is called to clone a GPGene.
-
GPGene(GPNode)
- Constructor used when trees are first created with random
node types.
-
chooseFunctionNode(GPGeneReference)
- Finds a random function node within this gene, not
considering this gene itself.
-
chooseFunctionOrTerminalNode(GPGeneReference)
- Attempts to find a random function node within this gene, not
considering this gene itself.
-
clone()
- Implements the Cloneable interface.
-
countFunctions()
- Returns the total number of function genes included by
this gene and all of its children.
-
create(int, int, int, GPNodeSet)
- Creates the arguments to this gene and recursively creates
children according to the limits and methods specified.
-
createChild(GPNode)
- Creates a child gene while new trees are being built.
-
createNodeIndex(GPAdfNodeSet)
- Must be called by the stream manager before saving and
loading genes.
-
depth()
- Returns the largest depth of the tree attached to this gene.
-
depth(int)
- Called internally by depth() to compute the tree depth.
-
drawOn(GPDrawing, String, String, GPVariables)
- Writes a GPGene in graphic gif file format.
-
equals(Object)
- Determines whether this GPGene equals another object.
-
findNthNode(GPGeneReference, boolean)
- Returns true if a gene of specified position and type can be
found within this gene.
-
geneNode()
- Returns the node reference of this gene.
-
geneRep()
- Returns the string representation of this gene as given
by its node type.
-
isA()
- Returns a code identifying the class in a stream file.
-
isFunction()
-
Returns true if this gene represents a function.
-
isTerminal()
-
Returns true if this gene represents a terminal.
-
length()
- Returns the number of genes attached to this one, including
itself.
-
load(DataInputStream)
- Loads a GPGene from the specified stream.
-
printOn(PrintStream, GPVariables)
- Writes a GPGene in text format to a PrintStream.
-
printTree(PrintStream, GPVariables)
- Writes a GPGene in text tree format to a PrintStream.
-
save(DataOutputStream)
- Saves a GPGene to the specified stream.
-
testNull()
- A debugging/testing method to ensure that no null node or gene
references are found in this gene or any of its children.
allNodes
protected static GPNode allNodes[]
- An array of all node types in all branches used temporarily
while loading the array from a stream.
- See Also:
- createNodeIndex
node
protected GPNode node
- A reference to the node type for this gene.
GPGene
public GPGene()
- Public null constructor used during stream loading only.
GPGene
public GPGene(GPNode gpo)
- Constructor used when trees are first created with random
node types.
- Parameters:
- gpo - a node type that is an element of the current
branch's node set.
GPGene
public GPGene(GPGene gpo)
- A constructor that is called to clone a GPGene. Used
whenever a tree is selected for reproduction or crossover.
clone
protected synchronized Object clone()
- Implements the Cloneable interface.
This (or its user subclass) is called during reproduction.
- Returns:
- the cloned object.
- Overrides:
- clone in class GPContainer
isA
public byte isA()
- Returns a code identifying the class in a stream file.
- Returns:
- the ID code GENEID.
- Overrides:
- isA in class GPContainer
createChild
public GPGene createChild(GPNode gpo)
- Creates a child gene while new trees are being built. The
user must generally override this in a subclass to create
genes of user type. See the example programs.
- Parameters:
- gpo - a node type that is an element of the current
branch's node set.
- Returns:
- the newly created gene with an empty container.
testNull
public void testNull()
- A debugging/testing method to ensure that no null node or gene
references are found in this gene or any of its children.
- Throws: RuntimeException
- if a null gene or node reference is found.
isFunction
public boolean isFunction()
- Returns true if this gene represents a function.
isTerminal
public boolean isTerminal()
- Returns true if this gene represents a terminal.
geneNode
public GPNode geneNode()
- Returns the node reference of this gene.
geneRep
public String geneRep()
- Returns the string representation of this gene as given
by its node type. This method can be overridden in a user
subclass of GPGene if the string representation should vary
depending on the data value of each gene. See the Lawn
program for an example.
countFunctions
protected int countFunctions()
- Returns the total number of function genes included by
this gene and all of its children. If this gene is a terminal,
countFunctions returns 0. Otherwise it returns at least 1
and recursively traces all of its children. Used internally
during shrink mutation.
length
public int length()
- Returns the number of genes attached to this one, including
itself. This is the complexity or length of the branch.
depth
public int depth()
- Returns the largest depth of the tree attached to this gene.
If this gene is a terminal, the depth is 1.
depth
protected int depth(int depthSoFar)
- Called internally by depth() to compute the tree depth.
create
public synchronized int create(int creationType,
int allowableDepth,
int allowableLength,
GPNodeSet ns)
- Creates the arguments to this gene and recursively creates
children according to the limits and methods specified. The
gene for which this method is called should have been created
by calling the createGene()
method of GP, which allocates an argument container of
appropriate size and assigns the node field but doesn't fill
in the children. create() is called by
GP.create().
- Parameters:
- creationType - the method used to create the tree, either
GPGROW (use
function nodes to fill the tree to allowable depth) or
GPVARIABLE
(choose function and terminal nodes with 50:50 probability).
- allowableDepth - the maximum allowable depth of the tree
starting from this level. If the allowable depth is 1,
the children are always chosen to be terminals.
- allowableLength - the maximum allowable number of nodes
in the tree starting at this level. Since create() cannot
predict how many nodes will be added recursively it
simply stops adding nodes if it exceeds allowableLength.
A higher level routine in GPPopulation rejects the
returned tree if the GP complexity exceeds a global limit.
- ns - the node set used to select functions and terminals
for this branch of the GP tree.
- Returns:
- the total number of nodes in the created tree. If this
value exceeds allowableLength, the tree will be rejected.
create() ensures that it doesn't waste much time creating
extra nodes.
equals
public boolean equals(Object obj)
- Determines whether this GPGene equals another object. It returns
true if obj is not null, is an instance of a GPGene (or a
descendant), and has the same structure and node values as this
gene. This function is called when a GPPopulation is testing the
diversity of the population. equals() is called only after a
hashCode() function
determines that two GPs are at least quite similar.
You might need to override this in cases where two terminal
genes can have identical node types but still not be the same.
This occurs for node types that represent random constants, for
example.
- Parameters:
- obj - any Java object reference, including null.
- Returns:
- true if this and obj are equivalent.
- Overrides:
- equals in class Object
findNthNode
protected boolean findNthNode(GPGeneReference ref,
boolean findFunction)
- Returns true if a gene of specified position and type can be
found within this gene. Called within
chooseFunctionOrTerminalNode and
chooseFunctionNode of this same class, which are used to
select appropriate genes for crossover and mutation.
- Parameters:
- ref - on entry specifies the container that holds this
gene, the index of this gene within that container,
and the count of genes to scan. On exit returns
the container that holds the found gene and the
index of that gene within the container.
- findFunction - true if only function nodes are to be
counted, false if function and terminal nodes
are acceptable.
- Returns:
- true if an acceptable node is found; false if
there are fewer nodes than specified.
chooseFunctionOrTerminalNode
public void chooseFunctionOrTerminalNode(GPGeneReference ref)
- Attempts to find a random function node within this gene, not
considering this gene itself. If 10 random attempts don't find
a function, it returns a terminal as a last resort. Used for
crossover and
swap mutation
by the GP class.
- Parameters:
- ref - on entry specifies the container that holds this
gene and the index of this gene within that
container. On exit returns the container that
holds the found gene, the index of that gene
within the container, and the number of genes
counted to reach the found one.
- Throws: RuntimeException
- if findNthNode can't find a node that should
rightfully exist in the tree.
chooseFunctionNode
public boolean chooseFunctionNode(GPGeneReference ref)
- Finds a random function node within this gene, not
considering this gene itself. If no such function exists,
the function returns false. Used for
shrink mutation
by the GP class.
- Parameters:
- ref - on entry specifies the container that holds this
gene and the index of this gene within that
container. On exit returns the container that
holds the found gene, the index of that gene
within the container, and the number of genes
counted to reach the found one.
- Returns:
- true if any function node can be found, else false.
createNodeIndex
public static synchronized void createNodeIndex(GPAdfNodeSet adfNs)
- Must be called by the stream manager before saving and
loading genes. The routine initializes the
allNodes static
array and also stores within each GPNode its allNodes index.
This byte-sized index is stored on the stream
to represent the type of each gene. When the stream is loaded
again, the index is converted back to a node reference.
- Throws: RuntimeException
- if the total number of node types exceeds 255.
load
protected synchronized void load(DataInputStream is) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException
- Loads a GPGene from the specified stream. Reads the
node index from the stream and converts it to a GPNode
reference. Then loads the container of child genes.
- Throws: ClassNotFoundException
- if the class indicated by the stream's ID code
is not registered with GPObject.
- Throws: InstantiationException
- if an error occurs while calling new or the null
constructor of the specified class.
- Throws: IllegalAccessException
- if the specified class or its null constructor is
not public.
- Throws: IOException
- if an error occurs while reading the stream.
- Overrides:
- load in class GPContainer
- See Also:
- createNodeIndex
save
protected void save(DataOutputStream os) throws IOException
- Saves a GPGene to the specified stream. Writes the
node index value to the stream and then stores the container.
- Throws: IOException
- if an error occurs while writing the stream.
- Overrides:
- save in class GPContainer
- See Also:
- createNodeIndex
printOn
public void printOn(PrintStream os,
GPVariables cfg)
- Writes a GPGene in text format to a PrintStream.
Each node is printed as its representation string followed
by its children, if any, in recursive depth-first order.
Parentheses are used to surround the arguments to each
function. The entire expression is written on a single
text line.
- Overrides:
- printOn in class GPContainer
printTree
public void printTree(PrintStream os,
GPVariables cfg)
- Writes a GPGene in text tree format to a PrintStream.
The GPGenePrint class
is used to format the tree in a pseudo-graphic format. Each node
is printed as its representation string and is connected to its
children on a lower text row using line drawing characters.
- Parameters:
- os - a PrintStream.
- cfg - the set of gpjpp configuration variables,
sometimes used to control the output.
drawOn
public void drawOn(GPDrawing ods,
String fname,
String title,
GPVariables cfg) throws IOException
- Writes a GPGene in graphic gif file format. The
GPGenePrint
class is used to format the tree, which is drawn onto the
AWT-based offscreen drawing surface represented by GPDrawing.
This offscreen drawing is then encoded into the gif format and
stored in the file named by fname.
- Parameters:
- ods - an instantiated drawing surface. GPDrawing is
a subclass of java.awt.Frame containing a
single Canvas component whose image is dynamically
sized to hold the tree.
- fname - the name of the file to hold the gif image.
This name should include the .gif extension.
- title - a string that is drawn on the first line of the
image to title it. Can be null or empty.
- cfg - configuration parameters for the genetic
run. The
TreeFontSize field is used to determine the font
size for text in the drawing.
- Throws: IOException
- if an error occurs while writing the image file.
All Packages Class Hierarchy This Package Previous Next Index