This code relates to an adaption of the NetLogo sandpile model. When the count go grains in each patch exceeds the threshold which in this case is 3, the grains are re-distributed to the surrounding neighbors. I am trying to re-distribute one grain to 4 random neighbour patches.
The code returns a run time error because patches at the edge will not have all the 8 neighbors, so when asking for 4 random neighbours it returns an errors stating can't request 4 ransom agents from 3 etc.
I am trying to find a piece of code that will resolve this problem.
to-report stabilize [animate?] let active-patches patches with [ n > threshold ] ;; The number iterations the avalanche has gone for. Use to calculate lifetimes. let iters 0 ;; we want to count how many patches became overloaded at some point ;; during the avalanche, and also flash those patches. so as we go, we'll ;; keep adding more patches to to this initially empty set. let avalanche-patches no-patches while [ any? active-patches ] [ let overloaded-patches active-patches with [ n > threshold ] if any? overloaded-patches [ set iters iters + 1 ] ask overloaded-patches [ set base-color fired-color ;; subtract 'threshold' amount from this patch update-n -4 if animate? [ recolor ] ;; edge patches have less than four neighbors, so some sand may fall off the edge ask n-of 4 neighbors [ update-n 1 if animate? [ recolor ] ] ] if animate? [ display ] ;; add the current round of overloaded patches to our record of the avalanche ;; the patch-set primitive combines agentsets, removing duplicates set avalanche-patches (patch-set avalanche-patches overloaded-patches) ;; find the set of patches which *might* be overloaded, so we will check ;; them the next time through the loop set active-patches patch-set [ neighbors ] of overloaded-patches ] report (list avalanche-patches iters) end