# Basic creature simulation

June 27, 2020

## Introduction

For this first simulation, I made the most basic ecosystem I could think of. Little bits of green food appear at random, and a single type of creature moves around at random, eating food any food it comes across. Creatures die if they run out of food and split into two when they have a surplus. The code for this simulation and all the others in this series is here.

## Simulation details

The simulation is updated in discrete steps, called "ticks" approximately once every 20 milliseconds.

#### Food

Each tick there is a 10% chance that a piece of food will appear in a random place in the world. Food contains 500 units of energy.

#### Movement

Creatures move in the direction they are pointing 0.2 pixels per tick. There is a 5% chance a creature will change its direction each tick. This give creatures a more life-like movement than if they were moving completely randomly. When a creature touches a piece of food, it eats it, gaining its energy. Currently, creatures cannot interact with other creatures and pass straight through one another. If a creature moves off the screen, it reappears on the opposite side of the screen.

#### Metabolism

Each tick, creatures metabolise 1 unit of their energy, to represent energy lost in moving and staying alive. If a creature's energy drops to 0, then it dies. If it eats some food, then it gains 500 units of energy.

#### Reproduction

If a creature ever has more than 1000 units of energy, it splits into two creatures, with each getting half the energy.

## Population predictions

When making simulations I tend to tweak its parameters fairly randomly to see which give an interesting result. But it makes sense, especially in a simple simulation like this, to think a bit more carefully about how the parameters will affect the outcome. In this simulation all the creatures are equal, so who lives and who dies is completely random. Nonetheless, we can make some predictions about the population as a whole.

To start with, we can try to predict the average population size by thinking about the energy in the system. The simulated world is not closed: it has energy coming in, in the form of food, and energy leaving in creatures' metabolism.

The amount of energy entering the system each tick, $E_{in}$, is, on average, the probability of food appearing times the amount of energy in the food.

$E_{in} = 0.1 \times 500 = 50$

The amount of energy leaving the system each tick, $E_{out}$ is equal to the number of creatures, $n$, times the metabolic rate, $1$.

$E_{out} = n \times 1 = n$

The simulation will be stable when the change in energy, $\Delta E$, is 0. This happens when the number of creatures, $n$, is 50.

$\Delta E = E_{in} - E_{out} = 50 - n$

If there are more than 50 creatures, the energy in the system will fall over time. To start with, energy could be lost as creatures eat food and burn the energy, but eventually the number of creatures must decrease.

If there are fewer than 50 creatures, then the energy increases over time. This means that food grows faster than it's eaten, so amount of food in the system increases. Eventually, the number of creatures must also increase it will be impossible for them to avoid gaining enough energy to divide. The exception to this is when there are no creatures - the most stable situation. Once the world reaches that state, the number of creatures can never change (though the amount of food will increase forever).

## Implementation details

There are a few subtle issues with how I've made this simulation, which I think are worth mentioning in case anyone wants to build their own.

### Creature order matters

The way I've coded the simulation, food is generated at the beginning of the tick. Then one-by-one each creature moves, eats and reproduces. This means that a creature could move to a piece of food and eat it, then a second creature could move where that food was and miss out. A more correct way to implement this would be to work out which creature reaches the food first, and have that creature take the food.

In this case, I don't think it's too important, and the situation is going to be relatively rare. Since new creatures are added to the end of the array, the creatures will be in age order, so this gives older creatures an advantage which we could imagine as is experience. However, there are some cases where this sort of first-mover advantage is important and can bias your results. One simple solution would be to randomise the order of the creature array each turn so creatures don't have a consist advantage.

### Creatures swim through food

The simulation is a discrete time simulation, so we check whether creatures overlap with food in one tick, then update their positions by some amount in the next tick and check again. This represents the creature moving through space during that time period, but it's possible that it will miss the food check, especially if it moves a long way.

If a creature moves fast enough it could move through a piece of food directly in front of it. Currently creatures move 0.2 pixels per tick so it's unlikely they will miss food, but it's worth being aware of if we want faster creatures. If we were making an accurate physics simulation of colliding particles it would be worth fixing, but here it just means creatures have a slightly smaller chance of getting food than you might expect.

### Edge effects

If creatures move off the left side of the world they will appear on the right side of the world as though the world were toroidal. However, when calculating the distance between creatures and food, I do a straight calculation of distance between their coordinates in the world. This means that a creature on the extreme edge of the world should overlap with food on opposite side, but it won't until its centre crosses to the next side. I also don't display half a creature on each side, so creatures will suddenly disappear and reappear when their centre crosses an edge.

The reason for this issue basically comes down to laziness and the fact that it doesn't seem worth fixing. In later simulations I will probably remove the wrapping effect and instead have creatures collide with the edges.