Network Import Example
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
turtles-own [node-id] links-own [strength] globals [links-list] to import-network clear-all set-default-shape turtles "circle" import-attributes layout-circle (sort turtles) (max-pxcor - 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 items read-from-string (word "[" file-read-line "]") crt 1 [ set node-id item 0 items set size item 1 items set color item 2 items ] ] 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" ;; 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-to 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 are 9 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Network Import Example.png | preview | Preview for 'Network Import Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.