001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package org.jgap.gp;
011:
012: import java.io.*;
013:
014: import java.awt.*;
015: import javax.swing.tree.*;
016:
017: import org.jgap.*;
018: import org.jgap.gp.function.*;
019: import org.jgap.gp.impl.*;
020: import org.jgap.util.tree.*;
021:
022: /**
023: * Abstract base class for all GP problems. See package examples.gp for sample
024: * implementations.
025: *
026: * @author Klaus Meffert
027: * @since 3.0
028: */
029: public abstract class GPProblem {
030: /** String containing the CVS revision. Read out via reflection!*/
031: private final static String CVS_REVISION = "$Revision: 1.6 $";
032:
033: private GPConfiguration m_conf;
034:
035: public GPProblem(GPConfiguration a_conf)
036: throws InvalidConfigurationException {
037: if (a_conf == null) {
038: throw new InvalidConfigurationException(
039: "Configuration must not be null!");
040: }
041: m_conf = a_conf;
042: }
043:
044: /**
045: * Default constructor for dynamic instantiation.
046: *
047: * @author Klaus Meffert
048: * @since 3.2
049: */
050: public GPProblem() {
051:
052: }
053:
054: /**
055: * @return newly created GPGenotype
056: * @throws InvalidConfigurationException
057: *
058: * @author Klaus Meffert
059: * @since 3.0
060: */
061: public abstract GPGenotype create()
062: throws InvalidConfigurationException;
063:
064: /**
065: * Creates a tree out of a given GP program and saves it to a file.
066: *
067: * @param a_prog the GP program to visualize a tree for
068: * @param a_filename the name of the file to save the tree in
069: * @throws InvalidConfigurationException
070: *
071: * @author Klaus Meffert
072: * @since 3.0
073: */
074: public void showTree(IGPProgram a_prog, String a_filename)
075: throws InvalidConfigurationException {
076: if (a_prog == null) {
077: return;
078: }
079: TreeNode myTree = createTree(a_prog);
080: if (myTree == null) {
081: return;
082: }
083: TreeVisualizer tv = new TreeVisualizer();
084: tv.setTreeBranchRenderer(new JGAPTreeBranchRenderer());
085: tv.setTreeNodeRenderer(new JGAPTreeNodeRenderer());
086: tv.setBranchStartWidth(18.0);
087: tv.setArenaColor(Color.black);
088: tv.setBkgndColor(Color.black); //new Color(10, 10, 10));
089: tv.setRenderNodes(true);
090: tv.setSide(1024);
091: tv.setCircleDiminishFactor(0.5);
092: tv.writeImageFile(tv.renderTree(myTree), new File(a_filename));
093: }
094:
095: /**
096: * Creates a tree out of a given GP program and saves it to a file. Allows to
097: * preset the tree renderers.
098: *
099: * @param a_prog the GP program to visualize a tree for
100: * @param a_filename the name of the file to save the tree in
101: * @param a_treeBranchRenderer renderer for the tree's branches
102: * @param a_treeNodeRenderer renderer for the tree's nodes
103: * @throws InvalidConfigurationException
104: *
105: * @author Klaus Meffert
106: * @since 3.0
107: */
108: public void showTree(IGPProgram a_prog, String a_filename,
109: TreeBranchRenderer a_treeBranchRenderer,
110: TreeNodeRenderer a_treeNodeRenderer)
111: throws InvalidConfigurationException {
112: TreeNode myTree = createTree(a_prog);
113: if (myTree == null) {
114: return;
115: }
116: TreeVisualizer tv = new TreeVisualizer();
117: tv.setTreeBranchRenderer(a_treeBranchRenderer);
118: tv.setTreeNodeRenderer(a_treeNodeRenderer);
119: tv.setBranchStartWidth(18.0);
120: tv.setArenaColor(Color.black);
121: tv.setBkgndColor(Color.black); //new Color(10, 10, 10));
122: tv.setRenderNodes(true);
123: tv.setSide(1024);
124: tv.setCircleDiminishFactor(0.5);
125: tv.writeImageFile(tv.renderTree(myTree), new File(a_filename));
126: }
127:
128: /**
129: * Creates a tree out of a given GP program.
130: *
131: * @param a_prog the GPGenotype to visualize a tree for
132: * @return the TreeNode object corresponding to the GP program
133: * @throws InvalidConfigurationException
134: *
135: * @author Klaus Meffert
136: * @since 3.0
137: */
138: public TreeNode createTree(IGPProgram a_prog)
139: throws InvalidConfigurationException {
140: if (a_prog == null) {
141: return null;
142: }
143: ProgramChromosome master = new ProgramChromosome(m_conf);
144: master.setIndividual(a_prog);
145: TreeNode tree;
146: if (a_prog.size() > 1) {
147: Class[] types = new Class[a_prog.size()];
148: for (int i = 0; i < a_prog.size(); i++) {
149: types[i] = CommandGene.VoidClass; //arbitrary
150: }
151: master.setGene(0, new SubProgram(m_conf, types));
152: int index = 1;
153: for (int i = 0; i < a_prog.size(); i++) {
154: ProgramChromosome child = a_prog.getChromosome(i);
155: for (int j = 0; j < child.size(); j++) {
156: master.setGene(index++, child.getGene(j));
157: }
158: }
159: master.redepth();
160: tree = new JGAPTreeNode(master, 0);
161: } else {
162: tree = new JGAPTreeNode(a_prog.getChromosome(0), 0);
163: }
164: return tree;
165: }
166:
167: /**
168: * @return the GPConfiguration set
169: *
170: * @author Klaus Meffert
171: * @since 3.0
172: */
173: public GPConfiguration getGPConfiguration() {
174: return m_conf;
175: }
176:
177: /**
178: * Sets the configuration. Only use in case of dynamic instantiation (in case
179: * constructor with parameter GPConfiguration is not used).
180: *
181: * @param a_conf the configuration to set
182: *
183: * @author Klaus Meffert
184: * @since 3.2
185: */
186: protected void setGPConfiguration(GPConfiguration a_conf) {
187: m_conf = a_conf;
188: }
189: }
|