KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > gp > function > SubProgram


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.function;
11
12 import org.jgap.*;
13 import org.jgap.gp.*;
14 import org.apache.commons.lang.builder.*;
15 import org.jgap.gp.impl.*;
16 import org.jgap.util.*;
17
18 /**
19  * A connector for independent subprograms (subtrees). Each subtree except the
20  * last one must have a memory- or stack-modifying command (such as push or
21  * store), otherwise there is no connection between the subtrees (which would
22  * be useless bloating).
23  *
24  * @author Klaus Meffert
25  * @since 3.0
26  */

27 public class SubProgram
28     extends CommandGene implements ICloneable {
29   /** String containing the CVS revision. Read out via reflection!*/
30   private final static String JavaDoc CVS_REVISION = "$Revision: 1.12 $";
31
32   /**
33    * Number of subprograms. Redundant, because equal to m_types.length.
34    */

35   private int m_subtrees;
36
37   /**
38    * Return types of the subprograms to excecute.
39    */

40   private Class JavaDoc[] m_types;
41
42   public SubProgram(final GPConfiguration a_conf, Class JavaDoc[] a_types)
43       throws InvalidConfigurationException {
44     this(a_conf, a_types, 0, null);
45   }
46
47   public SubProgram(final GPConfiguration a_conf, Class JavaDoc[] a_types,
48                     int a_subReturnType, int[] a_subChildTypes)
49       throws InvalidConfigurationException {
50     super(a_conf, a_types.length, a_types[a_types.length - 1], a_subReturnType,
51           a_subChildTypes);
52     if (a_types.length < 1) {
53       throw new IllegalArgumentException JavaDoc("Number of subtrees must be >= 1");
54     }
55     m_types = a_types;
56     m_subtrees = a_types.length;
57   }
58
59   public String JavaDoc toString() {
60     String JavaDoc ret = "sub[";
61     for (int i = 1; i < m_subtrees; i++) {
62       ret += "&" + i + " --> ";
63     }
64     ret += "&" + m_subtrees + "]";
65     return ret;
66   }
67
68   /**
69    * @return textual name of this command
70    *
71    * @author Klaus Meffert
72    * @since 3.2
73    */

74   public String JavaDoc getName() {
75     return "Sub program";
76   }
77
78   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
79     check(c);
80     int value = -1;
81     for (int i = 0; i < m_subtrees; i++) {
82       if (i < m_subtrees - 1) {
83         c.execute_void(n, i, args);
84       }
85       else {
86         value = c.execute_int(n, i, args); /**@todo evaluate m_types*/
87       }
88 // if (i < m_subtrees - 1) {
89
// ( (GPConfiguration) getConfiguration()).storeThruput(i,
90
// new Integer(value));
91
// }
92
}
93     return value;
94   }
95
96   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
97     check(c);
98     for (int i = 0; i < m_subtrees; i++) {
99       c.execute_void(n, i, args); /**@todo evaluate m_types*/
100     }
101   }
102
103   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
104     check(c);
105     long value = -1;
106     for (int i = 0; i < m_subtrees; i++) {
107       value = c.execute_long(n, i, args);
108     }
109     return value;
110   }
111
112   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
113     check(c);
114     float value = -1;
115     for (int i = 0; i < m_subtrees; i++) {
116       value = c.execute_float(n, i, args);
117     }
118     return value;
119   }
120
121   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
122     check(c);
123     double value = -1;
124     for (int i = 0; i < m_subtrees; i++) {
125       value = c.execute_double(n, i, args);
126     }
127     return value;
128   }
129
130   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
131     check(c);
132     Object JavaDoc value = null;
133     for (int i = 0; i < m_subtrees; i++) {
134       value = c.execute_object(n, i, args);
135     }
136     return value;
137   }
138
139   public boolean isValid(ProgramChromosome a_program) {
140     return true;
141   }
142
143   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
144     return m_types[a_chromNum];
145   }
146
147   /**
148    * The compareTo-method.
149    *
150    * @param a_other the other object to compare
151    * @return -1, 0, 1
152    *
153    * @author Klaus Meffert
154    * @since 3.0
155    */

156   public int compareTo(Object JavaDoc a_other) {
157     if (a_other == null) {
158       return 1;
159     }
160     else {
161       SubProgram other = (SubProgram) a_other;
162       return new CompareToBuilder()
163           .append(m_types, other.m_types)
164           .toComparison();
165     }
166   }
167
168   /**
169    * The equals-method.
170    *
171    * @param a_other the other object to compare
172    * @return true if the objects are seen as equal
173    *
174    * @author Klaus Meffert
175    * @since 3.0
176    */

177   public boolean equals(Object JavaDoc a_other) {
178     if (a_other == null) {
179       return false;
180     }
181     else {
182       try {
183         SubProgram other = (SubProgram) a_other;
184         return new EqualsBuilder()
185             .append(m_types, other.m_types)
186             .isEquals();
187       } catch (ClassCastException JavaDoc cex) {
188         return false;
189       }
190     }
191   }
192
193   /**
194    * @return deep clone of this instance
195    *
196    * @author Klaus Meffert
197    * @since 3.2
198    */

199   public Object JavaDoc clone() {
200     try {
201       int[] subChildTypes = getSubChildTypes();
202       if (subChildTypes != null) {
203         subChildTypes = (int[])subChildTypes.clone();
204       }
205       SubProgram result = new SubProgram(getGPConfiguration(), m_types,
206           getSubReturnType(), subChildTypes);
207       result.m_subtrees = m_subtrees;
208       result.m_types = (Class JavaDoc[])m_types.clone();
209       return result;
210     } catch (Throwable JavaDoc t) {
211       throw new CloneException(t);
212     }
213   }
214 }
215
Popular Tags