Make turtles move ONCE according to their ranked order in a list

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

This question is built off my previous 2. I've been working to get a rank-ordered list for my turtles, ranked by an owned factor. They're then required to move in ranked order. That part works perfectly fine, and the code can be found at: Assigning turtles a ranked number in netlogo

The problem: using that code, they're moving in the correct order but instead of moving once, the program seems to be continuously re-running it for every turtle in the list so the first turtle in a world of 10 ends up moving 10 times, the second moves 9 times, etc.

I'm completely stumped on this, but I need it to stop. They're supposed to move once and be done for that tick. Ideas?

The list creation code is:

let rank-list sort-on [sociability] turtles     let ranks n-values length rank-list [ ? ]      (foreach rank-list ranks [ask ?1 [set social_rank ?2] ] )  ;;you can replicate the problem with this movement proc, or the full code in the link above         ask turtles [foreach rank-list [ask ? [set heading 270 forward 1]]] 

回答1:

In the line:

ask turtles [foreach rank-list [ask ? [set heading 270 forward 1]]] 

You're asking each turtle to execute a command for each turtle in your rank-list.

Just drop the ask turtles part.

As a more general note: foreach is to lists what ask is to agentsets. They both do the same basic thing, which is to perform a command for each item of a "collection". Agentsets are unordered collections of unique agents. Lists are ordered collections of possibly duplicated items of any type.



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