kenjanaki
kenjanaki

Reputation: 21

How do I code a Hierarchical Network according to this paper using Netlogo?

I'm trying to code a hierarchical network using NetLogo following the paper "Hierarchical organization in complex networks" (2003) by Erzsebet Ravasz and Albert-Laszlo Barabasi.

I first would like to build the model in Section II (Hierarchical Network Model) and then Section III (Hierarchical Organization in Real Networks) but struggling to find a way to implement it due to lack of Object Oriented Programming in NetLogo.

So far, here's my code for Section II (Hierarchical Network Model):

globals [ dist help generation-count ]

turtles-own [
  center-node?
  generation
  model?
]

to setup
  ca
  set dist 1.2
   crt num [
    create-links-with other turtles
    set shape "dot"
    set size 1.5
  ]
  let c 0
  repeat (num - 1)[
    ask one-of turtles with [xcor = 0 and ycor = 0][
      set heading 360 / (num - 1) * c fd dist
    ]
    set c c + 1
  ]
  ask turtles [
    ifelse ( xcor = 0 and ycor = 0 ) [ set center-node? true ][ set center-node? false ]
    set color      red
    set generation 1
    set model?     true
  ]
  set generation-count 1
 ; repeat x [ recreate ]
end

to recreate
  set generation-count generation-count + 1
  let c 0
  set help count turtles
  loop [
    ask turtles with [ model? = true ][
      hatch 1 [
        set generation generation-count
        repeat generation - 1 [
          set heading 0
          set heading (heading + 360 / (num - 1) * c)
          fd generation * dist
        ]
        set model? false
      ]
    ]
    finish-member ( turtles with [ count link-neighbors = 0 ] )
    set c c + 1
    ask turtles with [ center-node? = false and generation = generation-count ][ create-links-with other turtles with [ center-node? = true and generation = 1 ] ]
    ;layout
    if ( count turtles = help * num ) [ stop ]
  ]
end

to finish-member [ people ]
  ask people [ create-links-with other people ]
end

to layout
  ;; the number 3 here is arbitrary; more repetitions slows down the
  ;; model, but too few gives poor layouts
  repeat 100 [
    ;; Refactoring the link lengths (MODIFY DENSITY HERE?)
    ;; the more turtles we have to fit into the same amount of space,
    ;; the smaller the inputs to layout-spring we'll need to use
    let factor ((sqrt count turtles) / 3) ;; Here SF-density-mod influences the distance factor across the network - will impact search function...
                                          ;; numbers here are arbitrarily chosen for pleasing appearance
    layout-spring turtles links (1 / factor) (7 / factor) (1 / factor)
    display  ;; for smooth animation
  ]
  ;;; Centering network ;;;
  ;; don't bump the edges of the world
  let x-offset max [xcor] of turtles + min [xcor] of turtles
  let y-offset max [ycor] of turtles + min [ycor] of turtles
  ;; big jumps look funny, so only adjust a little each time
  set x-offset limit-magnitude x-offset 0.1
  set y-offset limit-magnitude y-offset 0.1
  ask turtles [ setxy (xcor - x-offset / 2) (ycor - y-offset / 2) ]
end

to-report limit-magnitude [ number limit ]
  if number > limit [ report limit ]
  if number < (- limit) [ report (- limit) ]
  report number
end

I was unable to position them like in the paper so I resorted to the NetLogo "layout" procedure but then I realized the way they were being linked is also incorrect; the peripheral outer nodes are not correctly linking to the peripheral central nodes.

Expected enter image description here

My Model enter image description here

The attached picture is for when n = 2, which is just incorrect.

I want to be able to somehow use recursion so I can also use it for Section III (Hierarchical Organization in Real Networks) as well.

Upvotes: 2

Views: 103

Answers (1)

I didn't make it on NetLogo, but if it is still useful for you or other people in the future looking at this post, I upload a Python code for building the Network following the paper "Hierarchical organization in complex networks" (2003) by Erzsebet Ravasz and Albert-Laszlo Barabasi.

def RavaszBarabasi(n):

  G = nx.Graph() #Create an empty network to join the graphs there
  if n == 0:
      return nx.complete_graph(5) #We create the base block of this fractal, a K5

  for i in range(5):  #We create four copies of the previous step
      H = RavaszBarabasi(n - 1)
      mapping = {node: f"{i}_{node}" for node in H.nodes()} #Rename the nodes of H to make them disjoint
      G = nx.compose(G, nx.relabel_nodes(H, mapping)) #We join the H_i to G with the renamed nodes to make them disjoint

  #We create this list because we are going to iterate and change the nodes of G at the same time, if we use G directly, the for loop gives an error
  nodes_to_connect = list(G.nodes())
  for node in nodes_to_connect[1:]: #By starting at 1, we eliminate the origin and avoid unnecessary comparisons
    if n < 2:
      if node[-1] != "0": #Since only 4 out of every 5 nodes of a K5 need to be connected to "0_0", we do it for all except those ending in 0
        G.add_edge(nodes_to_connect[0], node)
    else:
      if iter_if(n, node) == True:
        G.add_edge(nodes_to_connect[0], node)
  return G

node_color = ["#b31237", "#f03813", "#ff8826", "#ffb914", "#2c9fa3"]
colorcin = [node_color[i % len(node_color)] for i in range(len(G_RB.nodes()))]

plt.figure(figsize=(10, 10))

Upvotes: 0

Related Questions