juliana
juliana

Reputation: 101

How to set follower follow any leader nearest to them in NetLogo

I really need some advice on this, I try to create few leaders and some amount of followers, however it seems that followers do not follow any leader, how do I make a follower follow the nearest leader to them.thanks

turtles-own 
[
is-leader? 
follower
]

to setup
clear-all
reset-ticks
ask n-of 50 patches 
[sprout 1
[set color blue 
set follower self]]
choose-leaders
end

to choose-leaders
ask max-n-of 5 turtles [count turtles in-radius 3] 
[
set is-leader? true
set color white
]
end

to go
ask turtles [follow_leader]
tick
end


to follow_leader
ask follower 
if any? is-leader? in-radius 30
[ set heading (towards min-one-of is-leader? [distance self]) fd 1]
end

Upvotes: 0

Views: 1445

Answers (1)

Arthur Hjorth
Arthur Hjorth

Reputation: 889

It's a bit hard to make sense of what you are trying to do with the follower turtle variable. With the code you posted, all turtles have themselves as follower, which I am almost certain is not right. In other words, currently the variable doesn't do anything, and you can delete it unless you want to do something else with it.

Regardless, the problem with your code is in your follow_leader procedure. This will work - I added comments so you can see what

to follow_leader
  ask follower 
  ;; since all turtles are asked to run this procedure, and all turtles have themselves as
  ;;follower, this asks turtles to ask themselves to do something. 

  if any? is-leader? in-radius 30
  ;; this is incorrect syntax.

  [ set heading (towards min-one-of is-leader? [distance self]) fd 1]
  ;; 'self' refers to the turtle itself, so this will always return 0, and turtles will face themselves
end

Given these errors, this is probably what you want:

to go
ask turtles with [not leader?] [follow_leader];; we only want to ask turtles that are not leaders
tick
end

to follow-leader ;; changed just for NetLogo convention
  let nearby-leaders turtles with [leader? and distance myself < 30] ;; find nearby leaders
  if any? nearby-leaders [ ;; to avoid 'nobody'-error, check if there are any first
    face min-one-of nearby-leaders [distance myself] ;; then face the one closest to myself
    fd 1
  ]
end

Make sure to initialize the leader? boolean in all your turtles. Add set leader? false in the command block that you send to turtles that you sprout in your to setup procedure.

Upvotes: 3

Related Questions