Extending Chromosome

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Extending Chromosome

Hi folks,

First of all many thanks for your effort on sharing and maintaining such a great library :)

For some reason, I needed to extend the Chromosome class that let my custom genes access the parent chromosome to retrieve information about other genes in the chromosome. The task wasn't straightforward, and I would like to discuss the way I did it.

1) I needed to override the clone() method to do the additional setup of my chromosome. I first tried to simply write:

public synchronized Object clone(){
    MyChromosome c = super.clone();
    c.linkGenes(); // allows to register the chromosome to each new gene instance
    return c;

which led to a stack overflow since the super.clone() calls MyChromosome.clone() somewhere:
(IChromosome) getConfiguration().getSampleChromosome().clone()

So I simply copy-pasted and adapted the Chromosome.clone() method to have it working for MyChromosome. The bad thing is that the field m_multiObjective is private, so I can't completely finish the task of cloning. Up to now it's ok since I'm not using multiobjectives.

> Maybe the best would be to have a postClone() method doing nothing for Chromosome, and allowing users to add work?

2) Then, I noticed that the method Chromosome.randomInitialChromosome() is not suitable to generate subclasses of Chromosome. Indeed, it finishes by: 
new Chromosome(a_configuration, newGenes);
instead of trying to initialize MyChromosome.

So I had to copy-paste and adapt this method to allow the generation of the appropriate class. Since the method is static, I also had to override the perform method:
public Object perform(Object a_obj, Class a_class, Object a_params) throws Exception {
    return randomInitialChromosome(getConfiguration());

> Maybe a newChromosome() method could be called from the sample Chromosome? 

I do not really like the way I fixed my problems, so I think you may have suggestions to do it in a cleaner way?


Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing

jgap-users mailing list
[hidden email]