First let’s make sure we are all on the same page here. Earth wasn’t made 6000 years ago and all things on this planet were not created by some grand designer called “God” (or whatever you call him/her). We see evolution as Darwin explained it to us, everything starts somewhere and with more or less random mutation and survival of the fittest you get to the results you see today. With that out of the way, let’s continue.

The goal of this article is to create a simple program that will allow you to “evolve” something into something else. Evolution here means that we start with some form of DNA and we derive next generations from it by random mutation. In our program we are going to generate images from the DNA and the user will select which image is used as a basis for the next generation.

The coding itself will be done in ActionScript 3 but should translate well to other languages.

What we need

Everything is based from some sort of DNA. In our case DNA is a string of numbers. We need something that gives us a DNA string. At first it should be randomly generated but the next generation should be based on some previous DNA. We also need a way to randomly mutate any given DNA to produce a variation of it.

The other main function should be to generate an image out of a DNA string. An image consists of colors and points. This function should always generate the same image from the same DNA string. And any change in the DNA should have a maximum effect on the resulting image; this is also called the avalanche effect.

The program

The final program will look like this (without the text).

The user will be able to click on any of the children which will generate the next generation based on the selected image and so forth. As you can see most of the children resemble the parent pretty closely, but with a closer look they should all have something different. Of course each generation can have some major changes as well. (Someone with 6 toes for example)

Let’s go

The theory behind this program is simple, but it’s interesting how you go about generating the DNA and the resulting image from that DNA string so I’ll explain how I approached this problem.

Generating the DNA string

The DNA represents everything about the image, a complete DNA string consists of several elements. In this case you could generate an image from a DNA with 10 elements, but it wouldn’t be anything interesting so we need “a lot” of elements. The elements in this example are integers that go from 0 to 9. Just like how our base DNA elements are A, C, G and T (look it up if you want to know more about our DNA).

We have to start somewhere so the first DNA string should be generated by itself, but the others are all derived from a previous DNA string. We can do this simply by adding a parameter in our function that defaults to null and check whether we have a “parent” or not.

If we are deriving from a previous DNA string, then we should allow a certain amount of mutations, we are assuming that evolution progresses in small steps and over a long time span (the other theory is that evolution happens in big steps over a short amount of time, people are still debating over this theory). We can also make sure that there is a certain degree of mutation.

So now we have our DNA string and we can generate a mutated version by simply giving the original DNA string as a parameter in the constructor.

Generating the image

With the DNA string we are halfway done; the other half is visualizing it. From the DNA string, essentially a group of numbers, we need to derive coordinates and colors. I’ve tried a few ways of doing this and for now settled, the problem here is that we want to have some sort of avalanche effect when an element in the DNA string changes. This means we cannot simple group 1234 as x = 12 and y = 34, a rather boring image would result from that code.

Here’s what I do. The x value is the sum of a defined amount of numbers defined by squaring the current number, we modulo every step so it always falls in the range of the list. The y value is basically the same but instead of looking in the list from beginning to the end, for y you go from the end to the beginning. Here’s an example for a few x values:

As a final step we multiply the x value and modulo it with 100, the same goes for the y value. Also notice that we are working without knowing the dimensions of the image. All coordinates will go from 0 to 100, which will make it pretty easy to rescale it to any given dimension.
To make our coordinates list a bit bigger (double it) we use the original set to make a “new” one. Where the x value is 100 – y and y is 100 – x.

Great, we’ve got our coordinates all we need is some color. As you know every pixel is divided in 3 values (4 with transparency) red, green and blue. The hexadecimal representation goes from 0x000000 to 0xFFFFFF (256 * 256 * 256 is about 16.5 million colors) and is set up as 0xRRGGBB. The way I generate the colors is far from ideal. It depends a lot from the coordinates (obviously because I use the coordinates to generate the colors…). I do get many color variations so for now this is good enough.

The idea behind this code is again to go from 0 to 100 (eventually from 0 to 255 because it gets multiplied by 0xFF). The R value looks at current coordinate value and at an offset from the end of the coordinate list. G does this as well, but switches the x and y values. I got B by playing with the values a bit until I got the right amount of color variation. I finally bit-shift the values and get them in the 0xRRGGBB format.

To get a better idea look at the complete source code. I tried to get as much “randomization” without really using a random function. The code itself isn’t all that difficult, the evolution part here is that we can mutate a given input and get “major” changes in the output (images).

The only code needed for the application itself is to let the user select which image looks better and generate a new generation from that image. This process can go on forever. If you would keep a list of DNA strings, you could eventually print them out and see all the changes from generation to generation.

I hope you found this subject interesting, I did. This was a first try at creating something like this; the logical next step would be to evolve with some goal (in nature there is no goal, just improving what is already there).

To see other small tests check out my site: Mathiasverboven.net , there is no source code but you can always ask.