-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parallel RNetLogo Simulation.R
99 lines (91 loc) · 2.34 KB
/
Parallel RNetLogo Simulation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
library(parallel)
#create connection of NetLogo to R
RNetlogoSetup <- function(dummy,
gui,
nl.path,
nl.jarname,
model.path) {
library(RNetLogo)
NLStart(nl.path = nl.path,
gui = gui,
nl.jarname = nl.jarname)
NLLoadModel(model.path)
}
NetLogoSimulation <-
function(dataSetPath) {
#generate cluster and load with variables
#NOTICE: NetLogo Connection.R has to be loaded first
#in order for the Global Environment to feed each core
load(dataSetPath)
cores <- detectCores()
cl <- makeCluster(cores)
clusterExport(cl, ls())
clusterEvalQ(cl, as.vector(lsf.str(.GlobalEnv)))
#setup Netlogo
parLapply(
cl,
1:cores,
RNetlogoSetup,
gui = FALSE,
nl.path = nl.path,
nl.jarname = nl.jarname,
model.path = model.path
)
#procedure
paramsGlobal <- c("alpha", "beta", "gamma", "psi", "h", "mu")
paramsLocal <-
c("alpha", "beta", "gamma", "psi", "h", "mu", "delta")
#generate prior mean and variance for global and local control strategies
testGlobal <-
parsapply(
cl,
1:n,
calculateNorm,
TRUE,
alphaGlobal,
betaGlobal,
hGlobal,
gammaGlobal,
muGlobal,
psiGlobal,
0
)
expectedGlobal <- mean(testGlobal)
tolGlobal <- c(2 * sd(testGlobal), sd(testGlobal))
testLocal <-
parsapply(
cl,
1:n,
calculateNorm,
FALSE,
alphaLocal,
betaLocal,
hLocal,
gammaLocal,
muLocal,
psiLocal,
deltaLocal
)
expectedLocal <- mean(testLocal)
tolLocal <- c(2 * sd(testLocal), sd(testLocal))
resultsGlobal <-
parSapply(cl,
paramsGlobal,
runProcedure(),
TRUE,
expectedGlobal,
tolGlobal)
resultsLocal <-
parSapply(cl,
paramsLocal,
runProcedure(),
FALSE,
expectedLocal,
tolLocal)
#close Netlogo and report results
parLapply(cl, RNetLogoQuit, 1:cores)
return(list(Global = resultsGlobal, Local = resultsLocal))
}
RNetLogoQuit <- function(dummy) {
NLQuit()
}