Skip to content

Commit 99a461e

Browse files
hebo fixes and comments
1 parent 4a8fece commit 99a461e

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

R/helper_hebo.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
paramset_to_hebo_space = function(search_space) {
2-
pdt = search_space$params
3-
defs = lapply(seq_len(nrow(pdt)), function(i) {
4-
id = pdt$id[[i]]
5-
cls = pdt$cls[[i]]
2+
hebo = reticulate::import("hebo")
3+
param = search_space$params
4+
defs = lapply(seq_len(nrow(param)), function(i) {
5+
id = param$id[[i]]
6+
cls = param$cls[[i]]
67
if (cls == "ParamDbl") {
7-
list(name = id, type = "num", lb = as.numeric(pdt$lower[[i]]), ub = as.numeric(pdt$upper[[i]]))
8+
list(name = id, type = "num", lb = as.numeric(param$lower[[i]]), ub = as.numeric(param$upper[[i]]))
89
} else if (cls == "ParamInt") {
9-
list(name = id, type = "int", lb = as.integer(pdt$lower[[i]]), ub = as.integer(pdt$upper[[i]]))
10+
list(name = id, type = "int", lb = as.integer(param$lower[[i]]), ub = as.integer(param$upper[[i]]))
1011
} else if (cls == "ParamLgl") {
1112
list(name = id, type = "bool")
1213
} else if (cls == "ParamFct") {
13-
list(name = id, type = "cat", categories = as.list(pdt$levels[[i]]))
14+
list(name = id, type = "cat", categories = as.list(param$levels[[i]]))
1415
} else {
1516
stop(sprintf("Unsupported parameter class: %s", cls))
1617
}
1718
})
1819
hebo$design_space$design_space$DesignSpace()$parse(defs)
1920
}
21+
22+
# check whether dependencies can (or how) be implemented in HEBO Space
23+
# paradox => ConfigSpace => HEBO-space (wenn der zweite Pfeil einfach so geht als One-Liner); erstmal nur mit numerischen Räumen ohne dependencies
24+
# kann HEBO hierarchische gemischete Räume???

attic/OptimizerBatchHEBO.R

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#' @title Heteroscedastic evolutionary bayesian optimization
22
#'
3-
#'
4-
#'
53
#' @export
64
#' @examples
75
#' \dontrun{
@@ -50,7 +48,9 @@
5048
# IMPORTANT; configspace sind Python Objekte
5149
# search space -- R Objekt
5250
# cs // config_space -- Python Objekt
51+
5352
optimize = function(inst) {
53+
browser()
5454
# installing of packages
5555
assert_python_packages(c("hebo"))
5656
hebo = reticulate::import("hebo")
@@ -61,43 +61,84 @@ optimize = function(inst) {
6161

6262
space = paramset_to_hebo_space(search_space)
6363

64-
# terminator criterion
65-
terminator = inst$terminator
66-
6764
optimizer = hebo$optimizers$hebo$HEBO(space = space)
6865

6966
repeat {
67+
if (inst$is_terminated) {
68+
break
69+
}
70+
7071
# tryCatch muss noch etwas ausgestaltet werden, siehe folgender Kommentar
7172
# hebo$optimizers$hebo$HEBO()$quasi_sample --- default HEBO sampler evtl notwendig, wenn suggest fehlschlägt; useful wenn SM zusammenbricht; wrappen in Trycatch von suggest funktion falls das passiert
73+
# ISSUE 1 - HEBO suggest doesnt expose metadata as SMAC3
74+
# trial_info exposes paraeters as a pandas DataFrame
7275
trial_info = tryCatch(
7376
optimizer$suggest(),
77+
# rausfinden wie HEBO suggest verwendet um mehrere configs oder einzelne configs zu kriegen; gibt mehrere Möglichkeiten das zu machen bei MBO interessant wie HEBO das macht
7478
error = function(e) NULL
7579
)
7680
if (is.null(trial_info)) {
7781
break
7882
}
7983

80-
proposal_dt = as.data.table(reticulate::py_to_r(trial_info))
84+
# convert pandas DataFrame to R data.frame-like object
85+
trial_info_dt = setDT(reticulate::py_to_r(trial_info))
8186

82-
all_params = search_space$ids()
83-
missing_params = setdiff(all_params, names(proposal_dt))
84-
if (length(missing_params)) {
85-
proposal_dt[, (missing_params) := NA]
86-
}
87-
xdt = proposal_dt[, ..all_params]
87+
# load paramset names
88+
# all_params = search_space$ids()
8889

89-
search_space$assert_dt(xdt)
90+
# identify missing parameters
91+
# missing_params = setdiff(all_params, names(trial_info_dt))
9092

91-
ydt = inst$eval_batch(xdt)
93+
# when there are missing parameters we set them to NA
94+
# if (length(missing_params)) {
95+
# trial_info_dt[, (missing_params) := NA]
96+
# }
9297

93-
y = as.matrix(ydt[, inst$archive$cols_y, with = FALSE])
98+
# keep only columns in all params and saved as xdt
99+
# xdt = trial_info_dt[, ..all_params]
100+
101+
# search_space$assert_dt(xdt)
102+
103+
# evaluation of that config
104+
res = inst$eval_batch(xdt)
105+
106+
# evaluated target columns are turned into a numeric matrix
107+
y = as.matrix(res[, inst$archive$cols_y, with = FALSE])
108+
109+
# signs are flipped
110+
# ist es möglich bei HEBO auch multi-objective zu machen, also dass da mehrere y zurückkomen; das auch wichtig für denOptimizer ob er nicht nur SingleCrit oder auch MultiCrit kann
94111
y = y * matrix(inst$objective_multiplicator, nrow = nrow(y), ncol = ncol(y), byrow = TRUE)
95112

96-
opt$observe(trial_info, y)
113+
optimizer$observe(trial_info, y)
97114
}
98115
}
99116

117+
# 1 - rausfinden ob HEBO mixed/mixed hierarchical kann; falls nein alles andere außer numeric rauskicken
118+
100119
# EXAMPLE
120+
objective_fun = function(xs) {
121+
booster_term = if (xs$booster == "dart") {
122+
0.8
123+
} else if (xs$booster == "gbtree") {
124+
0.4
125+
} else {
126+
0.0
127+
}
128+
bias_term = if (isTRUE(xs$use_bias)) 0.25 else -0.15
129+
130+
score = 12 -
131+
(log10(xs$lr) + 1.1)^2 -
132+
((xs$depth - 7) / 3)^2 -
133+
0.03 * (xs$reg_lambda - 4)^2 -
134+
8 * (xs$subsample - 0.82)^2 -
135+
((xs$max_bin - 256) / 200)^2 +
136+
booster_term +
137+
bias_term
138+
139+
list(score = score)
140+
}
141+
101142
search_space = ps(
102143
lr = p_dbl(lower = 1e-4, upper = 3e-1),
103144
depth = p_int(lower = 2L, upper = 14L),

0 commit comments

Comments
 (0)