Genetic Programming

I and some friends had heard about genetic programming and one day Carl told me about his attempt. He had let Brainfuck-programs try to print out some strings. For each generation, the best half survived and split into two.

I got home and started making my own environment for genetic programming. Firstly, I made up a simple programming language. There were 16 registers, and instructions which were packed into 4 byte (one byte opcode and then three bytes arguments) could, for example, add two registers and put the result into a third, or compare two integers and if the first was greater than the second, jump to anyplace else in the code, determined by the third argument. It was very alike assembly, but a lot simpler.

Then I wrote an intepreter, and some code that evaluated the programs and let them struggle for the limited amount of food. For every generation, some dies, and some survives and reproduces themself. Some random mutation is then introduced into the new copies.

The result was succesful. In about 200 generations, with an average of 1000 programs each, they learned how to print strings like abcde..... by making instructions that first loaded a register with 'a', then intstructions to print out the register, increase it by 1, and jump to the beginning.

Carl took a look at my code and made several improvements. Now we have observed the programs printing out the arithmetic serie 1,2,3... in 10-20 generations, the geometric 1,2,4,8,16... in 20-30, square numbers in 400 generations, factorials in 3000 generations, fibonacci integers in 5000 generations and cubes in 7000 generations. These are our best records and in most cases it takes more than double this time. Simulating 1000 generations on my computer (a Duron 750mhz) takes approximately 40s-2min.

You can look at the program here. The application is in Swedish, but I think there is no problem understanding it. In the beginning, it asks you for some numbers which is what you want the program to output. The first question is how many. Try to input the first square numbers or something.

If you drop me a letter, I can mail you the sourcecode.

An example - the programs mutated into this code, which outputs squares:

7c: 7-4->1 (put register 7 minus register 4 into register 1) 90: 8=25834 (set register 8 to 25834) a4: u0 (increase register 0 with 1) 114: uE 144: 8+6->C 19c: 0*E->0 1b0: o0 (print register 0) 1c4: E+7->0 228: g5c (goto line 5c) 258: E=26359 2c4: oE 314: 3<A=>gac 3c4: 5%5->5 3d8: 6>9=>g13c 3ec: !E It's interesting to see how ugly the code is. For example, everything after line 228 is completely unnecessary. And many lines above, too.

malfunction.org | Erik Bernhardsson.