如何在NetLogo中创建图形对象

时间:2019-05-12 15:05:56

标签: netlogo

我想使用NetLogo的R扩展将图形对象发送到R,然后使用iGraph包计算并返回一些度量。 iGraph可以从邻接矩阵或边缘列表创建图形(还有其他选项)。我想发送给R的图只是一个代理集之间的链接。有谁知道如何做到这一点? NW:save-matrix会将邻接矩阵导出到文件。我需要这样做,然后将文件读回R还是有更直接的方法?

一如既往,谢谢!

1 个答案:

答案 0 :(得分:2)

我过去所做的是在NetLogo中构建网络,将网络导出到R,计算R中的网络指标,然后检索指标。我在这些项目之一中使用的相关代码是:

to export-nw2r
  ; create file with useful graph format
  nw:set-context people links
  let filename (word "Networks/netlogo" behaviorspace-run-number ".gml")
  export-simple-gml filename

  ;; reset the R-workspace
  r:clearLocal
  let dir pathdir:get-model
  r:eval "library(igraph)"

  ; read network in R
  set filename (word dir "/" filename)
  r:put "fn" filename
  r:eval "gg <- read_graph(file = fn, format = 'gml')"
  r:eval "V(gg)$name <- V(gg)$id"          ; gml uses 'id', but igraph uses 'name'
  r:eval "if (file.exists(fn)) file.remove(fn)"
end

to calc-network-properties
  r:eval "library(ineq)"

  ; network size
  set sizeN count people
  set sizeE count links
  output-type "Nodes: " output-print sizeN
  output-type "Edges: " output-print sizeE

  ; calculate degree properties
  r:eval "degs <- degree(gg)"
  r:eval "aveDeg <- mean(degs)"
  set aveDeg r:get "aveDeg"
  output-type "Mean Degree: " output-print precision aveDeg 2
  r:eval "giniDeg <- ineq(degs, type = \"Gini\")"
  set giniDeg r:get "giniDeg"
  output-type "Gini of Degree: " output-print precision giniDeg 2

  ; calculate transitivity properties
  r:eval "lccs <- transitivity(gg, type = \"localundirected\")"
  r:eval "aveCC <- mean(lccs, na.rm = TRUE)"
  set aveCC r:get "aveCC"
  output-type "Mean Clustering: " output-print precision aveCC 2
  r:eval "trans <- transitivity(gg, type = \"undirected\")"
  set trans r:get "trans"
  output-type "Transitivity: " output-print precision trans 2

  ; paths and betweenness
  r:eval "paths <- distances(gg)"
  r:eval "paths <- paths[upper.tri(paths)]"
  r:eval "avePath <- mean(paths)"
  set avePath r:get "avePath"
  output-type "Mean Shortest Path: " output-print precision avePath 2
  r:eval "diam <- max(paths)"
  set diam r:get "diam"
  output-type "Max Shortest Path: " output-print diam
  r:eval "giniPaths <- ineq(paths, type = \"Gini\")"
  set giniPaths r:get "giniPaths"
  output-type "Gini of Paths: " output-print precision giniPaths 2
  r:eval "btws <- betweenness(gg)"
  r:eval "giniBtwn <- ineq(btws, type = \"Gini\")"
  set giniBtwn r:get "giniBtwn"
  output-type "Gini of Betweenness (V): " output-print precision giniBtwn 2
end