ec.gp
Class GPTree

java.lang.Object
  |
  +--ec.gp.GPTree
All Implemented Interfaces:
java.lang.Cloneable, GPNodeParent, Prototype, java.io.Serializable, Setup

public class GPTree
extends java.lang.Object
implements GPNodeParent

GPTree is a GPNodeParent which holds the root GPNode of a tree of GPNodes. GPTrees typically fill out an array held in a GPIndividual (their "owner") and their roots are evaluated to evaluate a Genetic programming tree. GPTrees also have constraints, which are shared, and define items shared among several GPTrees.

GPTrees can print themselves for humans in one of two ways. First, a GPTree can print the tree as a Koza-style Lisp s-expression, which is the default. Second, a GPTree can print the tree as a LaTeX2e code snippet, which can be inserted into a LaTeX2e file and will result in a picture of the tree! Cool, no?

You turn this feature on with the latex parameter below. Here's how it works. To insert the code, you'll need to include the epic,ecltree, and probably the fancybox packages, in that order. You'll also need to define the command \gpbox, which takes one argument (the string name for the GPNode) and draws a box with that node. Lastly, you might want to set a few parameters dealing with the ecltree package.

Here's an example which looks quite good (pardon the double-backslashes in front of the usepackage statements -- javadoc is freaking out if I put a single backslash. So you'll need to remove the extra backslash in order to try out this example):


\documentclass[]{article}
\\usepackage{epic}	% required by ecltree and fancybox packages
\\usepackage{ecltree}	% to draw the GP trees
\\usepackage{fancybox}	% required by \Ovalbox

\begin{document}

% minimum distance between nodes on the same line
\setlength{\GapWidth}{1em}    

% draw with a thick dashed line, very nice looking
\thicklines \drawwith{\dottedline{2}}   

