hierarchy of linked turtles

14 views
Skip to first unread message

Jonathan Brooks

unread,
Sep 20, 2021, 1:40:55 PM9/20/21
to netlogo-users
Hello All,

I'm having trouble figuring out how to get all linked turtles to perform an action. Here is my situation, one turtle is linked to two other turtles. Those two turtles are each linked to two other turtles and so on. This forms a hierarchal tree of linked turtles. What I'm trying to do is get all the turtles in this tree to perform an action. If I ask link-neighbors to perform the action, only the first two levels of the will perform the action. Is there a way to get all linked turtles to perform the action without using a long series of nested ask statements? Thanks.

Steve Railsback

unread,
Sep 22, 2021, 6:37:13 PM9/22/21
to netlogo-users
You could use "recursion": have one turtle ask its link-neighbors to execute some code that includes asking their link-neighbors to execute the same code.

ask one-of turtles [ move-neighbors ]

to move-neighbors
  fd 1 ; or whatever you want all the linked turtles to do
  ask link-neighbors [ move-neighbors ]
end

But I think you will be happier if you learn to use the built-in Networks extension. It has commands for doing things like this.

Steve R.

Steve Railsback

unread,
Sep 22, 2021, 6:41:55 PM9/22/21
to netlogo-users
Actually, do not try the recursion approach with "link-neighbors" because it will never stop: two turtles will keep telling each other to execute move-neighbors. It could work with directed links and out-link-neighbors etc.

But use the Network extension.
Reply all
Reply to author
Forward
0 new messages