jGap won't use my chromosome

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

jGap won't use my chromosome

Gabriel Rossetti-3
Hello,

I was using the regular Chromosome class with BooleanGenes, until I
needed to use them (the chromosomes) as keys in a hashmap. I noticed
that I had collisions, and after verifying that the jGap source code
didn't have a bug (and it doesn't) I decided to create a
UniqueChromosome class that inherits from Chromosome and overrides
hashcode() and equals. There is an internal static counter that gives a
unique id to each UniqueChromosome, thus the only way for them to be
euqal is for them to have the same genes and the same id, which is
impossible since there is no way to set/change the id after the object
is created.

My problem is that I give the UniqueChromosome as the as the sample
Chromosome to the configuration and as soon as I generate a random
chromosome using this code :

private IChromosome generateNewChromosome(Configuration conf) {

      IChromosome c = conf.getSampleChromosome();

      IInitializer chromIniter =
conf.getJGAPFactory().getInitializerFor(c, c.getClass());
      try {
         return (IChromosome) chromIniter.perform(c, c.getClass(), null);
      } catch (Exception e) {
         e.printStackTrace();
      }

      return null;
   }

I get a Chromosome back and not a UniqueChromosome. I checked in debug
mode, and c is a UniqueChromosome, and c.class() returns the correct
class, but as soon as the initializer's perform() method returns, I get
a Chromosome back.

Since I get a Chromosome back, my hashcode() and equals() methods aren't
used (of course) and I get collisions.

The DefaultInitializer's perform() method is what is being called by the
way.

Does anyone know why this isn't working correctly? Or did I do something
wrong?

Best regards,
Gabriel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: jGap won't use my chromosome

Klaus Meffert-5
Gabriel,

good question! A possible solution can be found in the example in package
examples.distinctGenes. See class Main where MyChromosome is set as sampel
chromosome. In class MyChromosome the static method
randomInitialMyChromosome is important! At the end of that method, a new
custom Chromosome is returned (it could also be a MyChromosome, but in the
example it is not - I just changed that to MyChromosome to make things more
clear, and checked that in).

Best

Klaus
www.klaus-meffert.de


> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf
> Of Gabriel Rossetti
> Sent: Friday, November 09, 2007 11:45 AM
> To: [hidden email]
> Subject: [jgap-users] jGap won't use my chromosome
>
> Hello,
>
> I was using the regular Chromosome class with BooleanGenes,
> until I needed to use them (the chromosomes) as keys in a
> hashmap. I noticed that I had collisions, and after verifying
> that the jGap source code didn't have a bug (and it doesn't)
> I decided to create a UniqueChromosome class that inherits
> from Chromosome and overrides
> hashcode() and equals. There is an internal static counter
> that gives a unique id to each UniqueChromosome, thus the
> only way for them to be euqal is for them to have the same
> genes and the same id, which is impossible since there is no
> way to set/change the id after the object is created.
>
> My problem is that I give the UniqueChromosome as the as the
> sample Chromosome to the configuration and as soon as I
> generate a random chromosome using this code :
>
> private IChromosome generateNewChromosome(Configuration conf) {
>
>       IChromosome c = conf.getSampleChromosome();
>
>       IInitializer chromIniter =
> conf.getJGAPFactory().getInitializerFor(c, c.getClass());
>       try {
>          return (IChromosome) chromIniter.perform(c,
> c.getClass(), null);
>       } catch (Exception e) {
>          e.printStackTrace();
>       }
>
>       return null;
>    }
>
> I get a Chromosome back and not a UniqueChromosome. I checked
> in debug mode, and c is a UniqueChromosome, and c.class()
> returns the correct class, but as soon as the initializer's
> perform() method returns, I get a Chromosome back.
>
> Since I get a Chromosome back, my hashcode() and equals()
> methods aren't used (of course) and I get collisions.
>
> The DefaultInitializer's perform() method is what is being
> called by the way.
>
> Does anyone know why this isn't working correctly? Or did I
> do something wrong?
>
> Best regards,
> Gabriel


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users
Reply | Threaded
Open this post in threaded view
|

Re: jGap won't use my chromosome

Gabriel Rossetti-3
Klaus,

A bit late in thanking you, but here it is : Thank you!

Great work on JGAP, I loved working with it!

Best,
Gabriel Rossetti

Klaus Meffert wrote:

> Gabriel,
>
> good question! A possible solution can be found in the example in package
> examples.distinctGenes. See class Main where MyChromosome is set as sampel
> chromosome. In class MyChromosome the static method
> randomInitialMyChromosome is important! At the end of that method, a new
> custom Chromosome is returned (it could also be a MyChromosome, but in the
> example it is not - I just changed that to MyChromosome to make things more
> clear, and checked that in).
>
> Best
>
> Klaus
> www.klaus-meffert.de
>
>
>  
>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf
>> Of Gabriel Rossetti
>> Sent: Friday, November 09, 2007 11:45 AM
>> To: [hidden email]
>> Subject: [jgap-users] jGap won't use my chromosome
>>
>> Hello,
>>
>> I was using the regular Chromosome class with BooleanGenes,
>> until I needed to use them (the chromosomes) as keys in a
>> hashmap. I noticed that I had collisions, and after verifying
>> that the jGap source code didn't have a bug (and it doesn't)
>> I decided to create a UniqueChromosome class that inherits
>> from Chromosome and overrides
>> hashcode() and equals. There is an internal static counter
>> that gives a unique id to each UniqueChromosome, thus the
>> only way for them to be euqal is for them to have the same
>> genes and the same id, which is impossible since there is no
>> way to set/change the id after the object is created.
>>
>> My problem is that I give the UniqueChromosome as the as the
>> sample Chromosome to the configuration and as soon as I
>> generate a random chromosome using this code :
>>
>> private IChromosome generateNewChromosome(Configuration conf) {
>>
>>       IChromosome c = conf.getSampleChromosome();
>>
>>       IInitializer chromIniter =
>> conf.getJGAPFactory().getInitializerFor(c, c.getClass());
>>       try {
>>          return (IChromosome) chromIniter.perform(c,
>> c.getClass(), null);
>>       } catch (Exception e) {
>>          e.printStackTrace();
>>       }
>>
>>       return null;
>>    }
>>
>> I get a Chromosome back and not a UniqueChromosome. I checked
>> in debug mode, and c is a UniqueChromosome, and c.class()
>> returns the correct class, but as soon as the initializer's
>> perform() method returns, I get a Chromosome back.
>>
>> Since I get a Chromosome back, my hashcode() and equals()
>> methods aren't used (of course) and I get collisions.
>>
>> The DefaultInitializer's perform() method is what is being
>> called by the way.
>>
>> Does anyone know why this isn't working correctly? Or did I
>> do something wrong?
>>
>> Best regards,
>> Gabriel
>>    
>
>
>  

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
jgap-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgap-users