Python Genetic Algorithm

Max thelanguageofcities at gmail.com
Sun Jan 27 18:09:52 EST 2008


Hi all. I'm just getting introduced to Python (mostly through Dive
Into Python), and I've decided to use it for a project where I have to
write my own Genetic Algorithm. Even if you don't know about GAs, you
might be able to help with an issue I'm having. I'm just starting the
project off, so I'm still in the conceptual phase, and I'm stuck on
how I'm going to be able to implement something.

In GAs, you operate on a Population of solutions. Each Individual from
the Population is a potential solution to the problem you're
optimizing, and Individuals have what's called a chromosome - a
specification of what it contains. For example, common chromosomes are
bit strings, lists of ints/floats, permutations...etc. I'm stuck on
how to implement the different chromosomes. I have a Population class,
which is going to contain a list of Individuals. Each individual will
be of a certain chromosome. I envision the chromosomes as subclasses
of an abstract Individual class, perhaps all in the same module. I'm
just having trouble envisioning how this would be coded at the
population level. Presumably, when a population is created, a
parameter to its __init__ would be the chromosome type, but I don't
know how to take that in Python and use it to specify a certain class.

I'm doing something similar with my crossover methods, by specifying
them as functions in a module called Crossover, importing that, and
defining

crossover_function = getattr(Crossover, "%s_crossover" % xover)

Where xover is a parameter defining the type of crossover to be used.
I'm hoping there's some similar trick to accomplish what I want to do
with chromosomes - or maybe I'm going about this completely the wrong
way, trying to get Python to do something it's not made for. Any help/
feedback would be wonderful.

Thanks,
Max Martin



More information about the Python-list mailing list