首页 > 解决方案 > 如何使用 Netlogo 中的 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'

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

标签: netlogoflockboids

解决方案


您遇到的错误发生在这一行:

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为您想要的队友数量。


推荐阅读