1 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 27 public class MathProblem 28 extends GPProblem { 29 30 private final static String CVS_REVISION = "$Revision: 1.15 $"; 31 32 static Variable vx; 33 34 static Float [] x = new Float [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 [] types = { 47 CommandGene.FloatClass}; 48 Class [][] 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 for (int i = 0; i < 20; i++) { 68 float f = 8.0f * (random.nextFloat() - 0.3f); 69 x[i] = new Float (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 return GPGenotype.randomInitialGenotype(conf, types, argTypes, nodeSets, 76 20, true); 77 } 78 79 88 public static void main(String [] args) 89 throws Exception { 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 [] noargs = new Object [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 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 |