如何使乌龟能够在Netlogo中保存其他乌龟ID? [英] How to make a turtle able to save other turtles ID in Netlogo?

查看:138
本文介绍了如何使乌龟能够在Netlogo中保存其他乌龟ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

  1. 我希望一个团队由一组中的 3只乌龟组成.

所有海龟都需要在名为 teammatesID 的变量中存储自己的ID和teammatesID. (假设您想创建一个组,将您的姓名和朋友的姓名放在列表中,这就是为什么此变量需要存储ownID的原因,我很难解释.)

All turtles need to store their own ID and teammatesID inside the variable called teammatesID. (Imagine you want to create a group, you will put your name and your friends' name in a list, that's why this variable need to store ownID, I am bad in explaning things..)

我不仅要显示(agentset,3 turtles),还需要他们能够显示所有队友的ID.

Instead of just showing (agentset,3 turtles), I need them to be able to show all teammates ID.

之后,他们聚集了所有3个成员,他们将得到 teamID .

After they have gathered all 3 members, they will get teamID.

这里的问题是我不知道,如何使这些海龟在名为teammatesID的变量中存储自己的ID和teammatesID .在中,他们应该具有 3个不同的成员.这些队友应该是同一组的.以及如何给予他们 teamID 他们获得所有成员.

The problem here is I don't know how to make these turtles store their own ID and teammatesID in variable called teammatesID. In a group, they supposed to have 3 different members. These teammates are not supposed to be from the same group. And how to give them teamID after they get all members.

这是我的编码:

global
[ teamID]

turtles-own
[
  myID
  teammatesID
]

to setup
  clear-all
  set-default-shape turtles "arrow"
  create-turtles 10

  ask turtles [ set myID who]
  reset-ticks
  setup-group
end

to setup-group

   set teamID []
   let agent one-of other turtles
   let  nearestNeighbor one-of other turtles in-radius 1 
   set teamID = 0
   ask agent with [ teammatesID < 3]
   [ ask nearestNeighbor with [teammatesID < 3]
     [ ;set teammatesID = myID ; here is the problem, I need to make sure they did not save the same turtle in one group.
       show teammatesID]]

       ask agent with [teammatesID > 3]
       set teamID fput teamID teamID
        end

感谢您在这里的宝贵时间.谢谢.

I appreciate your extra time here. Thank you.

推荐答案

如果团队是随机的,我认为您不需要为此循环,因为ask无论如何都会以随机顺序调用海龟.

If teams are random, I don't think you need a loop for this as ask will call the turtles in a random order anyway.

我认为您仍然应该在这里使用代理集,因为这会使某些事情变得容易.例如,一旦海龟知道了它们的(agentset,3 turtles),您就可以轻松地查询该代理集的myID或您要查找的任何变量.

I think you should still make use of agentsets here as it makes certain things easier. For example, once turtles know their (agentset,3 turtles), you can easily query that agentset for the myIDs or whatever variable you're after.

我对teamID的目的尚不完全清楚,所以我可能不在这里,但是如果teamID对于每个组来说都是唯一的,我不认为您希望将其作为全局变量三只乌龟.您可能还希望它也是一个turtles-own变量.

I'm not entirely clear on the purpose of teamID so I may be off base here, but I don't think you want it as a global variable if the teamIDs are to be unique to each group of three turtles. You'll probably want it to be a turtles-own variable as well.

这里是一个包含以上思想的示例.使用此设置:

Here is an example that incorporates the above ideas. With this setup:

turtles-own [ myteamset teamID myID teammatesID ]

to setup 
  ca
  crt 10 [
    set myID who
    set myteamset nobody 
    set teammatesID [ ]
  ]
  setup-groups
  print remove-duplicates [teammatesID] of turtles
  print sort [teamID] of turtles
  reset-ticks
end

以及setup-groups过程(在注释中有更多详细信息):

And the setup-groups procedure (more detail in comments):

to setup-groups
  ask turtles [
    ; If you don't have a team yet
    if myteamset = nobody [
      ; Make a temporary agent-set out of all other turtles that
      ; are also not yet part of a team
      let possible-teammates other turtles with [ myteamset = nobody ]

      ; If there are at least two more turtles, make a new team:
      ifelse count possible-teammates > 1 [
        ; Make a team out of myself and two possible teammates
        set myteamset ( turtle-set self n-of 2 possible-teammates )

        ; Make a temporary variable to pass on to my entire team
        ; (yourself included) for the team ids and the team members.
        ; Also, assign a random teamID to the whole team
        let ids sort [myID] of myteamset
        let teammmembers myteamset
        let tempteam random 1000
        ask myteamset [
          set teammatesID ids
          set myteamset teammmembers
          set teamID tempteam
        ]
      ] [
        ; If there aren't enough turtles to form a new team, 
        ; print a warning to the console.
        show "Not enough turtles to make a new team!"
      ]
    ]
  ]
end

让我知道您所追求的是那样,希望对您有所帮助.

Let me know if that's kind of what you're after, hope it helps.

编辑-根据您的评论:

要获取顺序的团队编号,您可以使用一个简单的计数器,该计数器分配给团队,然后递增到下一个被修改的setup-groups看起来像:

To get sequential team numbers, you can make use of a simple counter that gets assigned to a team and then incremented for the next one- modified setup-groups would look like:

to setup-groups
  ; Create a temporary variable to use as a counter
  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
        let teammmembers myteamset

        ; Assign the current teamCounter as team number, then 
        ; increment it by one for the next team
        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

第二个问题可能值得单独提出一个新问题,具体取决于您希望获得的深度,但是这是一种非常简单的方法,您只需获取所有唯一的团队ID并拥有一个带有该ID的乌龟即可.让他们的团队一起前进:

The second question may be worth a new question on its own, depending on how in depth you're hoping to get, but here is one very simplistic approach where you just get all unique team IDs and have one turtle with that ID have their team move together:

to move-in-groups
  ; Get the unique team IDs
  let teamNumbers remove-duplicates [teamID] of turtles 

  ; Get one member of each team to ask all members
  ; of its team (itself included) to move 
  foreach teamNumbers [
    tn ->
    ask one-of turtles with [ teamID = tn ] [
      let newHeading heading + random 60 - 30
      if myteamset != nobody [
        ask myteamset [
          set heading newHeading
          fd 1
        ]
      ]
    ]
  ]
end

NetLogo 5友好版本

to move-in-groups
  ; Get the unique team IDs
  let teamNumbers remove-duplicates [teamID] of turtles

  ; Get one member of each team to ask all members
  ; of its team (itself included) to move
  foreach teamNumbers [
    ask one-of turtles with [ teamID = ? ] [
      let newHeading heading + random 60 - 30
      if myteamset != nobody [
        ask myteamset [
          set heading newHeading
          fd 1
        ]
      ]
    ]
  ]
end

这篇关于如何使乌龟能够在Netlogo中保存其他乌龟ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