Adam Bandel


Predator-Prey Neural Net Simulation

May 2025
Type: game
Code: 8k lines
Files: 112
Active: May 2025 to May 2025
Stack:
PythonPyGameModernGLNumPyPyOpenGL
Tags:
aisimulationgame-dev

Overview

Predator-Prey Neural Net Simulation is a real-time ecosystem simulation in which every creature is driven by a small neural network. Prey look for food and run from predators, predators hunt to survive, and both species evolve through inheritance and mutation. The world has procedurally generated terrain, weather that shifts between sun, rain, drought and cold snaps, a day/night cycle, and an OpenGL renderer for hardware acceleration.

The rules are simple: creatures whose genes and networks suit their conditions live longer and reproduce more. Population cycles and evolutionary pressure come out of that on their own.

Screenshots

Simulation Overview

Creature Interactions

Day/Night Cycle

Problem

Artificial life simulations often rely on hand-coded behavior rules, which produce predictable outcomes. I wanted to see whether meaningful survival strategies could come out of neural networks shaped by natural selection instead of training, and whether complex population dynamics could come out of simple genetic rules.

Approach

Nothing is trained. Each creature’s network weights are part of its genome, so behavior is inherited, mutated and selected along with everything else.

Stack

Challenges

Outcomes

Prey populations develop faster movement genes when predator pressure is high. Predators evolve better perception when prey get harder to catch. The networks are small, 4-5 inputs and 5 outputs, and evolution alone still gets them to survival strategies that work.

It holds 60 FPS with more than 300 active entities. Entity classes use __slots__ to save memory. Simulation, rendering and AI are separate modules, and there are more than 30 test modules.

Implementation Notes

Neural Network Evolution

Each creature’s brain is a simple feed-forward network where weights are encoded in its genome:

# Prey brain inputs: [hunger, fear, light_level, food_distance]
# Outputs: [food_weight, flee_weight, mate_weight, wall_weight, wander_weight]

class FeedForwardNet:
    def forward(self, inputs: list[float]) -> list[float]:
        x = inputs
        for i, (weights, bias) in enumerate(self.layers):
            x = [max(0, sum(w * xi for w, xi in zip(row, x)) + b)
                 for row, b in zip(weights, bias)]  # ReLU activation
        return x

Offspring inherit the average of their parents’ weights with a 10% mutation rate, so behavior that helps can spread through the population.

Spatial Indexing Strategy

The uniform grid is the default index, and the quadtree is still available as an alternative:

class UniformGrid(SpatialIndex):
    def query_circle(self, cx, cy, radius):
        # O(1) cell lookup + O(k) entity checks where k << n
        min_cell_x = int((cx - radius) / self.cell_size)
        max_cell_x = int((cx + radius) / self.cell_size)
        # ... check only cells that intersect the query circle

Environmental Systems

Weather uses a Markov chain model where state transitions affect plant growth:

State Sunny Rain Drought Cold Snap
Sunny 0.7 0.2 0.05 0.05
Rain 0.3 0.5 0.1 0.1
Drought 0.4 0.1 0.4 0.1
Cold Snap 0.3 0.2 0.1 0.4

Rain increases plant spawn rates by 1.5x, creating seasonal abundance that drives population cycles.

View on GitHub →


Related Posts

No posts yet.