我有6个成员,他们属于两个团队中的一个(所以2 x 3个成员的团队)。从每个团队中随机选择两名团队成员来完成团队任务——这两名成员同时与各自的团队任务形成任务链接。我的目标是创建并更新每个团队的两个成员交互次数(interactive -link)的计数(links-自己的变量n-interaction)。我尝试使用ifelse语句调用who号码,但无法获得n-交互更新。如有任何意见,不胜感激。
breed [members member]
breed [team-tasks team-task]
members-own [
teamID
tasklink?
teammembers
interactwith ]
team-tasks-own [
teamID
]
undirected-link-breed [team-links team-link]
undirected-link-breed [task-links task-link]
undirected-link-breed [interact-links interact-link]
interact-links-own [ n-interaction ]
to go
create-initialform-task
dyads-work-task
update-interaction
tick
clear-taskwork
end
to dyads-work-task
ask n-of 2 members with [teamID = 1]
[ create-task-link-with one-of team-tasks with [teamID = 1 and completed? = false]
set tasklink? true
]
ask n-of 2 members with [teamID = 2]
[ create-task-link-with one-of team-tasks with [teamID = 2 and completed? = false]
set tasklink? true
]
end
to update-interaction
ask members with [ teamID = 1 and tasklink? ]
[ let my-work-partner (other teammembers with [ teamID = 1 and tasklink? ])
set interactwith (sentence interactwith my-work-partner)
]
ask members with [ teamID = 2 and tasklink? ]
[
let my-work-partner (other teammembers with [ teamID = 2 and tasklink? ])
set interactwith (sentence interactwith [ who ] of my-work-partner)
]
end
to clear-taskwork
ask task-links [ die ]
ask members
[ set tasklink? false ]
end
没有create-initialform-task
我无法让你的代码工作,所以我创建了一个使用简化代码的快速示例,其中我要求两只海龟将它们的颜色设置为相同的随机颜色,并要求它们之间的链接将计数器更新为1并设置为相同的颜色(颜色易于快速可视化)。
go-1
首先选择将执行任务的两只海龟,并使用let
将它们放入局部变量中,因为我将多次调用它们。然后执行任务(改变它们的颜色)。最后,它要求其中一只海龟检查它们与另一只海龟的链接,并要求该链接更新其颜色和计数器。我在ask link-with one-of other actors
中使用的one-of
感觉有点多余,但link-with
只适用于乌龟,而other actors
是一个包含一只乌龟的乌龟,这是不一样的。因此one-of other actors
go-2
从链接的角度代替。对我来说,它感觉更优雅一些,但它可能对你的目的不太有用。我没有选择两只海龟,而是选择了一个随机链接,并让它更新两端海龟的颜色。
links-own [counter]
to setup
ca
crt 4 [
setxy random-xcor random-ycor
create-links-with other turtles [set counter 0]
]
end
to go-1
let random-color random 14 * 10 + 5
let actors n-of 2 turtles
ask actors [ set color random-color ]
ask one-of actors [
ask link-with one-of other actors [
set counter counter + 1
set color random-color
]
]
end
to go-2
let random-color random 14 * 10 + 5
ask one-of links [
set counter counter + 1
set color random-color
ask both-ends [
set color random-color
]
]
end
我注意到的另外两件事:在你的dyads-work-task
中,你让一个团队的两只乌龟都用一个随机任务创建一个链接,但在你的代码中没有任何东西确保它们都用同一个随机任务创建一个链接。这就是局部变量派上用场的地方。
to dyads-work-task
let current-task one-of team-tasks with [teamID = 1 and completed? = false]
ask n-of 2 members with [teamID = 1]
[ create-task-link-with current-task
set tasklink? true
]
end
在update-interaction
中,你对待团队1和团队2是不同的。Team 1创建一个海龟列表,而Team 2创建一个数字(who)列表。
最后,有必要同时拥有一个交互链接和一个团队链接吗?既然你已经有了团队链接,你就不能直接将交互计数器添加到团队链接中吗?