Genetic Programming Tree Visualizer

Genetic Programming

Free, Open Source function tree rendering and visualization Java code.

Understanding the results of genetic programming can be a daunting task. Are your trees being generated correctly? Are recombination and mutation happening as desired? Which branches are more stable than others – where is the evolution taking place? This code could potentially help you quickly answer such questions.

With the help of these objects you can easily export graphic images representing your trees. You can see what a tree looks like from one generation to the next. You could view the best individual per generation in a sequence of images to see how evolution is affecting your populaiton. This heuristic feel for what is going on could provide valuable insight and inspiration for your GP experiments.

Implementation is simple. Your GP trees need to implement the standard TreeNode interface found under javax.swing.tree.*. If you want to customize the rendering of the branches or nodes, simply create your own renderers which implement the TreeBranchRenderer and TreeNodeRenderer interfaces. Here is an example implementation:

TreeVisualizer tv = new TreeVisualizer();
tv.setTreeBranchRenderer(new METreeBranchRenderer());
tv.setTreeNodeRenderer(new METreeNodeRenderer());
// i.getRoot() is example code.  
// It is there that you reference your TreeNode object.
tv.writeImageFile(tv.renderTree(i.getRoot()), new File("tree1.png"));

Download the Code (1.0)


Documentation of the TreeVisualizer methods.

Tree Comparison

As a population converges towards an optimal solution, the differences in structure can be very subtle. One of the most profound benefits of this rendering method is it makes these minor variations evident. The following four images are two sets of a tree and the same tree mutated.
(Hint to easily see the difference: If you are using a browser capable of tabbed browsing, open each of these images in a separate tab then flip between the tabs to notice the mutation. This technique takes advantage of our brain’s ability to detect motion).

Tree A before and after Tree B before and after
A) Before.
A) After.
B) Before.
B) After.

Not convinced of the usefulness? Look at the before and after String representations of Tree B. Can you spot the difference? Plus, think of the interesting animation produced if you take a snapshot of your best individual for every generation.

Custom Rendering:

You can program your own tree node and tree branch renderers by extending the TreeNodeRenderer and TreeBranchRenderer interfaces. Your representations could contain any amount of information about your trees. In the examples on this page, branch color is governed by the type of node from which the branches sprout. These examlple implementations are included with the code package.

Tree nodes or branches could be colored according to a persistence number – the number of generations since the node was last mutated. This could be very useful in determining which part of your trees are vital “old growth”.

Rendered Examples:

function tree
(Larger View)
Nodes get darker based on level.
White branches – boolean operators.
Blue branches – comparison operators.
Golden branches – number operators.
function tree
(Larger View)
circleDiminishFactor set to 0.5.
branchStartWidth set to 8.0
renderNodes is false.
function tree
(Larger View)
Green branches – boolean operators.
Blue branches – comparison operators.
Red branches – number operators.
renderNodes is false.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Geneffects Software