AnotherSamWilson / ParBayesianOptimization

Parallelizable Bayesian Optimization in R
107 stars 18 forks source link

Using "ParBayesianOptimization" Without Installing the Library #42

Closed swaheera closed 3 years ago

swaheera commented 3 years ago

Hello,

I am working on an older computer in which I can't install any new R packages, I only have an older version of R with some of the commonly used libraries in R. I am interested in using your library, but I can not install it on this older computer.

Question: Is it somehow possible to manually copy all the functions definitions from the github page and paste them into your R session, and then use this library?

For example:

I copy/pasted all the definitions from these links into R:

Then I opened R and loaded the libraries that I have access to:

#load libraries 
library(data.table)
library(stats)
library(foreach)
library(dbscan)
library(ggplot2)

Then, I tried to run the optimization code:

#perform bayesian optimization (run on first computer)

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_1 = sum(var_1 + var_2 + var_3 + var_4)

    if (var_1 > 0.6 ) {
        goal_1 =999999 }
    else {
        goal_1 = sum(var_1 + var_2 + var_3 + var_4)
    }

    return(c(goal_1))

}

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

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 )

But I got the following error:

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

Starting Epoch 1 
  1) Fitting Gaussian Process...

 Returning results so far. Error encountered while training GP: <unable to find an inherited method for function ‘covParametersBounds’ for signature ‘"covTensorProduct"’> 

I know that this is not the intended way of using a library (i.e. manually copy/paste functions instead of installing the library from CRAN), but do you have any idea why this error might be appearing? In general, is it possible to use your library by only copy/pasting function definitions and not directly installing it?

Thanks!

samFarrellDay commented 3 years ago

You would need to be able to load all of the dependencies. It looks like in this case you are missing the DiceKriging package.

swaheera commented 3 years ago

Hi Sam,

I loaded DiceKrigging as well - It still doesn't work. Do you think that it should technically be able to work?

Thanks

samFarrellDay commented 3 years ago

If you can't actually install and load the packages it probably won't work - when packages are loaded, dependent functions are also automatically loaded. These dependency trees can go pretty deep. Defining the function locally won't load these dependent functions / packages. Loading the packages also keeps track of which packages duplicate function names belong to. If loading the functions locally does not work, I would not be surprised, but R is pretty good at forcing janky environmental usage like this to work, so IDK.