Spontaneous Animal Death

Posted January 4, 2018 by JoeyScott

I am trying to create a balanced ecosystem with three animals (sheephead fish, urchins, and kelp). Things go along great until (depending on the age of the urchins) all the urchins die simultaneously. Even though they are all being born at a different time and they are coded to age at the same rate, the urchins are all disappearing at the same time of old age. (I tested this by removing the code that has them die based on age. The simultaneous death stopped happening when this code was removed.) Can anyone help with my code?



Submitted by sgibbs on Fri, 01/05/2018 - 12:47 · Permalink

Hi Joey

What a cool project!  I may see the issue you identify in your code.  You create 5 urchins on setup, and give them a random age.  In fact, those 5 don't die simultaneously - when I run the program I sometimes see one die right away.

The problem is in your reproduce procedure for the urchins.  They have a very low age threshold for reproduction - greater than or equal to 1, which all 5 are likely to reach very quickly.  Then, they have a 40% change of reproducing at each tick, so long as there is a kelp within 10 steps.  The kelp can die by aging or by having an urchin within 9 steps.  So, in theory, an urchin can reproduce multiple times when it is 10 steps away from the kelp, so long as neither moves closer or farther and the kelp doesn't die from old age.

Each of those new urchins inherit the age of the parent, so once your remaining urchins all come from the same parent (or from multiple parents that were the same age at reproduction) your urchins will all die at once.  

The simplest way to solve this is to give the new urchins a random age, inside your create/each do block in your reproduce procedure.  You might also think about imposing a "reproduction cost" on the parent (setting the age below the reproduction threshold) so the same parent doesn't reproduce multiple times within the space of a few ticks.  I have one other suggestion for your reproduce procedure for all agents -- to avoid having your agents appear to zoom around, don't scatter them in the reproduce procedures but have them set a random heading and then move forward a few steps - that will make them appear near, but not on the same patch, as the parent.  

Happy coding!