netlogoagentuniform-distribution

Uniform distribution of turtles


I am attempting to create a uniform distribution of turtles in a world with max-pxcor & max-pycor = 49, with 0,0 at bottom left hand corner, in a world that does not wrap. I have used code from the Virtual Lab (https://virtualbiologylab.org/population-ecology/) MarkRecpature model, to create the uniform distribution, but I am running into problems that I don’t fully understand. (My NetLogo knowledge would not be good enough to attempt to create my own code for uniform distribution of points!) The model stops with a Runtime Error:

 “ASK expected input to be an agent or agentset but got nobody instead” 

relating to: 
“ask patch x y” part of the code. 

Some of the turtles have been setup, but not all and I cannot figure what the error is, as when I inspect the turtle creating the error, it has a counter & counter2 value that falls within the world dimensions, and from this code I would expect the patch with counter & counter2 as x,y coordinates also falls within the world. So there is something about this piece of code that I do not fully understand. Can anyone help? Thanks, Aine

turtles-own [counter counter2 home_x home_y]

to setup
clear-all                               
reset-ticks                             
  
create-turtles N-turtles                  ;;create N turtles based on slider

end
  
to go
  
    ask turtles
[
    set counter max-pxcor  / sqrt N-turtles  / 2
set counter2 max-pycor / sqrt N-turtles  / 2
repeat N-turtles
[
let x (counter + random 5 - random 5)
let y (counter2 + random 5 - random 5)
ask patch x y
[
    sprout 1
    [
    set home_x x
    set home_y y
    ]
]
set counter counter +  max-pxcor  / sqrt N-turtles
if counter >= max-pxcor
[
    set counter max-pxcor  / sqrt N-turtles  / 2
    set counter2  counter2 + max-pycor  / sqrt N-turtles
    ]]]
  
end

Solution

  • I was able to reproduce a crash in two distinct scenarios. To find out where exactly the error lies, it helps to add in some extra commands to your code that provide output:

      let x (counter + random 5 - random 5)
    show word "x: " x
      let y (counter2 + random 5 - random 5)
    show word "y: " y
      ask patch x y [...]
    

    This showed me that the error mostly occurs when your Y goes over 49, at which point there is no longer a patch that corresponds to the coordinates. You already fixed this problem for the X coordinate yourself. Repurposing your solution, by adding if counter2 >= max-pycor [set counter2 max-pycor / sqrt N-turtles / 2] near the end of your code this problem is solved.

    The second scenario where I got a crash was when either X or Y falls below 0. This can happen when you have a high N-turtles, so that your max-pxcor / sqrt N-turtles / 2 is lower than 5. If this happens, let x (counter + random 5 - random 5) might occasionally return a negative value. I suggest having the random number here scale with your world size and turtle count as well. (I am not knowledgeable enough about uniform distributions to advise the best option here)

    The original model didn't have to worry about any of these since there, world wrapping is turned on.

    Finally, although you didn't specifically ask for it, your code would generate (N-turtles^2 + N-turtles) turtles , not N-turtles, since you first generate N-turtles and then let every single one of them generate another N-turtles. I removed your first turtle generation, and moved the counters to a be global variables.

    turtles-own [home_x home_y]
    globals [counter counter2]
    
    to setup
    clear-all                               
    reset-ticks                             
    end
      
    to go
    
      let distance-x max-pxcor  / sqrt N-turtles
      let distance-y max-pycor / sqrt N-turtles
      
      set counter distance-x / 2
      set counter2 distance-y / 2
      repeat N-turtles [
        let x (counter + random distance-x / 2 - random distance-x / 2)
        show word "x: " x
        let y (counter2 + random distance-y / 2 - random distance-y / 2)
        show word "y: " y
        
        ask patch x y [
          sprout 1 [
            set home_x x
            set home_y y
          ]
        ]
        
        set counter counter + distance-x
        if counter >= max-pxcor [
          set counter distance-x / 2
          set counter2  counter2 + distance-y
          if counter2 >= max-pycor [
            set counter2 distance-y / 2
          ]
        ]
      ]
      
    end