Infinite Loop in Chromosome.clone() for Subclasses

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

Infinite Loop in Chromosome.clone() for Subclasses

Peter Molettiere

Hello!

I've recently started using JGAP to learn more about GA and GP. I've run across an issue which looks to me like an infinite loop in Chromosome.clone() when trying to clone any subclass of Chromosome which does not override clone(), or which calls super.clone() in its constructor.

Check out this section starting at line 389 of version 1.99 of org.jgap.Chromosome:

         // Now construct a new Chromosome with the copies of the genes and
         // return it. Also clone the IApplicationData object later on.
         // ---------------------------------------------------------------
         if (getClass() == Chromosome.class) {
           copy = new Chromosome(getConfiguration(), copyOfGenes);
         }
         else {
           copy = (IChromosome) getConfiguration().getSampleChromosome().clone();
           copy.setGenes(copyOfGenes);
         }


Note that all subclasses of Chromosome fail the "getClass() == Chromosome.class" test, which falls through to calling clone() on the sample chromosome set on the configuration. If this sample chromosome is the same Chromosome subclass, then calling clone on it results in coming back to the top of this paragraph.

I've managed to work around this for myself by simply copying the gene cloning code in Chromosome.clone() into my subclass, but I think it should properly be fixed in Chromosome itself.

My suggestion would be to require Chromosome and its subclasses to define a no arg constructor, use this constructor to create the clone of the current class using reflection via getClass().newInstance(), and setting the appropriate configuration and gene array on the clone via setters. Alternatively, use reflection to invoke a single arg constructor with the configuration, to retain the current Chromosome semantics, at least with regard to configurations being required at construction.

Subclasses could then call super.clone in their clone implementation to get the clone, and then set any subclass specific state on the clone before returning.

--Peter
------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: Infinite Loop in Chromosome.clone() for Subclasses

Klaus Meffert-5
Peter,

thanx a lot for your pointing out the problem with clone and your detailled
analysis.

I just checked in an extended version of Chromosome's clone() method and
added two test cases to unit test class ChromosomeTest.
My solution follows your suggestion to call a constructor via reflection.
This works fine, except for inner classes (which should be avoided in this
case, anyway).

If you encounter anything else, I would be grateful to know.

Best and an Happy New Year!

Klaus
www.klaus-meffert.com

 

> -----Original Message-----
> From: Peter Molettiere [mailto:[hidden email]]
> Sent: Saturday, December 12, 2009 10:13 PM
> To: [hidden email]
> Subject: [jgap-users] Infinite Loop in Chromosome.clone() for
> Subclasses
>
>
> Hello!
>
> I've recently started using JGAP to learn more about GA and
> GP. I've run across an issue which looks to me like an
> infinite loop in Chromosome.clone() when trying to clone any
> subclass of Chromosome which does not override clone(), or
> which calls super.clone() in its constructor.
>
> Check out this section starting at line 389 of version 1.99
> of org.jgap.Chromosome:
>
>          // Now construct a new Chromosome with the copies of
> the genes and
>          // return it. Also clone the IApplicationData object
> later on.
>          //
> ---------------------------------------------------------------
>          if (getClass() == Chromosome.class) {
>            copy = new Chromosome(getConfiguration(), copyOfGenes);
>          }
>          else {
>            copy = (IChromosome)
> getConfiguration().getSampleChromosome().clone();
>            copy.setGenes(copyOfGenes);
>          }
>
>
> Note that all subclasses of Chromosome fail the "getClass()
> == Chromosome.class" test, which falls through to calling
> clone() on the sample chromosome set on the configuration. If
> this sample chromosome is the same Chromosome subclass, then
> calling clone on it results in coming back to the top of this
> paragraph.
>
> I've managed to work around this for myself by simply copying
> the gene cloning code in Chromosome.clone() into my subclass,
> but I think it should properly be fixed in Chromosome itself.
>
> My suggestion would be to require Chromosome and its
> subclasses to define a no arg constructor, use this
> constructor to create the clone of the current class using
> reflection via getClass().newInstance(), and setting the
> appropriate configuration and gene array on the clone via
> setters. Alternatively, use reflection to invoke a single arg
> constructor with the configuration, to retain the current
> Chromosome semantics, at least with regard to configurations
> being required at construction.
>
> Subclasses could then call super.clone in their clone
> implementation to get the clone, and then set any subclass
> specific state on the clone before returning.
>
> --Peter
> --------------------------------------------------------------
> ----------------
> Return on Information:
> Google Enterprise Search pays you back
> Get the facts.
> http://p.sf.net/sfu/google-dev2dev
> _______________________________________________
> jgap-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jgap-users


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: Infinite Loop in Chromosome.clone() for Subclasses

Peter Molettiere

My pleasure Klaus. Glad to be able to give something back. Thanks for making such an excellent package available.

 I'll check out your fix during the next week.

Happy New Year!

--Peter
 
On Jan 4, 2010, at 3:05 AM, Klaus Meffert wrote:

> Peter,
>
> thanx a lot for your pointing out the problem with clone and your detailled
> analysis.
>
> I just checked in an extended version of Chromosome's clone() method and
> added two test cases to unit test class ChromosomeTest.
> My solution follows your suggestion to call a constructor via reflection.
> This works fine, except for inner classes (which should be avoided in this
> case, anyway).
>
> If you encounter anything else, I would be grateful to know.
>
> Best and an Happy New Year!
>
> Klaus
> www.klaus-meffert.com
>
>
>
>> -----Original Message-----
>> From: Peter Molettiere [mailto:[hidden email]]
>> Sent: Saturday, December 12, 2009 10:13 PM
>> To: [hidden email]
>> Subject: [jgap-users] Infinite Loop in Chromosome.clone() for
>> Subclasses
>>
>>
>> Hello!
>>
>> I've recently started using JGAP to learn more about GA and
>> GP. I've run across an issue which looks to me like an
>> infinite loop in Chromosome.clone() when trying to clone any
>> subclass of Chromosome which does not override clone(), or
>> which calls super.clone() in its constructor.
>>
>> Check out this section starting at line 389 of version 1.99
>> of org.jgap.Chromosome:
>>
>>         // Now construct a new Chromosome with the copies of
>> the genes and
>>         // return it. Also clone the IApplicationData object
>> later on.
>>         //
>> ---------------------------------------------------------------
>>         if (getClass() == Chromosome.class) {
>>           copy = new Chromosome(getConfiguration(), copyOfGenes);
>>         }
>>         else {
>>           copy = (IChromosome)
>> getConfiguration().getSampleChromosome().clone();
>>           copy.setGenes(copyOfGenes);
>>         }
>>
>>
>> Note that all subclasses of Chromosome fail the "getClass()
>> == Chromosome.class" test, which falls through to calling
>> clone() on the sample chromosome set on the configuration. If
>> this sample chromosome is the same Chromosome subclass, then
>> calling clone on it results in coming back to the top of this
>> paragraph.
>>
>> I've managed to work around this for myself by simply copying
>> the gene cloning code in Chromosome.clone() into my subclass,
>> but I think it should properly be fixed in Chromosome itself.
>>
>> My suggestion would be to require Chromosome and its
>> subclasses to define a no arg constructor, use this
>> constructor to create the clone of the current class using
>> reflection via getClass().newInstance(), and setting the
>> appropriate configuration and gene array on the clone via
>> setters. Alternatively, use reflection to invoke a single arg
>> constructor with the configuration, to retain the current
>> Chromosome semantics, at least with regard to configurations
>> being required at construction.
>>
>> Subclasses could then call super.clone in their clone
>> implementation to get the clone, and then set any subclass
>> specific state on the clone before returning.
>>
>> --Peter
>> --------------------------------------------------------------
>> ----------------
>> Return on Information:
>> Google Enterprise Search pays you back
>> Get the facts.
>> http://p.sf.net/sfu/google-dev2dev
>> _______________________________________________
>> jgap-users mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/jgap-users
>


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users