Reputation: 37
I am studying flocking behavior in netlogo, and in order to keep track of various flocks I am using a hidden "flock-holder" turtle that I can hatch or let die if a new flock is created or an existing flock runs out of members. I am coming across an issue however, where on occasion when I am trying to interface with some data within a flock, as referred to through an individual member of a flock, I get a message saying "That is dead", causing the code to fail.
From what I understand of the "die" command, if a turtle of any kind dies, it should remove itself from any agentset or variable that references it, and therefore this sort of error shouldn't be a problem? How can I fix or at least debug this strange issue?
Code of my flock-evaluation function that is having the issues below:
to evaluate-flock
if is-in-flock = True ; checking to see if a flock has died out
[
if get-flock-size flock-reference < 2 ; is the turtle the only one in the flock?
[
if verbose = True [ print "Flock has dwindled to nothing" ]
ask flock-reference ; has no more members, so is removed.
[
ask flock-members
[
set flock-reference nobody ; clear any remaining flock members of association with this flock
]
die
]
set is-in-flock False ; no longer in a flock
]
]
ifelse is-in-flock = True ; is turtle in a flock?
[
if verbose = True [ type "This turtle is in flock " print [ who ] of [ flock-reference ] of self ]
if any? other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ]; check for nearby turtles that are in different flocks
[
if verbose = True [ print "There are other nearby flocks" ]
let current-school-size ( get-flock-size [ flock-reference ] of self )
if verbose = True [ type "I am part of a school of " print current-school-size ]
let temp-list turtle-set other preys in-radius vision with [ is-in-flock = True ] with [ flock-reference != [ flock-reference ] of myself ] with [ ( get-flock-size flock-reference ) > current-school-size ] with [ subtract-headings ( average-schoolmate-heading [ flock-members ] of flock-reference ) heading < 60]; are any nearby turtles in different, larger flocks that I am alligned with? if so, add them to a list
if count temp-list > 0 ; does the list have any members?
[
if verbose = True [ print "Found a bigger flock" ]
ask flock-reference
[
remove-from-flock myself ; remove myself from my old flock
]
set flock-reference [ flock-reference ] of ( max-one-of temp-list [ get-flock-size flock-reference ] ); join the biggest flock on this list
set is-in-flock True ; sets it to true in case it wasn't for some reason.
]
]
]
[
if verbose = True [ type "Turtle " type [ who ] of self print " is not in a flock" ]
ifelse any? other preys in-radius vision with [ is-in-flock = True ] ; are there any pre-existing flocks the turtle can join?
[
if verbose = True [ print "There are nearby flocks" ]
let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = True ] ; grab any nearby turtles that are already in a flock
***set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading ( [ flock-members ] of flock-reference ) ) heading < 60]; remove any that are not aligned with this turtle***
if count potential-flock > 0
[
if verbose = True [ print "There are nearby flocks that I am aligned with" ]
set flock-reference [ flock-reference ] of ( max-one-of potential-flock [ get-flock-size flock-reference ] ); join the biggest flock on this list
set is-in-flock True ; turtle is now in a flock
]
]
[ ; if there are no pre-existing flocks, turtle starts its own
let potential-flock turtle-set other preys in-radius vision with [ is-in-flock = False ] ; Grab any nearby turtles not already in a flock
set potential-flock potential-flock with [ subtract-headings ( average-schoolmate-heading potential-flock ) heading < 60]; remove any that that are not aligned with this turtle
if count potential-flock > 0
[
if visualize-flock-creation = True
[
set color green
ask potential-flock [ set color green ]
wait 0.25
set color blue
ask potential-flock [ set color blue ]
]
if verbose = True [ type "Number of nearby potential flockmates " print count potential-flock ]
hatch-flock-holders 1 ; create a new flock-holder
[
set size 0
set color black ; sets the new flock's placeholder color to the background
set flock-members potential-flock ; adds the list of members to the new flock
ask flock-members
[
set flock-reference myself ; asks the new flock members to add the new flock as their flock-reference
set is-in-flock True ; all these turtles are now in a flock
]
]
]
]
]
end
Potentially unclear variable name reference for code above: flock-reference: - A variable held by each flocking "prey" turtle, that just points to the hidden "flock-holder" turtle. flock-members: - An agentset of "prey" turtles that are attached to the hidden "flock-holder" turtle.
I have added an image of the full error message below.
Please let me know if there is any confusion about what's going on here, or if there is anything I can clarify. Thank you!
Upvotes: 1
Views: 317
Reputation: 4168
I can't test this, but I expect that the error stems from agent prey 2
having a reference to a flock-holder
that has died. When a flock-holder
dies, it is (as you know) deleted from any angentsets that it was a member of and variables that hold a pointer to it are reset to point to nobody
. However, NetLogo is smart enough to know that this nobody
is a dead flock-holder
and gives you the error message you've encountered. If after the error you inspected prey 2
, or entered show [flock-reference] of prey 2
at the command line, I expect that you would find that flock-reference
was indeed set to nobody
.
My guess is that somewhere in your code, not all the prey that were in the (now dead) flock were reassigned to another flock, but rather kept their old value of flock-reference
, now nobody
. When you ask a flock to die, you might add the line show preys with [flock-reference = nobody]
. If there are any, you could track down why.
Hope this helps, Charles
Upvotes: 4