Quantcast

Re: GP question: Ifelse never included in program

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

Re: GP question: Ifelse never included in program

Johannes Göbel
Klaus,

thanks once again for your advice!

By attachment you find my proposal for an implementation of an
case switch operator mimicking Java's "?" (code based on the
Ifelse-command, as recommended):

"Switch" has three children, first ("condition") must be able to
return a boolean, result is fetched from the appropriate
execute-method of the second child if the condition is true,
otherwise from third child.

As far as I can see, it works fine!
(My programs now do contain instances of this "case switch" operator
and they are evaluated correctly as far as I can see.)

Nevertheless, any corrections, if necessary, are highly appreciated!

Best! Merry Xmas!
- Johannes


---

Am Thursday, December 16, 2010, 3:19:00 PM schrieb Klaus:

> Johannes,

> well, I think your assumption makes sense: But you can create an operator
> (=command) as you like.
> So "simply" take the IfElse class, copy & paste it, and adapt it so that it
> has return type float (and in getChildType() make it float for the second
> child instead of void, too). Maybe this works, at least this is the way it
> should, maybe there will be some details to take care of.

> In case you won't get it, please send me the source of your final try (and
> allow me some days because of XMAS).

> Best

> Klaus
> www.klaus-meffert.com

>  

>> -----Original Message-----
>> From: Johannes Göbel [mailto:[hidden email]]
>> Sent: Thursday, December 16, 2010 3:03 PM
>> To: Klaus Meffert
>> Subject: Re: [jgap-users] GP question: Ifelse never included
>> in program
>>
>> Klaus,
>>
>> thanks for your detailed reply!
>>
>> Since I do *not* want to create an AntTrail-like programm
>> (sequence of commands returning void), but a function
>> returning a float, similar to the "?"-operator in java...
>>
>>    condition ? value1 : value2;
>>    [always returning a float - which one depends on the condition]
>>
>> ...I understand that IfElse does not serve my purpose!
>>
>> Am I right in observing that such an "?"-operator does not
>> exist in JGAP?
>>
>> Best!
>> - Johannes
>>
>>
>> ---
>>
>> Am Thursday, December 16, 2010, 2:45:19 PM schrieb Klaus:
>>
>> > Johannes,
>>
>> > the reason why IfElse is never used is as follows:
>>
>> > You declared the GP program to be evolved (more exatc: the one and
>> > only
>> > ProgramChromosome) to have the type Float, see this line in
>> your code.
>> >         Class<?>[] types = {CommandGene.FloatClass};
>>
>> > But IfElse has return type Void:
>> >   public IfElse(final GPConfiguration a_conf, Class a_type, int
>> > a_subReturnType,
>> >                 int[] a_subChildTypes)
>> >       throws InvalidConfigurationException {
>> >     super(a_conf, 3, CommandGene.VoidClass, a_subReturnType,
>> > a_subChildTypes);
>> >     m_type = a_type;
>> >   }
>>
>> > See source of IfElse class, third argument in the super-call.
>>
>> > Besides, the second child of IfElse (that is the command to be
>> > executed when the if-condition holds true) also must have
>> void type,
>> > see source code of same class:
>>
>> >   public Class getChildType(IGPProgram a_ind, int a_chromNum) {
>> >     if (a_chromNum == 0) {
>> >       return m_type;
>> >     }
>> >     return CommandGene.VoidClass;
>> >   }
>>
>> > Means: The first child of IfElse is the condition, which
>> has boolean
>> > type in your case. The second type is Void.
>>
>> > For a hint on how to construct programs with Void return
>> type, see the
>> > GP examples, e.g. Fibonacci, AntTrail or TictacToeMain (the latter
>> > does not produce good results, but the setup may give some clues).
>>
>> > Best
>>
>> > Klaus
>> > www.klaus-meffert.com
>>
>> >  
>>
>> >> -----Original Message-----
>> >> From: Johannes Göbel [mailto:[hidden email]]
>> >> Sent: Tuesday, November 30, 2010 10:33 AM
>> >> To: [hidden email]
>> >> Subject: [jgap-users] GP question: Ifelse never included in program
>> >>
>> >> 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.*;
>> >> import org.jgap.gp.*;
>> >> import org.jgap.gp.function.*;
>> >> import org.jgap.gp.impl.*;
>> >> import org.jgap.gp.terminal.*;
>> >>
>> >> 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 {
>> >>         super(a_conf);
>> >>     }
>> >>
>> >>     /**
>> >>      * 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.setMaxInitDepth(4);
>> >>         config.setPopulationSize(1000);
>> >>         config.setMaxCrossoverDepth(8);
>> >>         config.setFitnessFunction(new GP.FitnessFunction());
>> >>         config.setStrictProgramCreation(true);
>> >>         GPProblem problem = new GP(config);
>> >>         GPGenotype gp = problem.create();
>> >>         gp.setVerboseOutput(true);
>> >>
>> >>         for (int g = 9; g < 100; g = g + 10) {
>> >>            
>> >>             gp.evolve(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) {
>> >>            
>> >>             v_Q.set(...);
>> >>             v_G.set(...);
>> >>             v_N.set(...);
>> >>            
>> >>             try {
>> >>
>> >>                 double result = a_subject.execute_float(0, new
>> >> Object[0]);
>> >>                 return Math.max(0, result);
>> >>
>> >>             } catch (ArithmeticException ex) {
>> >>
>> >>                 System.out.println(a_subject);
>> >>                 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.
>> >> http://p.sf.net/sfu/lotusphere-d2d
>> >> _______________________________________________
>> >> jgap-users mailing list
>> >> [hidden email]
>> >> https://lists.sourceforge.net/lists/listinfo/jgap-users
>>
------------------------------------------------------------------------------
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.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users

Switch.java (6K) Download Attachment
Loading...