KickJava   Java API By Example, From Geeks To Geeks.

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


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.jgap.gp.impl.*;
15
16 /**
17  * The add operation.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class Add
23     extends MathCommand implements IMutateable {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
26
27   public Add(final GPConfiguration a_conf, Class JavaDoc type)
28       throws InvalidConfigurationException {
29     super(a_conf, 2, type);
30   }
31
32   public CommandGene applyMutation(int index, double a_percentage)
33       throws InvalidConfigurationException {
34     Subtract mutant = new Subtract(getGPConfiguration(), getReturnType());
35     return mutant;
36   }
37
38   public String JavaDoc toString() {
39     return "&1 + &2";
40   }
41
42   /**
43    * @return textual name of this command
44    *
45    * @author Klaus Meffert
46    * @since 3.2
47    */

48   public String JavaDoc getName() {
49     return "Add";
50   }
51
52   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
53     return c.execute_int(n, 0, args) + c.execute_int(n, 1, args);
54   }
55
56   public long execute_long(ProgramChromosome c, int n, Object JavaDoc[] args) {
57     return c.execute_long(n, 0, args) + c.execute_long(n, 1, args);
58   }
59
60   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
61     return c.execute_float(n, 0, args) + c.execute_float(n, 1, args);
62   }
63
64   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
65     return c.execute_double(n, 0, args) + c.execute_double(n, 1, args);
66   }
67
68   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
69     return ( (Compatible) c.execute_object(n, 0, args)).execute_add(
70         c.execute_object(n, 1, args));
71   }
72
73   protected interface Compatible {
74     public Object JavaDoc execute_add(Object JavaDoc o);
75   }
76 }
77
Popular Tags