KickJava   Java API By Example, From Geeks To Geeks.

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


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.CompareToBuilder;
15 import org.apache.commons.lang.builder.EqualsBuilder;
16 import org.jgap.gp.impl.*;
17
18 /**
19  * Automatically Defined Function (ADF). Works with output of other chromosomes.
20  *
21  * @author Klaus Meffert
22  * @since 3.0
23  */

24 public class ADF
25     extends CommandGene {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
28
29   private int m_chromosomeNum;
30
31   /**
32    * Constructor.
33    *
34    * @param a_conf te configuration to use
35    * @param a_chromosomeNum the index of the chromosome to execute
36    * @throws InvalidConfigurationException
37    *
38    * @author Klaus Meffert
39    * @since 3.0
40    */

41   public ADF(final GPConfiguration a_conf, int a_chromosomeNum)
42       throws InvalidConfigurationException {
43     super(a_conf, 0, null);
44     m_chromosomeNum = a_chromosomeNum;
45   }
46
47   /**
48    * @return the index of the chromosome to execute
49    *
50    * @author Klaus Meffert
51    * @since 3.0
52    */

53   public int getChromosomeNum() {
54     return m_chromosomeNum;
55   }
56
57   public String JavaDoc toString() {
58     return "ADF(" + m_chromosomeNum + ")";
59   }
60
61   public int getArity(IGPProgram a_individual) {
62     return a_individual.getChromosome(m_chromosomeNum).getArity();
63   }
64
65   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
66     check(c);
67     int numargs = c.getIndividual().getChromosome(m_chromosomeNum).getArity();
68     Object JavaDoc[] vals = new Object JavaDoc[numargs];
69     for (int i = 0; i < numargs; i++) {
70       vals[i] = new Integer JavaDoc(c.execute_int(n, i, args));
71     }
72     // Call the chromosome.
73
// --------------------
74
return c.getIndividual().execute_int(m_chromosomeNum, vals);
75   }
76
77   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
78     check(c);
79     int numargs = c.getIndividual().getChromosome(m_chromosomeNum).getArity();
80     Object JavaDoc[] vals = new Object JavaDoc[numargs];
81     for (int i = 0; i < numargs; i++) {
82       vals[i] = new Float JavaDoc(c.execute_float(n, i, args));
83     }
84     return c.getIndividual().execute_float(m_chromosomeNum, vals);
85   }
86
87   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
88     check(c);
89     int numargs = c.getIndividual().getChromosome(m_chromosomeNum).getArity();
90     Object JavaDoc[] vals = new Object JavaDoc[numargs];
91     for (int i = 0; i < numargs; i++) {
92       vals[i] = new Double JavaDoc(c.execute_double(n, i, args));
93     }
94     return c.getIndividual().execute_double(m_chromosomeNum, vals);
95   }
96
97   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
98     check(c);
99     int numargs = c.getIndividual().getChromosome(m_chromosomeNum).getArity();
100     Object JavaDoc[] vals = new Object JavaDoc[numargs];
101     for (int i = 0; i < numargs; i++) {
102       vals[i] = c.execute(n, i, args);
103     }
104     return c.getIndividual().execute_object(m_chromosomeNum, vals);
105   }
106
107   public Class JavaDoc getChildType(IGPProgram a_ind, int i) {
108     return a_ind.getChromosome(m_chromosomeNum).getArgTypes()[i];
109   }
110
111   public boolean isValid(ProgramChromosome a_chrom) {
112     // Avoid endless recursion.
113
// ------------------------
114
StackTraceElement JavaDoc[] stack = new Exception JavaDoc().getStackTrace();
115     if (stack.length > 60) { /**@todo enhance*/
116       return false;
117     }
118     return true;
119   }
120
121   /**
122    * The compareTo-method.
123    *
124    * @param a_other the other object to compare
125    * @return -1, 0, 1
126    *
127    * @author Klaus Meffert
128    * @since 3.0
129    */

130   public int compareTo(Object JavaDoc a_other) {
131     if (a_other == null) {
132       return 1;
133     }
134     else {
135       ADF other = (ADF) a_other;
136       return new CompareToBuilder()
137           .append(m_chromosomeNum, other.m_chromosomeNum)
138           .toComparison();
139     }
140   }
141
142   /**
143    * The equals-method.
144    *
145    * @param a_other the other object to compare
146    * @return true if the objects are seen as equal
147    *
148    * @author Klaus Meffert
149    * @since 3.0
150    */

151   public boolean equals(Object JavaDoc a_other) {
152     if (a_other == null) {
153       return false;
154     }
155     else {
156       try {
157         ADF other = (ADF) a_other;
158         return new EqualsBuilder()
159             .append(m_chromosomeNum, other.m_chromosomeNum)
160             .isEquals();
161       } catch (ClassCastException JavaDoc cex) {
162         return false;
163       }
164     }
165   }
166 }
167
Popular Tags