KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > GPProblem


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.gp;
11
12 import java.io.*;
13
14 import java.awt.*;
15 import javax.swing.tree.*;
16
17 import org.jgap.*;
18 import org.jgap.gp.function.*;
19 import org.jgap.gp.impl.*;
20 import org.jgap.util.tree.*;
21
22 /**
23  * Abstract base class for all GP problems. See package examples.gp for sample
24  * implementations.
25  *
26  * @author Klaus Meffert
27  * @since 3.0
28  */

29 public abstract class GPProblem {
30   /** String containing the CVS revision. Read out via reflection!*/
31   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
32
33   private GPConfiguration m_conf;
34
35   public GPProblem(GPConfiguration a_conf)
36       throws InvalidConfigurationException {
37     if (a_conf == null) {
38       throw new InvalidConfigurationException("Configuration must not be null!");
39     }
40     m_conf = a_conf;
41   }
42
43   /**
44    * @return newly created GPGenotype
45    * @throws InvalidConfigurationException
46    *
47    * @author Klaus Meffert
48    * @since 3.0
49    */

50   public abstract GPGenotype create()
51       throws InvalidConfigurationException;
52
53   /**
54    * Creates a tree out of a given GP program and saves it to a file.
55    *
56    * @param a_prog the GP program to visualize a tree for
57    * @param a_filename the name of the file to save the tree in
58    * @throws InvalidConfigurationException
59    *
60    * @author Klaus Meffert
61    * @since 3.0
62    */

63   public void showTree(IGPProgram a_prog, String JavaDoc a_filename)
64       throws InvalidConfigurationException {
65     TreeNode myTree = createTree(a_prog);
66     if (myTree == null) {
67       return;
68     }
69     TreeVisualizer tv = new TreeVisualizer();
70     tv.setTreeBranchRenderer(new JGAPTreeBranchRenderer());
71     tv.setTreeNodeRenderer(new JGAPTreeNodeRenderer());
72     tv.setBranchStartWidth(18.0);
73     tv.setArenaColor(Color.black);
74     tv.setBkgndColor(Color.black); //new Color(10, 10, 10));
75
tv.setRenderNodes(true);
76     tv.setSide(1024);
77     tv.setCircleDiminishFactor(0.5);
78     tv.writeImageFile(tv.renderTree(myTree), new File(a_filename));
79   }
80
81   /**
82    * Creates a tree out of a given GP program and saves it to a file. Allows to
83    * preset the tree renderers.
84    *
85    * @param a_prog the GP program to visualize a tree for
86    * @param a_filename the name of the file to save the tree in
87    * @param a_treeBranchRenderer renderer for the tree's branches
88    * @param a_treeNodeRenderer renderer for the tree's nodes
89    * @throws InvalidConfigurationException
90    *
91    * @author Klaus Meffert
92    * @since 3.0
93    */

94   public void showTree(IGPProgram a_prog, String JavaDoc a_filename,
95                        TreeBranchRenderer a_treeBranchRenderer,
96                        TreeNodeRenderer a_treeNodeRenderer)
97       throws InvalidConfigurationException {
98     TreeNode myTree = createTree(a_prog);
99     if (myTree == null) {
100       return;
101     }
102     TreeVisualizer tv = new TreeVisualizer();
103     tv.setTreeBranchRenderer(a_treeBranchRenderer);
104     tv.setTreeNodeRenderer(a_treeNodeRenderer);
105     tv.setBranchStartWidth(18.0);
106     tv.setArenaColor(Color.black);
107     tv.setBkgndColor(Color.black); //new Color(10, 10, 10));
108
tv.setRenderNodes(true);
109     tv.setSide(1024);
110     tv.setCircleDiminishFactor(0.5);
111     tv.writeImageFile(tv.renderTree(myTree), new File(a_filename));
112   }
113
114   /**
115    * Creates a tree out of a given GP program.
116    *
117    * @param a_prog the GPGenotype to visualize a tree for
118    * @return the TreeNode object corresponding to the GP program
119    * @throws InvalidConfigurationException
120    *
121    * @author Klaus Meffert
122    * @since 3.0
123    */

124   public TreeNode createTree(IGPProgram a_prog)
125       throws InvalidConfigurationException {
126     if (a_prog == null) {
127       return null;
128     }
129     ProgramChromosome master = new ProgramChromosome(m_conf);
130     TreeNode tree;
131     if (a_prog.size() > 1) {
132       Class JavaDoc[] types = new Class JavaDoc[a_prog.size()];
133       for (int i = 0; i < a_prog.size(); i++) {
134         types[i] = CommandGene.VoidClass; //arbitrary
135
}
136       master.setGene(0, new SubProgram(m_conf, types));
137       int index = 1;
138       for (int i = 0; i < a_prog.size(); i++) {
139         ProgramChromosome child = a_prog.getChromosome(i);
140         for (int j = 0; j < child.size(); j++) {
141           master.setGene(index++, child.getGene(j));
142         }
143       }
144       master.redepth();
145       tree = new JGAPTreeNode(master, 0);
146     }
147     else {
148       tree = new JGAPTreeNode(a_prog.getChromosome(0), 0);
149     }
150     return tree;
151   }
152
153   /**
154    * @return the GPConfiguration set
155    *
156    * @author Klaus Meffert
157    * @since 3.0
158    */

159   public GPConfiguration getGPConfiguration() {
160     return m_conf;
161   }
162 }
163
Popular Tags