Wednesday, October 14, 2015

20151014_generate_grid_networks.R


Purpose: generate grid network to my simulation code, to see whether simulation can reproduce analytic results. 

 file: 20151014_generate_grid_networks.R
helen:mactower-network-failure-simulation hqin$ pwd

/Users/hqin/github/mactower-network-failure-simulation

helen:mactower-network-failure-simulation hqin$ ll -t | head
total 201880
-rw-r--r--   1 hqin  staff    35K Oct 14 22:05 _Degree4N1000_network.csv
-rw-r--r--   1 hqin  staff   9.8K Oct 14 22:05 _Degree4N100_EssenLookupTb.csv

-rw-r--r--   1 hqin  staff   1.3K Oct 14 22:05 20151014_generate_grid_networks.R


##########################################
# generate gride network for simulation and quality control

rm(list=ls())
debug = 0; 

# R -f file --args Degree numOfEssenNode outNetworkFile outEssenLookupTbFile 
# R -f 20151014_generate_grid_networks.R --args 4 1000 _Degree4N1000_network.csv _Degree4N100_EssenLookupTb.csv

options(echo=TRUE) # if you want see commands in output file
args <- commandArgs(trailingOnly = TRUE)
print(args)
degree = as.numeric(args[1]); degree;
numOfEssenNodes = as.numeric(args[2]); numOfEssenNodes;
outNetworkFile = args[3]
outEssenLookupTbFile = args[4]

#for debug 
# degree=4; numOfEssenNodes=1000; outNetworkFile = "__test.csv"; outEssenLookupTb="__EssenLookupTb.csv"

pairs = data.frame(integer(numOfEssenNodes*degree), integer(numOfEssenNodes*degree))
names(pairs) = c("No1","No2")

for( i in 1:numOfEssenNodes) {
  current_i_row = (i-1)*degree + 1
  for( j in 1:degree){
     pairs$No1[current_i_row +j-1] = i;
     pairs$No2[current_i_row +j-1] = current_i_row +j-1 + numOfEssenNodes
   }
}
# check
table( pairs$No1)[1:10]
table( pairs$No2)[1:10]
length(unique(pairs[,1]))
length(unique(pairs[,2]))


write.csv(pairs, outNetworkFile, row.names = F)

outEssenLookupTb = c(rep(1, numOfEssenNodes), rep(0, numOfEssenNodes*degree))
write.csv(outEssenLookupTb, outEssenLookupTbFile, row.names = F)







No comments:

Post a Comment