Bug in CrossoverOperator with ICompositeGene

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug in CrossoverOperator with ICompositeGene

Sebastien Huet
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: Bug in CrossoverOperator with ICompositeGene

Klaus Meffert-5
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:[hidden email]]
Sent: Monday, September 28, 2009 9:31 AM
To: [hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users