Hare-Lynx: Interactively Explore Population Dynamics

Introduction

This notebook uses a SystemModelParametricSimulate object to interactively explore a typical predator/prey system. The model requires the free SystemDynamics Modelica library, which can be downloaded from here.

Explore the available scenarios by evaluating each cell in order. Evaluation is done by selecting a cell and pressing Shift+Enter.

In order to get the full experience of this example, you need the following:

Wolfram SystemModeler; a free trial download is available at www.wolfram.com/system-modeler

The model that came in the same bundle as this notebook

For more information about Wolfram SystemModeler, visit www.wolfram.com/system-modeler.

Initialize

Define the model name:

mmodel="HareLynxPopulation";

Load the model:

Import[FileNameJoin[{NotebookDirectory[],"HareLynxPopulation.mo"}]]

Simulation

First, create a parametric simulation object with the SystemModelParametricSimulate command. Specify which values should be extracted from the simulation (hare and lynx population values) and also which parameter values should be changed between subsequent simulations (initial populations and birth factors):

sim=SystemModelParametricSimulate[mmodel,{"hare","lynx"},{"HareInitialPopulation","LynxInitialPopulation","HareBirthFactor","LynxBirthFactor"}]

This object can be now be called with different parameter values and will return the time values for the population levels:

res=sim[150000,25000,1.5,0.3]

This can be used to, for example, plot the lynx population using the given parameter values:

ListLinePlot[Part[res,2],PlotRange->All,PlotLabel->"Lynx Population"]

Since the simulation model has been compiled, performing new simulations is very fast:

Quantity[Part[AbsoluteTiming[sim[150000,25000,1.5,0.3]],1],"Seconds"]

Analysis

With the parametric simulation object, different custom visualizations and interactions can be constructed. Here a parametric plot of the hare versus lynx population is plotted on top of a locator pane where the initial conditions can be chosen.

First, define start values to use and the allowed region for the initial values:

pts={100000,1500};
hareBirthFraction=1.25;
lynxBirthFraction=0.25;
allowedRegion=Rectangle[{20000,750},{100000,2000}];

Also define some functions to help with updating the plot:

evaluateParams[pts_,hareBirthFraction_,lynxBirthFraction_]:=Through[Apply[sim,Join[pts,{hareBirthFraction,lynxBirthFraction}]][t]];
intervalTest[pts_]:=pts∈allowedRegion;
updatePlot[]:=simData=Evaluate[evaluateParams[pts,hareBirthFraction,lynxBirthFraction]];
updatePlot[];

The following command will bring up a visualization that can be interacted with. Move the crosshair to change the initial population levels and drag the sliders to change the parameter values:

showBoundry=False;
Column[{
Row[{Slider[Dynamic[hareBirthFraction,{Function[val,hareBirthFraction=val],updatePlot[]&}],{1.25,1.5}],Spacer[10],Dynamic["Hare Birth Factor: " <> ToString[Round[hareBirthFraction,0.01]]]}],
Row[{Slider[Dynamic[lynxBirthFraction,{Function[val,lynxBirthFraction=val],updatePlot[]&}],{0.25,0.5}],Spacer[10],Dynamic["Lynx Birth Factor: " <> ToString[Round[lynxBirthFraction,0.01]]]}],
LocatorPane[
Dynamic[pts,{Function[{},showBoundry=True],Function[val,If[intervalTest[val],pts=val]],Function[val,showBoundry=False;If[intervalTest[val],updatePlot[]]]}],
Dynamic[
ParametricPlot[simData,{t,0,15},
ImageSize->500,AspectRatio->1,PlotRange-> {{0,150000},{0,3000}},PlotStyle-> ColorData[97][1],
 Epilog->Join[
If[showBoundry,{LightPink,Opacity[0.4],allowedRegion,Opacity[1]},{}],
Prepend[Table[Tooltip[Arrow[Transpose[simData/.t-> {\[FormalT],\[FormalT]+0.2}]],"Time = "<>ToString[N@\[FormalT]]],{\[FormalT],Subdivide[1,10,9]}],ColorData[97][1]]
],
AxesLabel-> {"Hares","Lynx"}],
TrackedSymbols:> {simData,showBoundry}]],
Dynamic[Row[{
Labeled[Plot[simData[[1]],{t,0,15},ImageSize->250,AxesLabel->{"Years","Population"}],"Hares"],
Labeled[Plot[simData[[2]],{t,0,15},ImageSize->250,AxesLabel->{"Years","Population"}],"Lynx"]}],
TrackedSymbols:> {simData}]
}]

Terms and Conditions of Use

This domain example is an informational resource made freely available by Wolfram Research.

Use of This Example

• You may not use this example for any purpose that is unlawful or dangerous.
• You assume total responsibility and risk for your use of this example.
• You may not present this example with any alteration to its trade dress, trademarks, or design.

License

All content in this bundle is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. By accessing the content or using it in any way, you accept and agree to be bound by the terms of this license. If you do not agree to these Terms of Use, you may not use this content. Wolfram Research reserves the right to change, modify, add to, or remove portions of these Terms of Use at any time without notice. Please refer back to www.wolfram.com for the latest Terms of Use.

A summary of the licensing terms can be found at:
http://creativecommons.org/licenses/by-nc-sa/3.0

The full legal code can be found at: 
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode