I am following the instructions from this tutorial (<https://cran.r-project.org/web/packages/ParBayesianOptimization/vignettes/functionMaximization.html>) on how to optimize functions using the R programming language.

I wanted to modify the example in this tutorial to include a slightly more complicated example.

I defined the following function :

```
#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)

```

I specified the ranges that the variables of the function can take:

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

```

Then I ran the optimization algorithm :

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

```

But this results in the following error:

```
Running initial scoring function 10 times in 1 thread(s)...  2.06 seconds
          x1       x2       x3       x4                             errorMessage
 1: 29.71786 30.04940 15.97740 44.48221 Score returned from FUN was not numeric.
 2: 32.82714 32.83628 10.38391 40.60989 Score returned from FUN was not numeric.

etc

Error in bayesOpt(FUN = FUN, bounds = bounds, initPoints = 10, acq = "ei",  : 
  Errors encountered in initialization are listed above.

```

Seeing as the error indicated that the "score returned from the function was not numeric", I decided to manually convert this into a "numeric type":

```
   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(as.numeric(Score = bayesian_function))

```

Unfortunately, this is giving a new error:

```
Running initial scoring function 10 times in 1 thread(s)...  2.09 seconds
          x1       x2       x3       x4                                            errorMessage
 1: 37.78915 37.13628 16.12003 29.24897 cannot coerce type 'closure' to vector of type 'double'
 2: 32.58058 32.99083 11.03643 13.00817 cannot coerce type 'closure' to vector of type 'double'

etc

Error in bayesOpt(FUN = FUN, bounds = bounds, initPoints = 10, acq = "ei",  : 
  Errors encountered in initialization are listed above.

```

Can someone please show me how to fix this?
Thanks