Search code examples
netlogoagent

Speed up an age and position check for turtles in R?


I'm writing a program that is testing the ability of turtles to navigate in an environment. This line of code has two checks. I want it to kill off the oldest turtle that has made the least amount of progress. It does what it is supposed to, but the program slows down dramatically, creating little hiccups every time it makes this check. I was wondering if someone knew a better way to go about this or make this line more efficient. Thanks!

ask one-of turtles with [XCOR = min [XCOR] of turtles with [age = max [age] of turtles]] [
      die]

Solution

  • First of all, there are primitives that are specifically finding the turtles with maximum or minimum values of some variable using min-one-of or with-min. So your code would look something like this I think:

    ask min-one-of (turtles with-max [age]) [xcor] [...]
    

    I suspect that would solve your efficiency problem, since it may well be because of implicit brackets not being where you think they are so it's trying to loop everything a couple of times. But a much cleaner to read version that would solve the efficiency problem is to specifically limit the position loop to those who are oldest and then choose the lowest position from that set.

    let old-turtles turtles with-max [age]
    ask min-one-of old-turtles [xcor] [...]