A simple and flexible GA loop in Python
16 January 2008Yes, yes, I know, Python. I just been playing around to see how much you can squeeze out of it and I am very surprise of how elegant it can be. Just and example. I am pretty sure you have written way to many customize GA loops that you need to tweak every now and then, if so take a look at the Python version below.
def evolve(self): """Implements a simple evolution loop""" func_calls = [] func_calls.append(self.evaluate) if ( self.env.selection!=None ) : func_calls.append(self.selection) if ( self.env.crossover!=None ) : func_calls.append(self.crossover) if ( self.env.mutation!=None ) : func_calls.append(self.mutate) if ( self.env.replacement!=None ) : func_calls.append(self.replace) [f() for i in range(self.env.length/5) for f in func_calls ]
self.evaluate() return (self.best_fitness,self.best_individual)
Where the self.env object just contains references to the methods implementing each of the functionalities generically implemented in self.evaluate, etc.. Pretty sleek.
No comments yet
Leave a Reply
You must be logged in to post a comment.


