如何让不同的品种一起随机排列

时间:2014-04-29 19:49:50

标签: netlogo

假设我们有两个品种catsdogs。在go,我们希望catsmeowdogsbarkask导致给定代理集中的代理以随机顺序运行代码块,这很好。但是,如果我们这样做:

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),以应对您不想混入的品种的更复杂情况。有更好的解决方案吗?

2 个答案:

答案 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