Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal for hook for loop function that allows plotting #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion R/bayesopt_ego.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#' For example, if `random_interleave_iter = 2`, random interleaving is performed in the second,
#' fourth, sixth, ... iteration.
#' Default is `0`, i.e., no random interleaving is performed at all.
#' @param hook_fun ([function])\cr
#' [Function] to be called in each iteration of the loop, before evaluating the next proposed point.
#' See examples.
#'
#' @note
#' * The `acq_function$surrogate`, even if already populated, will always be overwritten by the `surrogate`.
Expand Down Expand Up @@ -82,6 +85,42 @@
#'
#' optimizer$optimize(instance)
#'
#' # same as above, but plot files with information at each iteration of the loop
#' library(ggplot2)
#' library(gridExtra)
#'
#' myPlot = function(xdt, instance, surrogate, acq_function, acq_optimizer) {
#' data.plot = data.table(x = seq(instance$objective$domain$lower, instance$objective$domain$upper, length.out = 100))
#' data.plot$y = instance$objective$eval_dt(data.plot)$y
#' data.plot$acq_ei = acq_optimizer$acq_function$eval_dt(data.table(x = data.plot$x))$acq_ei
#' data.plot = data.table(data.plot, surrogate$predict(data.plot))
#'
#' p1 = ggplot(data.plot, aes(x = x, y = y)) +
#' geom_ribbon(aes(ymin = mean - se, ymax = mean + se), fill = "lightgray") +
#' geom_line() +
#' geom_line(aes(y = mean), linetype = "dashed") +
#' geom_point(data = xdt, aes(x = x, y = surrogate$predict(data.table(x = x))$mean), color = "red") +
#' geom_point(data = instance$archive$data, aes(x = x, y = y), color = "darkgreen") +
#' theme(axis.title.x = element_blank(),
#' axis.text.x = element_blank(),
#' axis.ticks.x = element_blank())
#'
#' p2 = ggplot(data.plot, aes(x = x, y = acq_ei)) +
#' geom_line()
#'
#' p = grid.arrange(p1, p2, ncol = 1)
#' ggsave(p, file = paste("mbo-", instance$archive$n_evals, ".pdf", sep = ""))
#' }
#'
#' optimizer = opt("mbo",
#' loop_function = bayesopt_ego,
#' surrogate = surrogate,
#' acq_function = acqfun,
#' acq_optimizer = acqopt,
#' args = list("hook_fun" = myPlot))
#'
#' optimizer$optimize(instance)
#'
#' # expected improvement per second example
#' fun = function(xs) {
#' list(y = xs$x ^ 2, time = abs(xs$x))
Expand Down Expand Up @@ -112,7 +151,8 @@ bayesopt_ego = function(
acq_function,
acq_optimizer,
init_design_size = NULL,
random_interleave_iter = 0L
random_interleave_iter = 0L,
hook_fun = function(...) {}
) {

# assertions
Expand Down Expand Up @@ -154,6 +194,8 @@ bayesopt_ego = function(
generate_design_random(search_space, n = 1L)$data
})

hook_fun(xdt, instance, surrogate, acq_function, acq_optimizer)

instance$eval_batch(xdt)
if (instance$is_terminated) break
}
Expand Down
8 changes: 7 additions & 1 deletion R/bayesopt_emo.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#' For example, if `random_interleave_iter = 2`, random interleaving is performed in the second,
#' fourth, sixth, ... iteration.
#' Default is `0`, i.e., no random interleaving is performed at all.
#' @param hook_fun ([function])\cr
#' [Function] to be called in each iteration of the loop, before evaluating the next proposed point.
#' See examples in [mlr_loop_functions_ego].
#'
#' @note
#' * The `acq_function$surrogate`, even if already populated, will always be overwritten by the `surrogate`.
Expand Down Expand Up @@ -85,7 +88,8 @@ bayesopt_emo = function(
acq_function,
acq_optimizer,
init_design_size = NULL,
random_interleave_iter = 0L
random_interleave_iter = 0L,
hook_fun = function(...) {}
) {

# assertions
Expand Down Expand Up @@ -127,6 +131,8 @@ bayesopt_emo = function(
generate_design_random(search_space, n = 1L)$data
})

