Predator-Prey Simulation


This was a solo project I’ve wanted to do for a long time. I’ve always been interested in natural selection and to simulate even a little piece of it is very impressive. A predator-prey relationship is a basic food chain: predator->prey->plant/small food item. This relationship is what I went for with this project.

Genetic Algorithm

I used a genetic algorithm for this project. This is a generation based algorithm where a generation lasts for a given amount of seconds. When the generation is complete, the most successful creatures reproduce to the next generation. If a creature gets a certain amount of food, or reward, they will:

(-∞, 1): Die

[1, 2]: Survive

(2, ∞): Reproduce

Reproduction Cycle Setup

In this code, all the creatures in the population get put into a sorted list based on how much food they got that generation. Everything that is <=2 gets removed from the list, as these creatures were not successful enough to reproduce. Below what is pictured in this function, it calls Breed which calls Combine, which is explained below.

Food Steal

Normally, when someone thinks about a predator-prey involvement, they think predator eats prey. I also started with simulating just that; whenever a predator came into contact with a prey, the prey would get destroyed and the predator would get rewarded. With the small playfield, this did not work as I intended where the predators would overpower the prey and there was no balance. I came up with an idea of a “food steal”, where the predators would “steal” the prey’s food instead of destroying them outright. This proved to be much easier to balance and an overall better way to continue with the simulation without making the simulation completely incorrect.

Food Steal Interaction

Genetic Algorithm Reproduction

Combine Function

When the time for each generation ends, the population manager will call this function. This will create a new creature by combining the genes of the “mom” and “dad”. It goes through all the genes that the new creature would need, and combines them from the mom and dad to create a new DNA.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s