Search code examples
netlogo

Why does the network sometimes split in the "Link-Walking Turtles" NetLogo example? How can I avoid it?


I am analyzing NetLogo's Link-Walking Turtles example to learn how to create simple networks where an agent performs a random walk in that network. The problem is that I need help understanding why the network breaks when applying the layout-spring function. I have tried different values of forces, but I need help to avoid this behavior while avoiding links overlapping.

Here is an image of what I mean about the network splitting

Link-Walking Turtles network example 2

For example, I'm avoiding the network split with:

to layout
  layout-spring nodes links 2 12 1
end

However, the links are overlapping:

Link-Walking Turtles network example 2

Here is the code of the Link-Walking Turtles example:

breed [nodes node]
breed [walkers walker]

walkers-own [location]  ;; holds a node

to setup
  clear-all
  set-default-shape nodes "circle"
  ;; create a random network
  create-nodes 30 [ set color blue ]
  ask nodes [ create-link-with one-of other nodes ]
  ;; lay it out so links are not overlapping
  repeat 500 [ layout ]
  ;; leave space around the edges
  ask nodes [ setxy 0.95 * xcor 0.95 * ycor ]
  ;; put some "walker" turtles on the network
  create-walkers 5 [
    set color red
    set location one-of nodes
    move-to location
  ]
  reset-ticks
end

to layout
  layout-spring nodes links 0.5 2 1
end

to go
  ask links [ set thickness 0 ]
  ask walkers [
    let new-location one-of [link-neighbors] of location
    ;; change the thickness of the link I just crossed over
    ask [link-with new-location] of location [ set thickness 0.5 ]
    face new-location  ;; not strictly necessary, but improves the visuals a bit
    move-to new-location
    set location new-location
  ]
  tick
end

Solution

  • ask nodes [ create-link-with one-of other nodes ] would have each turtle pick a random turtle that is not itself and create a link with it, which means that some turtles may choose the same turtle. That is why you may sometimes get a fully connected network and sometimes multiple unconnected clusters.

    One way to overcome this is to check the entire network to ensure that you choose a random turtle from the fully-connected network. There is a Giant Component model in the library, which may give you an idea of how to check if a network is fully-connected.

    Alternatively, here is a hacky way to achieve something similar: creating turtles one by one by choosing a random turtle to hatch the next turtle and forming a link with it.

      create-nodes 1 [
        set color blue 
      ]
      
      repeat 29 [
        ask one-of nodes [
          hatch 1 [
           create-link-with myself
          ] 
        ]
      ]
    

    Note: we are creating one turtle first because otherwise, the first turtle created would throw a "found nobody" error.