How I Learned to Stop Worrying and Love Evolutionairy Algorithms

In an effort to become more organized, I want to share the stuff I program in my freetime. Because you know, somehow it helps, if you write about your projects. Makes you think them over once more.

So quite a while back, I stumbled upon evolutionary algorithms. They mimic some aspects of nature, to find good solutions to many-dimensional problems. You work with individuals, which represent possible solutions of your problem. The main methods to transform these solutions are mutation, crossover and selection.

  • Mutation, of course, means to change ones properties randomly, by a small percentage.

  • Crossover means, that certain sets of parameters (individuals) combine their parameters, to create a new set (offspring).

  • Selection means removing all parametersets (individuals), which dont't fit a certain fitness function. This often times happens at the crossover stage, simply by making it more likely, that a fit individual will mate, and less likely, that bad individuals breed.

Phew, this pseudo-bio talk sure sounds a little harsh at times.


So what got me into these algorithms was an example for a university script:

Jet nozzle, designed by genetic algorithm

It shows a two-phase fluid nozzle, that is being optimized for thrust. Amazing.

So that got me into my own humble example:

A small javascript example, that uses populations of brown dots. You can check it out here.

These want to keep their surface area to the surrounding white dots as small as possible. At the time I was both new to javascript, and new to evolutionary algorithms, so pardon my naive implementation.

The algorithm works as follows:

  1. It initializes a population, that has about 1/3 of the playing ground covered

    for (var i = 0; i < (dim * dim); i++) {
        current_pop[i] = 0;
    
        if (getRandomInt(0, 3) == 1) {
            cells++;
            current_pop[i] = 1;
        }
    }
    
  2. This population is our champion

  3. We decide how many populations to have in each generation
  4. The champion population is copied to all new populations
  5. Every population is being mutated (move each point of the population randomly)
  6. The fitness (min(surface)) is calculated
  7. The champion is being set to the best mutated population. Back to 4.

So as we can see, the algorithm does work, it produces a wall of dots, which appears to be the best configuration in discrete 2d space, if you want to keep your surface low.

different surface areas of discretized disk vs square

However the algorithm doesn't really follow most evolutionairy algorithms:

  • No crossover is implemented
  • It utilizes a variation of elitism, whereby all offsprings are copies of the champion
  • 100% chance of mutation

So a somewhat crude implementation, that still produces a nice visual outcome. Next up, I will describe another shenanigan, that includes more elements of real evolutionairy algorithms.