-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithm_14_AutoMultiLocalEI.R
382 lines (318 loc) · 11.8 KB
/
algorithm_14_AutoMultiLocalEI.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#### Algorithm depends on function curve!!!!
####
curveDf <- data.frame("batchSize" = c(16,8,4,2,1), "s" = c(1,1.7,3,5.3,7.8))
curveDf$budgetTakenPerIter <- rev(curveDf$s)
experimentPath <- "exAuto2804"
getEvalCost <- function(currentBatchSize){
return(curveDf$budgetTakenPerIter[curveDf$batchSize == currentBatchSize])
}
print("libPaths:")
print(.libPaths())
library(SPOT)
source("objFun.R")
require(reticulate)
library(dplyr)
require(DiceKriging)
require(DiceOptim)
require(mnormt)
library(flacco)
library(checkmate)
args = commandArgs(trailingOnly=TRUE)
print("Args:")
print(args)
retries = 10
while(tryCatch({
use_python("/usr/bin/python", required = T)
py_config()
return(0)
}, error = function(e) {
return(1)
})){
retries <- retries - 1
if(retries <= 0){
break
}
}
### RUN Parameters #########################################
### Recieve Setup ###
###
seed <- as.numeric(args[1])
set.seed(seed)
funID <- as.numeric(args[2])
algoID <- as.numeric(args[3])
nDim <- as.numeric(args[4])
budget <- as.numeric(args[5])
maxBatchSize <- as.numeric(args[6])
## In this algo the batchsize is the max possible batch Size!!
## Definition of a simple 1+1-ES
do1plus1EsIter <- function(histList, f){
sr=0.2 #succes rate limit
a=1.2 #step size multiplier
g=10 #length of memory list (for success rate)
if(is.null(histList$sigma)){
sigma=0.1 #initial step size
}else{
sigma=histList$sigma
}
x <- histList$x
y <- histList$y
memory <- histList$memory
n <- length(x) #number of variables
x1 <- x+sigma*rnorm(n,0,1)
y1 <- f(x1)
if(y1 < y){ #success
x <- x1
y <- y1
if(length(memory)<g){
memory <- c(memory,1)
}else{
memory <- c(memory[-1],1)
}
}else{ #fail
if(length(memory)<g){
memory <- c(memory,0)
}else{
memory <- c(memory[-1],0)
}
}
if(length(memory)>=g){
successrate <- sum(memory)/g
print(paste("Sigma adaptation:",successrate))
if(successrate>sr)
sigma <- sigma * a
else
sigma <- sigma / a
print(paste("New Sigma:",sigma))
}
histList <- list(x=x,y=y,sigma=sigma,memory=memory, x1 = x1, y1=y1)
return(histList)
}
featureModel <- readRDS("autoRF.rds")
predictCores <- function(features, maxBatchSize){
features <- features[featureModel$trainCols]
features[is.na(features)] <- 0
minVal <- -10e12
maxVal <- 10e12
features <- data.frame(t(sapply(features, function(y) min(max(y,minVal),maxVal))))
p <- predict(featureModel$rf, as.matrix(features))
p <- as.numeric(as.character(p))
return(min(p, maxBatchSize))
}
optim1plus1ES <- function(x,f,control=list(),...){
con<-list(
sr=0.2, #succes rate limit
sigma0=1, #initial step size
a=1.2, #step size multiplier
g=8,#length of memory list (for success rate)
budget=100) #budget of function evaluations
con[names(control)] <- control
control<-con
sigma <- control$sigma0
a <- control$a
budget <- control$budget
g <- control$g
sr <- control$sr
n <- length(x) #number of variables
y <- f(x)
xhist <- x
yhist <- y
memory <- NULL
for(i in 1:(budget-1)){
x1 <- x+sigma*rnorm(n,0,1)
y1 <- f(x1)
xhist <- append(xhist,x1)
yhist <- c(yhist,y1)
if(y1 < y){ #success
x <- x1
y <- y1
if(length(memory)<g)
memory <- c(memory,1)
else
memory <- c(memory[-1],1)
}else{ #fail
if(length(memory)<g)
memory <- c(memory,0)
else
memory <- c(memory[-1],0)
}
if(i>g){
successrate <- sum(memory)/g
if(successrate>sr)
sigma <- sigma * a
else
sigma <- sigma / a
}
}
return(list(x=x,y=y,sigma=sigma,xhist=xhist,yhist=yhist))
}
getPVPointDACE <- function(x, y, lower, upper){
optimizerControl = list(funEvals = as.integer(log(length(lower)) * 1000),
populationSize = 5 * length(lower))
model <- km(~1, design=x, response=y,
covtype="gauss", control=list(pop.size=50,trace=FALSE), parinit=c(0.5, 0.5),
nugget = 0.000001,nugget.estim = T, iso = F)
getPV <- function(xNew){
krig <- predict(object = model, newdata = xNew, type = "UK",
se.compute = FALSE, cov.compute = TRUE, checkNames = FALSE)
krig$mean
}
optimDE(,fun = getPV, lower, upper,optimizerControl)$xbest
}
createEIPoints <- function(xAll, yAll, lower, upper, pointsEI){
optimizerControl = list(funEvals = as.integer(log(length(lower)) * 1000 * pointsEI), ## 1000!!!!
populationSize = 5 * length(lower))
if(pointsEI > 1){
## EI should be used to create multiple points in this iteration
print(paste("Creating", pointsEI, "Points with q-EI"))
#browser()
model <- km(~1, design=xAll, response=yAll,
covtype="gauss", control=list(pop.size=50,trace=FALSE), parinit=c(0.5, 0.5),
nugget = 0.000001,nugget.estim = T, iso = F)
getQEI <- function(x){
res <- -qEI(matrix(x,nrow = pointsEI), model)
if(is.nan(res)) res <- 0
return(res)
}
result <- optimDE(,fun = getQEI, rep(lower,pointsEI),rep(upper,pointsEI),optimizerControl)$xbest
newX <- matrix(result,nrow = pointsEI)
}else{
print("singleCore SBO (PV) will be combined with ES")
print("Creating a point with SBO (PV)")
newX <- matrix(getPVPointDACE(xAll, yAll, lower, upper),ncol = length(lower))
}
return(newX)
}
createESPoints <- function(lastESAmount, currentAmntES, xEI, yEI, esList, fun){
print(paste("Creating", currentAmntES, "Points with ES"))
## TODO Method for restarting old bad ES is missing
## If necessary create new ES
if(currentAmntES > length(esList)){
esToStart <- currentAmntES - length(esList)
xEI <- xEI[order(yEI),]
yEI <- yEI[order(yEI)]
for(i in 1:esToStart){
xStart <- xEI[i,]
yStart <- yEI[i]
print(paste("Starting a new ES at",xStart," Value:", yStart, collapse=" "))
esList[[length(esList) + 1]] <- list(x=xStart,y=yStart)
}
}
## Sort ES run only the best ones
esYResults <- unlist(lapply(esList, function(l){return(l$y)}))
esList[order(esYResults)]
## Run all ES
for(i in 1:currentAmntES){
esList[[i]] <- do1plus1EsIter(esList[[i]], fun)
}
return(esList)
}
getFeatures <- function(X, y){
X <- as.matrix(X)
fObj <- createFeatureObject(X = X, y = y)
ctrl <- list(allow_cellmapping = FALSE, show_progress = F,
subset = c("basic","ela_meta",
"ic"))
features <- data.frame(calculateFeatures(fObj, control = ctrl))
return(features)
}
getESAmnt <- function(algoBudget, remainingBudget, batchSize){
progress <- (algoBudget-remainingBudget)/algoBudget
progressSwitches <- seq(from = 0, to = 1, by = 1/batchSize)
return(max(sum(progress > progressSwitches)-1,0))
}
################# 1+1es
solver <- function(fun,lower,upper,solverParameterList){
tfun <- function(x){
apply(x,1,fun)
}
print(paste("Lower bounds:",paste(lower, collapse = " ")))
print(paste("maxBatchSize:",maxBatchSize))
initialDesignSize <- length(lower) * 2 * maxBatchSize
initialDesign <- designLHD(x = NULL,
lower = lower,
upper = upper,
control = list(
size = initialDesignSize,
retries=1000))
xAll <- initialDesign
yAll <- tfun(xAll)
xMBO <- NULL
print(paste("Total budget:",budget))
remainingBudget <- budget - 2 * length(lower) * getEvalCost(maxBatchSize)
print(paste("Budget after initDesign:",remainingBudget))
algorithmBudget <- remainingBudget
if(maxBatchSize == 1){
print("BatchSize is 1, using singleCore SBO (PV)")
for(i in 1:remainingBudget){
print("Creating a point with SBO (PV)")
print(yAll)
newX <- matrix(getPVPointDACE(xAll, yAll, lower, upper),ncol = length(lower))
newY <- tfun(newX)
xAll <- rbind(xAll, newX)
yAll <- c(yAll,newY)
}
}else{
### Start Multi-Local EI ####################
print("MaxBatchSize is > 1, using multi-localEI")
xEI <- xAll
yEI <- yAll
lastESAmount <- 0
esList <- list()
batchSizeDF <- NULL
while(remainingBudget >= 1){
## Calculate features and predict best batch size based on them
features <- getFeatures(xAll, yAll)
batchSize <- predictCores(features, maxBatchSize)
print(paste("Prediction says batchSize=",batchSize))
batchSizeDF <- rbind(batchSizeDF, data.frame("time" = budget - remainingBudget,
"batchSize" = batchSize))
## If not enough budget is left, batchSize might need to be reduced:
batchSize <- min(batchSize,
max(curveDf$batchSize[curveDf$budgetTakenPerIter<= remainingBudget]))
## Reduce remaining budget by the respective amount
remainingBudget <- remainingBudget - getEvalCost(batchSize)
print(paste("Budget was reduced by",getEvalCost(batchSize),"to",remainingBudget))
## get the desired amount of ES cores
currentAmntES <- getESAmnt(algorithmBudget, remainingBudget, batchSize)
amntEI <- batchSize - currentAmntES
print(paste("the algorithm will use", currentAmntES, "ES evals and", amntEI, "EI evals"))
## run ESs
newESX <- NULL
newESy <- NULL
if(currentAmntES > 0){
esList <- createESPoints(lastESAmount, currentAmntES, xEI, yEI, esList, fun)
for(i in 1:currentAmntES){
newESX <- rbind(newESX, esList[[currentAmntES]]$x1)
newESy <- c(newESy, esList[[currentAmntES]]$y1)
}
}
## create EI Cores
newXEI <- createEIPoints(xAll, yAll, lower, upper, amntEI)
newYEI <- tfun(newXEI)
## add EI results to result list
xEI <- rbind(xEI, newXEI)
yEI <- c(yEI, newYEI)
print("new EI points:")
print(newXEI)
print(newYEI)
print("new ES points:")
print(newESX)
print(newESy)
## add all results to overall list
xAll <- rbind(xAll, newESX, newXEI)
yAll <- c(yAll, newESy, newYEI)
}
csvName <- paste0(experimentPath,"/",paste("14AutoMultiLocalEI",paste(args,collapse="_"),sep="_"),".csv")
write.csv(batchSizeDF, csvName)
}
}
wrapped <- getBBOBWrappedFun(functionID = funID,
algoName = paste("14AutoMultiLocalEI",paste(args,collapse="_"),sep="_"),
experimentPath = experimentPath,
nDim = nDim,
iid = seed)
start_time<-Sys.time()
solver(wrapped$fun, wrapped$lower, wrapped$upper)
end_time<-Sys.time()
print("Time taken: \n")
print(end_time-start_time)