Supergene isValid not working as expected ?

View: New views
4 Messages — Rating Filter:   Alert me  

Supergene isValid not working as expected ?

by Guido Garcia Bernardo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am trying my first JGAP application.  It is just a simple Supergene
composed by a pair of DoubleGene.  The supergene should be valid when
the value of the first one (min) is lower than the value of the second
one (max).

The FitnessFunction returns the difference between the two genes
conforming the supergene (that is max - min).  It should be always
positive, as the isValid method only return true when the max is
greater than the min.

But in fact it fails, showing the following error message and stack trace :

Exception in thread "main" java.lang.RuntimeException: Fitness values
must be positive! Received value: -486.6778986396248
       at org.jgap.FitnessFunction.getFitnessValue(FitnessFunction.java:68)
       at org.jgap.Chromosome.calcFitnessValue(Chromosome.java:479)
       at org.jgap.Chromosome.getFitnessValue(Chromosome.java:450)
       at org.jgap.impl.GABreeder.updateChromosomes(GABreeder.java:204)
       at org.jgap.impl.GABreeder.evolve(GABreeder.java:76)
       at org.jgap.Genotype.evolve(Genotype.java:225)
       at test.LimitSupergene.main

I suppose I am missing something.  Anyone knows why it is not working
as expected ?
I am using Java 5 and JGAP 3.3.3.

Thank you very much,
Guido

PS. My code is below, just in case it helps:

public class LimitSupergene extends AbstractSupergene {

       public LimitSupergene() throws InvalidConfigurationException {
               super();
       }

       public LimitSupergene(Configuration a_conf) throws
InvalidConfigurationException {
               super(a_conf);
       }

       public LimitSupergene(Configuration a_conf, Gene[] a_genes)
throws InvalidConfigurationException {
               super(a_conf, a_genes);
       }

       @Override
   public boolean isValid(Gene[] genes, Supergene s)
   {
        DoubleGene min = (DoubleGene) genes[0];
        DoubleGene max = (DoubleGene) genes[1];

        return min.doubleValue() < max.doubleValue();
   }

   public static void main(String[] args) throws InvalidConfigurationException {
       FitnessFunction function = new FitnessFunction() {
               @Override
               protected double evaluate(IChromosome chromosome) {
                       LimitSupergene lg = (LimitSupergene)
chromosome.getGene(0);
                       double diff = // max - min
                               (Double) lg.getGenes()[1].getAllele() -
                               (Double) lg.getGenes()[0].getAllele();

                       return diff;
               }
       };

       Configuration conf = new DefaultConfiguration();
       conf.setFitnessFunction(function);

       Gene[] sampleGenes = new Gene[1];
       sampleGenes[0] = new LimitSupergene(
                       conf,
                       new Gene[] {
                                       new DoubleGene( conf, 1.0,
1000.0 ), // min
                                       new DoubleGene( conf, 1.0,
1000.0 ) // max
                       }
       );

       IChromosome sampleChromosome = new Chromosome(conf, sampleGenes);
               conf.setSampleChromosome( sampleChromosome );
               conf.setPopulationSize( 100 );

               Genotype population = Genotype.randomInitialGenotype( conf );
               for (int i=0; i<10; i++) {
                       population.evolve();
               }

               IChromosome sol = population.getFittestChromosome();

               LimitSupergene lg = (LimitSupergene) sol.getGene(0);
               System.out.println( "min = " + lg.getGenes()[0].getAllele() );
               System.out.println( "max = " + lg.getGenes()[1].getAllele() );
       }
}

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
jgap-users mailing list
jgap-users@...
https://lists.sourceforge.net/lists/listinfo/jgap-users

Re: Supergene isValid not working as expected ?

by Klaus Meffert-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Guido,

I cannot remember the exact details about SuperGene. But to my knowledge it
could be that in cases where too many invalid solutions i a row came up, a
following invalid solution may be passed through. That means, please care in
your fitness function that only positive values are returned. If you would
get a negative fitness value, return the worst positive number possible in
your scenario (e.g. Double.MAX_VALUE).

Best

Klaus
www.klaus-meffert.com

 

> -----Original Message-----
> From: jgap-users-bounces@...
> [mailto:jgap-users-bounces@...] On Behalf
> Of Guido García Bernardo
> Sent: Wednesday, July 16, 2008 4:13 PM
> To: jgap-users@...
> Subject: [jgap-users] Supergene isValid not working as expected ?
>
> Hi,
>
> I am trying my first JGAP application.  It is just a simple
> Supergene composed by a pair of DoubleGene.  The supergene
> should be valid when the value of the first one (min) is
> lower than the value of the second one (max).
>
> The FitnessFunction returns the difference between the two
> genes conforming the supergene (that is max - min).  It
> should be always positive, as the isValid method only return
> true when the max is greater than the min.
>
> But in fact it fails, showing the following error message and
> stack trace :
>
> Exception in thread "main" java.lang.RuntimeException:
> Fitness values must be positive! Received value: -486.6778986396248
>        at
> org.jgap.FitnessFunction.getFitnessValue(FitnessFunction.java:68)
>        at org.jgap.Chromosome.calcFitnessValue(Chromosome.java:479)
>        at org.jgap.Chromosome.getFitnessValue(Chromosome.java:450)
>        at
> org.jgap.impl.GABreeder.updateChromosomes(GABreeder.java:204)
>        at org.jgap.impl.GABreeder.evolve(GABreeder.java:76)
>        at org.jgap.Genotype.evolve(Genotype.java:225)
>        at test.LimitSupergene.main
>
> I suppose I am missing something.  Anyone knows why it is not
> working as expected ?
> I am using Java 5 and JGAP 3.3.3.
>
> Thank you very much,
> Guido
>
> PS. My code is below, just in case it helps:
>
> public class LimitSupergene extends AbstractSupergene {
>
>        public LimitSupergene() throws InvalidConfigurationException {
>                super();
>        }
>
>        public LimitSupergene(Configuration a_conf) throws
> InvalidConfigurationException {
>                super(a_conf);
>        }
>
>        public LimitSupergene(Configuration a_conf, Gene[]
> a_genes) throws InvalidConfigurationException {
>                super(a_conf, a_genes);
>        }
>
>        @Override
>    public boolean isValid(Gene[] genes, Supergene s)
>    {
>         DoubleGene min = (DoubleGene) genes[0];
>         DoubleGene max = (DoubleGene) genes[1];
>
>         return min.doubleValue() < max.doubleValue();
>    }
>
>    public static void main(String[] args) throws
> InvalidConfigurationException {
>        FitnessFunction function = new FitnessFunction() {
>                @Override
>                protected double evaluate(IChromosome chromosome) {
>                        LimitSupergene lg = (LimitSupergene)
> chromosome.getGene(0);
>                        double diff = // max - min
>                                (Double) lg.getGenes()[1].getAllele() -
>                                (Double) lg.getGenes()[0].getAllele();
>
>                        return diff;
>                }
>        };
>
>        Configuration conf = new DefaultConfiguration();
>        conf.setFitnessFunction(function);
>
>        Gene[] sampleGenes = new Gene[1];
>        sampleGenes[0] = new LimitSupergene(
>                        conf,
>                        new Gene[] {
>                                        new DoubleGene( conf,
> 1.0, 1000.0 ), // min
>                                        new DoubleGene( conf,
> 1.0, 1000.0 ) // max
>                        }
>        );
>
>        IChromosome sampleChromosome = new Chromosome(conf,
> sampleGenes);
>                conf.setSampleChromosome( sampleChromosome );
>                conf.setPopulationSize( 100 );
>
>                Genotype population =
> Genotype.randomInitialGenotype( conf );
>                for (int i=0; i<10; i++) {
>                        population.evolve();
>                }
>
>                IChromosome sol = population.getFittestChromosome();
>
>                LimitSupergene lg = (LimitSupergene) sol.getGene(0);
>                System.out.println( "min = " +
> lg.getGenes()[0].getAllele() );
>                System.out.println( "max = " +
> lg.getGenes()[1].getAllele() );
>        }
> }


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
jgap-users mailing list
jgap-users@...
https://lists.sourceforge.net/lists/listinfo/jgap-users

