Chapter 7: Half-Life & Stink-Bomb
Many simulations involve one or both of these situations; it is very important to do these and other things in your simulations in ways that are biologically reasonable. These exercises will show you biologically-reasonable ways to include these processes in your simulations.
The next two exercises will show you how to code these two very useful tricks.
1) Exponential decay (a.k.a. "half-life")
In many simulations, you'll need a biologically-reasonable way to get rid of unwanted agents.
Exponential decay is a very realistic and easily-implemented way to do this.
![]() ![]() |
1) Put some agents in the world that randomly 'self-destruct'. Steps:
|
You can download a template project here. ![]() ![]() to setup clear-all create-turtles 100 [ set color red set shape "triangle" set xcor (random 32) - 16 set ycor (random 32) - 16 ] reset-ticks end to forever ask turtles [ if (random 100) < 1 [ die ] ] tick end |
![]() |
2) The result: exponential decay. You should see the number of turtles drop. But, more specifically:
|
![]() |
3) Play with the numbers.
|
||
![]() |
4) Add a source of agents.
Modify the code to:
|
![]() to setup clear-all create-turtles 100 [ set color red set shape "triangle" set xcor (random 32) - 16 set ycor (random 32) - 16 ] reset-ticks end to forever ask turtles [ if (random 100) < 1 [ die ] ] tick end to addAgents create-turtles 10 [ set color red set shape "triangle" set xcor (random 32) - 16 set ycor (random 32) - 16 ] end |
![]() |
5) Play around and see how it behaves.
Try to answer some of these questions:
|
You can download a complete version of this project here. ![]() |
2) Diffusible signals (a.k.a. "stink-bomb")
This is a realistic way for agents without eyes to learn about their neighborhood.
![]() ![]() |
1) Start a project with a 'stinky' agent in the center and have it spray out 'stinky' molecules. Steps:
|
You can download a template project here. breed [stinkers stinker] breed [stinks stink] to setup clear-all create-stinkers 1 [ set color blue set shape "square" set size 2 ] end to forever create-stinks 5 [ set color red set shape "circle" set size 1 set heading random 360 ] ask stinks [ forward 1 left random 11 right random 11 ;; need a higher chance of self-destructing ;; since world is smaller than in SLN if (random 100) < 25 [ die ] ] end |
![]() |
2) Test your code. You should see a stinker in the center spewing out stinks in all directions. The stinks should go a little distance before
disappearing. Some will go farther than others since they have a random chance of self-destructing. |
![]() |
![]() ![]() |
3) Build a very slow-moving detector agent to detect the stink. Create a new "detector" breed that is big enough to see clearly. Have it be born far from the center. Have it also 'meander' but have it move very slowly - give it a 75% chance of taking one step each 'tick' (this is because "forward 0.75" doesn't work). |
breed [stinkers stinker] breed [stinks stink] breed [detectors detector] to setup clear-all create-stinkers 1 [ set color blue set shape "square" set size 2 ] create-detectors 1 [ set color white set shape "square" set size 2 set xcor 12 set ycor 12 ] end to forever create-stinks 5 [ set color red set shape "circle" set size 1 set heading random 360 ] ask stinks [ forward 1 left random 11 right random 11 ;; need a higher chance of sel-destructing ;; since world is smaller than in SLN if (random 100) < 25 [ die ] ] ask detectors [ if (random 100) <= 74 [ forward 1 left random 11 right random 11 ] ] end |
![]() |
4) Test your code. Your detector should meander slowly and haltingly. |
![]() |
![]() ![]() |
5) Have the detector agent change color depending on how close it is to the 'stinker'. The closer the detector is to the stinker, the more frequently it will be hit by stinks. Steps:
|
breed [stinkers stinker] breed [stinks stink] breed [detectors detector] detectors-own [hitCount] to setup clear-all create-stinkers 1 [ set color blue set shape "square" set size 2 ] create-detectors 1 [ set color white set shape "square" set size 2 set xcor 12 set ycor 12 set hitCount 0 ] end to forever create-stinks 5 [ set color red set shape "circle" set size 1 set heading random 360 ] ask stinks [ forward 1 left random 11 right random 11 ;; need a higher chance of sel-destructing ;; since world is smaller than in SLN if (random 100) < 25 [ die ] ] ask detectors [ if (random 100) < 75 [ forward 1 left random 11 right random 11 ] ifelse hitCount > 9 [ set color pink ] [ set color white ] if any? stinks in-radius 1 [ set hitCount (hitCount + 1) ask one-of stinks in-radius 1 [ die ] ] ] end You can download a complete version of this project here. |
![]() |
6) Find and fix the problem with your code. Run your code and the detector should turn purple. But something isn't right. What is it and how can you fix it? |
![]() |