KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.*;
13 import org.jgap.gp.impl.*;
14
15 /**
16  * Base class for GPProgram's. See org.jgap.gp.impl.GPProgram for an
17  * implementation.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public abstract class GPProgramBase
23     implements IGPProgram {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.9 $";
26
27   private double m_fitnessValue = FitnessFunction.NO_FITNESS_VALUE;
28
29   private GPConfiguration m_conf;
30
31   /**
32    * Return type per chromosome.
33    */

34   private Class JavaDoc[] m_types;
35
36   /**
37    * Argument types for ADF's
38    */

39   private Class JavaDoc[][] m_argTypes;
40
41   /**
42    * Available GP-functions.
43    */

44   private CommandGene[][] m_nodeSets;
45
46   /**
47    * Minimum depth per each chromosome
48    */

49   private int[] m_minDepths;
50
51   /**
52    * Maximum depth per each chromosome
53    */

54   private int[] m_maxDepths;
55
56   /**
57    * Maximum number of nodes allowed per chromosome (when exceeded program
58    * aborts)
59    */

60   private int m_maxNodes;
61
62   /**
63    * Free to use data object.
64    */

65   private Object JavaDoc m_applicationData;
66
67   public GPProgramBase(GPConfiguration a_conf)
68       throws InvalidConfigurationException {
69     if (a_conf == null) {
70       throw new InvalidConfigurationException("Configuration must not be null!");
71     }
72     m_conf = a_conf;
73   }
74
75   public GPProgramBase(IGPProgram a_prog) throws InvalidConfigurationException {
76     this(a_prog.getGPConfiguration());
77     m_types = a_prog.getTypes();
78     m_argTypes = a_prog.getArgTypes();
79     m_nodeSets = a_prog.getNodeSets();
80     m_maxDepths = a_prog.getMaxDepths();
81     m_minDepths = a_prog.getMinDepths();
82     m_maxNodes = a_prog.getMaxNodes();
83   }
84
85   public GPConfiguration getGPConfiguration() {
86     return m_conf;
87   }
88
89   /**
90    * Compares this entity against the specified object.
91    *
92    * @param a_other the object to compare against
93    * @return true: if the objects are the same, false otherwise
94    *
95    * @author Klaus Meffert
96    * @since 3.0
97    */

98   public boolean equals(Object JavaDoc a_other) {
99     try {
100       return compareTo(a_other) == 0;
101     } catch (ClassCastException JavaDoc cex) {
102       return false;
103     }
104   }
105
106   /**
107    * @return fitness value of this program determined via the registered
108    * fitness function
109    *
110    * @author Klaus Meffert
111    * @since 3.0
112    */

113   public double calcFitnessValue() {
114     GPFitnessFunction normalFitnessFunction = getGPConfiguration().
115         getGPFitnessFunction();
116     if (normalFitnessFunction != null) {
117       // Grab the "normal" fitness function and ask it to calculate our
118
// fitness value.
119
// --------------------------------------------------------------
120
m_fitnessValue = normalFitnessFunction.getFitnessValue(this);
121     }
122     return m_fitnessValue;
123   }
124
125   /**
126    * @return fitness value of this program, cached access
127    *
128    * @author Klaus Meffert
129    * @since 3.0
130    */

131   public double getFitnessValue() {
132     if (m_fitnessValue >= 0.000d) {
133       return m_fitnessValue;
134     }
135     else {
136       return calcFitnessValue();
137     }
138   }
139
140   /**
141    * @return computed fitness value of this program, may be unitialized
142    *
143    * @author Klaus Meffert
144    * @since 3.2
145    */

146   public double getFitnessValueDirectly() {
147     return m_fitnessValue;
148   }
149
150   public void setFitnessValue(double a_fitness) {
151     m_fitnessValue = a_fitness;
152   }
153
154   public void setTypes(Class JavaDoc[] a_types) {
155     m_types = a_types;
156   }
157
158   public Class JavaDoc[] getTypes() {
159     return m_types;
160   }
161
162   public Class JavaDoc getType(int a_index) {
163     return m_types[a_index];
164   }
165
166   public void setArgTypes(Class JavaDoc[][] a_argTypes) {
167     m_argTypes = a_argTypes;
168   }
169
170   public Class JavaDoc[][] getArgTypes() {
171     return m_argTypes;
172   }
173
174   public Class JavaDoc[] getArgType(int a_index) {
175     return m_argTypes[a_index];
176   }
177
178   public void setNodeSets(CommandGene[][] a_nodeSets) {
179     m_nodeSets = a_nodeSets;
180   }
181
182   public CommandGene[][] getNodeSets() {
183     return m_nodeSets;
184   }
185
186   public CommandGene[] getNodeSet(int a_index) {
187     return m_nodeSets[a_index];
188   }
189
190   public void setMaxDepths(int[] a_maxDepths) {
191     m_maxDepths = a_maxDepths;
192   }
193
194   public int[] getMaxDepths() {
195     return m_maxDepths;
196   }
197
198   public void setMinDepths(int[] a_minDepths) {
199     m_minDepths = a_minDepths;
200   }
201
202   public int[] getMinDepths() {
203     return m_minDepths;
204   }
205
206   public void setMaxNodes(int a_maxNodes) {
207     m_maxNodes = a_maxNodes;
208   }
209
210   public int getMaxNodes() {
211     return m_maxNodes;
212   }
213
214   /**
215    * Sets the application data object.
216    *
217    * @param a_data the object to set
218    *
219    * @author Klaus Meffert
220    * @since 3.01
221    */

222   public void setApplicationData(Object JavaDoc a_data) {
223     m_applicationData = a_data;
224   }
225
226   /**
227    * @return the application data object set
228    *
229    * @author Klaus Meffert
230    * @since 3.01
231    */

232   public Object JavaDoc getApplicationData() {
233     return m_applicationData;
234   }
235
236
237   /**
238    * @return deep clone of this instance
239    *
240    * @author Klaus Meffert
241    * @since 3.2
242    */

243   public abstract Object JavaDoc clone();
244 }
245
Popular Tags