如何设置关注者跟随NetLogo中离他们最近的任何领导者

时间:2014-09-25 01:34:38

标签: netlogo

我真的需要一些建议,我尝试创建少数领导者和一些追随者,但似乎追随者不跟随任何领导者,我如何让追随者跟随最近的领导者。谢谢

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

1 个答案:

答案 0 :(得分:3)

要弄清楚你要对follower乌龟变量做什么感觉有点困难。根据您发布的代码,所有海龟都将自己视为追随者,我几乎可以肯定这是不对的。换句话说,目前变量没有做任何事情,你可以删除它,除非你想用它做其他事情。

无论如何,您的代码存在于follow_leader程序中。这将有效 - 我添加了评论,以便您可以看到

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

鉴于这些错误,这可能是您想要的:

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

确保在所有海龟中初始化leader?布尔值。在您发送给set leader? false程序中发芽的海龟的命令块中添加to setup