Genetic Algorithm Library for Processing or Java


Warning! Broken Library!: It's been brought to my attention that the fitness function for the GA Library is, put simply, wrong. I'd love to sit down and fix this but I'm busy with other stuff at the moment, so I'm allowing you to override the fitness function for the time being. Chris Calef of BrokeAss Games had a look and sent me a mail to set me straight. He also sent some code, which I've been too busy to look over and suspect that if I do look at it, I'll get sucked into fixing the GA, which I don't want to do right now. Sorry for the broken code, I promise to fix it when I'm done with my current project.


A genetic algorithm (GA) is a machine that generates random answers to a problem and splices them together in the hope of finding the correct answer.

You may want to read the wikipedia page on Genetic Algorithms before continuing.

You can use the GA Library to find a specific answer to a problem, or simply use it to create Chromosome classes to represent sets of information that you want to splice.

Because the nature of the problem to be solved is only known to you the library user, if you plan on using the breeding algorithm in the GeneticAlgorithm class (propagate()) you must extend the class and override its scoreFitness() method. This method by default returns 1, the maximum margin of error for solving the problem. You will also need a working understanding of what an ArrayList is. If you have your own ideas about splicing and breeding, that's fine, there are methods in the library that let you splice Chromosomes however and whenever you want.

Traditionally the dna in a GA is represented by a binary string. We then chop that string into even chunks representing codes for properties. With this library you simply have an array of numbers, eliminating the need to convert from binary to number. But since a number is binary anyway, this library will let you splice dna at the binary level. This is turned off as a default to keep things simple, but you can activate this feature in the constructor of the GA.

Genetic Algorithm links and code:

AI Junkie's Genetic Algorithms for Beginners
Introduction to Genetic Algorithms
Craig Reynolds' Evolutionary Computation
Karl Sim's Artificial Evolution for Computer Graphics
Daniel Shiffman's Nature of Code






Projects made with this library

Eye for an Eye

If you put together any work using the GA library Just drop me a line and I'll be happy to link to it here. Same goes for if you find any bugs, typos or just simply don't understand how this library works. Contact me at the Processing forums (st33d) or email me:



Genetic Algorithm Library

To add the library to your project, put the unzipped folder ai_ga into the libraries folder of your sketchbook

Add the library to your code with: import ai_ga.*;

Alternatively you can put the library jar file ai_ga.jar in the code folder of your sketch, no import command is needed with this method.

The Genetic Algorithm Library is released under the GNU Lesser General Public License

Tested with Processing and compiled with Eclipse

Powered by cups of tea, jam on toast and Boards of Canada records