Genetic Programming TreeVisualizerFree, 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()); tv.setBranchStartWidth(16.0); tv.setArenaColor(Color.white); tv.setSide(1024); tv.setCircleDiminishFactor(0.75); // 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)
Tree ComparisonAs 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).
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".