Network Import Example

Network Import Example preview image

1 collaborator

Default-person wang liting (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.4 • Viewed 314 times • Downloaded 30 times • Run 0 times
Download the 'Network Import Example' 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]

links-own [strength]

globals [links-list]

to import-network
  clear-all
  set-default-shape turtles "circle"
  import-attributes
  layout-spring turtles links 0.3 (world-width / (sqrt 62)) 1
  import-links
end 

;; This procedure reads in a files that contains node-specific attributes
;; including an unique identification number

to import-attributes
  ;; This opens the file, so we can use it.
  file-open "attributes.txt"
  ;; Read in all the data in the file
  ;; data on the line is in this order:
  ;; node-id attribute1 attribute2
  while [not file-at-end?]
  [
    ;; this reads a single line into a three-item list
    ;let patch-elevations file-read
   ; print (file-read)
    ;let items read-from-string (word "[" file-read-line "]")
    ;let next-turtle file-read
    crt 1 [
      let next-node-id file-read
     ; set node-id  item 0 items
      ;set size    item 1 items
      ;set color   item 2 items
     set label  next-node-id
    ]
  ]
  file-close
end 

;; This procedure reads in a file that contains all the links
;; The file is simply 3 columns separated by spaces.  In this
;; example, the links are directed.  The first column contains
;; the node-id of the node originating the link.  The second
;; column the node-id of the node on the other end of the link.
;; The third column is the strength of the link.

to import-links
  ;; This opens the file, so we can use it.
  file-open "links.txt"
  ; show file-read-line
   
  ;; 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-link-with get-node (item 1 items)
       ; [ set label item 2 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 


; Public Domain:
; To the extent possible under law, Uri Wilensky has waived all
; copyright and related or neighboring rights to this model.

There is only one version of this model, created over 12 years ago by wang liting.

Attached files

File Type Description Last updated
Network Import Example.png preview Preview for 'Network Import Example' over 12 years ago, by wang liting Download

This model does not have any ancestors.

This model does not have any descendants.