This page was automatically generated by NetLogo 5.0.2.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Oracle's Java site.


In order for this to work, this file, your model file (Network Extension General Demo.nlogo), and the files NetLogoLite.jar and NetLogoLite.jar.pack.gz must all be in the same directory. (You can copy NetLogoLite.jar and NetLogoLite.jar.pack.gz from the directory where you installed NetLogo.)

The applet will also need access to the nw extension. Copy the entire directory for each required extension into the same directory as the applet.

On some systems, you can test the applet locally on your computer before uploading it to a web server. It doesn't work on all systems, though, so if it doesn't work from your hard drive, please try uploading it to a web server.

You don't need to include everything in this file in your page. If you want, you can just take the HTML code beginning with <applet> and ending with </applet>, and paste it into any HTML file you want. It's even OK to put multiple <applet> tags on a single page.

If the NetLogoLite files and your model are in different directories, you must modify the archive= and value= lines in the HTML code to point to their actual locations. (For example, if you have multiple applets in different directories on the same web server, you may want to put a single copy of the NetLogoLite files in one central place and change the archive= lines of all the HTML files to point to that one central copy. This will save disk space for you and download time for your users.)

powered by NetLogo

view/download model file: Network Extension General Demo.nlogo

WHAT IS IT?

This model demonstrates various features of the Netlogo Network extension.

HOW IT WORKS

It is not a model of anything per se, but rather a collection of tools that allow you the generate various kind of networks, lay them out on screen, and get information about them.

HOW TO USE IT

The first thing to think about is if you want to use directed or undirected links. The links-to-use chooser will allow you to specify that: all the generators will use the kind of links specfied in the chooser. You can generate different networks with different kinds of links without clearing everything in between.

As an aside, also note that the value of the links-to-use chooser is used by the different clusterers and measures as well. Be careful to use the right value for the network you are interested in! For example, if you ask for betweenness centrality with “directed links” selected in the chooser, but the network on the screen is undirected, the betweenness centrality values will all be zero, because the algorithm only takes directed links into account!

On the right of the links-to-use chooser, is another one called layout. NetLogo currently offers four different kinds of layouts (this is not new in the extension - they were all available before): circle, radial, spring and tutte. The chosen layout will be applied automatically when generating new networks, but you can also apply yourself by clicking the layout button. One special case is the “spring” layout, which works better when applied multiple times (or even continuously). To make it easy to do that, we have a spring forever button. When you click this button, it will automatically choose “spring” layout and apply it as long as the button is pressed.

Now that you know what kind of links you want, and how you want it to look, it’s time to generate a network!

Generators

The first thing that you will see in the Generators section of the model is a slider labeled nb-nodes. As you might have guessed, this will allow you to specify the number of nodes you want to have in your network. The first five generator buttons (preferential attachment, ring, star, wheel, and random) will take the value of that slider into account.

Note that at any time, you can press the clear button to erase everything and start over.

Here is a description of each of them:

preferential attachment

Generates a new network using the Barabási–Albert algorithm. This network will have the property of being “scale free”: the distribution of degrees (i.e. the number of links for each turtle) should follow a power law.

Turtles are added, one by one, each forming one link to a previously added turtle, until nb-nodes is reached. The more links a turtle already has, the greater the probability that new turtles form links with it when they are added.

ring

Generates a ring network of nb-nodes turtles, in which each turtle is connected to exactly two other turtles.

star

Generates a star network in which there is one central turtle and every other turtle is connected only to this central node. The number of turtles can be as low as one, but it won’t look much like a star.

wheel

Generates a wheel network, which is basically a ring network with an additional “central” turtle that is connected to every other turtle. The number of nodes must be at least four.

On the right side of the wheel button, you will see a chooser allowing you the select either “inward” or “outward”. This will allow to specify if the “spokes” of the wheel point toward the central turtle (inward) or away from it (outward). This is, of course, meaningful only in the case of a directed network.

random

Generates a new random network of nb-nodes turtles in which each one has a connection probability (between 0 and 1) of being connected to each other turtles (this is specified through the connection-prob slider). The algorithm uses the Erdős–Rényi model.

lattice 2D

Generates a new 2D lattice network (basically, a grid) of nb-rows rows and nb-cols columns. The grid will wrap around itsef if the wrap switch is set to on.

small world

Generates a new small-world network using the Kleinberg Model.

The generator uses the same sliders and switch as the lattice 2D generator, namely, nb-rows, nb-cols and wrap. The algorithm proceeds by generating a lattice of the given number of rows and columns (the lattice will wrap around itself if wrap is on). The “small world effect” is created by adding additional links between the nodes in the lattice. The higher the clustering-exponent, the more the algorithm will favor already close-by nodes when adding new links. A clustering exponent of 2.0 is typically used.

