Predator-Prey Ecosystem: A Real-Time Agent-Based Simulation

Fox=1.;
Rabbit=2.;
maxRabbits=500.;
maxFoxes=500.;
dummyPoint={-1.,-1.};

initialAgents[]:=Developer`ToPackedArray[
Table[
Join[
RandomReal[{0,1},2],(* Location *)
{0.,If[RandomReal[]<0.1,Fox,Rabbit]}
]
,{100}
]
]

visualize[agents_]:=Module[{foxes=Take[#,2]&/@Select[agents,(#[[4]]==Fox)&],rabbits=Take[#,2]&/@Select[agents,(#[[4]]==Rabbit)&],dummyList={dummyPoint}},
Show[
(* Plotting foxes *)
Graphics[{
AbsolutePointSize[10],RGBColor[.9,.42,.17],
Point@If[foxes=={},dummyList,foxes]
}],

(* Plotting rabbits *)
Graphics[{
AbsolutePointSize[10],RGBColor[.48,.11,.56],
Point@If[rabbits=={},dummyList,rabbits]
}],

ImageSize->{375,375},
AspectRatio->Automatic,
Frame->False,
Axes->False,
PlotRange->{{0,1},{0,1}}
]
]

updateAgents[agents_,rabbitGrowthRate_,rabbitMobility_,foxGrowthRate_,foxMobility_,foxEndurance_]:=Module[{
foxPop=Length[Select[agents,(#[[4]]==Fox)&]],
rabbitPop=Length[Select[agents,(#[[4]]==Rabbit)&]],
nearestFoxes=Nearest[Append[Take[#,2]&/@Select[agents,(#[[4]]==Fox)&],dummyPoint]],
nearestRabbits=Nearest[Append[Take[#,2]&/@Select[agents,(#[[4]]==Rabbit)&],dummyPoint]],
i,nextLocation,survivors
},

Table[

survivors={};

nextLocation =agents[[i,{1,2}]]+
If[agents[[i,4]]==Rabbit,
RandomReal[{-rabbitMobility,rabbitMobility},2],
RandomReal[{-foxMobility,foxMobility},2]
];
nextLocation=Clip[nextLocation,{0.,1.}];

If[agents[[i,4]]==Rabbit,

(* rabbits' behavior *)
If[
Norm[nearestFoxes[nextLocation][[1]]-nextLocation]>0.02,

survivors={Join[nextLocation,{0.,Rabbit}]};
If[RandomReal[]<rabbitGrowthRate(1.-rabbitPop/maxRabbits),
AppendTo[survivors,Join[nextLocation,{0.,Rabbit}]]
];(* Probabilistic reproduction with logistic growth restriction; adding one more rabbit *)

],

(* foxes' behavior *)
If[
Norm[nearestRabbits[nextLocation][[1]]-nextLocation]>0.02,

If[agents[[i,3]]<foxEndurance,
(* starving but still survive *)
survivors={Join[nextLocation,{agents[[i,3]]+1.,Fox}]};
],

(* just ate rabbit(s) and survive *)
survivors={Join[nextLocation,{0.,Fox}]};
If[RandomReal[]<foxGrowthRate(1.-foxPop/maxFoxes),
AppendTo[survivors,Join[nextLocation,{0.,Fox}]]
];(* Probabilistic reproduction; adding one more fox *)

]
];

survivors

,{i,Length[agents]}

]//Flatten[#,1]&//Developer`ToPackedArray
]

Manipulate[
Refresh[
If[moving,ag=updateAgents[ag,rabbitGrowthRate,rabbitMobility,foxGrowthRate,foxMobility,foxEndurance]];
visualize[ag]
,UpdateInterval->If[moving,0,Infinity]
]
,{{rabbitGrowthRate,0.5,"growth rate of rabbits"},0,1,ImageSize->Tiny}
,{{rabbitMobility,0.03,"mobility of rabbits"},0,0.05,ImageSize->Tiny}
,{{foxGrowthRate,0.5,"growth rate of foxes"},0,1,ImageSize->Tiny}
,{{foxMobility,0.05,"mobility of foxes"},0,0.05,ImageSize->Tiny}
,{{foxEndurance,10,"number of steps\nfoxes can survive\nwithout food"},1,20,1,Appearance->"Labeled",ImageSize->Tiny}
," "
,{{moving,False,"run simulation"},{True, False}}
,{{ag,initialAgents[]},ControlType->None}
," "
,Button["Reset",moving=False;ag=initialAgents[],ImageSize->Medium]
,SaveDefinitions->True,AutorunSequencing->{6},TrackedSymbols:>{moving,ag},
SynchronousUpdating->True,ControlPlacement->Left
]

CAPTION

This Demonstration simulates the dynamics of predators (foxes, in orange) and prey (rabbits, in purple) in a 2D bounded square habitat. An individual of each species is simulated as a particle moving in a random walk. The initial condition is such that there are 100 particles randomly distributed in the space, 10% of which are foxes and the rest rabbits. A rabbit particle can replicate itself at a given growth rate. A fox particle has to eat (i.e., collide into) a rabbit particle in order to replicate itself, and will die if it doesn't eat any rabbit particles for a certain time period. The maximum population of each species is bounded to 500. Depending on the parameter settings, this ecosystem shows several distinct behaviors, including extinction of foxes (or both species), oscillation of populations, and spatial waves of foxes chasing clusters of rabbits. You can stop and restart the real-time simulation using the "run simulation" checkbox.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Predator-Prey Ecosystem: A Real-Time Agent-Based Simulation" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/PredatorPreyEcosystemARealTimeAgentBasedSimulation/

Contributed by: Hiroki Sayama

A full-function Wolfram Mathematica system (Version 6 or higher) is required to edit this notebook.
GET WOLFRAM MATHEMATICA »

© Wolfram Demonstrations Project & Contributors   |   Terms of Use   |   Make a new version of this Demonstration »