netlogopatchspaceneighbours

Connect turtles who are within a certain radius


I use the nw extension in NetLogo to create a network.

My aim is the following:

  1. Check if any turtles are close by
  2. If any turtles are close by (I tried with next 3 patches, but that might change), connect to them + there is an upper limit of possible links

I tried (and, I think, succeeded) to implement the approach, described here. Which means that the upper limit works. However, I am not able to only link to the turtles close by. I also need something to catch the error if no turtle is close by. I tried to use the in-radius command but for some reasons, it doesn't do what I want.

extensions [nw]

breed [ AT1s AT1]

turtles-own [ friends ]

to setup
  ca
  create-AT1s 20 [
   setxy random-xcor random-ycor
   set friends 3
   set color green
   get-radius-friends friends AT1s
  ]
end

to get-radius-friends [ x AgentT]
  let lonely AgentT with [count my-links < x]
  let candidates other AgentT with [ any? AgentT in-radius 3 AND count my-links < x ]
   let new-links x - count my-links
    if new-links > 0 AND any? AgentT in-radius 3
    [ let chosen n-of min (list new-links count other candidates) other candidates
      create-links-with chosen [ hide-link ]
      set candidates other candidates
      ask chosen [ if my-links = x [ set candidates other candidates ] ]
    ] 
end

I also found the neighbors and the distance commands but these only consider the immediate surroundings which isn't what I need.


Solution

  • Actually, that's not the best question to pull from if you want to spatially restrict the turtles. And there's a serious problem with having the connection within the turtle creation block because there are no potential friends for the turtles created first. Unless you have a very large number of turtles, you probably don't need to worry about efficiency.

    I also think the variable 'x' is unnecessary because you have the variable 'friends' available (which appears to be the number of links you want the turtle to have). And there is a new reporter up-to-n-of which makes the whole list and min unnecessary.

    I think this does what you want. You may want to test is without the hide-link so you can see what it does.

    breed [ AT1s AT1]
    
    turtles-own [ friends ]
    
        to setup
          clear-all
          create-AT1s 100
          [ setxy random-xcor random-ycor
            set friends 3
            set color green
          ]
          get-radius-friends 10 AT1s
        end
        
        to get-radius-friends [ #radius #breed ]
          let linkers turtles with [breed = #breed ]
          ask linkers
          [ let new-links friends - count my-links
            if new-links > 0
            [ let candidates other linkers with [ count my-links < friends ] in-radius #radius
              let chosen up-to-n-of new-links candidates
              create-links-with chosen [ hide-link ]
            ]
          ]
        end