Created
March 25, 2019 15:50
-
-
Save shawngraham/23c5cc0ceb4286fe74eb076f227665b2 to your computer and use it in GitHub Desktop.
implementing tom brughmans' https://archaeologicalnetworks.files.wordpress.com/2013/11/netlogo_networks_tutorial_v2.pdf on the network import code snippet netlogo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Written in Netlogo 6.0.4 | |
;; We implement Tom Brughmans simple pot trade model on top of a network | |
;; created by the code snippet 'network import example' | |
;; Brughmans' model code and explanation is at: https://archaeologicalnetworks.files.wordpress.com/2013/11/netlogo_networks_tutorial_v2.pdf | |
;; Here we have also implemented a 'breakage' feature that uses the | |
;; 'strength' value of the network as an indication of the difficulty | |
;; of trading pots over a particular route. You might interpret that value as bumpiness of the road, or likelihood of cargoes being lost, etc. | |
;; in the interface, create a slider with minimum 0, maximum 1 and set its value to 0.5. Call it trade-threshold | |
;; in the interface, create a slider with minimum 1, maximum 100 and set its value at 50. Call it num-pots | |
;; you can create a 'kill' button by right-clicking in the interface, selecting button, and putting the code below | |
;; into its code panel (without the ;; of course): | |
;; ask one-of links [die] | |
;; you can add links to the network randomly by making a button with the following code (without the ;;) : | |
;; ask one-of turtles [ create-link-to one-of other turtles | |
;; [set strength random 3 | |
;; set label strength]] | |
;; when you run the code, notice how the default network seems to shunt pots to particular nodes; notice how adding links | |
;; rejigs the way the 'economy' of this model works | |
turtles-own [node-id pots] | |
links-own [strength] | |
globals [links-list breakage-factor] ;; we add the global variable 'breakage-factor' to enable links and nodes to communicate data | |
to go | |
let total-pots sum [pots] of turtles | |
;let x count sites | |
repeat total-pots | |
[ trade-pots | |
] | |
let poorturtles turtles with [ pots < 0 ] | |
if any? poorturtles | |
[ | |
ask poorturtles [set pots 1] ;; this prevents division errors, but could also represent a kind of cyclical production | |
] | |
ask turtles [set size 0.1 + 5 * sqrt (pots / total-pots)] | |
tick | |
end | |
to trade-pots | |
; let breakage 0 ; represents a friction value, breakage in transit | |
let target one-of turtles with [pots > 0 and count out-link-neighbors > 0] | |
if target != nobody and random-float 1 > trade-threshold ;; target represents one site, one turtle at a time that we want to run our trade routine | |
[ | |
;ask target [ask one-of my-out-links [set breakage strength]] ;; converts the strength of the link to a number the turtles can use | |
ask links [set breakage-factor strength] | |
ask target | |
[ | |
print breakage-factor | |
set pots pots - (1 + breakage-factor) ;; 1 pot plus the breakage - so we're treating the strength of the link as a kind of friction, a certain kind of wastage on that route | |
ask one-of out-link-neighbors | |
[set pots pots + 1] ;;; no breakage, only the pot arrives | |
] | |
] | |
end | |
;; original import code | |
to import-network | |
clear-all | |
set-default-shape turtles "circle" | |
import-attributes | |
layout-circle (sort turtles) (max-pxcor - 1) | |
import-links | |
reset-ticks | |
;; added stuff | |
ask turtles [ | |
set pots random (num-pots) | |
] | |
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 "]") | |
create-turtles 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 | |
set strength item 2 items] ; MAR24 added this | |
] | |
] | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment