GP question: Ifelse never included in program

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

GP question: Ifelse never included in program

Johannes Göbel
Dear JGAP-users,

my target is creating math expressions using GP
(similar to examples.GP.MathProblem); however, I need
the ability to create piecewise functions like

   f(q,g,n) = 3*g    if n > 0
   f(q,g,n) = q      otherwise,

e.g. based on the IfElse-command gene.

Quoted below, you find my code - it runs without errors,
but the programs generated do *NEVER* contain an Ifelse.

Can someone please tell me what I am doing wrong?
Thank you very much in advance!

Best regards!
- Johannes


import org.jgap.*;

public class GP extends GPProblem {

    /** Variables */
    public static Variable v_Q;
    public static Variable v_G;
    public static Variable v_N;

     * Instantiates a new problem.
    public GP(GPConfiguration a_conf) throws InvalidConfigurationException {

     * This method is used for setting up the commands and terminals that can be
     * used to solve the problem.
    public GPGenotype create() throws InvalidConfigurationException {
        GPConfiguration conf = getGPConfiguration();
        Class<?>[] types = {CommandGene.FloatClass};
        Class<?>[][] argTypes = {{}};
        CommandGene[][] nodeSets = {{

            v_Q = Variable.create(conf, "Q", CommandGene.FloatClass),
            v_G = Variable.create(conf, "G", CommandGene.FloatClass),
            v_N = Variable.create(conf, "N", CommandGene.FloatClass),

            new Add(conf, CommandGene.FloatClass),
            new Subtract(conf, CommandGene.FloatClass),
            new Multiply(conf, CommandGene.FloatClass),
            new Divide(conf, CommandGene.FloatClass),
            new Exp(conf, CommandGene.FloatClass),
            new Log(conf, CommandGene.FloatClass),
            new Pow(conf, CommandGene.FloatClass),
            new Terminal(conf, CommandGene.FloatClass, -100.0d, 100.0d, true),
            new IfElse(conf, CommandGene.BooleanClass),
            new Not(conf),
            new Or(conf),
            new And(conf),
            new GreaterThan(conf, CommandGene.FloatClass)


        return GPGenotype.randomInitialGenotype
           (conf, types, argTypes, nodeSets, 20, true);

    public static void main(String[] args) throws Exception {
        GPConfiguration config = new GPConfiguration();
        config.setGPFitnessEvaluator(new DefaultGPFitnessEvaluator());
        config.setFitnessFunction(new GP.FitnessFunction());
        GPProblem problem = new GP(config);
        GPGenotype gp = problem.create();

        for (int g = 9; g < 100; g = g + 10) {
            System.out.println("Best after " + (g+1) + " evolutions:");
            System.out.println("- Program: " + gp.getAllTimeBest().toStringNorm(0));
            System.out.println("- Fitness: " + gp.getAllTimeBest().getFitnessValue());

     * Fitness function.
    public static class FitnessFunction extends GPFitnessFunction {

        public double evaluate(final IGPProgram a_subject) {
            try {

                double result = a_subject.execute_float(0, new Object[0]);
                return Math.max(0, result);

            } catch (ArithmeticException ex) {

                throw ex;

Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
jgap-users mailing list
[hidden email]