We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Detecting inheritance pickle
Page Index Toggle Pages: 1
Detecting inheritance pickle (Read 522 times)
Detecting inheritance pickle
Aug 7th, 2006, 1:30pm
 
I'm working on a Genetic Algorithm library and it occurs to me that I need the user to develop their own method for determining fitness.

I thought that I might do this by making the Chromosome abstract and leaving the fitness method abstract. Then they extend that class and write their own fitness method.

This of course leaves me in a tremendous pickle because the Chromosome in the model I'm working from is an inner class of GeneticAlgorithm. So I could make Chromosome an outside class. But although I can reserve objects in GeneticAlgorithm with the name ChromosomeTemplate, I then have the trouble of targeting the inherited class made by the user.

Or perhaps I could make GeneticAlgorithm abstract and put fitness scoring outside of the Chromosome, but I'm still a little unsure about that too because of dragging the user into the guts of something I shouldn't need them to understand.

Each path I take is a bit messy, an I haven't written abstract classes before for other people or even myself. I'm really interested to know what other peole think before I embark on making a really confusing library.
Re: Detecting inheritance pickle
Reply #1 - Aug 7th, 2006, 3:42pm
 
i'm not entirely clear from your message what you're trying to do, but i think you might be missing part of how inheritance works.. you can have a reference to a GeneticAlgorithm object in your class, and when its methods are called, the version for the subclass written by the user will be used if it's an instance of the subclass. just because the field is the more generic type, the methods of the instance will be used.

as for abstract classes, etc.. use an 'interface' when you want to force someone to implement certain methods, and those objects need to be passed around as references. use an abstract class for similar reasons, but where you need to have some local variables, and a few methods already implemented. if all methods are already implemented, it needn't be an abstract class at all. interfaces are also good when you need multiple inheritance (like a class that sort of extends two different classes).
Re: Detecting inheritance pickle
Reply #2 - Aug 7th, 2006, 5:24pm
 
How I'd like to do things is (kind of pseudo code):
Code:

class Genetic Algorithm
{
// Global Variables
GeneticAlgorithm(various variables)
{
// Init involving reference to Chromosome
}
// Methods
class Chromosome
{
// Variables
Chromosome(various variables)
{
// Init involving reference to
// GeneticAlgorithm Global Variables
}
// Methods and also ideally I could do..

abstract void scoreFitness();

// But I'm pretty certain I can't
}
}

So basically my issue was that I wanted this fleshed out class with one method that the user creates. So I'm pretty certain I need an abstract class of some sort.

But what would be the best way to do it? Because I don't think I can do it the way I've outlined above. (Unless there's actually some way of doing it?)
Re: Detecting inheritance pickle
Reply #3 - Aug 8th, 2006, 3:33am
 
nah, don't use an inner class. make them two separate classes. use the Chromosome class to store data, and the GeneticAlgorithm class to store the fitness function that acts on the data. but it depends on what sort of data you want to store in each, so i may be giving bad advice...
Re: Detecting inheritance pickle
Reply #4 - Aug 9th, 2006, 1:48pm
 
The Chromosome is going to be using a lot of variables from GeneticAlgorithm. Two separate classes would be a real pain in the arse. I think a way round it might be to do it this way:
Code:

class GeneticAlgorithm
{
// yadda yadda and...

abstract float parentScoreFitness();

class Chromosome
{
// stuff and...
void scoreFitness(){
parentScoreFitness(Object target);
}
}
}

That way the user only has to extend GeneticAlgorithm and provide a fitness scoring method plus any other methods they may need. Would that be okay?

I'm thinking of parentScoreFitness() handling an Object because I've had the idea that the list of traits that represent the possible genetic traits could be like a Python list. I'd use a Vector to store them in. Then the user could get pretty creative.

Or should I just stick with a char[] for the traits and force them to use a simpler model?
Re: Detecting inheritance pickle
Reply #5 - Sep 18th, 2006, 2:08pm
 
I've found a solution to this problem of mine I believe. I previously didn't know about the Tardis like behaviour of Java and it's referencing stuff.

I put a project together with my library and found that I wanted Chromosomes but not the GA, but there is too much information on the behaviour of the Chromosomes in the GA. That would mean sticking a GA in every Chromosome to use them separately. Which sounded expensive for memory.

But in effect I could just have a reference to the GA. Here is the bare minimum code explaining how I've figured it out:
Code:

Foo foo;
Chromosome c;

void setup(){
foo = new Foo();
c = foo.makeChromosome();
println(foo.genePool.size());
println(c.ga.genePool.size());
}

abstract class GeneticAlgorithm{
Vector genePool;
GeneticAlgorithm(int chromosomeLength, int poolSize){
genePool = new Vector();
}
abstract float scoreFitness(Chromosome o);
Chromosome makeChromosome(){
Chromosome temp = new Chromosome(this);
genePool.add(temp);
return temp;
}
}

class Chromosome{
GeneticAlgorithm ga;
float score;
Chromosome(GeneticAlgorithm ga){
this.ga = ga;
score = ga.scoreFitness(this);
}
}

class Foo extends GeneticAlgorithm{
Foo(){
super(5, 0);
}
float scoreFitness(Chromosome o){
return 1.0;
}
}

This setup allows users to work with a GA or just utilise the nature of Chromosomes on their own. (Credit to TomC with his AStar Nodes - I never would have figured this out otherwise).

My question is, would this setup be bad form or am I on the right track?
Re: Detecting inheritance pickle
Reply #6 - Sep 22nd, 2006, 9:31am
 
What about using the power of interfaces? That way you could have inner classes and still utilise inheritance in a way ...

Code:

abstract class GeneticAlgorithm {
// member variables and common GA methods ...

// because of the interface definition, you can work with the Chromosome in any of the methods here ...
abstract float scoreFitness(Chromosome o);
}

interface Chromosome {
// define headers for common methods here ...
}

// then you can use
class SpecificGA extends GeneticAlgorithm {
// some additional member variables and methods

class SpecificChromosome implements Chromosome {
// this is where you couple them ... the GA and the Chromosome are in the tight relation you probably need.
}
}


Re: Detecting inheritance pickle
Reply #7 - Sep 23rd, 2006, 2:40pm
 
The issue is that the user will want access to individual Chromosomes. By defining Chromosome as an inner class people have to say:
Code:
GeneticAlgorithm.Chromosome temp = (GeneticAlgorithm.Chromosome)GeneticAlgorithm.genePool.get(i);

Every time they want to retrieve a Chromosome from the system. But I was also concerned about having Chromosomes separate from the GA because every time you would want to instance one you would have to say:
Code:
Chromosome myChromosome = new Chromosome(int dnaLength, int traitSize, int poolSize, int crossOverRate, int mutationRate, boolean subInteger);

Instead of using a blank constructor or myGA.makeChromosome().

They way I've done it now means much less typing, so I'm sticking to what I've written, but thank you for your input.
Page Index Toggle Pages: 1