Re: Supergene isValid not working as expected ?

by Guido Garcia Bernardo :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you, Klaus.

Now I return 1 + Math.pow( Math.E, old_result ), that is always
positive and greater than one, as needed.

A good improvement in further versions could be to allow all kind of
values, no matter whether they are positive or not.  Maybe
redistributing them later between 1 and Double.MAX_VALUE or something
like that, if possible.

BR,
guido.

On Sat, Jul 19, 2008 at 6:08 PM, Klaus Meffert <jgap@...> wrote:

> Guido,
>
> I cannot remember the exact details about SuperGene. But to my knowledge it
> could be that in cases where too many invalid solutions i a row came up, a
> following invalid solution may be passed through. That means, please care in
> your fitness function that only positive values are returned. If you would
> get a negative fitness value, return the worst positive number possible in
> your scenario (e.g. Double.MAX_VALUE).
>
> Best
>
> Klaus
> www.klaus-meffert.com
>
>
>
>> -----Original Message-----
>> From: jgap-users-bounces@...
>> [mailto:jgap-users-bounces@...] On Behalf
>> Of Guido García Bernardo
>> Sent: Wednesday, July 16, 2008 4:13 PM
>> To: jgap-users@...
>> Subject: [jgap-users] Supergene isValid not working as expected ?
>>
>> Hi,
>>
>> I am trying my first JGAP application.  It is just a simple
>> Supergene composed by a pair of DoubleGene.  The supergene
>> should be valid when the value of the first one (min) is
>> lower than the value of the second one (max).
>>
>> The FitnessFunction returns the difference between the two
>> genes conforming the supergene (that is max - min).  It
>> should be always positive, as the isValid method only return
>> true when the max is greater than the min.
>>
>> But in fact it fails, showing the following error message and
>> stack trace :
>>
>> Exception in thread "main" java.lang.RuntimeException:
>> Fitness values must be positive! Received value: -486.6778986396248
>>        at
>> org.jgap.FitnessFunction.getFitnessValue(FitnessFunction.java:68)
>>        at org.jgap.Chromosome.calcFitnessValue(Chromosome.java:479)
>>        at org.jgap.Chromosome.getFitnessValue(Chromosome.java:450)
>>        at
>> org.jgap.impl.GABreeder.updateChromosomes(GABreeder.java:204)
>>        at org.jgap.impl.GABreeder.evolve(GABreeder.java:76)
>>        at org.jgap.Genotype.evolve(Genotype.java:225)
>>        at test.LimitSupergene.main
>>
>> I suppose I am missing something.  Anyone knows why it is not
>> working as expected ?
>> I am using Java 5 and JGAP 3.3.3.
>>
>> Thank you very much,
>> Guido
>>
>> PS. My code is below, just in case it helps:
>>
>> public class LimitSupergene extends AbstractSupergene {
>>
>>        public LimitSupergene() throws InvalidConfigurationException {
>>                super();
>>        }
>>
>>        public LimitSupergene(Configuration a_conf) throws
>> InvalidConfigurationException {
>>                super(a_conf);
>>        }
>>
>>        public LimitSupergene(Configuration a_conf, Gene[]
>> a_genes) throws InvalidConfigurationException {
>>                super(a_conf, a_genes);
>>        }
>>
>>        @Override
>>    public boolean isValid(Gene[] genes, Supergene s)
>>    {
>>         DoubleGene min = (DoubleGene) genes[0];
>>         DoubleGene max = (DoubleGene) genes[1];
>>
>>         return min.doubleValue() < max.doubleValue();
>>    }
>>
>>    public static void main(String[] args) throws
>> InvalidConfigurationException {
>>        FitnessFunction function = new FitnessFunction() {
>>                @Override
>>                protected double evaluate(IChromosome chromosome) {
>>                        LimitSupergene lg = (LimitSupergene)
>> chromosome.getGene(0);
>>                        double diff = // max - min
>>                                (Double) lg.getGenes()[1].getAllele() -
>>                                (Double) lg.getGenes()[0].getAllele();
>>
>>                        return diff;
>>                }
>>        };
>>
>>        Configuration conf = new DefaultConfiguration();
>>        conf.setFitnessFunction(function);
>>
>>        Gene[] sampleGenes = new Gene[1];
>>        sampleGenes[0] = new LimitSupergene(
>>                        conf,
>>                        new Gene[] {
>>                                        new DoubleGene( conf,
>> 1.0, 1000.0 ), // min
>>                                        new DoubleGene( conf,
>> 1.0, 1000.0 ) // max
>>                        }
>>        );
>>
>>        IChromosome sampleChromosome = new Chromosome(conf,
>> sampleGenes);
>>                conf.setSampleChromosome( sampleChromosome );
>>                conf.setPopulationSize( 100 );
>>
>>                Genotype population =
>> Genotype.randomInitialGenotype( conf );
>>                for (int i=0; i<10; i++) {
>>                        population.evolve();
>>                }
>>
>>                IChromosome sol = population.getFittestChromosome();
>>
>>                LimitSupergene lg = (LimitSupergene) sol.getGene(0);
>>                System.out.println( "min = " +
>> lg.getGenes()[0].getAllele() );
>>                System.out.println( "max = " +
>> lg.getGenes()[1].getAllele() );
>>        }
>> }
>
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
jgap-users mailing list
jgap-users@...
https://lists.sourceforge.net/lists/listinfo/jgap-users

