cwren
cwren

Reputation: 61

Why is NetLogo's in-radius so slow, is there a faster way around it?

I was finding my model was running very slow and I narrowed it down to an in-radius command that the turtles are running. The bottom line is that out of the two procedures below, test1 checks [var] of every patch but runs faster than test2 which should only check a small subset of 8 patches.

set patches-of-interest (patch-set patches with [var > 1])   

to test1
  ask min-one-of other patches-of-interest with-max [var][distance myself][set pcolor red]
end

to test2
  ask min-one-of other patches-of-interest in-radius 1.5 with-max [var][distance myself][set pcolor yellow]
end

You can check their speeds using the Profiler extension and the following code:

profiler:start         
repeat 100 [ 
ask one-of turtles [test1 test2]
 ]      
profiler:stop         
print profiler:report  
profiler:reset    

Firstly, why is test2 running slower? And secondly, is there an alternative to test2 which does approximately the same thing but more efficiently?

I found a couple discussions on the Netlogo list about this but they're a bit old so may be out of date: https://github.com/NetLogo/NetLogo/issues/402

http://netlogo-users.18673.x6.nabble.com/Re-in-radius-efficiency-question-td5003840.html

EDIT: I left out that in my model I'm actually using a patch-set not the full "patches". I've updated the code example above, but Bryan already partially explained the reason for the slowdown in the comments. Bryan is right that if using all patches, test2 is much faster, but I haven't been able to subset the patches in advance or within the two tests without slowing down test2.

Upvotes: 4

Views: 450

Answers (1)

cwren
cwren

Reputation: 61

@bryan-head gave the reason for why the code is slow (in-radius checks on patch-sets cannot be optimized). I assume this is something internal to Netlogo's code.

However, I did finally find a work around that speeds things up and has the same effect so I thought I'd post it. First add the patch variable binaryvar then,

ask patches [ifelse var > 1 [ set binaryvar 1][set binaryvar 0]
to test3
  ask min-one-of other patches-of-interest in-radius 1.5 with-max [var * binaryvar][distance myself][set pcolor yellow]
end

Of course this still fails if all the cells in-radius also have var = 0...

Upvotes: 2

Related Questions