Clusters and cliques

Now that you have generated one or more networks, there are things that you might want to know about them.

k-means

Partitions the turtles in the current network into nb-clusters different groups. The k-means algorithm is an iterative process that will produce groupings based on the x y coordinates of the turtles. If this does not seem very network related, it is because it isn’t. But in a future version of the extension, we will support clustering based on the distance in the network.

weak component clusters

This button will assign a different color to all the “weakly” connected components in the current network. A weakly connected component is simply a group of nodes where there is a path from each node to every other node. A “strongly” connected component would be one where there is a directed path from each node to every other. The extension does not support the identification of strongly connected components at the moment.

highlight bicomponents

Clicking on this button will put you in a mode where you use your mouse to highlight the different bicomponent clusters in the current network. A bicomponent (also known as a maximal biconnected subgraph) is a part of a network that cannot be disconnected by removing only one node (i.e. you need to remove at least two to disconnect it).

Note that one turtle can be a member of more than one bicomponent at once. If it is the case, all the bicomponents that the target turtle is part of will be highlighted when you move your mouse pointer near it, but they will be of different color.

highlight maximal cliques

The general usage for this is the same as for the highlight bicomponents mode. Note you should not try to use both highlight modes at the same time.

A clique is a subset of a network in which every node has a direct link to every other node. A maximal clique is a clique that is not, itself, contained in a bigger clique.

biggest maximal clique

This simply highlights the biggest of all the maximal cliques in the networks. If there are multiple cliques that are equally big (as is often the case), it simply highlights one at random.

Centrality measures

Besides all the clusterers and the clique finder, you can also calculate some centrality measures on your networks. All the centrality measures will label the nodes will the result of the calculation and adjust their size and color to reflect that result.

betweenness

To calculate the betweenness centrality of a turtle, you take every other possible pairs of turtles and, for each pair, you calculate the proportion of shortest paths between members of the pair that passes through the current turtle. The betweeness centrality of a turtle is the sum of these.

eigenvector

The Eigenvector centrality of a node can be thought of as the proportion of its time that an agent forever “walking” at random on the network would spend on this node. In practice, turtles that are connected to a lot of other turtles that are themselves well-connected (and so) get a higher Eigenvector centrality score.

Eigenvector centrality is only defined for connected networks, and will report false for disconnected graphs.

closeness

The closeness centrality of a turtle is defined as the inverse of the sum of it’s distances to all other turtles.

Note that this primitive reports the intra-component closeness of a turtle, that is, it takes into account only the distances to the turtles that are part of the same component as the current turtle, since distance to turtles in other components is undefined. The closeness centrality of an isolated turtle is defined to be zero.

Import / Export

Finally, you can save and load your networks. This is done through the use of simple text files containing an adjacency matrix.

The model currently always save the network to your NetLogo directory in a file called matrix.txt when you click the save button. When you click the load button, it reads from the same location and creates a new network from the file.

THINGS TO NOTICE

THINGS TO TRY

EXTENDING THE MODEL

The current version of the demo does not take link weights into account. You can add a “weight” variable to each link breed. Can you add a button assigning random weights to the links? Can you make it so that link thickness reflects the “weight” of the link? Look at the extensions documentation for primitive that take weights into account. Can you integrate those in the demo?

NETLOGO FEATURES

Well, this model obviously shows the network extension primitives.

But aside from that, notice the interesting use it makes of tasks for the centrality buttons. We have only one centrality procedure in the code that does all the hard work, and the other procedures call it with a measure reporter task as a parameter, that the centrality primitive then runs with runresult. This removes a lot of code duplication.

Another nice tidbit is how the foreach command is used in the color-clusters primitive. Notice how it loops over both the clusters list and the colors and then uses ?1 and ?2 to access members of each pair of cluster/color.

RELATED MODELS

A couple of models already in the model library, namely the “Giant Component” model and the “Small World” model could be build much more easily by using the primitives in the network extension. Such versions of these two models are included in the “demo” folder of the extension, but trying to make the modifications yourself would be an excellent exercice.

CREDITS AND REFERENCES

Copyright 2012 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

CODE

extensions [ nw ]

; we have two different kinf of link breeds, one directed and one undirected, just
; to show what the different networks look like with directed vs. undirected links
directed-link-breed [ dirlinks dirlink ]
undirected-link-breed [ unlinks unlink ]

