I'm trying to assign turtles a number which I can tell them to move in order of. Using previous posts and some general playing around I've managed to create a ranked order list of the turtles, but now I want to assign turtles a number based on their relative position in that list.
Example: current list: [(turtle 8) (turtle 1) (turtle 9) (turtle 0)] desired turtle designation: turtle 8 = 1, turtle 1= 2, turtle 9 = 3, etc.
So far I've reached:
globals [rank_list]
turtles-own [var.
rank]
set rank_list sort-on [var.] turtles
create-turtles (50)
[setxy (random-float max-pxcor) (random-float max-pycor)
set var. random-normal 0.5 0.175
if var. > 1 [set sociability 0.99999999]
if var. < 0 [set sociability 0.00000001]
foreach rank_list ask ? [set rank ... ;this is where I get stumped
to go
ask turtles [foreach rank [ask ? [move]]]
end
any tips for assigning values based on the ranked order in a list would be very much appreciated!
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
来源:https://stackoverflow.com/questions/38250139/assigning-turtles-a-ranked-number-in-netlogo