Implementing Evolutionary Algorithms

If an evolutionary algorithm is a good fit for a particular problem, there are plenty of options when it comes to implementing it. You may choose to use a high-level programming language for simplicity, or a low-level language for performance. You could write all of the code yourself from scratch, or you could reuse pre-written components and libraries. In this book we will necessarily be using one particular approach, but it is worth noting that there are alternatives.

Choice of Programming Language

Evolutionary algorithms can be implemented in any general purpose programming language. Most programmers will simply choose the language that they are most comfortable with. A quick web search will return examples of evolutionary programs written in C, C++, Java, C#, Python, Ruby, Perl, Lisp and several other languages.

Performance may be a consideration when choosing a language. Almost all evolutionary algorithms are CPU-bound. For this reason, compiled languages typically offer better EA performance than interpreted languages. For short-lived programs the difference is unlikely to be significant, but for long-running programs it could be considerable.

If you can recall the title of this book it should come as no surprise that we will be using Java for all of the example code. Java offers a good balance of performance, ease-of-use and a rich standard library.

Evolution Frameworks

As we saw above, the basic outline of an evolutionary algorithm is fairly straightforward. It consists of a main loop that performs one generation per iteration, supplemented by a few functions to perform fitness evaluation, selection and mutation/cross-over. When implementing a simple EA, writing this structural code is not particularly onerous. However, if you write many different evolutionary programs, as we will be doing in the remainder of this book, you end up writing code that is very similar over and over again.

A good programmer will usually want to extract and reuse this common code. Once you have done this, you have the basis of an evolutionary computation framework. Typically this will consist of an evolution engine that is reusable and that can accept different functions to customise fitness evaluation, selection and evolutionary operators.

An alternative to using a home-grown framework is to choose a ready-made one. There are open source evolutionary computation frameworks available for most programming languages. For popular languages, such as C, C++ and Java, there are dozens.

The advantage of a ready-made framework that is used by many other programmers is that it will have been well tested and should be free of significant bugs and performance problems. It may also provide advanced features such as parallel and/or distributed processing.