globals [
  highlighted-node                ; used for the "highlight mode" buttons to keep track of the currently highlighted node
  highlight-bicomponents-on       ; indicates that highlight-bicomponents mode is active
  stop-highlight-bicomponents     ; indicates that highlight-bicomponents mode needs to stop
  highlight-maximal-cliques-on    ; indicates highlight-maximal-cliques mode is active
  stop-highlight-maximal-cliques  ; indicates highlight-maximal-cliques mode needs to stop
]

to clear
  clear-all
  set-current-plot "Degree distribution"
  set-default-shape turtles "circle"
end

;; Reports the link set corresponding to the value of the links-to-use combo box
to-report get-links-to-use
  report ifelse-value (links-to-use = "directed")
    [ dirlinks ]
    [ unlinks ] 
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Layouts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to redo-layout [ forever? ]
  if layout = "radial" and count turtles > 1 [
    layout-radial turtles links ( max-one-of turtles [ count my-links + count my-out-links + count my-in-links ] )
  ]
  if layout = "spring" [
    let factor sqrt count turtles
    if factor = 0 [ set factor 1 ]
    repeat ifelse-value forever? [ 1 ] [ 50 ] [
      layout-spring turtles links (1 / factor) (14 / factor) (1.5 / factor)
      display
      if not forever? [ wait 0.005 ]
    ]
  ]
  if layout = "circle" [
    layout-circle sort turtles max-pxcor * 0.9
  ]
  if layout = "tutte" [
    layout-circle sort turtles max-pxcor * 0.9
    repeat 10 [
      layout-tutte max-n-of (count turtles * 0.5) turtles [ count my-links ] links 12
    ]
  ]
end

to layout-once
  redo-layout false
end

to spring-forever
  set layout "spring" 
  redo-layout true
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Clusterers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Clusters the graph according to turtle position
to k-means
  nw:set-snapshot turtles get-links-to-use
  let clusters nw:k-means-clusters nb-clusters 1000 0.001
  if length clusters > 0 [ color-clusters clusters ]
end   

;; Colorizes each node according to which component it is part of
to weak-component
  nw:set-snapshot turtles get-links-to-use
  color-clusters nw:weak-component-clusters
end

;; Allows the user to mouse over and highlight all bicomponents
to highlight-bicomponents
  
  if stop-highlight-bicomponents = true [
    ; we're asked to stop - do so
    set stop-highlight-bicomponents false
    set highlight-bicomponents-on false
    stop
  ]
  set highlight-bicomponents-on true ; we're on!
  if highlight-maximal-cliques-on = true [
    ; if the other guy is on, he needs to stop
    set stop-highlight-maximal-cliques true
  ]
  
  if mouse-inside? [ 
    nw:set-snapshot turtles get-links-to-use
    highlight-clusters nw:bicomponent-clusters
  ]
  display
end

;; Allows the user to mouse over and highlight all maximal cliques
to highlight-maximal-cliques
  if (links-to-use != "undirected") [
    user-message "Maximal cliques only work with undirected links."
    stop
  ]
  if stop-highlight-maximal-cliques = true [
    ; we're asked to stop - do so
    set stop-highlight-maximal-cliques false
    set highlight-maximal-cliques-on false
    stop
  ]
  set highlight-maximal-cliques-on true ; we're on!
  if highlight-bicomponents-on = true [
    ; if the other guy is on, he needs to stop
    set stop-highlight-bicomponents true
  ]

  if mouse-inside? [ 
    nw:set-snapshot turtles unlinks
    highlight-clusters nw:maximal-cliques
  ]
  display
end

;; Colorizes the biggest maximal clique in the graph, or a random one if there is more than one
to find-biggest-clique
  if (links-to-use != "undirected") [
    user-message "Maximal cliques only work with undirected links."
    stop
  ]
  nw:set-snapshot turtles unlinks
  ;; color-clusters takes a list of lists (clusters) 
  ;; but we only have one, so we package it in a list
  color-clusters (list nw:biggest-maximal-clique)
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Highlighting and coloring of clusters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Allows the user to mouse over different nodes and 
;; highlight all the clusters that this node is a part of
to highlight-clusters [ clusters ]
  ;; get the node with neighbors that is closest to the mouse
  let node min-one-of turtles [ distancexy mouse-xcor mouse-ycor ]
  if node != nobody and node != highlighted-node [
    set highlighted-node node
    ;; find all clusters the node is in and assign them different colors
    color-clusters filter [ member? node ? ] clusters
    ;; highlight target node
    ask node [ set color white ]
  ]
end

