Skip to content

Instantly share code, notes, and snippets.

@sorashido
Created January 16, 2018 07:20
Show Gist options
  • Save sorashido/60f1f9bbeee18e546107c5d8c576ca4a to your computer and use it in GitHub Desktop.
Save sorashido/60f1f9bbeee18e546107c5d8c576ca4a to your computer and use it in GitHub Desktop.
## エージェント配置の画像化
source("./scripts/const.R")
library(GGally)
library(network)
library(sna)
library(ggplot2)
library(igraph)
#データの読み込み
file <- file.path(const$log_dir, const$data_dir, 'agent-network.csv')
csv_data <- read.csv(file, header=FALSE, colClasses=c(rep("character",3),"numeric"),col.names=c("id", "from", "to", "length"))
#ノード情報#
GPowerNodes<-data.frame("name"=head(csv_data$from[!duplicated(csv_data$from)],1),"group"=2,"size"=1)
GHomeNodes<-data.frame("name"=tail(csv_data$from[!duplicated(csv_data$from)],const$agent_num),"group"=1,"size"=1)
GNodes<-rbind(GPowerNodes,GHomeNodes)
#エッジの情報#
#エッジの重複を消す
csv_data <- csv_data[!duplicated(csv_data$id),]
#(始点,終点,辺の値)
GLinks <- data.frame("source"=as.numeric(gsub("power",0,gsub("home","",csv_data$from))),
"target"=as.numeric(gsub("power",0,gsub("home","",csv_data$to))),
"value"=3000/csv_data$length)
#グラフの作成
grid.net <- graph.data.frame(GLinks,directed=FALSE)
E(grid.net)$weight <- GLinks$value
V(grid.net)$name = GNodes$name
V(grid.net)$faction = GNodes$group
#ネットワークの特徴量
#ノード数
vcount(grid.net)
#エッジ数
ecount(grid.net)
#平均最短距離
average.path.length(grid.net)
#クラスタリング係数
transitivity(grid.net)
#平均次数
sum(degree(grid.net))/vcount(grid.net)
#描画処理
filePath <- paste(const$figure_dir,const$data_dir,"network.png", sep = "/")
png(filePath,width=1000,height=1000) #Open device
plot(grid.net,
vertex.size=10,
vertex.shape="circle",
vertex.color=ifelse(V(grid.net)$faction==1,"Pink","Lightgreen"),
edge.width=E(grid.net)$weight,
edge.arrow.size=0.2,
layout=layout.fruchterman.reingold)
dev.off() #Close device
#saveFigure(fig, 'agent-network.pdf')
@sorashido
Copy link
Author

sorashido commented Jan 16, 2018

出力画像
network

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment