How to run several procedures simultaneously in NetLogo?

痞子三分冷 提交于 2019-12-11 13:54:41

问题


There are different colored turtles in my model each operating under different rules. I want procedures governing the movement of one turtle(say, red turtle) to run simultaneously with other procedures governing the movement of different colored turtles.


回答1:


Alan's answer is the correct one. However, just FYI - each turtle (whether red or blue) will act in turn with the above procedure, and none with act "at the same time." That just doesn't happen in NetLogo, by default.

However, you can use a form of simulated concurrency. There is a section of the user guide on "ask-concurrent" that explains this built-in function in detail.




回答2:


Suppose you have two turtle procedures do-red and do-blue that you want to run on red and blue turtles. Then you can just ask turtles [do-something] and condition on the color. Assuming you are not changing the colors:

to do-something  ;; turtle proc
  if (color = red) [do-red]
  if (color = blue) [do-blue]
end

EDIT:

This does not provide true concurrency, but seriously, how often can agent behavior be truly concurrent? For example, if do-red affects other turtles (red or blue), what is the "concurrent" outcome when turtle 0 and turtle 1 both influence turtle 2, who responds only to individual influences. To give another example, if you want every turtle to choose an unoccupied patch to move to, and two choose the same patch, who wins? The is why update-state solutions cannot address the general problem of concurrency. Of course, it is still crucial for some problems. (E.g., CA.)




回答3:


I don't think Alan's answer is complete. Here he is assuming do-red doesn't effect blue turtles. To simulate concurrency you need to store the state and update it later. All computations will performed on the stored state in the given time-step.

Example Using Alan's code:

to do-something  ;; turtle proc
  if (color = red) [do-red]
  if (color = blue) [do-blue]
  update-turtles-state
end

Note: do-blue shouldn't use the computational output of do-red in any way for a given time-step.



来源:https://stackoverflow.com/questions/24523335/how-to-run-several-procedures-simultaneously-in-netlogo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!