Ranging from predator - prey system, to trade markets, automobile traffic, thermodynamics, fluid dynamics and so on, agent-based models (ABM) encompass a wide variety of applications . Seemingly trivial appearing update rules (~equations of motion) yield a plethora of unexpected complexity, such as swarm synchronization, phase transitions, or the spread of diseases . Thus it comes to no surprise that many programming languages tailored to the execution of ABMs have been developed since the inception of ABMs in the late 1940’s. While plenty of different adaptions of ABMs for Mathematica have been explored, a concise and intuitive implementation yet has to be shaped. The aim of this work is to grant a simple agent simulation function encompassing both, pre-defined models as well as customizability hosting many of the applications above. In the following, the two implemented models (Predator-Prey & Conway’s Game of Life) are laid out in short, followed by two motivations on how-to create different models building on the existing paradigm.
Available Models
Available Models
In general, the model is divided into different parts:
◼
Model | Static
◼
Grid/Network | Static
◼
Agents | Dynamic
◼
Individual Update Rules | Dynamic on creation of new agent
◼
Collision/Interaction | Static
First, a grid/network is generated, which is held static throughout the simulation. Then, agents are generated based on the model and the related model parameters. Every single agent is equipped with a set of update rules, which informs it about is surroundings and then how to act upon new information. The user can also add/overwrite any rule that can be attributed to a single agent. Unless such an update rule makes changes to the rules themselves, these rules do not change throughout the lifetime of the agent. On Collision, however, new agents can be created in the case of the predator-prey model. The new generation inherits the update rules of one of their parents at random. A collision, in this framework, is triggered, when there are at least two agents sharing the same node of the network.
Predator Prey
Predator Prey
Idea
Idea
Individuals run around, grazing and chasing, reproducing and dying from hunger. In general, the movement of the agents can depend on their vision, although, in the base model, prey just wiggles around randomly. On collision, agents reproduce and predators eat prey.
Attributes
Attributes
Information that each individual has together with pre-defined dynamic behavior.
◼
Index (Name) | Static, depends on parent generation
◼
Coordinate | Dynamic
◼
VertexNumber | Dynamic
◼
Rile (Predator/Prey) | Static
◼
Birth Rate | Static, identical for predators and prey
◼
Death Rates | Dynamic for predators and constant 0 for prey
◼
Velocity -> 2D Vector | Dynamic
◼
Maximum Speed |Static/Dynamic: dependent on nearest neighbor network
◼
VisionRadius | Static (5)
◼
Neighbours | Dynamic, euclidian distance
◼
UpdateFunctions | Static in the base model
Rules
Rules
Individual
◼
observe --> Update Neighbours
◼
die --> Die at random according to deathrate
◼
accelerate --> Change velocity based on individual attributes
Move: It follows a movement phase based on the current velocity of the agents. For the case of agents sharing momentum, bounce back boundaries can be employed.
Move: It follows a movement phase based on the current velocity of the agents. For the case of agents sharing momentum, bounce back boundaries can be employed.
Collision
◼
eat --> All prey that shares a node with a predator is deleted
◼
starve --> If predators meet without eating, their death rate increases
◼
procreate --> According to the fertility of all colliding agents, a new agents spawns with its attributes inherited from on of its parents
Conway’s Game of Life
Conway’s Game of Life
Idea
Idea
Each node on a 2D grid of set dimensions has either property among alive/dead. This state changes based on the local neighbourhood and it is the defining system variable.
Attributes
Attributes
◼
VertexNumber | Static
◼
Coordinate | Static
◼
Neighbourhood | Static
◼
Neighbours | Dynamic (while the neighbours don’t change their position, their “alive” state changes nevertheless)
◼
Alive (True/False) | Dynamic
◼
UpdateFunctions | Static
Rules
Rules
◼
observe --> Update Neighbours
◼
If alive with 2-3 Neighbours --> Alive
◼
If dead with exactly 3 Neighbours --> Alive
◼
All other cases --> Dead
Possible Adaptions
Possible Adaptions
Topology
Bounce Back Boundary
Bounce Back Boundary
Grid
Grid
In[]:=
createGridBounceBack[n_Integer?Positive,m_Integer?Positive,gridType_:"Rectangular"]:=Module[{surface,neigbourCoordinates,bulkDegree},(*Functions*)surface[grid_,boundaryVertex_]:=Module[{surfaceVector,position=PropertyValue[{grid,boundaryVertex},VertexCoordinates]},surfaceVector={position[[1]]/.{1.->-1.,N@n->1.,_->0},position[[2]]/.{1.->-1.,N@m->1.,_->0.}};surfaceVector/Norm@surfaceVector];(*Values*)Clear[grid,gridCoordinatesToIndex,gridBoundaries,gridNeighbourhood,gridBoundaryNormalVectors];Switch[gridType,"Rectangular",grid=GridGraph[{m,n}];bulkDegree=4,"RectangularDiag",grid=NearestNeighborGraph[GraphEmbedding@GridGraph[{m,n}],DistanceFunction->ChessboardDistance];bulkDegree=8;];gridCoordinatesToIndex=AssociationThread[GraphEmbedding@grid,VertexList[grid]];gridBoundaries=VertexList[grid,_?(VertexDegree[grid,#]<bulkDegree&)];gridBoundaryNormalVectors=AssociationThread[gridBoundaries,surface[grid,#]&/@gridBoundaries];gridNeighbourhood=generateNeighbourhoods@grid;]
Actual Bounce Back Move
Actual Bounce Back Move
Closed Boundary
Closed Boundary
GeneralFunctions
GeneralFunctions
Generate Neighbourhood List
Generate Neighbourhood List
Conways Game of Life
Update Rules
Update Rules
Initialize Model
Initialize Model
Grid and Agents
Grid and Agents
Usage
Manual
Manual
The function “simulateAgents[model, modelParameters]” expects the following
◼
model == PredatorPreyClosedB/PredatorPreyBB with modelParameters containing
◼
◼
“ΔdeathRate” (how much does the death rate of predators increase after they meet one another)
◼
Concerning grid if it was not provided through options:
◼
“GridType”
◼
“Dimension”
◼
Concerning creation of initial agents if no “AgentDistribution” is provided:
◼
“TotalPop” (< Times@@dimensions)
◼
“PredatorPreyRatio” (<1)
Further, modelParameters always should contain
Further, modelParameters always should contain
◼
“Iterations”
◼
“Visualize” (True if a 2D list plot should be shown, depicting the current state of the system)
Options
Options
The functionality of "simulateAgents" can be customized through:
Examples
Examples
1
1
2
2
3
3
4
4
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
General framework for agent based modeling
by Moritz Kalhöfer-Köchling
Wolfram Community, STAFF PICKS, July 12, 2023
https://community.wolfram.com/groups/-/m/t/2959189
by Moritz Kalhöfer-Köchling
Wolfram Community, STAFF PICKS, July 12, 2023
https://community.wolfram.com/groups/-/m/t/2959189