to color-clusters [ clusters ]
  ;; reset all colors
  ask turtles [ set color gray - 3 ]
  ask links [ set color gray - 3 ]
  let n length clusters
  let colors ifelse-value (n <= 12)
    [ n-of n remove gray remove white base-colors ] ;; choose base colors other than white and gray
    [ n-values n [ approximate-hsb (random 255) (255) (100 + random 100) ] ] ; too many colors - pick random ones
  
    ;; loop through the clusters and colors zipped together
    (foreach clusters colors [      
      let cluster ?1
      let cluster-color ?2
      foreach cluster [ ;; for each node in the cluster
        ask ? [
          ;; give the node the color of its cluster
          set color cluster-color
          ;; colorize the links from the node to other nodes in the same cluster
          ;; link color is slightly darker...
          ask my-unlinks [ if member? other-end cluster [ set color cluster-color - 1 ] ]
          ask my-in-dirlinks [ if member? other-end cluster [ set color cluster-color - 1 ] ]
          ask my-out-dirlinks [ if member? other-end cluster [ set color cluster-color - 1 ] ]
        ] 
      ]
    ])
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Centrality Measures
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to betweenness
  centrality task nw:betweenness-centrality
end

to eigenvector
  centrality task nw:eigenvector-centrality
end

to closeness
  centrality task nw:closeness-centrality
end

;; Takes a centrality measure as a reporter task, runs it for all nodes
;; and set labels, sizes and colors of turtles to illustrate result
to centrality [ measure ]
  nw:set-snapshot turtles get-links-to-use
  ask turtles [
    let res (runresult measure) ;; run the task for the turtle
    ifelse is-number? res [
      set label precision res 2
      set size res ;; this will be normalized later
    ]
    [ ;; if the result is not a number, it is because eigenvector returned false (in the case of disconnected graphs
      set label res
      set size 1
    ]
  ]
  normalize-sizes-and-colors
end

;; We want the size of the turtles to reflect their centrality, but different measures 
;; give different ranges of size, so we normalize the sizes according to the formula 
;; below. We then use the normalized sizes to pick an appropriate color.
to normalize-sizes-and-colors
  if count turtles > 0 [
    let sizes sort [ size ] of turtles ;; initial sizes in increasing order
    let delta last sizes - first sizes ;; difference between biggest and smallest
    ifelse delta = 0 [ ;; if they are all the same size
      ask turtles [ set size 1 ]
    ]
    [ ;; remap the size to a range between 0.5 and 2.5
      ask turtles [ set size ((size - first sizes) / delta) * 2 + 0.5 ]
    ]
    ask turtles [ set color scale-color red size 0 5 ] ; using a higher range max not to get too white...
  ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Generators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to generate [ generator-task ]
  ; we have a general "generate" procedure that basically just takes a task 
  ; parameter and run it, but takes care of calling layout and update stuff
  set-default-shape turtles "circle"
  run generator-task
  layout-once
  update-plots
end

to preferential-attachment
  generate task [ nw:generate-preferential-attachment turtles get-links-to-use nb-nodes ]
end

to ring
  generate task [ nw:generate-ring turtles get-links-to-use nb-nodes ]
end  

to star
  generate task [ nw:generate-star turtles get-links-to-use nb-nodes ]
end  

to wheel
  ifelse (links-to-use = "directed") [
    ifelse spokes-direction = "inward" [ 
      generate task [ nw:generate-wheel-inward turtles get-links-to-use nb-nodes ] 
    ]
    [ ; if it's not inward, it's outward
      generate task [ nw:generate-wheel-outward turtles get-links-to-use nb-nodes ] 
    ]
  ]
  [ ; for an undirected network, we don't care about spokes
    generate task [ nw:generate-wheel turtles get-links-to-use nb-nodes ]
  ]
end  

to lattice-2d
  generate task [ nw:generate-lattice-2d turtles get-links-to-use nb-rows nb-cols wrap ]
end

to small-world
  generate task [ nw:generate-small-world turtles get-links-to-use nb-rows nb-cols clustering-exponent wrap ]
end

to generate-random
  generate task [ nw:generate-random turtles get-links-to-use nb-nodes connexion-prob ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Saving and loading of matrices
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to save
  nw:set-snapshot turtles get-links-to-use
  nw:save-matrix "matrix.txt"
end

to load
  generate task [ nw:load-matrix "matrix.txt" turtles get-links-to-use ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Reporters for monitors
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to-report mean-path-length
  nw:set-snapshot turtles links
  report nw:mean-path-length
end


;; Copyright 2012 Uri Wilensky.
;; See Info tab for full copyright and license.