# "Errors encountered in initialization are listed above"

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` "

bayesian_function uses x5 with nonx5 defined.
Your idea for FUN seems to be that you would pass a single vector x and then unpack it to pass multiple x params for bayesian function, but you wrote it in reverse where FUN takes multiple params and only passes a single x to bayesian_function and regardless there needs to be some unpacking code written not just different argument lists in the function defs.

My advice generally would be to test the lower level functions that you will be using in a higher level function.
At the highest level you are concerned with bayesOpt. You want it to use FUN, which you.should manually tests that you can judge it works the way you expect, similarly FUN relies on another function, you should test that also

Thank you for your answer! I tried to make the following changes but the code still doesn't work:

``````#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 <- x3 + x4 -7
goal = sum(var_1 + var_2 + var_3 + var_4)

return(goal)

}

FUN <- function(x1, x2, x3, x4) list(Score = bayesian_function)

bounds <- list(x1 =c(20,40), x2 = c(30,45), x3 = c(10,20), x4 = c(10,50))

optObj <- bayesOpt(
FUN = FUN
, bounds = bounds
, initPoints = 10
, acq = "ei"
, iters.n = 2
, gsPoints = 25
)
``````

Did I understand your suggestions correctly?
Thanks for everything!

``````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 <- x3 + x4 -7
goal = sum(var_1 + var_2 + var_3 + var_4)

return(goal)

}
#test
bayesian_function(1,2,3,4)
#  5.681422

FUNwrapper <- function(x1,x2,x3,x4) list(
"Score"=bayesian_function(x1=x1,
x2=x2,
x3=x3,
x4=x4)
)
FUNwrapper(1,2,3,4)
#\$Score
# 5.681422

bounds <- list(x1 =c(20,40),
x2= c(30,45),
x3 = c(10,20),
x4 = c(10,50))

optObj <- bayesOpt(
FUN = FUNwrapper
, bounds = bounds
, initPoints = 10
, acq = "ei"
, iters.n = 2
, gsPoints = 25
)``````
1 Like

Thank you so much! This works perfectly!

Do you know why the "FUNwrapper" function has to be written this way?

``````FUNwrapper <- function(x1,x2,x3,x4) list(
"Score"=bayesian_function(x1=x1,
x2=x2,
x3=x3,
x4=x4)
``````

Thank you so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.