1

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.

Hannah H.
  • 266
  • 3
  • 14

1 Answers1

2

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
JenB
  • 17,620
  • 2
  • 17
  • 45
  • Thank you. What would you consider to be "a very large number of turtles"? I'll have around 300 000 turtles in my model. – Hannah H. Aug 28 '20 at 18:04
  • I am also super confused. I get the error message "nothing named up-to-n-of has been defined" even though it is in the dictionary. Do you happen to know the reason for this error? I am working with Netlogo 6.0.4. I didn't find anything online – Hannah H. Aug 28 '20 at 18:15
  • 1
    `up-to-n-of` is new in version 6.1. Yes, 300000 is more than very large. Honestly, I would be surprised if NetLogo can handle it because you will not only have 300k turtles (which is already pushing it), you will also have links. Perhaps you can prototype with NetLogo and then switch once you have fully worked out your model. – JenB Aug 28 '20 at 18:44
  • I switched to 6.1 Now it's working. Unfortunately it is now to late for me to switch the language as I am at the very end of my dissertation. I will see if I can work with fewer agents. Do you have a recommendation to which language I could switch for the following project? – Hannah H. Aug 28 '20 at 18:51
  • 1
    honestly, choice of language depends on the project - number of agents, whether you want GIS etc. But I usually go the other direction and evaluate if I really need that many agents. – JenB Aug 29 '20 at 11:50
  • The definition of "very large" depends on the nature of the model. A very simple model with 300000 turtles could be just fine. A complex or poorly coded model with 1000 turtles might be too big. See http://ccl.northwestern.edu/netlogo/docs/faq.html#how-big-can-my-model-be-how-many-turtles-patches-procedures-buttons-and-so-on-can-my-model-contain – Seth Tisue Sep 02 '20 at 20:02