Loading...
evolve the future
Evolution the way God intended

Writing your first animal

Animals here are written in a custom assembly code for a stack machine. The code is written in JSON literal array form as an array of arrays. Each instruction is an array of [operation,operand]. Some operations don't require an operand, and in these cases the operand is ignored.

Reproduction

The only required function that your animal must perform is reproduction. If a species does not reproduce, then it's species is a failure and will not be stored to the genebank. Refer to the api for more detailed information.

The tree is an animal that only reproduces itself. Lets look at how it works.

There are three main stages to actually reproduce.
  1. Allocation
  2. Writing the child
  3. Division

Allocation

Before an animal can start writing the childs genome, it must first allocate memory to fit new animal. Allocation has an expense (5*requested size) in cputime, and this allocation will fail if the animal does not have enough cputime. So, checking if allocation succeeded is probably a good idea, and if it failed the animal can sleep to try and build up more cputime.

Ignore line 1 for the moment. Alloc allocates space at the end of the animal, the amount of memory to allocate is popped from the stack. So pushMemSize puts the current animals memory size on the stack. alloc pops that off and attempts to allocate that amount of memory, and then puts true/false on the stack to indicate success. ifNotDo (line 4) pops a value off the stack and if it is false, execution goes straight to line 5. If true, execution jumps to ["nop",200]. If allocation fails, line 5 tells the animal to sleep for 450 loops, and then line 6 jmps back to ["nop",5] to try allocation again.
["nop", 5],
["pushMemSize", 0], 
["alloc", 0], 
["ifNotDo", 200],
["sleep", 450],
["jmpB", 5],
["nop",200],
All of the jmp* instructions move a particular pointer to point to a named nop instruction. On line 1 we are moving the read pointer to the start of the animal, and on line 2 and 3 we are moving the write pointer to the end of the animal, and then 1 past that, into the newly allocated space.
["jmpReadPtrB", 5],
["jmpWritePtrF", 2],
["incWritePtr", 0],

Write the child

Here is the actual copy loop. Line 2 copies from the read pointer to the write pointer. Then on line 3 and 4 the read and write pointers are incremented. On line 5 and 6, the current write pointer and total memory length are pushed to the stack. Line 7 pops and compares those two values and puts back a boolean. On line 8, if we are finished (false), jump the execution pointer to ["nop",6], and finish the copy loop. If we aren't finished, then jmpB to ["nop",1].
["nop", 1],
["copy", 0], 
["incReadPtr", 0], 
["incWritePtr", 0],
["pushWritePtr", 0], 
["pushMemSize", 0], 
["lt", 0], 
["ifDo", 6], 
["jmpB", 1],
["nop", 6], 

Divide

Attempt to divide, and check success. If it worked, then jmpB all the way to the start of the animal. The new animal has been placed in front of this animal. Divide won't work if there is no available space to put the child. If it didn't work jump to ["nop",9]. Here the animal will turnR 4 times, attempting to divide. If after 4 times it still fails, then it sleeps for 450 loops, and then tries again.
["divide", 0], 
["ifDo", 9], 
["jmpB", 5],
["nop", 9],
["incCounter", 0], 
["pushCounter", 0], 
["push", 4], 
["gte", 0],
["ifDo", 8],
["sleep", 450], 
["resetCounter", 0], 
["nop", 8], 
["turnR", 0],
["jmpB", 6],
["nop", 2] 

Run your animal

In the sidebar there is a section "Create a New Species", put your animal's code here to test it out. The name that you choose will be given to this animal and all descendants of this animal. It should be in a JSON literal format, but without the surrounding "[" and "]".

Debugging

When writing your animal, you might not get it right the first time. I certainly didn't. Some debugging tools are provided. When you create an animal, or if you click on an animal, then details of that animal will be shown in the sidebar in the "Single Process Information" section.

Here you can examine the state of the animal, and of each thread.

News