KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > MathProblem


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 examples.gp;
11
12 import java.util.*;
13
14 import org.jgap.*;
15 import org.jgap.gp.*;
16 import org.jgap.gp.function.*;
17 import org.jgap.gp.impl.*;
18 import org.jgap.gp.terminal.*;
19
20 /**
21  * Example demonstrating Genetic Programming (GP) capabilities of JGAP.<p>
22  * The problem is to find a formula for a given truth table (X/Y-pairs).
23  *
24  * @author Klaus Meffert
25  * @since 3.0
26  */

27 public class MathProblem
28     extends GPProblem {
29   /** String containing the CVS revision. Read out via reflection!*/
30   private final static String JavaDoc CVS_REVISION = "$Revision: 1.15 $";
31
32   static Variable vx;
33
34   static Float JavaDoc[] x = new Float JavaDoc[20];
35
36   static float[] y = new float[20];
37
38   public MathProblem(GPConfiguration a_conf)
39       throws InvalidConfigurationException {
40     super(a_conf);
41   }
42
43   public GPGenotype create()
44       throws InvalidConfigurationException {
45     GPConfiguration conf = getGPConfiguration();
46     Class JavaDoc[] types = {
47         CommandGene.FloatClass};
48     Class JavaDoc[][] argTypes = { {}
49     };
50     CommandGene[][] nodeSets = { {
51         vx = Variable.create(conf, "X", CommandGene.FloatClass),
52         new Add(conf, CommandGene.FloatClass),
53         new Add3(conf, CommandGene.FloatClass),
54         new Subtract(conf, CommandGene.FloatClass),
55         new Multiply(conf, CommandGene.FloatClass),
56         new Multiply3(conf, CommandGene.FloatClass),
57         new Divide(conf, CommandGene.FloatClass),
58         new Sine(conf, CommandGene.FloatClass),
59         new Exp(conf, CommandGene.FloatClass),
60         new Pow(conf, CommandGene.FloatClass),
61         new Terminal(conf, CommandGene.FloatClass, 2.0d, 10.0d, true),
62     }
63     };
64     Random random = new Random();
65     // Randomly initialize function data (X-Y table) for x^4+x^3+x^2-x
66
// ---------------------------------------------------------------
67
for (int i = 0; i < 20; i++) {
68       float f = 8.0f * (random.nextFloat() - 0.3f);
69       x[i] = new Float JavaDoc(f);
70       y[i] = f * f * f * f + f * f * f + f * f - f;
71       System.out.println(i + ") " + x[i] + " " + y[i]);
72     }
73     // Create genotype with initial population.
74
// ----------------------------------------
75
return GPGenotype.randomInitialGenotype(conf, types, argTypes, nodeSets,
76         20, true);
77   }
78
79   /**
80    * Starts the example.
81    *
82    * @param args ignored
83    * @throws Exception
84    *
85    * @author Klaus Meffert
86    * @since 3.0
87    */

88   public static void main(String JavaDoc[] args)
89       throws Exception JavaDoc {
90     System.out.println("Formula to discover: X^4 + X^3 + X^2 - X");
91     GPConfiguration config = new GPConfiguration();
92     config.setGPFitnessEvaluator(new DeltaGPFitnessEvaluator());
93     config.setMaxInitDepth(4);
94     config.setPopulationSize(1000);
95     config.setMaxCrossoverDepth(8);
96     config.setFitnessFunction(new MathProblem.FormulaFitnessFunction());
97     config.setStrictProgramCreation(true);
98     GPProblem problem = new MathProblem(config);
99     GPGenotype gp = problem.create();
100     gp.setVerboseOutput(true);
101     gp.evolve(800);
102     gp.outputSolution(gp.getAllTimeBest());
103     problem.showTree(gp.getAllTimeBest(), "mathproblem_best.png");
104   }
105
106   public static class FormulaFitnessFunction
107       extends GPFitnessFunction {
108     protected double evaluate(final IGPProgram a_subject) {
109       return computeRawFitness(a_subject);
110     }
111
112     public double computeRawFitness(final IGPProgram ind) {
113       double error = 0.0f;
114       Object JavaDoc[] noargs = new Object JavaDoc[0];
115       for (int i = 0; i < 20; i++) {
116         vx.set(x[i]);
117         try {
118           double result = ind.execute_float(0, noargs);
119           error += Math.abs(result - y[i]);
120         } catch (ArithmeticException JavaDoc ex) {
121           System.out.println("x = " + x[i].floatValue());
122           System.out.println(ind);
123           throw ex;
124         }
125       }
126       if (error < 0.000001) {
127         error = 0.0d;
128       }
129       return error;
130     }
131   }
132 }
133
Popular Tags