com.groovyj.jgprog
Class Chromosome

java.lang.Object
  |
  +--com.groovyj.jgprog.Chromosome
All Implemented Interfaces:
java.io.Serializable

public class Chromosome
extends java.lang.Object
implements java.io.Serializable

Represents a single chromosome.

Copyright (c) 2000 Robert Baruch. This code is released under the GNU General Public License (GPL).

Version:
$Id: Chromosome.java,v 1.4 2000/10/12 15:22:55 groovyjava Exp $
Author:
Robert Baruch (jgprog@sourceforge.net)
See Also:
Serialized Form

Constructor Summary
Chromosome()
          Construct an empty chromosome
Chromosome(Chromosome c)
          Perform a deep copy of a chromosome.
 
Method Summary
 boolean execute_boolean()
          Executes this node as a boolean.
 boolean execute_boolean(int n, int child)
           
 double execute_double()
          Executes this node as a double.
 double execute_double(int n, int child)
           
 float execute_float()
          Executes this node as a float.
 float execute_float(int n, int child)
           
 int execute_int()
          Executes this node as an integer.
 int execute_int(int n, int child)
           
 long execute_long()
          Executes this node as a long.
 long execute_long(int n, int child)
           
 java.lang.Object execute_object()
          Executes this node as an object.
 java.lang.Object execute_object(int n, int child)
           
 void execute_void()
          Executes this node, returning nothing.
 void execute_void(int n, int child)
           
 java.lang.Object execute()
          Executes this node without knowing its return type.
 java.lang.Object execute(int n, int child)
           
 void full(int num, int depth, Type type, Type[] argTypes, Function[] functionSet)
          Initialize this chromosome using the full method.
 int getChild(int n, int child)
           
 int getDepth(int n)
           
 int getFunction(int i)
          Gets the i'th function in this chromosome.
 int getFunction(int i, Type type)
          Gets the i'th function of the given type in this chromosome.
 Function getNode(int i)
          Gets the i'th node in this chromosome.
 int getNode(int i, Type type)
          Gets the i'th node of the given type in this chromosome.
 int getParentNode(int child)
          Gets the node which is the parent of the given node in this chromosome.
 int getSize(int n)
           
 int getTerminal(int i)
          Gets the i'th terminal in this chromosome.
 int getTerminal(int i, Type type)
          Gets the i'th terminal of the given type in this chromosome.
 void grow(int num, int depth, Type type, Type[] argTypes, Function[] functionSet)
          Initialize this chromosome using the grow method.
 boolean isPossible(Function f)
           
static boolean isPossible(Type type, Function[] nodeSet, boolean function)
          Determines whether there exists a function or terminal in the given node set with the given type.
static void main(java.lang.String[] args)
           
 int numFunctions()
          Counts the number of functions in this chromosome.
 int numFunctions(Type type)
          Counts the number of functions of the given type in this chromosome.
 int numTerminals()
          Counts the number of terminals in this chromosome.
 int numTerminals(Type type)
          Counts the number of terminals of the given type in this chromosome.
 void redepth()
           
protected  int redepth(int n)
           
static Function selectNode(Type type, Function[] functionSet, boolean function, boolean growing)
          Randomly chooses a node from the node set.
 java.lang.String toString()
          Returns the string representing this chromosome.
protected  java.lang.String toString(int n)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Chromosome

public Chromosome(Chromosome c)
Perform a deep copy of a chromosome.
Parameters:
c - the chromosome to copy
Since:
1.0

Chromosome

public Chromosome()
Construct an empty chromosome
Since:
1.0
Method Detail

isPossible

public static boolean isPossible(Type type,
                                 Function[] nodeSet,
                                 boolean function)
Determines whether there exists a function or terminal in the given node set with the given type.
Parameters:
type - the type to look for
nodeSet - the array of nodes to look through
function - true to look for a function, false to look for a terminal
Returns:
true if such a node exists, false otherwise
Since:
1.0

isPossible

public boolean isPossible(Function f)

selectNode

public static Function selectNode(Type type,
                                  Function[] functionSet,
                                  boolean function,
                                  boolean growing)
Randomly chooses a node from the node set.
Parameters:
type - the type of node to choose
nodeSet - the array of nodes to choose from
function - true to choose a function, false to choose a terminal
growing - true to ignore the function parameter, false otherwise
Returns:
the node chosen
Since:
1.0

full

public void full(int num,
                 int depth,
                 Type type,
                 Type[] argTypes,
                 Function[] functionSet)
Initialize this chromosome using the full method.
Parameters:
num - the number of this chromosome
depth - the depth of the chromosome to create
type - the type of the chromosome to create
argTypes - the array of types of arguments for this chromosome
functionSetSet - the set of nodes valid to pick from
Since:
1.0

grow

public void grow(int num,
                 int depth,
                 Type type,
                 Type[] argTypes,
                 Function[] functionSet)
Initialize this chromosome using the grow method.
Parameters:
num - the chromosome number of this chromosome
depth - the maximum depth of the chromosome to create
type - the type of the chromosome to create
argTypes - the array of types of arguments for this chromosome
functionSet - the set of nodes valid to pick from
Since:
1.0

