-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathaddIterations.R
More file actions
311 lines (277 loc) · 10.4 KB
/
addIterations.R
File metadata and controls
311 lines (277 loc) · 10.4 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
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
#' Run Additional Optimization Iterations
#'
#' Use this function to continue optimization of a bayesOpt object.
#'
#' By default, this function uses the original parameters used to create
#' \code{optObj}, however the parameters (including the bounds) can be customized.
#' If new bounds are used which cause some of the prior runs to fall outside of
#' the bounds, these samples are removed from the optimization procedure, but
#' will remain in \code{scoreSummary}. \code{FUN} should return the same elements
#' and accept the same inputs as the original, or this function may fail.
#'
#' @param optObj an object of class \code{bayesOpt}.
#' @param iters.n The total number of additional times to sample the scoring function.
#' @param iters.k integer that specifies the number of times to sample FUN
#' at each Epoch (optimization step). If running in parallel, good practice
#' is to set \code{iters.k} to some multiple of the number of cores you have designated
#' for this process. Must belower than, and preferrably some multiple of \code{iters.n}.
#' @param otherHalting Same as \code{bayesOpt()}
#' @param bounds Same as \code{bayesOpt()}
#' @param acq Same as \code{bayesOpt()}
#' @param kappa Same as \code{bayesOpt()}
#' @param eps Same as \code{bayesOpt()}
#' @param gsPoints Same as \code{bayesOpt()}
#' @param convThresh Same as \code{bayesOpt()}
#' @param acqThresh Same as \code{bayesOpt()}
#' @param errorHandling Same as \code{bayesOpt()}
#' @param saveFile Same as \code{bayesOpt()}
#' @param parallel Same as \code{bayesOpt()}
#' @param plotProgress Same as \code{bayesOpt()}
#' @param verbose Same as \code{bayesOpt()}
#' @param ... Same as \code{bayesOpt()}
#' @return An object of class \code{bayesOpt} having run additional iterations.
#' @examples
#' scoringFunction <- function(x) {
#' a <- exp(-(2-x)^2)*1.5
#' b <- exp(-(4-x)^2)*2
#' c <- exp(-(6-x)^2)*1
#' return(list(Score = a+b+c))
#' }
#'
#' bounds <- list(x = c(0,8))
#'
#' Results <- bayesOpt(
#' FUN = scoringFunction
#' , bounds = bounds
#' , initPoints = 3
#' , iters.n = 1
#' , gsPoints = 10
#' )
#' Results <- addIterations(Results,iters.n=1)
#' @export
addIterations <- function(
optObj
, iters.n = 1
, iters.k = 1
, otherHalting = list(timeLimit = Inf,minUtility = 0)
, bounds = optObj$bounds
, acq = optObj$optPars$acq
, kappa = optObj$optPars$kappa
, eps = optObj$optPars$eps
, gsPoints = optObj$optPars$gsPoints
, convThresh = optObj$optPars$convThresh
, acqThresh = optObj$optPars$acqThresh
, errorHandling = "stop"
, saveFile = optObj$saveFile
, parallel = FALSE
, plotProgress = FALSE
, verbose = 1
, ...
) {
startT <- Sys.time()
if (!inherits(x = optObj, what = "bayesOpt")) stop("optObj must be of class bayesOpt")
# Check the parameters
checkParameters(
bounds
, iters.n
, iters.k
, otherHalting
, acq
, acqThresh
, errorHandling
, plotProgress
, parallel
, verbose
)
optObj$stopStatus <- "OK"
optObj <- changeSaveFile(optObj,saveFile)
otherHalting <- formatOtherHalting(otherHalting)
# Set up for iterations
FUN <- optObj$FUN
boundsDT <- boundsToDT(bounds)
scoreSummary <- optObj$scoreSummary
Epoch <- max(scoreSummary$Epoch)
`%op%` <- ParMethod(parallel)
if(parallel) Workers <- getDoParWorkers() else Workers <- 1
iters.s <- nrow(scoreSummary)
iters.t <- iters.n + iters.s
# Store information we know about the different acquisition functions:
# Display name
# Base - upper conf bound will always be over 1, unless there was convergence issue.
# For the sake of simplicity, ucb is subtracted by 1 to keep the utility on the same scale
# It is more easily described as the 'potential' left in the search this way.
acqN <- getAcqInfo(acq)
# Check if bounds supplied can be used with prior parameter-score pairs.
inBounds <- checkBounds(optObj$scoreSummary,bounds)
scoreSummary$inBounds <- as.logical(apply(inBounds,1,prod))
if (any(!scoreSummary$inBounds)) {
message(
"Bounds have been tightened. There are "
, sum(!scoreSummary$inBounds)
, " parameter pairs in scoreSummary which cannot"
, " be used with the defined bounds. These will be"
, " ignored this round. Continue? [y/n]"
)
line <- readline()
if (tolower(line) == "y") invisible() else stop("Process Stopped by User.")
}
if (nrow(scoreSummary) <= 2) stop("Not enough samples in scoreSummary to perform optimizations.")
# Output from FUN is sunk into a temporary file.
sinkFile <- file()
on.exit(
{
while (sink.number() > 0) sink()
close(sinkFile)
}
)
# Start the iterative GP udpates.
while(nrow(scoreSummary) < iters.t){
Epoch <- Epoch + 1
if (verbose > 0) cat("\nStarting Epoch",Epoch,"\n")
# How many runs to make this session
runNew <- pmin(iters.t-nrow(scoreSummary), iters.k)
# Fit GP
if (verbose > 0) cat(" 1) Fitting Gaussian Process...\n")
optObj <- updateGP(optObj,bounds = bounds, verbose = 0,...)
# See if updateGP altered the stopStatus.
# If so, the km() failed and we need to return optObj
if (optObj$stopStatus != "OK") {
printStopStatus(optObj,verbose)
optObj$elapsedTime <- totalTime(optObj,startT)
return(optObj)
}
# Find local optimums of the acquisition function
if (verbose > 0) cat(" 2) Running local optimum search...")
tm <- system.time(
LocalOptims <- getLocalOptimums(
optObj
, bounds = bounds
, parallel=parallel
, verbose=verbose
)
)[[3]]
if (verbose > 0) cat(" ",tm,"seconds\n")
tm_local_search = tm
# Should we continue?
if (otherHalting$minUtility > max(LocalOptims$gpUtility)) {
optObj$stopStatus <- makeStopEarlyMessage(paste0("Returning Results. Could not meet minimum required (",otherHalting$minUtility,") utility."))
printStopStatus(optObj,verbose)
optObj$elapsedTime <- totalTime(optObj,startT)
return(optObj)
} else if (otherHalting$timeLimit < totalTime(optObj,startT)) {
optObj$stopStatus <- makeStopEarlyMessage(paste0("Time Limit - ",otherHalting$timeLimit," seconds."))
printStopStatus(optObj,verbose)
optObj$elapsedTime <- totalTime(optObj,startT)
return(optObj)
}
# Filter out local optimums to our specifications
# Obtain new candidates if we don't have enough
nextPars <- getNextParameters(
LocalOptims
, boundsDT
, scoreSummary
, runNew
, acq
, kappa
, eps
, acqThresh
, acqN
, scoreGP = optObj$GauProList$scoreGP
, timeGP = optObj$GauProList$timeGP
)
if(any(class(nextPars) == "stopEarlyMsg")) {
optObj$stopStatus <- nextPars
printStopStatus(optObj,verbose)
optObj$elapsedTime <- totalTime(optObj,startT)
return(optObj)
}
# Try to run the scoring function. If not all (but at least 1) new runs fail,
# then foreach cannot call rbind correctly, and an error is thrown.
if (verbose > 0) cat(" 3) Running FUN",nrow(nextPars),"times in",Workers,"thread(s)...")
sink(file = sinkFile)
tm <- system.time(
NewResults <- foreach(
iter = 1:nrow(nextPars)
, .options.multicore = list(preschedule=FALSE)
, .combine = rbindFE
, .multicombine = TRUE
, .inorder = FALSE
, .errorhandling = 'stop'
, .verbose = FALSE
) %op% {
Params <- nextPars[get("iter"),boundsDT$N,with=FALSE]
Elapsed <- system.time(
Result <- tryCatch(
{
do.call(what = FUN, args = as.list(Params))
}
, error = function(e) e
)
)
# Handle the Result.
if (any(class(Result) %in% c("simpleError","error","condition"))) {
return(data.table(nextPars[get("iter"),], Elapsed = Elapsed[[3]], ElapsedLocalSearch = tm_local_search, Score = NA, errorMessage = conditionMessage(Result)))
} else {
if (any(lengths(Result) != 1)) {
stop(
paste0(
"FUN returned list with elements of length > 1. Cannot collapse into a data.table, so this is a fatal error. Parameters used were <"
, paste(names(Params),"=",Params,collapse = ", ")
, ">"
)
)
}
if (!is.numeric(Result$Score)) {
return(data.table(nextPars[get("iter"),], Elapsed = Elapsed[[3]], ElapsedLocalSearch = tm_local_search, as.data.table(Result),errorMessage = "Score returned from FUN was not numeric."))
} else {
return(data.table(nextPars[get("iter"),], Elapsed = Elapsed[[3]], ElapsedLocalSearch = tm_local_search, as.data.table(Result),errorMessage = NA))
}
}
}
)[[3]]
while (sink.number() > 0) sink()
# Leaves room for flexability in the future.
optObj$stopStatus <- getEarlyStoppingErrorStatus(NewResults,scoreSummary,errorHandling,verbose)
if (verbose > 0) cat(" ",tm,"seconds\n")
# Print updates on parameter-score search
if (verbose > 1) {
cat("\nResults from most recent parameter scoring:\n")
print(NewResults, row.names = FALSE)
if (max(NewResults$Score) > max(scoreSummary$Score)) {
cat("\nNew best parameter set found:\n")
print(NewResults[which.max(get("Score")),c(boundsDT$N,"Score"),with=FALSE], row.names = FALSE)
} else {
cat("\nMaximum score was not raised this round. Best score is still:\n")
print(scoreSummary[which.max(get("Score")),c(boundsDT$N,"Score"),with=FALSE], row.names = FALSE)
}
}
# Keep track of performance.
# fill is true because users can pass their own columns.
scoreSummary <- rbindlist(
list(
scoreSummary
, data.table(
"Epoch" = rep(Epoch,nrow(NewResults))
, "Iteration" = 1:nrow(NewResults) + nrow(scoreSummary)
, "inBounds" = rep(TRUE,nrow(NewResults))
, NewResults
)
)
, use.names=TRUE
, fill=TRUE
)
optObj$scoreSummary <- scoreSummary
optObj$GauProList$gpUpToDate <- FALSE
# Save Intermediary Results
saveSoFar(optObj,verbose)
# Plotting
if(plotProgress) plot(optObj)
# Check for change in stop status before we continue.
if (optObj$stopStatus != "OK") {
printStopStatus(optObj,verbose)
optObj$elapsedTime <- totalTime(optObj,startT)
return(optObj)
}
}
return(optObj)
}