cellular-automata

Explanation of the Predator-Prey Simulation Algorithm

Important Note

The algo runs much better in the 14.45 version of the Turtles System than the 14.47 version of the Turtles System, for reasons unknown to me. Here, there is a demo of the algorithm on the 14.45 and 14.47 version. Click on the image to redirect to the YouTube video, or check it out at DEMO

Demo

Introduction

The provided algorithm simulates a predator-prey ecosystem within a grid environment, where wolves (predators) and sheep (prey) interact with each other and with their environment, represented by grass. This simulation is a simplified model of real-world ecological dynamics, capturing essential aspects like reproduction, starvation, and overcrowding.

Environment Setup

Map Parameters

The simulation environment is a nxm grid where each cell can contain a wolf, a sheep or grass. The initial population of wolves and sheep is determined randomly based on predefined spawn probabilities:

Entity Probabilities

Populating the map
Reproduction
Starvation
Random Consumption
Overcrowding

This setup creates a diverse environment where the initial number of entities varies with each simulation run, introducing variability and mimicking natural ecosystems’ unpredictability.

Entity Behavior

Wolves (Predators)

Wolves move through the grid, seeking adjacent sheep to eat. If a sheep is found in a neighboring cell, the wolf moves to that cell, consuming the sheep, which is then removed from the grid. If no adjacent sheep are available, wolves have a chance of starving and are removed from the grid if they don’t eat a sheep.

After eating a sheep a wolf can reproduce, the offspring will be spawned at the wolf’s original position before it moved to eat the sheep.

Sheep (Prey)

Sheep behave similarly to wolves but seek grass instead of other entities. If adjacent grass is available, a sheep will move to that cell, consuming the grass. If no grass is nearby, sheep have a chance of starving.

After eating grass, sheep reproduction follows the same rules as wolves, a chance to reproduce if there’s available space.

Overcrowding

Both wolves and sheep face the risk of death due to overcrowding. If more than three wolves or sheep are adjacent to a cell, the entity in that cell dies, simulating natural mechanisms where high population density can lead to increased competition for resources and higher mortality rates.

Initial Setup of Envionment

initial_setup

In the image above, we see that we have populated the environment. Our parameter are as follows.

# Set up probabilities (change it as you like to see different simulations)
wolf_initial_spawn=10 # Probability of initially spawning wolf
sheep_initial_spawn=40 # Probability of initially spawning sheep

wolf_reproduction_prob=100  # Probability of wolf reproduction
sheep_reproduction_prob=50  # Probability of sheep reproduction

wolf_starvation_chance=80  # Chance of wolf dying if not eating
sheep_starvation_chance=10  # Chance of sheep dying if not eating

overcrowding_death=True # True if overcrowding can cause deaths

random_walk_wolf=20 # Probability of wolf not eating an adjacent sheep
random_walk_sheep=10 # Probability of sheep not eating an adjacent grass

Each number represents the likelihood that an event happens. For example, wolf_initial_spawn=3 means that there is a 3% chance that a wolf will spawn into any grid.

Simulation Loop

The simulation progresses in cycles, where each cycle involves randomly selecting a grid cell and executing the behavior logic for the entity in that cell. This process includes movement, eating, potential starvation, reproduction, and overcrowding checks.

The simulation continues as long as there are BOTH wolves and sheep present in the grid, reflecting an ongoing ecosystem. However, if one species goes extinct, the dynamics change significantly, demonstrating the interdependence of species within ecosystems.

In the case the sheep goes extinct, the wolves cannot find prey to eat, and therefore will eventually go extinct. However, assuming that plants will never go extinct (the case is very rare), the sheep can still coexist alongside the plants in an equilibrium.

Simulation Environment Variations & Expected Outcomes

By adjusting various parameters in the simulation, we can explore how different conditions affect the predator-prey dynamics. Here are some variations and their potential impacts on the ecosystem:

Variation 1: Increased Wolf Reproduction Probability

Variation 2: Decreased Sheep Starvation Chance

Variation 3: Overcrowding Death Disabled

Variation 4: High Random Walk Probability

Variation 5: High Initial Spawn Rates

By experimenting with these variations, observers can gain insights into how different factors influence population dynamics in predator-prey systems. These simulations can serve as a simplified model for understanding more complex ecological and biological systems in the real world.

Conclusion

This algorithm offers a simplified yet insightful representation of predator-prey dynamics, illustrating how individual behaviors can lead to complex system-level behaviors in ecology. By adjusting parameters like spawn probabilities and starvation chances, users can explore different ecological scenarios, gaining a deeper understanding of the delicate balance within natural ecosystems.

How the code works!

Helper Function

has_adjacent(x, y, target)

has_adjacent()

This methods takes in a coordinate x, y and a target to checks whether there are any matching targets around the block. Imagine you are in the centre of a 3X3 grid, we check the surrounding 8 blocks for any that matches the target. The target can either be grass, sheep or wolf. It returns True if target exists, else False. As shown below:

grid

is_overcroweded(x, y)

is_overcrowded()

This method takes in coordinates x, y and checks if there are more than 3 wolves OR sheep in the surrounding blocks. If condition is fulfilled, it will return True, else False.

set_adjacent(x, y, target, source)

set_adjacent()

This method takes in coordinates x, y as well as the target and source. It checks if any surrounding blocks match the source and replaces it with target. It does this by checking through every surrounding block and appending the coordinate of the blocks that match source into a list. The index is then randomised to pick a random valid source to be replaced with target. This allows the animals (wolf/sheep) to perform a random walk. Returns True is random walk is succesful, else False.

Main Algorithm

main program intro

wolf program

sheep program