-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevolution.R
More file actions
139 lines (131 loc) · 5.41 KB
/
evolution.R
File metadata and controls
139 lines (131 loc) · 5.41 KB
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
# This code handles the entire evolution process for the genetic algorithm, with
# the exception of the breeding, i.e. sharing of parameter information from
# parent models to child models.
#
# written by Gianmarc Grazioli
source("fibril_assay.R")
source("breeding.R")
library(ergm)
library(sna)
library(parallel)
runOneSim <- function(param, suffStats, nodeCount, burnTime=2^20, np=16){
initGraph <- network.initialize(nodeCount,directed=F)
formula <- as.formula(initGraph~suffStats)
out<-simulate(as.formula(paste("initGraph", suffStats, sep="~")), coef=param,
control=control.simulate.formula(MCMC.burn=burnTime,MCMC.interval=1),
constraint=~bd(maxout=12),mc.cores=np)
return(out)
}
simAllparams <- function(params, suffStats, nodeCount, burnTime=2^20, np=16){
funcForParallel <- function(p){
sim<-runOneSim(p, suffStats=suffStats, nodeCount=nodeCount, burnTime=burnTime, np=np)
return(list("sim" = sim, "params" = p, "stats" = suffStats))
}
sims <- mclapply(params, funcForParallel, mc.cores = np)
return(sims)
}
thinTheHerd <- function(sims, fibrilType, topFrac=.5, maxSurvivors=20){
fibFracsWithParams <- list()
for (par in 1:length(sims[[1]])) {#loop over each parameter in the rep
fibFracTot = 0
for (rep in 1:length(sims)) {#loop over reps containing one of each parameter
nets <- sims[[rep]][[par]]$"sim"
params <- sims[[rep]][[par]]$"params"
stats <- sims[[rep]][[par]]$"stats"
fibFracData <- get(fibrilType, fibril_assay(nets))
fibFracTot <- fibFracTot + sum(fibFracData)/as.double(length(fibFracData))
}
fibFracsWithParams[[par]] <- list("fibFrac" = fibFracTot/length(sims), "params" = params)
}
topN = floor(topFrac*length(fibFracsWithParams))
if(topN < 2){cat("topN in thinTheHerd() dropped below 2\n"); topN=2}
if(topN > maxSurvivors){topN = maxSurvivors}
fibFracsOnly <- c()
paramsOnly <- list()
for (i in 1:length(fibFracsWithParams)) {
fibFracsOnly <- c(fibFracsOnly, fibFracsWithParams[[i]]$fibFrac)
paramsOnly[[i]] <- fibFracsWithParams[[i]]$params
}
theFittest = order(fibFracsOnly, decreasing = TRUE)[1:topN]
bestYields = sort(fibFracsOnly, decreasing = TRUE)[1:topN]
return(list("params"=paramsOnly[theFittest], "stats"=stats, "yields"=bestYields))
}
evolution <- function(origParents, numGenerations, numLinKids, numMutantsInit,
fibrilType, suffStats, nodeCount, topFrac=.5, noiseVarInit=.1,
reps=1, burnTime=2^20, np=16, maxSurvivors=20, fixEdge=F,
fixEdgeValue=100, initYieldCut=0.0, smartVar = T,
fudge=0.0, smartPts = T, useLineDensity=TRUE,
minLineDensity=.5, printParams=FALSE, pairMax = 10000, childMax = 100000){
generations <- list()
parents <- origParents
yieldCut <- initYieldCut
noiseVar <- noiseVarInit
numMutants <- numMutantsInit
if(printParams){
sink("all_params.csv")
sink()
}
for (g in 1:numGenerations) {
cat("Generation ", g, " is born!\n", sep = '')
newGeneration <- breedGeneration(parents, numLinKids, numMutants, noiseVar = noiseVar,
useLineDensity=useLineDensity, minLineDensity=minLineDensity,
pairMax = pairMax, childMax = childMax)
if (fixEdge){
for (p in 1:length(newGeneration)) {
newGeneration[[p]][1] <- fixEdgeValue
}
}
if(printParams){
sink("all_params.csv", append = TRUE)
for(p in newGeneration){
#print(c(p,g))
lineToPrint = c(unlist(p), g)
cat(lineToPrint, sep = ', ')
#cat(c(p,g), sep = ', ')
cat('\n')
}
sink()
}
simReps <- list()
cat("Simulating",length(newGeneration)*reps,"parameters...\n")
for (r in 1:reps) {
simReps[[r]] <- simAllparams(newGeneration, suffStats, nodeCount, burnTime=burnTime, np=np)
}
survivors <- thinTheHerd(simReps, fibrilType, topFrac = topFrac, maxSurvivors=maxSurvivors)
cat(length(survivors$params), "survivors remaining\n")
cat("Best yields =\n", survivors$yields,"\n")
generations[[g]] <- survivors
# Prevent backtracking to weaker parameters with these lines:
if(g > 1){
if( (survivors$yields[1] >= (generations[[g-1]]$yields[1])*(1.0 - fudge)) & (survivors$yields[1] >= yieldCut) ){
parents <- survivors$params
yieldCut <- survivors$yields[1]
noiseVar <- noiseVarInit
numMutants <- numMutantsInit
} else {
cat("Offspring did not surpass the parents, rerun the parents.\n")
if(smartVar){
noiseVar = noiseVar + noiseVar*.05
cat("Increasing noise variance to ", noiseVar, ".\n", sep = '')
if(smartPts){
numMutants = numMutants + 1
cat("Increasing number of mutants to ", numMutants, ".\n", sep = '')
}
}
}
} else {
if(survivors$yields[1] >= yieldCut){
parents <- survivors$params
} else {cat("First generation did not surpass the initYieldCut, rerun the originals.\n")}
}
}
return(generations)
}
testIt = FALSE
if(testIt){
nCount=48 #number of nodes
stats = "edges+kstar(2)+nsp(1:2)+esp(0)"
theOriginals = list(c(157.912 -log(nCount),-24,-3.3,-2.7,-20),
c(157.912 -log(nCount),-24,-3.3,-2.7,-20))
myGenerations = evolution(theOriginals, 3, 2, 2, "1,2 2-ribbon", stats, nCount)
}