I am following the instructions from this website (Function Maximization) that introduces (bayesian) optimization with R.
The website only shows how to perform this kind of optimization on functions containing only a single input. I am trying to extend this to functions containing multiple inputs.
I started by defining some arbitrary "multi-input" function to be used in this example:
#load necessary library
library(ParBayesianOptimization)
bayesian_function <- function(x1, x2, x3, x4) {
var_1 <- sin(x1 + x2)
var_2 <- cos(x1 - x2)
var_3 <- x1 + x4
var_4 <- x5 + x4 -7
goal = sum(var1 + var2 + var3 + var4)
return(goal)
}
FUN <- FUN <- function(x1, x2, x3, x4) list(Score = bayesian_function(x))
Next, I specified the "bounds" for the input variables (i.e. lower and upper ranges that the variables in the function can accept).
bounds <- list(var_1 =c(20,40), var_2 = c(30,45), var_3 = c(10,20), var_4 = c(10,50))
Finally, I ran the bayesian optimization algorithm:
optObj <- bayesOpt(
FUN = FUN
, bounds = bounds
, initPoints = 10
, acq = "ei"
, iters.n = 2
, gsPoints = 25
)
This step is where the errors are produced:
Running initial scoring function 10 times in 1 thread(s)... 1.47 seconds
var_1 var_2 var_3 var_4
1: 29.45889 31.21080 16.39719 10.82736
2: 24.27307 36.96948 13.02469 49.02666
3: 37.18198 35.88711 17.12626 42.45725
etc
errorMessage
1: unused arguments (var_1 = 29.4588872906752, var_2 = 31.2107987893978, var_3 = 16.3971868706867, var_4 = 10.8273566914722)
2: unused arguments (var_1 = 24.2730703949928, var_2 = 36.9694840939483, var_3 = 13.0246920569334, var_4 = 49.0266618905589)
3: unused arguments (var_1 = 37.1819836101495, var_2 = 35.8871123429853, var_3 = 17.126257217722, var_4 = 42.4572485554963)
etc
Error in bayesOpt(FUN = FUN, bounds = bounds, initPoints = 10, acq = "ei", :
Errors encountered in initialization are listed above.
Does anyone know why this is error is being produced? Or is it because this procedure is not intended for functions with multiple inputs? The errors listed above suggest that there were "unused arguments" - is this arising from the way I have defined the function?
Thanks
Note: The function typically requires the user to define a parameter called " initGrid
", but I didn't understand how to define it correctly. Instead, I consulted the documentation and defined a similar parameter called " initPoints
"
Reference: https://cran.r-project.org/web/packages/ParBayesianOptimization/ParBayesianOptimization.pdf