How to get different breeds to be `ask`ed in a random order together

StackOverflow https://stackoverflow.com/questions/23373963

  •  12-07-2023
  •  | 
  •  

Вопрос

Suppose we have two breeds, cats and dogs. On go, we want cats to meow and dogs to bark. ask causes the agents in the given agentset to run the code block in a random order, which is great. However, if we do:

to go
  ask cats [ meow ]
  ask dogs [ bark ]
  tick
end

dogs always go after cats. We could solve this problem with conditionals:

to go
  ask turtles [
    if breed = cats [ meow ]
    if breed = dogs [ bark ]
  ]
  tick
end

but this is gross (note that you can replace the turtles with (turtle-set cats dogs) for more complicated situations with breeds that you don't want mixed in). Any better solutions?

Это было полезно?

Решение

Use tasks:

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

Другие советы

How about a shuffled list of paired agentsets and tasks?

to go
  let actions (list
    (list cats task meow)
    (list dogs task bark)
  )
  foreach shuffle actions [
    ask first ? [ run last ? ]
  ]
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top