AnotherSamWilson / ParBayesianOptimization

Parallelizable Bayesian Optimization in R
107 stars 18 forks source link

Error in rbindlist(scoreSummary) : Column 2 of item 1 is length 6 inconsistent with column 1 which is length 100. Only length-1 columns are recycled. #39

Closed swaheera closed 3 years ago

swaheera commented 3 years ago

Hello!

Great R package! I am trying to learn how to use this package for function optimization. I am following the example from this page (https://cran.r-project.org/web/packages/ParBayesianOptimization/vignettes/functionMaximization.html) - I tried to make a new example for a function with "multiple inputs".

For example:

#load necessary library
library(ParBayesianOptimization)

#define function to be optimized
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)

}

}

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

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

#run optimization algorithm
optObj <- bayesOpt(
    FUN = FUNwrapper
    , bounds = bounds
    , initPoints = 10
    , acq = "ei"
    , iters.n = 2
    , gsPoints = 25
)

When I look at the output for this example, it is indicated that the optimization algorithm did not converge:

Running initial scoring function 10 times in 1 thread(s)...  2.71 seconds

Starting Epoch 1 
  1) Fitting Gaussian Process...
  2) Running local optimum search...        0.32 seconds
  3) Running FUN 1 times in 1 thread(s)...  0.28 seconds

Starting Epoch 2 
  1) Fitting Gaussian Process...
  2) Running local optimum search...        0.19 seconds
  3) Running FUN 1 times in 1 thread(s)...  0.28 seconds

Starting Epoch 3 
  1) Fitting Gaussian Process...
  2) Running local optimum search...
     - Convergence Not Found. Trying again with tighter parameters...        0.5 seconds
  3) Running FUN 1 times in 1 thread(s)...  0.28 seconds

Therefore, I tried to increase the number of iterations so that the algorithm might be able to find a better point:

#increase the number of iterations
optObj <- bayesOpt(
    FUN = bayesian_function
    , bounds = bounds
    , initPoints = 1000
    , acq = "ei"
    , iters.n = 10
    , gsPoints = 25
)

But this produces the following error:

Running initial scoring function 1000 times in 1 thread(s)...  359 seconds
Error in rbindlist(scoreSummary) : 
  Column 2 of item 1 is length 6 inconsistent with column 1 which is length 100. Only length-1 columns are recycled.

Do you have any idea why this error is being produced? Is it because I have ran the function too many times? In general, is there way to assess the quality of the solution provided by the bayesian optimization algorithm when convergence is not met?

Thanks for everything!

samFarrellDay commented 3 years ago

Can you paste the result from sessionInfo() for me please. Also, when you see this:

Starting Epoch 3 
  1) Fitting Gaussian Process...
  2) Running local optimum search...
     - Convergence Not Found. Trying again with tighter parameters...        0.5 seconds
  3) Running FUN 1 times in 1 thread(s)...  0.28 seconds

This means the optimization did actually converge, it just had to tighten the parameters a little bit on the second go-around.