Serendip is an independent site partnering with faculty at multiple colleges and universities around the world. Happy exploring!

You are here

GA Emergence Test Code

Kathy Maffei's picture
As per Doug's request, here's my revision of Doug's emergence demo code. It's pretty simple. I added a second class, the same as his but with some adjustment in the fitness function to try to account for letters correct in a row. I marked the lines I added so I could later identify my changes. I also added three variables: trials (to indicate the number of trials to be run - and to make it easier to fiddle) and old & new to keep running totals of the number of generations each needed to achieve the correct phrase. I created a loop around the running of the ga, so I could have it repeat once for each trial. Inside it runs each version one time (with the same parameters), and the running sums accumulate. After the trials, the results are printed on the screen. This is useful not only for comparing fitness functions without sitting at the computer to watch (I ran mine overnight), but could be easily edited to compare the same function with different parameters.