% draw an oval and center it with the rule.  You may want to fool with the
% rule values, though these seem to work quite well for me.  If you make the
% rule smaller than the text height, then the GP nodes may not line up with
% each other horizontally quite right, so watch out.
\newcommand{\gpbox}[1]{\Ovalbox{#1\rule[-.7ex]{0ex}{2.7ex}}}

% Here's the tree which the GP system spat out
\begin{bundle}{\gpbox{progn3}}\chunk{\begin{bundle}{\gpbox{if-food-ahead}}
\chunk{\begin{bundle}{\gpbox{progn3}}\chunk{\gpbox{right}}
\chunk{\gpbox{left}}\chunk{\gpbox{move}}\end{bundle}}
\chunk{\begin{bundle}{\gpbox{if-food-ahead}}\chunk{\gpbox{move}}
\chunk{\gpbox{left}}\end{bundle}}\end{bundle}}\chunk{\begin{bundle}{\gpbox{progn2}}
\chunk{\begin{bundle}{\gpbox{progn2}}\chunk{\gpbox{move}}
\chunk{\gpbox{move}}\end{bundle}}\chunk{\begin{bundle}{\gpbox{progn2}}
\chunk{\gpbox{right}}\chunk{\gpbox{left}}\end{bundle}}\end{bundle}}
\chunk{\begin{bundle}{\gpbox{if-food-ahead}}\chunk{\begin{bundle}{\gpbox{if-food-ahead}}
\chunk{\gpbox{move}}\chunk{\gpbox{left}}\end{bundle}}
\chunk{\begin{bundle}{\gpbox{if-food-ahead}}\chunk{\gpbox{left}}\chunk{\gpbox{right}}
\end{bundle}}\end{bundle}}\end{bundle}

\end{document}

Parameters
base.tc
String
(The tree's constraints)
base.latex
bool = true or false (default)
(print for humans using latex?)

Default Base
gp.tree

See Also:
Serialized Form

Field Summary
 GPNode child
          the root GPNode in the GPTree
 byte constraints
          constraints on the GPTree -- don't access the constraints through this variable -- use the constraints() method instead, which will give the actual constraints object.
static int NO_TREENUM
           
 GPIndividual owner
          the owner of the GPTree
static java.lang.String P_TREE
           
static java.lang.String P_TREECONSTRAINTS
           
static java.lang.String P_USELATEX
           
 boolean useLatex
          Use latex to print for humans?
 
Constructor Summary
GPTree()
           
 
Method Summary
 void buildTree(EvolutionState state, int thread)
          Builds a new randomly-generated rooted tree and attaches it to the GPTree.
 GPTreeConstraints constraints()
           
 GPTree deepClone()
          Deep-clones the tree, including all the nodes.
 Parameter defaultBase()
          Returns the default base for this prototype.
 void printTree(EvolutionState state, int log, int verbosity)
          Prints out the tree in single-line fashion suitable for reading in later by computer.
 void printTree(EvolutionState state, java.io.PrintWriter writer)
          Prints out the tree in single-line fashion suitable for reading in later by computer.
 void printTreeForHumans(EvolutionState state, int log, int verbosity)
          Prints out the tree in a readable Lisp-like fashion.
 java.lang.Object protoClone()
          Proto-clones the tree but does NOT deep clone it.
 java.lang.Object protoCloneSimple()
          This should be implemented in a the top-level Prototype ONLY; in fact, it should probably be declared final.
 void readTree(EvolutionState state, java.io.LineNumberReader reader)
          Reads in the tree from a form printed by printTree.
 void setup(EvolutionState state, Parameter base)
          Sets up a prototypical GPTree with those features it shares with other GPTrees in its position in its GPIndividual, and nothhing more.
 boolean treeEquals(GPTree tree)
          Returns true if I am "genetically" the same as tree, though we may have different owners.
 int treeHashCode()
          Returns a hash code for comparing different GPTrees.
 int treeNumber()
          An expensive function which determines my tree number -- only use for errors, etc.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

P_TREE

public static final java.lang.String P_TREE

P_TREECONSTRAINTS

public static final java.lang.String P_TREECONSTRAINTS

P_USELATEX

public static final java.lang.String P_USELATEX

NO_TREENUM

public static final int NO_TREENUM

child

public GPNode child
the root GPNode in the GPTree

owner

public GPIndividual owner
the owner of the GPTree

constraints

public byte constraints
constraints on the GPTree -- don't access the constraints through this variable -- use the constraints() method instead, which will give the actual constraints object.

useLatex

public boolean useLatex
Use latex to print for humans?
Constructor Detail

GPTree

public GPTree()
Method Detail

constraints

public final GPTreeConstraints constraints()

defaultBase

public Parameter defaultBase()
Description copied from interface: Prototype
Returns the default base for this prototype. This should generally be implemented by building off of the static base() method on the DefaultsForm object for the prototype's package. This should be callable during setup(...).
Specified by:
defaultBase in interface Prototype

treeEquals

public boolean treeEquals(GPTree tree)
Returns true if I am "genetically" the same as tree, though we may have different owners.

treeHashCode

public int treeHashCode()
Returns a hash code for comparing different GPTrees. In general, two trees which are treeEquals(...) should have the same hash code.

protoClone

public java.lang.Object protoClone()
                            throws java.lang.CloneNotSupportedException
Proto-clones the tree but does NOT deep clone it. The tree's child is still the same, and not a copy. Owner is not changed.
Specified by:
protoClone in interface Prototype

protoCloneSimple

public final java.lang.Object protoCloneSimple()
Description copied from interface: Prototype
This should be implemented in a the top-level Prototype ONLY; in fact, it should probably be declared final. It should be implemented as follows:

public final Object protoCloneSimple()
{
try { return protoClone(); }
catch (CloneNotSupportedException e) 
{ throw new InternalError(); } // never happens
} 
Specified by:
protoCloneSimple in interface Prototype

deepClone

public GPTree deepClone()
Deep-clones the tree, including all the nodes. Owner is not changed.

treeNumber

public int treeNumber()
An expensive function which determines my tree number -- only use for errors, etc. Returns ec.gp.GPTree.NO_TREENUM if the tree number could not be determined (might happen if it's not been assigned yet).

setup

public void setup(EvolutionState state,
                  Parameter base)
Sets up a prototypical GPTree with those features it shares with other GPTrees in its position in its GPIndividual, and nothhing more. This must be called after the GPTypes and GPNodeConstraints have been set up. Presently they're set up in GPInitializer, which gets called before this does, so we're safe.
Specified by:
setup in interface Prototype

printTree

public void printTree(EvolutionState state,
                      int log,
                      int verbosity)
Prints out the tree in single-line fashion suitable for reading in later by computer. O(n). The default version of this method simply calls child's printRootedTree(...) method.

printTree

public void printTree(EvolutionState state,
                      java.io.PrintWriter writer)
Prints out the tree in single-line fashion suitable for reading in later by computer. O(n). The default version of this method simply calls child's printRootedTree(...) method.

readTree

public void readTree(EvolutionState state,
                     java.io.LineNumberReader reader)
              throws java.io.IOException,
                     java.lang.CloneNotSupportedException
Reads in the tree from a form printed by printTree.

printTreeForHumans

public void printTreeForHumans(EvolutionState state,
                               int log,
                               int verbosity)
Prints out the tree in a readable Lisp-like fashion. O(n). The default version of this method simply calls child's printRootedTreeForHumans(...) method.

buildTree

public void buildTree(EvolutionState state,
                      int thread)
               throws java.lang.CloneNotSupportedException
Builds a new randomly-generated rooted tree and attaches it to the GPTree.