-
Notifications
You must be signed in to change notification settings - Fork 0
/
models7to9.R
250 lines (229 loc) · 10 KB
/
models7to9.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Model 11 ####
#' Models 6-9 established that, for a simple estimation problem, an evolutionary
#' pressure can emerge favouring egocentric bias where advice is communicated
#' noisily (models 6 & 7), made with less sensitivity than initial decisions
#' (model 8), or is occasionally deliberately misleading (model 9).
#' Here we explore whether these findings remain true for a different type of
#' decision, namely a categorical problem. Here the world value is uniformly
#' distributed between 0 and 100 (excluding 50), and values of 0-49 should be
#' categorised as 0 while values of 51-100 should be categorised as 1. Advice
#' comes graded by confidence and is combined with an internal estimate of
#' confidence to arrive at a final categorical decision.
# Libraries
if(!require('parallel')) {
install.packages(repos="http://cran.r-project.org",'parallel')
library(parallel)
}
parallel <- T
# Agents have direct access to one another's confidence.
ARC <- Sys.info()[[1]] != 'Windows'
if(ARC)
setwd(paste0(getwd(), '/EvoEgoBias'))
if(parallel)
ARC <- T
if(ARC) {
print(Sys.info())
# Set up the parallel execution capabilities
nCores <- detectCores()
cl <- makeCluster(nCores)
print(paste('Running in parallel on', nCores, 'cores.'))
reps <- nCores
} else {
reps <- 1
}
# Define the function
runModel <- function(spec) {
print(spec)
setwd(spec$wd)
source('evoSim/evoSim/R/evoSim.R')
data <- evoSim(agentCount = spec$agents,
agentDegree = spec$degree,
decisionCount = spec$decisions,
generationCount = 2500,
mutationChance = 0.01,
other = list(sensitivity = spec$sensitivity,
sensitivitySD = spec$sensitivitySD,
startingEgoBias = spec$startingEgoBias,
adviceNoise = spec$adviceNoise,
badAdviceProb = spec$badAdviceProb),
makeAgentFun = function(modelParams, parents = NULL) {
# Inherit egoBias if there's a previous generation and we're not mutating
if(!is.null(parents)) {
if(runif(1) < modelParams$mutationChance) {
# mutate
egoBias <- rnorm(1, parents$egoBias, 0.1)
} else {
egoBias <- parents$egoBias
}
}
else {
egoBias <- modelParams$other$startingEgoBias#rnorm(1, .5, 1)
}
sensitivity <- abs(rnorm(1, mean = modelParams$other$sensitivity,
sd = modelParams$other$sensitivitySD))
# Keep egoBias to within [0-1]
egoBias <- clamp(egoBias, maxVal = 1, minVal = 0)
return(data.frame(sensitivity, egoBias))
},
selectParentsFun = function(modelParams, agents, world, ties) {
tmp <- agents[which(agents$generation == world$generation),]
tmp <- tmp[order(tmp$fitness, decreasing = T),]
# drop the worst half of the population
# tmp <- tmp[1:2,]#(floor(nrow(tmp)/2)), ]
# the others get weighted by relative fitness which are transformed to +ve values
tmp$fitness <- tmp$fitness - min(tmp$fitness) + 1
# scale appropriately
while(any(tmp$fitness < 10))
tmp$fitness <- tmp$fitness * 10
# and round off
tmp$fitness <- round(tmp$fitness)
tickets <- vector(length = sum(tmp$fitness)) # each success buys a ticket in the draw
i <- 0
for(a in 1:nrow(tmp)) {
tickets[(i+1):(i+1+tmp$fitness[a])] <- a
i <- i + 1 + tmp$fitness[a]
}
winners <- sample(tickets, modelParams$agentCount, replace = T)
# The winners clone their egocentric discounting
winners <- tmp[winners,'id']
return(winners)
},
getWorldStateFun = function(modelParams, world) {
return(50)
},
getAdviceFun = spec$getAdviceFun)
# save results
n <- length(unique(data$agents$generation))
results <- data.frame(generation = unique(data$agents$generation),
modelDuration = rep(data$duration, n),
sensitivity = rep(spec$sensitivity, n),
sensitivitySD = rep(spec$sensitivitySD, n))
# bind in the stats of interest aggregated by the generation
results <- cbind(results,
aggregate(data$agents,
list(data$agents$generation),
mean)[ ,c('fitness',
'degree',
'sensitivity',
'egoBias',
'initialDecision',
'finalDecision')])
rawdata <- data
rawdata$rep <- 1
return(list(rawdata = rawdata, results = results))
}
# Advice functions - there's noisy advice and bad advice
noisyAdviceFun <- function(modelParams, agents, world, ties) {
mask <- which(agents$generation == world$generation)
agents$advisor[mask] <- apply(ties, 1, function(x) sample(which(x != 0),1))
# Fetch advice as a vector
n <- length(mask)
agents$advice[mask] <- rnorm(n,
rep(world$state, n),
clamp(agents$sensitivity[agents$advisor[mask]]
+ modelParams$other$adviceNoise,
Inf))
return(agents)
}
badAdviceFun <- function(modelParams, agents, world, ties) {
mask <- which(agents$generation == world$generation)
agents$advisor[mask] <- apply(ties, 1, function(x) sample(which(x != 0),1))
# Fetch advice as a vector
n <- length(mask)
agents$advice[mask] <- rnorm(n,
rep(world$state, n),
clamp(agents$sensitivity[agents$advisor[mask]]
+ modelParams$other$adviceNoise,
Inf))
agents$advice[mask & (runif(n) < modelParams$other$badAdviceProb)] <-
world$state + modelParams$other$sensitivity + 3*modelParams$other$sensitivitySD
return(agents)
}
adviceFunctions <- c(noisyAdviceFun, badAdviceFun)
#for(modelNumber in 1:3) {
for(modelNumber in 3) {
# Storage path for results
resultsPath <- ifelse(ARC,'results/','results/')
time <- format(Sys.time(), "%F_%H-%M-%S")
resultsPath <- paste0(resultsPath,'mdl',modelNumber,'_',time)
# Clear the result storage variables
suppressWarnings(rm('rawdata'))
suppressWarnings(rm('results'))
# Parameter space to explore
specs <- list()
for(x in c(1000))
for(y in c(10))
for(z in c(30))
for(s in c(10,100))
for(sSD in c(10))
for(sEB in c(0.01, 0.99))
for(aN in c(0, 10))
for(bA in c(.1,.1,.5))
specs[[length(specs)+1]] <- list(agents=x,degree=y,decisions=z,
sensitivity=s,sensitivitySD=sSD,
startingEgoBias=sEB,
adviceNoise = aN,
badAdviceProb = bA,
wd = getwd())
if(modelNumber <= length(adviceFunctions))
for(spec in specs)
spec$getAdviceFun <- adviceFunctions[[modelNumber]]
# Testing code for debugging parallel stuff
# rm('x','y','z','s','sSD','sEB','aN','bA')
# runModel(specs[[1]])
#specs <- specs[1:24]
# Run the models
# Run parallel repetitions of the model with these settings
startTime <- Sys.time()
if(!ARC) {
degreeResults <- lapply(specs, runModel)
} else {
print('Executing parallel operations...')
degreeResults <- parLapply(cl, specs, runModel)
}
print('...combining results...')
# Join up results
for(res in degreeResults) {
if(!exists('results'))
results <- res$results
else
results <- rbind(results, res$results)
if(!exists('rawdata'))
rawdata <- list(res$rawdata)
else
rawdata[[length(rawdata)+1]] <- res$rawdata
}
print(paste0('...complete.'))
print(Sys.time() - startTime)
print('Estimated data size:')
print(object.size(rawdata), units = 'auto')
print('Saving data...')
# Save data
write.csv(results, paste(resultsPath, 'results.csv'))
print('...saved csv...')
save(rawdata, file = paste(resultsPath, 'rawdata.Rdata'))
print('...saved rawdata...')
# Smaller datafile for stopping me running out of memory during analysis
allAgents <- NULL
for(rd in rawdata) {
rd$agents$agentCount <- rep(rd$model$agentCount,nrow(rd$agents))
rd$agents$agentDegree <- rep(rd$model$agentDegree,nrow(rd$agents))
rd$agents$decisionCount <- rep(rd$model$decisionCount,nrow(rd$agents))
rd$agents$modelDuration <- rep(rd$duration,nrow(rd$agents))
rd$agents$meanSensitivity <- rep(rd$model$other$sensitivity,nrow(rd$agents))
rd$agents$sdSensitivity <- rep(rd$model$other$sensitivitySD,nrow(rd$agents))
rd$agents$startingEgoBias <- rep(rd$model$other$startingEgoBias,nrow(rd$agents))
rd$agents$adviceNoise <- rep(rd$model$other$adviceNoise,nrow(rd$agents))
rd$agents$badAdviceProb <- rep(rd$model$other$badAdviceProb,nrow(rd$agents))
# only take a subset because of memory limitations
allAgents <- rbind(allAgents, rd$agents[rd$agents$generation%%50 == 1
| (rd$agents$generation%%25 == 1 & rd$agents$generation < 250), ])
}
save(allAgents, file = paste(resultsPath, 'rawdata_subset.Rdata'))
print('...saved subset...')
print('...data saved.')
}
# Cleanup
if(ARC)
stopCluster(cl)
print('Complete.')