KickJava   Java API By Example, From Geeks To Geeks.

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


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 power operation.
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class Pow
23     extends MathCommand {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
26
27   public Pow(final GPConfiguration a_conf, Class JavaDoc a_type)
28       throws InvalidConfigurationException {
29     super(a_conf, 2, a_type);
30   }
31
32   public String JavaDoc toString() {
33     return "&1 ^ &2";
34   }
35
36   /**
37    * @return textual name of this command
38    *
39    * @author Klaus Meffert
40    * @since 3.2
41    */

42   public String JavaDoc getName() {
43     return "Power";
44   }
45
46   public int execute_int(ProgramChromosome c, int n, Object JavaDoc[] args) {
47     int i = c.execute_int(n, 0, args);
48     int j = c.execute_int(n, 1, args);
49     // clip to -10000 -> 20
50
return (int) Math.pow(Math.max( -10000.0f, Math.min(i, 20.0f)),
51                           Math.max( -10000.0f, Math.min(j, 20.0f)));
52   }
53
54   public float execute_float(ProgramChromosome c, int n, Object JavaDoc[] args) {
55     float f = c.execute_float(n, 0, args);
56     float g = c.execute_float(n, 1, args);
57     // clip to -10000 -> 20
58
return (float) Math.pow(Math.max( -10000.0f, Math.min(f, 20.0f)),
59                             Math.max( -10000.0f, Math.min(g, 20.0f)));
60   }
61
62   public double execute_double(ProgramChromosome c, int n, Object JavaDoc[] args) {
63     double f = c.execute_double(n, 0, args);
64     double g = c.execute_double(n, 1, args);
65     // clip to -10000 -> 20
66
return Math.pow(Math.max( -10000.0, Math.min(f, 20.0)),
67                     Math.max( -10000.0, Math.min(g, 20.0)));
68   }
69
70   public Object JavaDoc execute_object(ProgramChromosome c, int n, Object JavaDoc[] args) {
71     return ( (Compatible) c.execute_object(n, 0, args)).execute_pow(
72         c.execute_object(n, 1, args));
73   }
74
75   protected interface Compatible {
76     public Object JavaDoc execute_pow(Object JavaDoc o);
77   }
78 }
79
Popular Tags