KickJava   Java API By Example, From Geeks To Geeks.

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


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.gp.terminal.*;
13 import org.jgap.gp.impl.*;
14 import org.jgap.*;
15
16 /**
17  * Abstract base class for all implementations of IGPChromosome.
18  *
19  * @author Klaus Meffert
20  * @since 3.01
21  */

22 public abstract class BaseGPChromosome
23     implements IGPChromosome {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
26
27   /**
28    * The configuration object to use
29    */

30   private
31   /*transient*/ GPConfiguration m_configuration;
32
33   /**
34    * The individual the chromosome belongs to.
35    */

36   private IGPProgram m_ind;
37
38   public BaseGPChromosome(GPConfiguration a_configuration)
39       throws InvalidConfigurationException {
40     if (a_configuration == null) {
41       throw new InvalidConfigurationException(
42           "Configuration to be set must not"
43           + " be null!");
44     }
45     m_configuration = a_configuration;
46   }
47
48   public BaseGPChromosome(GPConfiguration a_configuration, IGPProgram a_ind)
49       throws InvalidConfigurationException {
50     this(a_configuration);
51     m_ind = a_ind;
52   }
53
54   /**
55    * @return the individual containing this chromosome
56    *
57    * @author Klaus Meffert
58    * @since 3.01 (since 3.0 in ProgramChromosome)
59    */

60   public IGPProgram getIndividual() {
61     return m_ind;
62   }
63
64   /**
65    * Sets the individual the chromosome belongs to.
66    *
67    * @param a_ind the individual containing this chromosome
68    *
69    * @author Klaus Meffert
70    * @since 3.01 (since 3.0 in ProgramChromosome)
71    */

72   public void setIndividual(IGPProgram a_ind) {
73     m_ind = a_ind;
74   }
75
76   /**
77    * Gets the i'th terminal in this chromosome. The nodes are counted in a
78    * depth-first manner, with node 0 being the first terminal in this
79    * chromosome.
80    *
81    * @param a_index the i'th terminal to get
82    * @return the terminal
83    *
84    * @author Klaus Meffert
85    * @since 3.01 (since 3.0 in ProgramChromosome)
86    */

87   public int getTerminal(int a_index) {
88     CommandGene[] functions = getFunctions();
89     int len = functions.length;
90     for (int j = 0; j < len && functions[j] != null; j++) {
91       if (functions[j].getArity(m_ind) == 0) {
92         if (--a_index < 0) {
93           return j;
94         }
95       }
96     }
97     return -1;
98   }
99
100   /**
101    * Gets the a_index'th function in this chromosome. The nodes are counted in a
102    * depth-first manner, with node 0 being the first function in this
103    * chromosome.
104    *
105    * @param a_index the a_index'th function to get
106    * @return the function
107    *
108    * @author Klaus Meffert
109    * @since 3.01 (since 3.0 in ProgramChromosome)
110    */

111   public int getFunction(int a_index) {
112     CommandGene[] functions = getFunctions();
113     int len = functions.length;
114     for (int j = 0; j < len && functions[j] != null; j++) {
115       if (functions[j].getArity(m_ind) != 0) {
116         if (--a_index < 0) {
117           return j;
118         }
119       }
120     }
121     return -1;
122   }
123
124   /**
125    * Gets the a_index'th terminal of the given type in this chromosome. The nodes
126    * are counted in a depth-first manner, with node 0 being the first terminal of
127    * the given type in this chromosome.
128    *
129    * @param a_index the a_index'th terminal to get
130    * @param a_type the type of terminal to get
131    * @param a_subType the subtype to consider
132    * @return the index of the terminal found, or -1 if no appropriate terminal
133    * was found
134    *
135    * @author Klaus Meffert
136    * @since 3.01 (since 3.0 in ProgramChromosome)
137    */

138   public int getTerminal(int a_index, Class JavaDoc a_type, int a_subType) {
139     CommandGene[] functions = getFunctions();
140     int len = functions.length;
141     for (int j = 0; j < len && functions[j] != null; j++) {
142       if ( (a_subType == 0 || functions[j].getSubReturnType() == a_subType)
143           && functions[j].getReturnType() == a_type
144           && functions[j].getArity(m_ind) == 0) {
145         if (--a_index < 0) {
146           return j;
147         }
148       }
149     }
150     return -1;
151   }
152
153   /**
154    * Gets the i'th function of the given return type in this chromosome. The
155    * nodes are counted in a depth-first manner, with node 0 being the first
156    * function of the given type in this chromosome.
157    *
158    * @param a_index the i'th function to get
159    * @param a_type the type of function to get
160    * @param a_subType the subtype to consider
161    * @return the index of the function found, or -1 if no appropriate function
162    * was found
163    *
164    * @author Klaus Meffert
165    * @since 3.01 (since 3.0 in ProgramChromosome)
166    */

167   public int getFunction(int a_index, Class JavaDoc a_type, int a_subType) {
168     CommandGene[] functions = getFunctions();
169     int len = functions.length;
170     for (int j = 0; j < len && functions[j] != null; j++) {
171       if (functions[j].getReturnType() == a_type
172           && (a_subType == 0 || a_subType == functions[j].getSubReturnType())
173           && functions[j].getArity(m_ind) != 0) {
174         if (--a_index < 0) {
175           return j;
176         }
177       }
178     }
179     return -1;
180   }
181
182   /**
183    * @return the number of terminals in this chromosome
184    *
185    * @author Klaus Meffert
186    * @since 3.0
187    */

188   public int numTerminals() {
189     int count = 0;
190     CommandGene[] functions = getFunctions();
191     int len = functions.length;
192     for (int i = 0; i < len && functions[i] != null; i++) {
193       if (functions[i].getArity(m_ind) == 0) {
194         count++;
195       }
196     }
197     return count;
198   }
199
200   /**
201    * @return the number of functions in this chromosome
202    *
203    * @author Klaus Meffert
204    * @since 3.0
205    */

206   public int numFunctions() {
207     int count = 0;
208     CommandGene[] functions = getFunctions();
209     int len = functions.length;
210     for (int i = 0; i < len && functions[i] != null; i++) {
211       if (functions[i].getArity(m_ind) != 0) {
212         count++;
213       }
214     }
215     return count;
216   }
217
218   /**
219    * Counts the number of terminals of the given type in this chromosome.
220    *
221    * @param a_type the type of terminal to count
222    * @param a_subType the subtype to consider
223    * @return the number of terminals in this chromosome
224    *
225    * @author Klaus Meffert
226    * @since 3.01 (since 3.0 in ProgramChromosome)
227    */

228   public int numTerminals(Class JavaDoc a_type, int a_subType) {
229     int count = 0;
230     CommandGene[] functions = getFunctions();
231     int len = functions.length;
232     for (int i = 0; i < len && functions[i] != null; i++) {
233       if (functions[i].getArity(m_ind) == 0
234           && functions[i].getReturnType() == a_type
235           && (a_subType == 0 || functions[i].getSubReturnType() == a_subType)) {
236         count++;
237       }
238     }
239     return count;
240   }
241
242   /**
243    * Counts the number of functions of the given type in this chromosome.
244    *
245    * @param a_type the type of function to count
246    * @param a_subType the subtype to consider
247    * @return the number of functions in this chromosome.
248    *
249    * @author Klaus Meffert
250    * @since 3.01 (since 3.0 in ProgramChromosome)
251    */

252   public int numFunctions(Class JavaDoc a_type, int a_subType) {
253     int count = 0;
254     CommandGene[] functions = getFunctions();
255     int len = functions.length;
256     for (int i = 0; i < len && functions[i] != null; i++) {
257       if (functions[i].getArity(m_ind) != 0
258           && functions[i].getReturnType() == a_type
259           && (a_subType == 0 || functions[i].getSubReturnType() == a_subType)) {
260         count++;
261       }
262     }
263     return count;
264   }
265
266   /**
267    * Gets the a_index'th node in this chromosome. The nodes are counted in a
268    * depth-first manner, with node 0 being the root of this chromosome.
269    *
270    * @param a_index the node number to get
271    * @return the node
272    *
273    * @author Klaus Meffert
274    * @since 3.01 (since 3.0 in ProgramChromosome)
275    */

276   public CommandGene getNode(int a_index) {
277     if (a_index >= getFunctions().length || getFunctions()[a_index] == null) {
278       return null;
279     }
280     return getFunctions()[a_index];
281   }
282
283   /**
284    * Helper: Find GP command with given class and return index of it
285    * @param a_n return the n'th found command
286    * @param a_class the class to find a command for
287    * @return index of first found matching GP command, or -1 if none found
288    *
289    * @author Klaus Meffert
290    * @since 3.01 (since 3.0 in ProgramChromosome)
291    */

292   public int getCommandOfClass(int a_n, Class JavaDoc a_class) {
293     CommandGene[] functions = getFunctions();
294     int len = functions.length;
295     for (int j = 0; j < len && functions[j] != null; j++) {
296       if (functions[j].getClass() == a_class) {
297         if (--a_n < 0) {
298           return j;
299         }
300       }
301     }
302     return -1;
303   }
304
305   /**
306    * Helper: Find GP Variable with given return type and return index of it
307    * @param a_n return the n'th found command
308    * @param a_returnType the return type to find a Variable for
309    * @return index of first found matching GP command, or -1 if none found
310    *
311    * @author Klaus Meffert
312    * @since 3.01 (since 3.0 in ProgramChromosome)
313    */

314   public int getVariableWithReturnType(int a_n, Class JavaDoc a_returnType) {
315     CommandGene[] functions = getFunctions();
316     int len = functions.length;
317     for (int j = 0; j < len && functions[j] != null; j++) {
318       if (functions[j].getClass() == Variable.class) {
319         Variable v = (Variable) functions[j];
320         if (v.getReturnType() == a_returnType) {
321           if (--a_n < 0) {
322             return j;
323           }
324         }
325       }
326     }
327     return -1;
328   }
329
330   public GPConfiguration getGPConfiguration() {
331     return m_configuration;
332   }
333
334 }
335
Popular Tags