Re: Supergene isValid not working as expected ?

by Klaus Meffert-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Guido,

great that it worked solving your issue!

The problem between normalizing values by distributing them is that you
don't know in which range the output values can occur. Maybe you have an
idea about that or someone else. Then I could add the concept to JGAP.

Best

Klaus
www.klaus-meffert.com

 

> -----Original Message-----
> From: jgap-users-bounces@...
> [mailto:jgap-users-bounces@...] On Behalf
> Of Guido García Bernardo
> Sent: Tuesday, July 22, 2008 7:41 PM
> To: jgap-users@...
> Subject: Re: [jgap-users] Supergene isValid not working as expected ?
>
> Thank you, Klaus.
>
> Now I return 1 + Math.pow( Math.E, old_result ), that is
> always positive and greater than one, as needed.
>
> A good improvement in further versions could be to allow all
> kind of values, no matter whether they are positive or not.  
> Maybe redistributing them later between 1 and
> Double.MAX_VALUE or something like that, if possible.
>
> BR,
> guido.
>
> On Sat, Jul 19, 2008 at 6:08 PM, Klaus Meffert
> <jgap@...> wrote:
> > Guido,
> >
> > I cannot remember the exact details about SuperGene. But to my
> > knowledge it could be that in cases where too many invalid
> solutions i
> > a row came up, a following invalid solution may be passed through.
> > That means, please care in your fitness function that only positive
> > values are returned. If you would get a negative fitness
> value, return
> > the worst positive number possible in your scenario (e.g.
> Double.MAX_VALUE).
> >
> > Best
> >
> > Klaus
> > www.klaus-meffert.com
> >
> >
> >
> >> -----Original Message-----
> >> From: jgap-users-bounces@...
> >> [mailto:jgap-users-bounces@...] On
> Behalf Of Guido
> >> García Bernardo
> >> Sent: Wednesday, July 16, 2008 4:13 PM
> >> To: jgap-users@...
> >> Subject: [jgap-users] Supergene isValid not working as expected ?
> >>
> >> Hi,
> >>
> >> I am trying my first JGAP application.  It is just a
> simple Supergene
> >> composed by a pair of DoubleGene.  The supergene should be
> valid when
> >> the value of the first one (min) is lower than the value of the
> >> second one (max).
> >>
> >> The FitnessFunction returns the difference between the two genes
> >> conforming the supergene (that is max - min).  It should be always
> >> positive, as the isValid method only return true when the max is
> >> greater than the min.
> >>
> >> But in fact it fails, showing the following error message
> and stack
> >> trace :
> >>
> >> Exception in thread "main" java.lang.RuntimeException:
> >> Fitness values must be positive! Received value: -486.6778986396248
> >>        at
> >> org.jgap.FitnessFunction.getFitnessValue(FitnessFunction.java:68)
> >>        at org.jgap.Chromosome.calcFitnessValue(Chromosome.java:479)
> >>        at org.jgap.Chromosome.getFitnessValue(Chromosome.java:450)
> >>        at
> >> org.jgap.impl.GABreeder.updateChromosomes(GABreeder.java:204)
> >>        at org.jgap.impl.GABreeder.evolve(GABreeder.java:76)
> >>        at org.jgap.Genotype.evolve(Genotype.java:225)
> >>        at test.LimitSupergene.main
> >>
> >> I suppose I am missing something.  Anyone knows why it is
> not working
> >> as expected ?
> >> I am using Java 5 and JGAP 3.3.3.
> >>
> >> Thank you very much,
> >> Guido
> >>
> >> PS. My code is below, just in case it helps:
> >>
> >> public class LimitSupergene extends AbstractSupergene {
> >>
> >>        public LimitSupergene() throws
> InvalidConfigurationException {
> >>                super();
> >>        }
> >>
> >>        public LimitSupergene(Configuration a_conf) throws
> >> InvalidConfigurationException {
> >>                super(a_conf);
> >>        }
> >>
> >>        public LimitSupergene(Configuration a_conf, Gene[]
> >> a_genes) throws InvalidConfigurationException {
> >>                super(a_conf, a_genes);
> >>        }
> >>
> >>        @Override
> >>    public boolean isValid(Gene[] genes, Supergene s)
> >>    {
> >>         DoubleGene min = (DoubleGene) genes[0];
> >>         DoubleGene max = (DoubleGene) genes[1];
> >>
> >>         return min.doubleValue() < max.doubleValue();
> >>    }
> >>
> >>    public static void main(String[] args) throws
> >> InvalidConfigurationException {
> >>        FitnessFunction function = new FitnessFunction() {
> >>                @Override
> >>                protected double evaluate(IChromosome chromosome) {
> >>                        LimitSupergene lg = (LimitSupergene)
> >> chromosome.getGene(0);
> >>                        double diff = // max - min
> >>                                (Double)
> lg.getGenes()[1].getAllele() -
> >>                                (Double)
> lg.getGenes()[0].getAllele();
> >>
> >>                        return diff;
> >>                }
> >>        };
> >>
> >>        Configuration conf = new DefaultConfiguration();
> >>        conf.setFitnessFunction(function);
> >>
> >>        Gene[] sampleGenes = new Gene[1];
> >>        sampleGenes[0] = new LimitSupergene(
> >>                        conf,
> >>                        new Gene[] {
> >>                                        new DoubleGene( conf, 1.0,
> >> 1000.0 ), // min
> >>                                        new DoubleGene( conf, 1.0,
> >> 1000.0 ) // max
> >>                        }
> >>        );
> >>
> >>        IChromosome sampleChromosome = new Chromosome(conf,
> >> sampleGenes);
> >>                conf.setSampleChromosome( sampleChromosome );
> >>                conf.setPopulationSize( 100 );
> >>
> >>                Genotype population =
> >> Genotype.randomInitialGenotype( conf );
> >>                for (int i=0; i<10; i++) {
> >>                        population.evolve();
> >>                }
> >>
> >>                IChromosome sol = population.getFittestChromosome();
> >>
> >>                LimitSupergene lg = (LimitSupergene) sol.getGene(0);
> >>                System.out.println( "min = " +
> >> lg.getGenes()[0].getAllele() );
> >>                System.out.println( "max = " +
> >> lg.getGenes()[1].getAllele() );
> >>        }
> >> }
> >
> >
>
> --------------------------------------------------------------
> -----------
> This SF.Net email is sponsored by the Moblin Your Move
> Developer's challenge Build the coolest Linux based
> applications with Moblin SDK & win great prizes Grand prize
> is a trip for two to an Open Source event anywhere in the
> world http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> jgap-users mailing list
> jgap-users@...
> https://lists.sourceforge.net/lists/listinfo/jgap-users


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
jgap-users mailing list
jgap-users@...
https://lists.sourceforge.net/lists/listinfo/jgap-users
LightInTheBox - Buy quality products at wholesale price