如何使用Netlogo中的BOIDS规则使座席人员蜂拥而至?

时间:2018-10-17 10:34:29

标签: netlogo flock boids

这就是我想要做的:

  1. 我需要使用BOIDS规则向前推进 分组

  2. 我希望他们在碰到墙之后能够转身(我的世界没有被包裹,他们不能穿透墙到达世界的另一端)

这是我的代码:

globals [nearest-teammates teammmembers]

turtles-own [ myteamset teamID myID teammatesID ]

to setup-groups

  let teamCounter 1 
  ask turtles [
    if myteamset = nobody [
      let possible-teammates other turtles with [ myteamset = nobody ]
      ifelse count possible-teammates > 1 [
        set myteamset ( turtle-set self n-of 2 possible-teammates )
        let ids sort [myID] of myteamset
        set teammmembers myteamset
        set nearest-teammates min-one-of myteamset[distance myself]

        let tempteam teamCounter         
        set teamCounter teamCounter + 1
        ask myteamset [
          set teammatesID ids
          set myteamset teammmembers
          set teamID tempteam
        ]

      ] [
        show "Not enough turtles to make a new team!"
      ]
    ]
  ]
end

植绒部分:

to move-in-groups

  let teamNumbers remove-duplicates [teamID] of turtles
  foreach teamNumbers [                         
    ask one-of turtles with [ teamID = ? ] [
      if myteamset != nobody [
        ask myteamset [
        set  heading mean-heading [ heading ] of myteamset
         ifelse distance one-of turtles with [ teamID = ? ] < 3
         [separate]
         [align
           cohere]]
          fd 1 ]]]
end

助手代码

to-report mean-heading [ headings ]
  let mean-x mean map sin headings
  let mean-y mean map cos headings
  report atan mean-x mean-y
end

to align  
  turn-towards average-flockmate-heading max-align-turn
end

to cohere  
  turn-towards average-heading-towards-flockmates max-cohere-turn
end


to separate  
  turn-away ([heading] of nearest-teammates) max-separate-turn
end

to turn-away [new-heading max-turn]  
  turn-at-most (subtract-headings heading new-heading) max-turn
end

to-report average-heading-towards-flockmates  
  let x-component mean [sin (towards myself + 180)] of nearest-teammates
  let y-component mean [cos (towards myself + 180)] of nearest-teammates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end

to-report average-flockmate-heading  
  let x-component sum [dx] of nearest-teammates
  let y-component sum [dy] of nearest-teammates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]
end

to turn-at-most [turn max-turn]  
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]
end

to turn-towards [new-heading max-turn]  
  turn-at-most (subtract-headings new-heading heading) max-turn
end

我得到的错误:

SUM expected input to be a list but got the number -0.961261695938319 instead.
error while turtle 4 running SUM
  called by procedure AVERAGE-FLOCKMATE-HEADING
  called by procedure ALIGN
  called by (command task from: procedure MOVE-IN-GROUPS)
  called by procedure MOVE-IN-GROUPS
  called by procedure GO
  called by Button 'go'

我该怎么办?我需要帮助...。·´(>▂<)´¯·。 感谢您的时间。

1 个答案:

答案 0 :(得分:1)

您遇到的错误发生在此行:

let x-component sum [dx] of nearest-teammates

NetLogo告诉您,您尝试将单个数字传递给sum,而不是将其传递给列表。怎么会这样如果nearest-teammates仅包含一个代理而不是一个代理集,则会发生这种情况。

因此,让我们看一下定义nearest-teammates的位置:

set nearest-teammates min-one-of myteamset[distance myself]

您看到问题了吗?您正在使用min-one-of,它确实为您提供了一个代理!

例如,您如何才能获得10个最接近的代理,而不仅仅是最接近的一个?幸运的是,NetLogo具有完成此操作的原语:min-n-of。这是您的用法:

set nearest-teammates min-n-of 10 myteamset [ distance myself ]

用您想要的队友数代替10