This Question have 3 answers right now.

How to structure a Genetic Algorithm class hierarchy?

Question!

I'm doing some work with Genetic Algorithms and want to write my own GA classes. Since a GA can have different ways of doing selection, mutation, cross-over, generating an initial population, calculating fitness, and terminating the algorithm, I need a way to plug in different combinations of these. My initial approach was to have an abstract class that had all of these methods defined as pure virtual, and any concrete class would have to implement them. If I want to try out two GAs that are the same but with different cross-over methods for example, I would have to make an abstract class that inherits from GeneticAlgorithm and implements all the methods except the cross-over method, then two concrete classes that inherit from this class and only implement the cross-over method. The downside to this is that every time I want to swap out a method or two to try out something new I have to make one or more new classes.

Is there another approach that might apply better to this problem?



Answers

your implementation looks like a Decorator pattern.

By : Pierre


As people say, don't make it one giant class. That would be horrible. Encapsulate behavior in different classes. Strategy is a solution.
If you need examples download sources and examples of JGAP. It has support for Genetic Programming and Genetic Algorithms. You will see there nice nice design. Mutation,Crossover,Selection,Population,Gene - all this are separate classes. You just setup Configuration object where you initiate defined interfaces with implementations you want to use, pass proper algorithm parameters and you run it. Really package is huge, javadoc nice, and you can always look into the source or check mail group for some answers. When I was looking for GA package I saw GAlib and others but I think this one is most complete with really nice design.

By : yoosiba


I would approach the GA as a collaboration of many objects, rather than one big Class encapsulating the whole algorithm. Basically, you could have an abstract class for every big point of variation, and concrete classes for every implementation choice you want. You then combine the concrete classes you want into many varieties of GA.

Also, you might want to familiarize yourself with the Strategy Pattern: http://en.wikipedia.org/wiki/Strategy_pattern

By : CBFraser


Video about How to structure a Genetic Algorithm class hierarchy?