Genetic Programming with JGAP

[Documentation Index]


Genetic Programming (GP) is supposed to be introduced by John Koza. GP is sort of different to Genetic Algorithms (GA) as GP is more complex than GA. GP is about evolving programs with a given set of commands. Each command is assigned a number of parameters. That way, programs can be constructed. They are represented internally as a tree. But you do not have to cope with the internal representation. For you it is only important to know that you can write sort of programs. Each program contains only those commands and terminals (such as numbers or Boolean values) that you define to be used! It is even possible evolving full-blown Java programs (see the Robocode example).

GP was introduced with JGAP 3.0. This document presents some useful information about how to utilize JGAP for GP.

JGAP's Philosophy

The main philosophy that was setup before extending JGAP towards GP was to reach the goal by adding parts to GA to get GP. Thus: GA + x =GP, with x being the added parts.

A first start

To get a feeling of how to implement your GP-Program with JGAP, please have a look at the example located in class examples.gp.MathProblem. This example tries to find a formula for a given truth table (X/Y value pairs) and a given set of operators (such as +, -, * etc). The mentioned example class also contains a fitness function named FormulaFitnessFunction. A more complex example can be found in class examples.gp.Fibonacci and in class examples.gp.anttrail.AntTrailProblem.

The math example at a glance:

1. Create configuration

/**
 * Starts the example
 * @param args ignored
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
  GPConfiguration config = new GPConfiguration();
  config.setGPFitnessEvaluator(new DeltaGPFitnessEvaluator());
  config.setMaxInitDepth(6);
  config.setPopulationSize(100);
  config.setFitnessFunction(new MathProblem.FormulaFitnessFunction());
  ... // continued below

 

2. Create initial Genotype (continued from above method main):

  GPGenotype gp = create(config);
}


public static GPGenotype create(GPConfiguration a_conf) throws InvalidConfigurationException {
  Class[] types = {
    CommandGene.FloatClass};
  Class[][] argTypes = {
    {}
  };
  // Define the commands and terminals the GP is allowed to use.
  // -----------------------------------------------------------
  CommandGene[][] nodeSets = {
    {
    vx = Variable.create(a_conf, "X", CommandGene.FloatClass),
    new AddCommand(a_conf, CommandGene.FloatClass),
    new SubtractCommand(a_conf, CommandGene.FloatClass),
    new MultiplyCommand(a_conf, CommandGene.FloatClass),
    new DivideCommand(a_conf, CommandGene.FloatClass),
    new SinCommand(a_conf, CommandGene.FloatClass),
    new CosCommand(a_conf, CommandGene.FloatClass),
    new ExpCommand(a_conf, CommandGene.FloatClass),
    // Use terminal with possible value from 2.0 to 10.0 decimal
    new Terminal(conf, CommandGene.FloatClass, 2.0d, 10.0d, false),
    }
  };
  Random random = new Random();
  // Randomly initialize function data (X-Y table) for x^4+x^3+x^2+x.
  // This is problem-specific and not necessary in other cases.
  // ----------------------------------------------------------------
  for (int i = 0; i < 20; i++) {
    float f = 2.0f * (random.nextFloat() - 0.5f);
    x[i] = new Float(f);
    y[i] = f * f * f * f + f * f * f + f * f - f;
    System.out.println(i + ") " + x[i] + " " + y[i]);
  }
  // Create genotype with initial population.
  // Allow max. 100 nodes within one program.
  // ----------------------------------------
  return randomInitialGenotype(a_conf, types, argTypes, nodeSets, 100, true);
}

3. Start the evolution and output the result: (continued in above method main): 

// Do 100 evolutions in a row.
// ---------------------------
  gp.evolve(100);
// Output best solution found.
// ---------------------------
gp.outputSolution(gp.getAllTimeBest());

} // end of method "main"

4. Optional: Implement your own functions and terminals

Please have a look at packages org.jgap.gp.function, org.jgap.gp.terminal and examples.gp.anttrail for available GP functions and terminals. There you can see how to implement your own classes and use them in the above step 2 during setup.

You can benefit from looking at the examples mentioned above. Go for the code and you'll get a better feeling. It's very easy using JGAP for GP!


[Documentation Index]
SourceForge Logo