如何使用 NetLogo 发送参数?

lin*_*nda 2 netlogo

我对 NetLogo 很陌生,这就是我在这里停留了数周的原因。

我想做的是让特工成为 2 支球队的 4 人一组。

我的计划是让一个函数包含 4 个海龟 id,

to assign-groupmates [a1 a2 a3 a4]
Run Code Online (Sandbox Code Playgroud)

并将它们分配给第 1 组

assign-groupmates [a1 a2 a3 a4] =team1[]
if team1 > 4
assign-groupmates [a1 a2 a3 a4] =team2[]
Run Code Online (Sandbox Code Playgroud)

我所做的是:

    to assign-groupmates [ f g h i]
      let A who random f
      let B who random g
      let C who random h
      let D who random i

      ifelse f != g, g != h, h != i, i != g
      [ set group-id 1
        separate 
        align
        cohere ]
      [assign-groupmates [W X Y Z]
      set group-id 2]  
    end
Run Code Online (Sandbox Code Playgroud)

如何找到turtles-id以及如何通过参数发送它们?我使用的turtles-id是谁随机的。

谢谢你。

Nic*_*tte 5

有很多不同的方法可以完成你想要的,但让我从一条一般性建议开始:

如果可以避免,请不要使用who 数字。

它们有一些合法的用途,但这些用途很少而且相去甚远。它们很容易出错,会导致代码丑陋,而且它们往往会让您以错误的方式思考要解决的问题。

NetLogo 允许您存储对代理的直接引用,例如:

ask turtles [ set friend one-of other turtles ]
Run Code Online (Sandbox Code Playgroud)

通常,请改用它。

但是,在您的情况下,您可能不应该存储对代理的单独引用。由于您正在处理一组代理,因此您应该使用代理集。

这是一个建议:制作一个名为 的全球代理集列表groups。除此之外,让每个代理存储对构成其组的代理集的引用。这是实现这一目标的一种方法:

globals [ groups ]
turtles-own [ my-group ]

to setup
  clear-all

  let number-of-groups 2
  let turtles-per-group 4  
  create-turtles turtles-per-group * number-of-groups 

  set groups [] ; empty list
  repeat number-of-groups [
    let new-group n-of turtles-per-group turtles with [
      not is-agentset? my-group
    ]
    ask new-group [ set my-group new-group ]
    set groups lput new-group groups
  ]

  print (word "First group:  " sort first groups)
  print (word "Second group: " sort last groups)
  ; each turtle can easily access other members of its group:
  ask turtles [ show sort other my-group ]

end
Run Code Online (Sandbox Code Playgroud)

此代码的优点是非常灵活。如果您想要超过两组,或每组超过四只海龟,这是一个非常容易的更改。

另一块一般的建议:如果你发现自己使用多个变量,如a1a2a3等等,你可能做错了什么。您应该改用列表和/或代理集。