如何让一只乌龟能够在 Netlogo 中保存其他乌龟的 ID?
How to make a turtle able to save other turtles ID in Netlogo?
这是我想要做的:
我想要一个 团队 由 3 只海龟组成一组 。
所有海龟都需要在名为teammatesID的变量中存储自己的ID和teammatesID。 (假设你要创建一个群,你会把你的名字和你朋友的名字放在一个列表中,所以这个变量需要存储ownID,我不好解释..)
而不是只显示 (agentset,3 turtles)
,我需要它们能够显示所有队友 ID。
在他们集齐所有3名成员后,他们将获得teamID
。
这里的问题是我不知道如何让这些海龟将自己的 ID 和队友 ID 存储在名为 teammatesID 的变量中。在一个群组中,他们应该有3个不同的成员。这些队友不应该来自同一组以及如何给他们teamID
after
他们获得所有成员.
这是我的编码:
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
感谢您抽出宝贵的时间。谢谢。
如果团队是随机的,我认为您不需要循环,因为 ask
无论如何都会以随机顺序调用海龟。
我认为您仍应在此处使用代理集,因为它使某些事情变得更容易。例如,一旦海龟知道它们的 (agentset,3 turtles)
,您就可以轻松地查询该代理集以获取 myID
或任何您想要的变量。
我不是很清楚 teamID
的目的,所以我可能在这里偏离了基础,但我认为如果 teamID
是每组三只乌龟都是独一无二的。您可能希望它也是一个 turtles-own
变量。
这是一个结合了上述想法的例子。使用此设置:
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
程序(评论中有更多详细信息):
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
如果这就是您想要的,请告诉我,希望对您有所帮助。
编辑- 根据您的评论:
要获得连续的团队编号,您可以使用一个简单的计数器,该计数器被分配给一个团队,然后为下一个团队递增 - 修改后的 setup-groups
看起来像:
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 让他们的团队一起行动:
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
编辑 2: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
我同意 Luke 的观点,与团队打交道的最合适方式是作为代理集,而不是标识符的复杂分配。这就是执行此操作的代码。请注意,我还没有解决你问题的运动部分。
globals [max-teamsize]
turtles-own
[ teamID
teammates
]
to testme
clear-all
create-turtles 100
[ setxy random-xcor random-ycor
set teamID 0
set teammates nobody
]
set max-teamsize 3
setup-groups
reset-ticks
end
to setup-groups
; assign turtles to team by allocating a team number
let counter 1
ask turtles
[ if teamID = 0
[ set teamID counter
let potential-teammates turtles with [teamID = 0]
ask n-of min (list (max-teamsize - 1) (count potential-teammates)) potential-teammates
[ set teamID counter
]
set counter counter + 1
]
]
; store teammates as agentset of other turtles in team
ask turtles
[ set teammates other turtles with [teamID = [teamID] of myself]
]
end
这是我想要做的:
我想要一个 团队 由 3 只海龟组成一组 。
所有海龟都需要在名为teammatesID的变量中存储自己的ID和teammatesID。 (假设你要创建一个群,你会把你的名字和你朋友的名字放在一个列表中,所以这个变量需要存储ownID,我不好解释..)
而不是只显示
(agentset,3 turtles)
,我需要它们能够显示所有队友 ID。在他们集齐所有3名成员后,他们将获得
teamID
。
这里的问题是我不知道如何让这些海龟将自己的 ID 和队友 ID 存储在名为 teammatesID 的变量中。在一个群组中,他们应该有3个不同的成员。这些队友不应该来自同一组以及如何给他们teamID
after
他们获得所有成员.
这是我的编码:
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
感谢您抽出宝贵的时间。谢谢。
如果团队是随机的,我认为您不需要循环,因为 ask
无论如何都会以随机顺序调用海龟。
我认为您仍应在此处使用代理集,因为它使某些事情变得更容易。例如,一旦海龟知道它们的 (agentset,3 turtles)
,您就可以轻松地查询该代理集以获取 myID
或任何您想要的变量。
我不是很清楚 teamID
的目的,所以我可能在这里偏离了基础,但我认为如果 teamID
是每组三只乌龟都是独一无二的。您可能希望它也是一个 turtles-own
变量。
这是一个结合了上述想法的例子。使用此设置:
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
程序(评论中有更多详细信息):
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
如果这就是您想要的,请告诉我,希望对您有所帮助。
编辑- 根据您的评论:
要获得连续的团队编号,您可以使用一个简单的计数器,该计数器被分配给一个团队,然后为下一个团队递增 - 修改后的 setup-groups
看起来像:
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 让他们的团队一起行动:
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
编辑 2: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
我同意 Luke 的观点,与团队打交道的最合适方式是作为代理集,而不是标识符的复杂分配。这就是执行此操作的代码。请注意,我还没有解决你问题的运动部分。
globals [max-teamsize]
turtles-own
[ teamID
teammates
]
to testme
clear-all
create-turtles 100
[ setxy random-xcor random-ycor
set teamID 0
set teammates nobody
]
set max-teamsize 3
setup-groups
reset-ticks
end
to setup-groups
; assign turtles to team by allocating a team number
let counter 1
ask turtles
[ if teamID = 0
[ set teamID counter
let potential-teammates turtles with [teamID = 0]
ask n-of min (list (max-teamsize - 1) (count potential-teammates)) potential-teammates
[ set teamID counter
]
set counter counter + 1
]
]
; store teammates as agentset of other turtles in team
ask turtles
[ set teammates other turtles with [teamID = [teamID] of myself]
]
end