This simulates the interaction between predators and prey with a classical Ising model on a 2D lattice, using a Monte Carlo algorithm that accepts states based on the Metropolis-Hastings rule.
The simulation models prey reproduction when empty space is available, predators hunting prey, and predators starving if no prey are nearby, using a Boltzmann probability to govern whether state transitions will occur.
As the system evolves over time it can capture emergent behavior such as predator-prey cycles, spatial clustering, and extinction events.
The classical Ising Model is used in statistical mechanics to describe interactions between binary spin states. In this project, we use the Ising model to model predator-prey dynamics by assigning discrete states to represent Prey (+1 or πΈ), Predator (-1 or π¦), and Empty space (0 or β¬).
Each site in the lattice interacts with its nearest-neighbors and follows the Metropolis-Hastings rule to determine if it's state should change.
The system is modeled as a 10Γ10 grid. Each cell represents a local ecosystem state.
For 1000 simulation steps
- Select a random site (i, j)
- Determine valid state transitions according to the following rules:
- Prey will reproduce if an empty neighbor exists, turning β¬ β πΈ
- Predators hunt prey, thereby growing their population and turning prey into predators, turning πΈ β π¦
- Predators starve if no prey are nearby, turning π¦ β β¬
- Compute energy change ΞH using the Ising-style Hamiltonian
where sα΅’ is the state of site i, and nearest-neighbor interactions drive the system - Apply the Metropolis-Hastings rule: always accept state transitions that lower the system energy, only accept state transitions to higher-energy system configurations with probability:
- Update the grid accordingly
Neighboring predator-prey interactions modify system energy. The probability of accepting state changes is governed by the Boltzmann factor
The lattice is shown before and after the simulation, represented by emojis: Prey = +1 = πΈ, Predator = -1 = π¦, and Empty space = 0 = β¬.
Enter the development environment with
nix develop
The following development commands are also made available:
watch-project
- Build the project, start the development server, and watch for changesbuild-project
- Build and bundle the application without servingserve-project
- Build and serve the application without watching for changesspago build
- Build the project (PureScript compilation only)spago test
- Run testsspago repl
- Start a PureScript REPL session