Assigning turtles a ranked number in netlogo

大憨熊 提交于 2019-12-02 10:39:03

You can use n-values to generate the ranks and the variadic version of foreach to loop through both the rank-list and the ranks at the same time:

turtles-own [ var rank ]

to setup
  clear-all
  create-turtles 50 [
    setxy random-pxcor random-pycor
    set var random-normal 0.5 0.175
  ]
  let rank-list sort-on [ var ] turtles
  let ranks n-values length rank-list [ ? ]
  (foreach rank-list ranks [ ask ?1 [ set rank ?2 ] ])
end

But the question is: do you really need a rank variable? Why not use the rank-list directly:

foreach rank-lisk [ ask ? [ move ] ]

Or even just sort your turtles on var each time:

foreach (sort-on [ var ] turtles) [ ask ? [ move ] ]

The latter is not the most efficient, but if you have only 50 turtles and do this only once per tick, you'll never notice the difference.

Using Nicolas' answer above I've achieved the goal or ranked order movement. It's still a bit processing-power-intensive for the 2000 turtles I'm working with, but at least it runs!

turtles-own [var]

to ranking
let rank-list sort-on [var] turtles
let ranks n-values length rank-list [ ? ]
    (foreach rank-list ranks [ask ?1 [set var ?2] ] )
end

to make_turtles
    create-turtles (5)
    [set var random-normal 0.5 0.2
      set color scale-color blue var 0 1
      set size 3]
     ranking
     ask turtles [set label var]

     end

to move
 let rank-list sort-on [var] turtles
ask turtles [foreach rank-list [ask ? [ forward random 9]]]
end

to setup
  clear-all
  make_turtles

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