% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/optim.R
\name{qHSRI_loop}
\alias{qHSRI_loop}
\title{BO loop with massive batches}
\usage{
qHSRI_loop(X0, Y0, model, q, fun, budget, lower, upper, control = NULL)
}
\arguments{
\item{X0}{initial design of experiments matrix}

\item{Y0}{initial vector of responses at \code{X0}}

\item{model}{\code{homGP} or \code{hetGP} model}

\item{q}{batch size}

\item{fun}{test function to minimize}

\item{budget}{optimization budget}

\item{lower, upper}{domain bounds}

\item{control}{list with parameters 
\itemize{
\item nunif: number of uniformly sampled candidates points for acquisition function optimization
\item pop,gen: population size and number of generations for \code{\link[mco]{nsga2}}
\item maxit: passed to \code{\link[stats]{optim}}
\item trackbest: if \code{TRUE}, the best estimated solution will be saved at each iteration
\item maxtime: alternative stopping criterion
\item trace: boolean for printing messages
\item minPI: minimum probability of improvement for filtering solutions on the exploration-exploitation Pareto front
\item extendper: parameter to set the reference point for hypervolume computation
\item tol_dist: minimal distance to an existing design
\item tol_bdist: minimal distance to a design in the batch
\item max_rep: maximal degree of replication for a given design
\item max_brep maximal degree of replication per batch point
}}
}
\value{
A list with components:
\itemize{
\item \code{par}: all points evaluated,
\item \code{value}: the matrix of objective values at the points given in \code{par},
\item \code{model}: the last kriging models fitted.
\item \code{membest}: a matrix of best estimated designs at each iteration.
\item \code{estbest}: corresponding predicted mean values.
}
}
\description{
Bayesian optimization loop with portfolio based batch EI
}
\examples{
d <- 2
n <- 10*d
N <- 5*n
budget <- 150 # Increase for better results

## Noise field via standard deviation
noiseFun <- function(x){
  if(is.null(nrow(x)))
    x <- matrix(x, nrow = 1)
  return(1/5*(3*(2 + 2*sin(x[,1]*pi)*cos(x[,2]*3*pi) + 5*rowSums(x^2))))
}

## Branin redefined in [0,1]^2
branin <- function(x){
 if(is.null(nrow(x))) x <- matrix(x, nrow = 1)
  x1 <- x[,1] * 15 - 5
  x2 <- x[,2] * 15
  return((x2 - 5/(4 * pi^2) * (x1^2) + 5/pi * x1 - 6)^2 + 10 * (1 - 1/(8 * pi)) * cos(x1) + 10)
 }

## data generating function combining mean and noise fields
ftest <- function(x){
  if(is.null(nrow(x))) x <- matrix(x, nrow = 1)
  return(branin(x) + rnorm(nrow(x), mean = 0, sd = noiseFun(x)))
}


ngrid <- 51
Xgrid <- as.matrix(expand.grid(seq(0,1,length.out = ngrid), seq(0,1,length.out = ngrid)))
Ygrid <- branin(Xgrid)
Ygrid <- cbind(Ygrid, noiseFun(Xgrid)^2)
x1 <- c(0.9616520, 0.15); x2 <- c(0.1238946, 0.8166644); x3 <- c(0.5427730, 0.15)
fstar <- 0.3978874

X0 <- matrix(runif(n*d),n, d)
X0 <- X0[sample(1:n, size = N, replace = TRUE),]
Y0 <- ftest(X0)

mod <- mleHetGP(X0, Y0, covtype = "Matern5_2", known = list(beta0 = 0))
opt <- qHSRI_loop(X0, Y0, mod, q = 10, fun = ftest, budget = budget,
  lower = rep(0, d), upper = rep(1, d))
est <- predict(opt$model, opt$model$X0)$mean
xbest <- opt$model$X0[which.min(est),,drop=FALSE]
preds <- predict(opt$model, Xgrid)

par(mfrow = c(2, 2))
contour(matrix(preds$mean, ngrid), levels = c(1, seq(0,300,10)), 
 main="Mean prediction")
points(opt$model$X0, col = 4, pch = 20, cex = opt$model$mult/10)
contour(matrix(sqrt(preds$nugs), ngrid), nlevels = 21, 
 main="Variance prediction")
points(opt$model$X0, col = 4, pch = 20, cex = opt$model$mult/10)
contour(matrix(Ygrid[,1], ngrid), levels = c(1, seq(0,300,10)),
 main = "True function")
points(opt$model$X0, col = 4, pch = 20, cex = opt$model$mult/10)
points(rbind(t(x1), t(x2), t(x3)), pch=20, col="red")
points(xbest, col = "pink", pch = 15)
contour(matrix(sqrt(Ygrid[,2]^2), ngrid), nlevels = 21,
 main = "True variance")
points(rbind(t(x1), t(x2), t(x3)), pch=20, col="red")
points(opt$model$X0, col = 4, pch = 20, cex = opt$model$mult/10)
points(xbest, col = "pink", pch = 15)
par(mfrow = c(1, 1))
}
\references{
Binois, M., Collier, N., Ozik, J. (2025). A portfolio approach to massively parallel Bayesian optimization. Journal of Artificial Intelligence Research, 82, pp. 137-167.
}
