Python Genetic Algorithm

Max thelanguageofcities at gmail.com
Mon Jan 28 17:13:53 CET 2008


On Jan 27, 7:25 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> Just pass the class itself. For example:
>
> # Define a class.
> class Parrot(object):
>     pass
>
> x = "Parrot"  # x is the NAME of the class
> y = Parrot  # y is the CLASS itself
> z = Parrot()  # z is an INSTANCE of the class
>
> You can use the class as a object, exactly the same as you can use a dict
> or a string or a float or any other object. y() will create a new Parrot
> instance exactly the same way that Parrot() would.
>

Okay, I'm getting into the thick of things, and I want to make sure
I'm implementing this correctly. I have a module Individual.py which
contains the abstract class Individual and the class BitString. My
population __init__ takes chromosome as a parameter, and checks:

if chromosome is not issubclass(Individual):
     raise Exception("Chromosome type must be a subclass of
Individual.")

Then it creates individuals as instances of chromosome (x =
chromosome(params)). I'm pretty sure this is all right - what I'm
wondering is, when actually creating a population, would I pass
Individual.BitString as a parameter? That's what I have now.

I have similar worries about my selection scheme. Right now I have the
function rouletteWheel defined as a member of Population, so I pass
the selector to my GA class as Population.rouletteWheel (making sure I
have Population imported). I just want to ensure that this is correct.



More information about the Python-list mailing list