toString

protected java.lang.String toString(int n)

toString

public java.lang.String toString()
Returns the string representing this chromosome.
Overrides:
toString in class java.lang.Object
Returns:
the string representing this chromosome.
Since:
1.0

redepth

public void redepth()

redepth

protected int redepth(int n)

numTerminals

public int numTerminals()
Counts the number of terminals in this chromosome.
Returns:
the number of terminals in this chromosome.
Since:
1.0

numFunctions

public int numFunctions()
Counts the number of functions in this chromosome.
Returns:
the number of functions in this chromosome.
Since:
1.0

numTerminals

public int numTerminals(Type type)
Counts the number of terminals of the given type in this chromosome.
Parameters:
type - the type of terminal to count
Returns:
the number of terminals in this chromosome.
Since:
1.0

numFunctions

public int numFunctions(Type type)
Counts the number of functions of the given type in this chromosome.
Parameters:
type - the type of function to count
Returns:
the number of functions in this chromosome.
Since:
1.0

getNode

public Function getNode(int i)
Gets the i'th node in this chromosome. The nodes are counted in a depth-first manner, with node 0 being the root of this chromosome.
Parameters:
i - the node number to get
Returns:
the node
Since:
1.0

getChild

public int getChild(int n,
                    int child)

getNode

public int getNode(int i,
                   Type type)
Gets the i'th node of the given type in this chromosome. The nodes are counted in a depth-first manner, with node 0 being the first node of the given type in this chromosome.
Parameters:
i - the node number to get
type - the type of node to get
Returns:
the node
Since:
1.0

getTerminal

public int getTerminal(int i)
Gets the i'th terminal in this chromosome. The nodes are counted in a depth-first manner, with node 0 being the first terminal in this chromosome.
Parameters:
i - the terminal number to get
Returns:
the terminal
Since:
1.0

getFunction

public int getFunction(int i)
Gets the i'th function in this chromosome. The nodes are counted in a depth-first manner, with node 0 being the first function in this chromosome.
Parameters:
i - the function number to get
Returns:
the function
Since:
1.0

getTerminal

public int getTerminal(int i,
                       Type type)
Gets the i'th terminal of the given type in this chromosome. The nodes are counted in a depth-first manner, with node 0 being the first terminal of the given type in this chromosome.
Parameters:
i - the terminal number to get
type - the type of terminal to get
Returns:
the terminal
Since:
1.0

getFunction

public int getFunction(int i,
                       Type type)
Gets the i'th function of the given type in this chromosome. The nodes are counted in a depth-first manner, with node 0 being the first function of the given type in this chromosome.
Parameters:
i - the function number to get
type - the type of function to get
Returns:
the function
Since:
1.0

getSize

public int getSize(int n)

getDepth

public int getDepth(int n)

getParentNode

public int getParentNode(int child)
Gets the node which is the parent of the given node in this chromosome. If the child is at depth d then the parent is the first function at depth d-1 when iterating backwards through the function list starting from the child.
Parameters:
child - the child node
Returns:
the parent node, or null if the child is the root node
Since:
1.0

execute_boolean

public boolean execute_boolean()
Executes this node as a boolean.
Returns:
the boolean return value of this node
Throws:
UnsupportedOperationException - if the type of this node is not boolean
Since:
1.0

execute_boolean

public boolean execute_boolean(int n,
                               int child)

execute_void

public void execute_void()
Executes this node, returning nothing.
Throws:
UnsupportedOperationException - if the type of this node is not void
Since:
1.0

execute_void

public void execute_void(int n,
                         int child)

execute_int

public int execute_int()
Executes this node as an integer.
Returns:
the integer return value of this node
Throws:
UnsupportedOperationException - if the type of this node is not integer
Since:
1.0

execute_int

public int execute_int(int n,
                       int child)

execute_long

public long execute_long()
Executes this node as a long.
Returns:
the long return value of this node
Throws:
UnsupportedOperationException - if the type of this node is not long
Since:
1.0

execute_long

public long execute_long(int n,
                         int child)

execute_float

public float execute_float()
Executes this node as a float.
Returns:
the float return value of this node
Throws:
UnsupportedOperationException - if the type of this node is not float
Since:
1.0

execute_float

public float execute_float(int n,
                           int child)

execute_double

public double execute_double()
Executes this node as a double.
Returns:
the double return value of this node
Throws:
UnsupportedOperationException - if the type of this node is not double
Since:
1.0

execute_double

public double execute_double(int n,
                             int child)

execute_object

public java.lang.Object execute_object()
Executes this node as an object.
Returns:
the object return value of this node
Throws:
UnsupportedOperationException - if the type of this node is not object
Since:
1.0

execute_object

public java.lang.Object execute_object(int n,
                                       int child)

execute

public java.lang.Object execute()
Executes this node without knowing its return type.
Returns:
the Object which wraps the return value of this node, or null if the return type is null or unknown.
Since:
1.0

execute

public java.lang.Object execute(int n,
                                int child)

main

public static void main(java.lang.String[] args)