At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Genetic Algorithms » Projects

Evolve Pi

This is a very simple exercise that will allow you to see how genetic algorithms work. Try and evolve pi! This exercise obviously assumes you have read the basic Generation5 GA essays.

Outline

While this sounds rather complicated it is very easy. Create a population of floating point numbers and run it through a fitness function. Then average all the good values together to create the next generation. Keep on going until you're satified with the results.

Guidelines

This project is very open because there are many ways to do it. For example, the fitness function could return the error between a fixed value of pi and the value passed to it:
#define gPI 3.14159f
float error_pi(float value) {
	return abs(value - gPI / gPI);
}
This would only evolve a value as good as the constant defined. You could use data that relates a radius of a circle with the circumference. Again, that would only evolve pi as good as defined in the data. Try different methods. For a high-precision evaluation, I used Texas Instruments' Derive 5 to generate pi to 100 decimal places:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

Again, you can keep evolving until you reach a certain precision, x-number of iterations, or you can keep it going infinitely until you otherwise specify. Tip: At least for the first few generations, print out the best results so that you can see how the GA generates "fit" chromosomes.

This project really strips down what a genetic algorithm does into the simplest form - but this simplicity comes with the added bonus that it makes it very accessible to people with little experience with GAs. You can see how the GA works, therefore it is definitely in your interest to try this project out if you are novice. Here is a printout from my solution:

Current best: 3.18944
Current best: 3.15513
Current best: 3.13034
Current best: 3.14163
Current best: 3.14214
Current best: 3.1416
Current best: 3.14157
Current best: 3.1416
Current best: 3.14159
Current best: 3.14159

Best value: 3.14159.


Solutions

evolvepi.zip (29 Kb) Solution submitted by James Matthews. Simply uses the value of pi listed above as a reference, uses bubble sort to sort fitnesses. Simple but effective.

Submitted: 12/03/2001

Article content copyright © James Matthews, 2001.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -