Network Modularity

No preview image

1 collaborator

Default-person David Weintrop (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0beta2 • Viewed 658 times • Downloaded 55 times • Run 3 times
Download the 'Network Modularity' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


## WHAT IS IT?

The code example provides an illustration of how to import network data from external files. This is useful when you have a specific network, perhaps created in another program or taken from real world data, that you would like to recreate in NetLogo.

It imports data from two different files. The first is the "attributes.txt" file, which contains information about the nodes -- in this case, the node-id, size, and color of each node. The second is the "links.txt" file, which contains information on how the nodes are connected and the strength of their connection.

## NETLOGO FEATURES

The link primitives are used to represent and process connections between nodes.

The file primitives are used to read data from external files.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

turtles-own [
  node-id
  community
]

globals [modularity]

undirected-link-breed [undirected-links undirected-link]

to create-simple-network
  create-nodes 95 
  import-simple-network-links
  calc-modularity
end 

to create-nodes [cnt]
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  __clear-all-and-reset-ticks
  set-default-shape turtles "circle"
  repeat cnt
  [
    crt 1 [
      set node-id who + 1
      set color blue
    ]
  ]
  ask turtles[ assign-community]
  reset-ticks
  reset-layout
end 

to import-simple-network-links
  file-open "simplenetwork.txt"
  ;; Read in all the data in the file
  while [not file-at-end?]
  [
    ;; this reads a single line into a three-item list
    let items read-from-string (word "[" file-read-line "]")
    ask get-node (item 0 items)
    [
      create-undirected-link-with get-node (item 1 items)
    ]
  ]
  file-close
end 

;; Helper procedure for looking up a node by node-id.

to-report get-node [id]
  report one-of turtles with [node-id = id]
end 

to calc-modularity
  let running-total 0
  let link-exists 0
  ask turtles [
    let parent-links count my-links
    ask other turtles [
      if community = [community] of myself [ ;; only worry about pairs in same community
        ifelse link-neighbor? myself ;; look at if they are connected
        [ set link-exists 1 ]
        [ set link-exists 0 ]
        set running-total running-total + (link-exists - ((parent-links * count my-links) / (count links * 2) ))
      ]
    ]
    
    ;; handle self-links (i.e. every node is in the same community as itself, but no link exists)
    set running-total running-total + (0 - ((count my-links * count my-links)/(count links * 2) ))    
  ]
  set modularity running-total / (count links * 2)
end 

to swap-community
  ask one-of turtles [assign-community]
  calc-modularity
end 

to assign-community
    set community random num-communities
    set color item community [blue red green yellow violet orange grey cyan]
end 

to find-best-modularity
  let old-community 0
  let old-color black
  let candidate-node-id 0
  
  repeat iterations [
    tick
    let old-modularity modularity
    ask one-of turtles [
      set candidate-node-id node-id
      set old-community community
      set old-color color
      assign-community
    ]
    calc-modularity
      
    ;; check to see if swap was improvement, if not, undo it    
    if old-modularity > modularity [
      ask get-node candidate-node-id [
        set community old-community
        set color old-color
      ]
      calc-modularity
    ]
    layout-spring turtles links 0.2 6 3    
  ]
end 

to radial-layout
  layout-radial turtles links (turtle center-node)
end 

to reset-layout
    layout-circle (sort turtles) (max-pxcor - 1)
end 

; Copyright 2006 Uri Wilensky. This code may be freely copied, distributed,
; altered, or otherwise used by anyone for any legal purpose.

There is only one version of this model, created over 13 years ago by David Weintrop.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.