2017-04-19

Wa-Tor

(Check out the simulator HERE)

Fig. 1. Wa-Tor simulator.
Introduction
I could not give an introduction to the world of Wa-Tor more perfectly than by quoting its inventor, Alexander Keewatin Dewdney [1]:
Somewhere, in a direction that can only be called recreational at a distance limited only by one's programming prowess, the planet Wa-Tor swims among the stars. It is shaped like a torus, or doughnut, and is entirely covered with water. The two dominant denizens of Wa-Tor are sharks and fish, so called because these are the terrestial creatures they most closely resemble. The sharks of Wa-Tor eat the fish and the fish of Wa-Tor seem always to be in plentiful supply.
Wa-Tor is a simple model of population dynamics in the context of two interacting species: one of them is prey and the other are predators (in Dewdney's implementation they are respectively: fish and sharks).

We know how that goes: in the time of abundance, the predators multiply without second thought - so much that the food now becomes depleted and they die of starvation. The prey can now graze undisturbed and returns to its high numbers quickly - and thus the cycle repeats.

A simple model for that process is given by a set of differential equations: Lotka-Volterra equations. The equations are as follows:

dx/dt = αx - βxy
dy/dt = δxy - γy

The x represents the number of prey. In time, it increases due to breeding and the number of current prey (thus the term +αx). The prey is eaten by the hunters, and the rate is dependent on how often the two species meet (the term -βxy). Similarly, the numbers of predators y grow due to the food supply (+δxy). Hunters die though - either due to accidents, or due to starvation, which is refleted by the last term: -γy. A sample solution to Lotka-Volterra equations is presented in fig. 2 (the Matlab code is also available in the simulator repository).

Fig. 2. Lotka-Volterra solution for α=2/3, β=4/3,
γ=1 and δ=1.
 
While the ecosystem described by Lotka-Volterra model never  crashes, the Wa-Tor presents a different story. It is more than likely that a selection of its parameters will result in one -or both- populations dying out. It is quite tricky to find the values that make the ecosystem of the planet stable (I already put the correct values in the simulation; if you feel adventurous you may change them).

Conveniently, the original paper of Dewdney [1] is available online, and it's sure worth taking a look at!


Rules of Wa-Tor
I shall give a brief presentation of the rules of Wa-Tor here. They are described in much finer detail in Dewdney's paper [1], but stay away from Wikipedia article (it has some bugs)!

The planet of Wa-Tor is represented as a two dimensional array of cells (squares). The array has no boundaries: if you pass through the left edge you wind up on the far right side (and vice versa). The same applies to the top and bottom edges. While boundless, the planet has only a limited surface, measured by its width and height.

The time of the planet Wa-Tor is discrete: it passes in turns (called chronons by Dewdney - it's such a cool word!). Each chronon the state of the world is updated:

  1. Each fish (prey) moves. The fish move randomly, and only to an empty adjacent square. If no free space is available, the fish stays put. If the fish had reached its reproduction age while moving, it leaves offspring (a new fish) in the space it has just left. The age is then set to 0.
  2. Each shark (predator) moves. If there are any adjacent fish, the shark picks one at random and devours it. If there are no fish in the neighbourhood, the shark moves randomly, just like the fish. If the shark has reached its reproduction age, it leaves a new shark in the square it left behind. The age is then set to 0. Each turn the shark gains an unit of starvation. If a certain starvation level is reached, the shark dies. If a shark eats a fish, its starvation level is set to 0.
  3. The screen is updated!
These steps are repeated over and over.
When speaking of adjacency, the classic Wa-Tor algorithm uses the von Neumann idea of the neighbourhood: only the squares that are directly left, right, up and down from a given square count as neighbours.

The parameters are then as follows:
  • width - the size of the world in x direction,
  • height - the size of the world in y direction,
  • initial prey - the number of fish at the start of the simulation,
  • initial predators - the number of sharks at the start of the simulation,
  • prey reproduction age - the number of turns after which a fish produces an offspring,
  • predator reproduction age - the number of turns after which a shark produces an offspring,
  • predator starvation age - the number of turns without finding prey after which a shark dies of hunger,
  •  optionally: age variance - a parameter that controls how random the initial ages and starvation levels are for newly born animals.


Wa-Tor Simulator
I made a simulator of the Wa-Tor world in JavaScript (see fig. 1). You can access it HERE. The code is available on GitHub: https://github.com/dagothar/wator.

I hope I made the simulator robust enough and parametrizable enough! You can probably change any relevant parameter (I skipped on the color customization this time though. I also wanted to put in sprites to represent the species but decided to abandon the idea. They wouldn't be readable enough on larger boards.)

Here's a list of features:
  • Wa-Tor simulation according to the original rules (plus a few modifications),
  • parametrizable: set the world size, age & starvation parameters for both prey and predators, initial species distribution,
  • watch the real-time data on the number of the prey and the predators,
  • a dynamically updated chart which shows the population stats,
  • optional: imagine the blue squares are people and the red ones are raptors!
Note: You have to 'Reset' every time you change the parameters.

It is quite captivating to observe the ecosystem evolve in time. The fish gather in groups which are then dispersed by sharks.

Here is how it looks like!
Fig. 3. An era in the world of Wa-Tor.
Fig. 4. This is how the population of the species change. The scale for the fish is on the left, the scale for the sharks - on the right.
An insightful observer may perhaps find the dynamic world of Wa-Tor a valid parallel for realities of human existence.

Notes on Implementation
The inventor of Wa-Tor supplies an abundance of pointers in his paper on how to implement the model. I tried to keep very close to the original idea, but included some changes of my own that I think improve on the performance and aesthetics of the simulation.

Dewdney suggests using one of two data structures for holding the information on fish and sharks: either a set of tables, or a set of linked lists. Both have their advantages. I use the lists to iterate through the animal instances in the update step, while still keep the tables to make the search for adjacent empty squares/prey faster.

The original algorithm calls for iterating through the table of animals each chronon and updating their state/position in turn: from the top left to the bottom right corner. This is not ideal, since it introduces artifacts in how the animals can move and group together - the ones that move first are of an advantage. I shuffle both the prey and predator lists before making the update step, so that the movement seems much more natural.

It also looks more natural when the update step for the predators is done before the prey moves. In this way, you can see the predators actually moving to the squares occupied by prey. If it were the other way around, the predators would seem to move to the squares occupied by updated prey and which were not yet displayed on the screen.

The author does recognize that while seeding the Wa-Tor world with the initial distribution of animals it is best to slightly randomize their age, so that once they reach maturity the population does not suddenly double in size. I do that by introducing a random age offset when placing new animals (also when they are born!). The offset distribution is centered at 0, so that no bias is introduced. I also vary the starvation age of the predators in the similar fashion.

Additionally, I made it possible to use the other classic rule of the neighbourhood for the model: Moore neighbourhood (4 squares on the: left, right, top, bottom + diagonals).



Can you play God and set the rules of the planet Wa-Tor such that it thrives for eternity?
Your chronon!


Literature
[1] A. K. Dewdney, Sharks and fish wage an ecological war on the toroidal planet Wa-Tor, Scientific American, December 1984, pp. 14-22, available online: http://home.cc.gatech.edu/biocs1/uploads/2/wator_dewdney.pdf

No comments:

Post a Comment