A simple and flexible GA loop in Python
Yes, 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 f in func_calls for i in range(self.env.length/5)]
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.
About this entry
You’re currently reading “A simple and flexible GA loop in Python,” an entry on Xavier Llorà
- Published:
- Wednesday, January 16th, 2008 at 10:34 am
- Author:
- Xavier
- Category:
- Software
No comments
Jump to comment form | comments rss | trackback uri