假设我们有两个品种cats
和dogs
。在go
,我们希望cats
为meow
,dogs
为bark
。 ask
导致给定代理集中的代理以随机顺序运行代码块,这很好。但是,如果我们这样做:
to go
ask cats [ meow ]
ask dogs [ bark ]
tick
end
dogs
始终追踪cats
。我们可以用条件解决这个问题:
to go
ask turtles [
if breed = cats [ meow ]
if breed = dogs [ bark ]
]
tick
end
但这很糟糕(请注意,您可以将turtles
替换为(turtle-set cats dogs)
,以应对您不想混入的品种的更复杂情况。有更好的解决方案吗?
答案 0 :(得分:5)
使用任务:
turtles-own [noise]
to setup
...
ask cats [ set noise task meow ]
ask dogs [ set noise task bark ]
...
end
to go
...
ask turtles [ run noise ]
...
end
答案 1 :(得分:1)
配对代理集和任务的混乱列表怎么样?
to go
let actions (list
(list cats task meow)
(list dogs task bark)
)
foreach shuffle actions [
ask first ? [ run last ? ]
]
end