Bug in CrossoverOperator with ICompositeGene

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

Bug in CrossoverOperator with ICompositeGene

by Sebastien Huet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

First thanks a lot for this incredible work.

He is the problem i faced.

I have a composite gene made with integers & booleans genes.
my program crashes when crossoving ( can not cast integer to boolean or boolean to integer).

if think the bug comes from CrossoverOperator.doCrossover:

the random index over the gene is generated two times and may choose two differents kind of gene.


// Make a distinction for ICompositeGene for the first gene.
      // ---------------------------------------------------------
      if (firstGenes[j] instanceof ICompositeGene) {
        // Randomly determine gene to be considered.
        // -----------------------------------------
        int index1 = generator.nextInt(firstGenes[j].size());
        gene1 = ( (ICompositeGene) firstGenes[j]).geneAt(index1);
      }
      else {
        gene1 = firstGenes[j];
      }
      // Make a distinction for the second gene if CompositeGene.
      // --------------------------------------------------------
      if (secondGenes[j] instanceof ICompositeGene) {
        // Randomly determine gene to be considered.
        // -----------------------------------------
        int index2 = generator.nextInt(secondGenes[j].size());
        gene2 = ( (ICompositeGene) secondGenes[j]).geneAt(index2);
      }
      else {
        gene2 = secondGenes[j];
      }

I think the code should be:

// Make a distinction for ICompositeGene for the first gene.

      int index = 0;
      // ---------------------------------------------------------
      if (firstGenes[j] instanceof ICompositeGene) {
        // -----------------------------------------
        // Randomly determine gene to be considered.
        index = generator.nextInt(firstGenes[j].size()); //Assuming the two genes have the same size
        gene1 = ( (ICompositeGene) firstGenes[j]).geneAt(index);
      }
      else {
        gene1 = firstGenes[j];
      }
      // Make a distinction for the second gene if CompositeGene.
      // --------------------------------------------------------
      if (secondGenes[j] instanceof ICompositeGene) {
        gene2 = ( (ICompositeGene) secondGenes[j]).geneAt(index);
      }
      else {
        gene2 = secondGenes[j];
      }


Regards.

Seb

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
jgap-users mailing list
jgap-users@...
https://lists.sourceforge.net/lists/listinfo/jgap-users

Re: Bug in CrossoverOperator with ICompositeGene

by Klaus Meffert-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sebastien,
 
you are right, I fixed the code according to your suggestion and checked it in to CVS.
In the future, a more sophisticated x-over could be used/provided for CompositeGenes: The second gene can be potentially any one with the same type as the already chosen gene.
 
Best
 


From: Sebastien Huet [mailto:sebhuet@...]
Sent: Monday, September 28, 2009 9:31 AM
To: jgap-users@...
Subject: [jgap-users] Bug in CrossoverOperator with ICompositeGene

Hi,

First thanks a lot for this incredible work.

He is the problem i faced.

I have a composite gene made with integers & booleans genes.
my program crashes when crossoving ( can not cast integer to boolean or boolean to integer).

if think the bug comes from CrossoverOperator.doCrossover:

the random index over the gene is generated two times and may choose two differents kind of gene.


// Make a distinction for ICompositeGene for the first gene.
      // ---------------------------------------------------------
      if (firstGenes[j] instanceof ICompositeGene) {
        // Randomly determine gene to be considered.
        // -----------------------------------------
        int index1 = generator.nextInt(firstGenes[j].size());
        gene1 = ( (ICompositeGene) firstGenes[j]).geneAt(index1);
      }
      else {
        gene1 = firstGenes[j];
      }
      // Make a distinction for the second gene if CompositeGene.
      // --------------------------------------------------------
      if (secondGenes[j] instanceof ICompositeGene) {
        // Randomly determine gene to be considered.
        // -----------------------------------------
        int index2 = generator.nextInt(secondGenes[j].size());
        gene2 = ( (ICompositeGene) secondGenes[j]).geneAt(index2);
      }
      else {
        gene2 = secondGenes[j];
      }

I think the code should be:

// Make a distinction for ICompositeGene for the first gene.

      int index = 0;
      // ---------------------------------------------------------
      if (firstGenes[j] instanceof ICompositeGene) {
        // -----------------------------------------
        // Randomly determine gene to be considered.
        index = generator.nextInt(firstGenes[j].size()); //Assuming the two genes have the same size
        gene1 = ( (ICompositeGene) firstGenes[j]).geneAt(index);
      }
      else {
        gene1 = firstGenes[j];
      }
      // Make a distinction for the second gene if CompositeGene.
      // --------------------------------------------------------
      if (secondGenes[j] instanceof ICompositeGene) {
        gene2 = ( (ICompositeGene) secondGenes[j]).geneAt(index);
      }
      else {
        gene2 = secondGenes[j];
      }


Regards.

Seb

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
jgap-users mailing list
jgap-users@...
https://lists.sourceforge.net/lists/listinfo/jgap-users