Как использовать данные в модели NetLogo

Материал из Поле цифровой дидактики
Описание У нас есть наборы данных, которые мы хотим использовать в NetLogo модели. Как мы можем обратиться к этим данным и использовать их?
Область знаний Информатика, Большие данные, Статистика
Область использования (ISTE)
Возрастная категория


Поясняющее видео
Близкие рецепту понятия Датасет, Данные, CSV
Среды и средства для приготовления рецепта: R, NetLogo, GitHub

Контекст использования

Код модели

extensions [resource csv table]

breed [authors author]

authors-own [
  author-id
  author-name
  degree
]

links-own [
  shared-pages
]

globals [
  data
 
  page-to-authors
  author-index
  total-pages-processed
  total-authors-created
]

to setup-simulation
  clear-all
  reset-ticks
  clear-output
  
  set data []
  set page-to-authors table:make
  set author-index table:make
  set total-pages-processed 0
  set total-authors-created 0
  
  set-default-shape turtles "person"
  
  output-print "══════════════════════════════════════════════"
  output-print "WIKI AUTHOR CO-EDIT NETWORK MODEL"
  output-print "Choose dataset and click 'Build Network'"
  output-print "══════════════════════════════════════════════"
  output-print (word "Current chooser value: " School-chooser)
end

to load-data-for-current-school
  output-print (word "DEBUG School-chooser = " School-chooser)
  
  if School-chooser = "EN" [
    load-csv-resource "contributors_all_en.csv"
    stop
  ]
  if School-chooser = "DE" [
    load-csv-resource "contributors_all_de.csv"
    stop
  ]
  if School-chooser = "FR" [
    load-csv-resource "contributors_all_fr.csv"
    stop
  ]
  if School-chooser = "JP" [
    load-csv-resource "contributors_all_ja.csv"
    stop
  ]
  if School-chooser = "ID" [
    load-csv-resource "contributors_all_id.csv"
    stop
  ]
  if School-chooser = "RU" [
    load-csv-resource "contributors_all_ru.csv"
    stop
  ]
  
  output-print "ERROR: School-chooser does not match any dataset."
end

to load-csv-resource [filename]
  set data []
  set page-to-authors table:make
  set author-index table:make
  
  output-print ""
  output-print (word "Loading file: " filename)
  
  carefully [
    let csv-content resource:get filename
    set data csv:from-string csv-content
    output-print (word "Loaded: " length data " rows (including header)")
  ] [
    output-print (word "ERROR loading " filename ": " error-message)
    stop
  ]
  
  if length data > 0 [
    set data but-first data
  ]
  
  build-index-tables
  
  output-print (word "Processed: " length data " edit rows from " filename)
  output-print (word "Unique authors: " length table:keys author-index)
  output-print (word "Unique pages: " length table:keys page-to-authors)
end

to build-index-tables
  foreach data [ row ->
    let a-id item 0 row
    let a-name item 1 row
    let p-id item 2 row
    
    if not table:has-key? author-index a-id [
      table:put author-index a-id a-name
    ]
    
    let current-authors table:get-or-default page-to-authors p-id []
    
    if not member? a-id current-authors [
      table:put page-to-authors p-id lput a-id current-authors
    ]
  ]
end

to build-network

 
  load-data-for-current-school

  
  clear-turtles
  reset-ticks
  set total-pages-processed 0
  set total-authors-created 0
  
    output-print ""
  output-print (word "Building co-edit network for " School-chooser " ...")
  
  create-author-network
  style-network
  repeat 30 [
    do-layout
  ]
  
  output-print (word "Visible authors: " count authors)
  output-print (word "Visible co-edit links: " count links)
end

to create-author-network
  let pageids table:keys page-to-authors
  
  foreach pageids [ p-id ->
    let auth-list table:get page-to-authors p-id
    
    if length auth-list >= 2 [
      ensure-authors-exist auth-list
      connect-coeditors auth-list
      set total-pages-processed total-pages-processed + 1
    ]
  ]
  
  ask authors [
    set degree count link-neighbors
  ]
end

to ensure-authors-exist [auth-list]
  foreach auth-list [ a-id ->
    if not any? authors with [author-id = a-id] [
      create-authors 1 [
        set author-id a-id
        set author-name table:get author-index a-id
        set degree 0
        set shape "person"
        set size 1
        set color blue
        setxy random-xcor random-ycor
      ]
      set total-authors-created total-authors-created + 1
    ]
  ]
end

to connect-coeditors [auth-list]
  let n length auth-list
  let i 0
  
  while [i < n] [
    let j (i + 1)
    while [j < n] [
      let a1 item i auth-list
      let a2 item j auth-list
      
      let t1 one-of authors with [author-id = a1]
      let t2 one-of authors with [author-id = a2]
      
      if (t1 != nobody) and (t2 != nobody) [
        ask t1 [
          ifelse link-neighbor? t2 [
            ask link-with t2 [
              set shared-pages shared-pages + 1
            ]
          ] [
            create-link-with t2 [
              set shared-pages 1
            ]
          ]
        ]
      ]
      
      set j j + 1
    ]
    set i i + 1
  ]
end


;;;
to style-network
  ask authors [
    let capped-degree min list degree 10
    set size 0.7 + capped-degree * 0.08
    
    ifelse degree >= 10 [
      set color red
    ] [
      ifelse degree >= 5 [
        set color orange
      ] [
        set color sky
      ]
    ]
  ]
  
  ask links [
    let capped-shared min list shared-pages 5
    set thickness 0.1 + capped-shared * 0.05
    
    ifelse shared-pages >= 5 [
      set color red
    ] [
      ifelse shared-pages >= 2 [
        set color orange
      ] [
        set color gray
      ]
    ]
  ]
end



to do-layout
  layout-spring authors links 0.3 5 0.9
  display
end

to remove-top-hub
  if not any? authors [
    output-print "No authors to remove."
    stop
  ]
  
  let top-author max-one-of authors [count link-neighbors]
  let top-name [author-name] of top-author
  let top-degree [count link-neighbors] of top-author
  
  output-print (word "Removing top hub: " top-name " (degree = " top-degree ")")
  
  ask top-author [
    die
  ]
  
  ask authors [
    set degree count link-neighbors
  ]
  
  style-network
  
  repeat 20 [
    do-layout
  ]
  
  output-print (word "Remaining authors: " count authors)
  output-print (word "Remaining links: " count links)
end

Модель