if (requireNamespace("adagio")) {
  search_space = domain = ps(
    x1 = p_dbl(-10, 10),
    x2 = p_dbl(-5, 5)
  )
  codomain = ps(y = p_dbl(tags = "maximize"))
  objective_function = function(xs) {
    c(y = -(xs[[1]] - 2)^2 - (xs[[2]] + 3)^2 + 10)
  }
  objective = ObjectiveRFun$new(
    fun = objective_function,
    domain = domain,
    codomain = codomain)
  instance = OptimInstanceSingleCrit$new(
    objective = objective,
    search_space = search_space,
    terminator = trm("evals", n_evals = 10))
  optimizer = opt("cmaes")
  # modifies the instance by reference
  optimizer$optimize(instance)
  # returns best scoring evaluation
  instance$result
  # allows access of data.table of full path of all evaluations
  as.data.table(instance$archive$data)
}
Run the code above in your browser using DataLab