hook_fun(xdt, instance, surrogate, acq_function, acq_optimizer)

instance$eval_batch(xdt)
if (instance$is_terminated) break
}
Expand Down
8 changes: 7 additions & 1 deletion R/bayesopt_mpcl.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#' For example, if `random_interleave_iter = 2`, random interleaving is performed in the second,
#' fourth, sixth, ... iteration.
#' Default is `0`, i.e., no random interleaving is performed at all.
#' @param hook_fun ([function])\cr
#' [Function] to be called in each iteration of the loop, before evaluating the next proposed point.
#' See examples in [mlr_loop_functions_ego].
#'
#' @note
#' * The `acq_function$surrogate`, even if already populated, will always be overwritten by the `surrogate`.
Expand Down Expand Up @@ -102,7 +105,8 @@ bayesopt_mpcl = function(
init_design_size = NULL,
q = 2L,
liar = mean,
random_interleave_iter = 0L
random_interleave_iter = 0L,
hook_fun = function(...) {}
) {

# assertions
Expand Down Expand Up @@ -175,6 +179,8 @@ bayesopt_mpcl = function(

acq_function$surrogate$archive = instance$archive

hook_fun(xdt, instance, surrogate, acq_function, acq_optimizer)

instance$eval_batch(xdt)

if (instance$is_terminated) break
Expand Down
8 changes: 7 additions & 1 deletion R/bayesopt_parego.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#' For example, if `random_interleave_iter = 2`, random interleaving is performed in the second,
#' fourth, sixth, ... iteration.
#' Default is `0`, i.e., no random interleaving is performed at all.
#' @param hook_fun ([function])\cr
#' [Function] to be called in each iteration of the loop, before evaluating the next proposed point.
#' See examples in [mlr_loop_functions_ego].
#'
#' @note
#' * The `acq_function$surrogate`, even if already populated, will always be overwritten by the `surrogate`.
Expand Down Expand Up @@ -105,7 +108,8 @@ bayesopt_parego = function(
q = 1L,
s = 100L,
rho = 0.05,
random_interleave_iter = 0L
random_interleave_iter = 0L,
hook_fun = function(...) {}
) {

# assertions
Expand Down Expand Up @@ -169,6 +173,8 @@ bayesopt_parego = function(
})
}, .fill = TRUE)

hook_fun(xdt, instance, surrogate, acq_function, acq_optimizer)

instance$eval_batch(xdt)
if (instance$is_terminated) break
}
Expand Down
8 changes: 7 additions & 1 deletion R/bayesopt_smsego.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#' For example, if `random_interleave_iter = 2`, random interleaving is performed in the second,
#' fourth, sixth, ... iteration.
#' Default is `0`, i.e., no random interleaving is performed at all.
#' @param hook_fun ([function])\cr
#' [Function] to be called in each iteration of the loop, before evaluating the next proposed point.
#' See examples in [mlr_loop_functions_ego].
#'
#' @note
#' * The `acq_function$surrogate`, even if already populated, will always be overwritten by the `surrogate`.
Expand Down Expand Up @@ -90,7 +93,8 @@ bayesopt_smsego = function(
acq_function,
acq_optimizer,
init_design_size = NULL,
random_interleave_iter = 0L
random_interleave_iter = 0L,
hook_fun = function(...) {}
) {

# assertions
Expand Down Expand Up @@ -134,6 +138,8 @@ bayesopt_smsego = function(
generate_design_random(search_space, n = 1L)$data
})

hook_fun(xdt, instance, surrogate, acq_function, acq_optimizer)

instance$eval_batch(xdt)
if (instance$is_terminated) break
}
Expand Down
Loading