@@ -13,19 +13,24 @@ CallbackAsyncTuning = R6Class("CallbackAsyncTuning",
13
13
public = list (
14
14
15
15
# ' @field on_eval_after_xs (`function()`)\cr
16
- # ' Stage called after xs is passed.
17
- # ' Called in `ObjectiveTuning $eval()`.
16
+ # ' Stage called after xs is passed.
17
+ # ' Called in `ObjectiveTuningAsync $eval()`.
18
18
on_eval_after_xs = NULL ,
19
19
20
20
# ' @field on_eval_after_resample (`function()`)\cr
21
- # ' Stage called after hyperparameter configurations are evaluated.
22
- # ' Called in `ObjectiveTuning $eval()`.
21
+ # ' Stage called after hyperparameter configurations are evaluated.
22
+ # ' Called in `ObjectiveTuningAsync $eval()`.
23
23
on_eval_after_resample = NULL ,
24
24
25
25
# ' @field on_eval_before_archive (`function()`)\cr
26
- # ' Stage called before performance values are written to the archive.
27
- # ' Called in `ObjectiveTuning$eval()`.
28
- on_eval_before_archive = NULL
26
+ # ' Stage called before performance values are written to the archive.
27
+ # ' Called in `ObjectiveTuningAsync$eval()`.
28
+ on_eval_before_archive = NULL ,
29
+
30
+ # ' @field on_tuning_result_begin (`function()`)\cr
31
+ # ' Stage called before the results are written.
32
+ # ' Called in `TuningInstance*$assign_result()`.
33
+ on_tuning_result_begin = NULL
29
34
)
30
35
)
31
36
@@ -54,7 +59,9 @@ CallbackAsyncTuning = R6Class("CallbackAsyncTuning",
54
59
# ' End Optimization on Worker
55
60
# ' - on_worker_end
56
61
# ' End Worker
57
- # ' - on_result
62
+ # ' - on_tuning_result_begin
63
+ # ' - on_result_begin
64
+ # ' - on_result_end
58
65
# ' - on_optimization_end
59
66
# ' End Tuning
60
67
# ' ```
@@ -68,39 +75,70 @@ CallbackAsyncTuning = R6Class("CallbackAsyncTuning",
68
75
# ' Tuning callbacks access [ContextAsyncTuning].
69
76
# '
70
77
# ' @param id (`character(1)`)\cr
71
- # ' Identifier for the new instance.
78
+ # ' Identifier for the new instance.
72
79
# ' @param label (`character(1)`)\cr
73
- # ' Label for the new instance.
80
+ # ' Label for the new instance.
74
81
# ' @param man (`character(1)`)\cr
75
- # ' String in the format `[pkg]::[topic]` pointing to a manual page for this object.
76
- # ' The referenced help package can be opened via method `$help()`.
82
+ # ' String in the format `[pkg]::[topic]` pointing to a manual page for this object.
83
+ # ' The referenced help package can be opened via method `$help()`.
84
+ # '
77
85
# ' @param on_optimization_begin (`function()`)\cr
78
- # ' Stage called at the beginning of the optimization.
79
- # ' Called in `Optimizer$optimize()`.
86
+ # ' Stage called at the beginning of the optimization.
87
+ # ' Called in `Optimizer$optimize()`.
88
+ # ' The functions must have two arguments named `callback` and `context`.
80
89
# ' @param on_worker_begin (`function()`)\cr
81
- # ' Stage called at the beginning of the optimization on the worker.
82
- # ' Called in the worker loop.
90
+ # ' Stage called at the beginning of the optimization on the worker.
91
+ # ' Called in the worker loop.
92
+ # ' The functions must have two arguments named `callback` and `context`.
83
93
# ' @param on_optimizer_before_eval (`function()`)\cr
84
- # ' Stage called after the optimizer proposes points.
85
- # ' Called in `OptimInstance$.eval_point()`.
94
+ # ' Stage called after the optimizer proposes points.
95
+ # ' Called in `OptimInstance$.eval_point()`.
96
+ # ' The functions must have two arguments named `callback` and `context`.
97
+ # ' The argument of `instance$.eval_point(xs)` and `xs_trafoed` and `extra` are available in the `context`.
98
+ # ' Or `xs` and `xs_trafoed` of `instance$.eval_queue()` are available in the `context`.
86
99
# ' @param on_eval_after_xs (`function()`)\cr
87
- # ' Stage called after xs is passed.
88
- # ' Called in `ObjectiveTuning$eval()`.
100
+ # ' Stage called after xs is passed to the objective.
101
+ # ' Called in `ObjectiveTuningAsync$eval()`.
102
+ # ' The functions must have two arguments named `callback` and `context`.
103
+ # ' The argument of `$.eval(xs)` is available in the `context`.
89
104
# ' @param on_eval_after_resample (`function()`)\cr
90
- # ' Stage called after a hyperparameter configuration is evaluated.
91
- # ' Called in `ObjectiveTuning$eval()`.
105
+ # ' Stage called after a hyperparameter configuration is evaluated.
106
+ # ' Called in `ObjectiveTuningAsync$eval()`.
107
+ # ' The functions must have two arguments named `callback` and `context`.
108
+ # ' The `resample_result` is available in the `context
92
109
# ' @param on_eval_before_archive (`function()`)\cr
93
- # ' Stage called before performance values are written to the archive.
94
- # ' Called in `ObjectiveTuning$eval()`.
110
+ # ' Stage called before performance values are written to the archive.
111
+ # ' Called in `ObjectiveTuningAsync$eval()`.
112
+ # ' The functions must have two arguments named `callback` and `context`.
113
+ # ' The `aggregated_performance` is available in `context`.
95
114
# ' @param on_optimizer_after_eval (`function()`)\cr
96
- # ' Stage called after points are evaluated.
97
- # ' Called in `OptimInstance$.eval_point()`.
115
+ # ' Stage called after points are evaluated.
116
+ # ' Called in `OptimInstance$.eval_point()`.
117
+ # ' The functions must have two arguments named `callback` and `context`.
98
118
# ' @param on_worker_end (`function()`)\cr
99
- # ' Stage called at the end of the optimization on the worker.
100
- # ' Called in the worker loop.
119
+ # ' Stage called at the end of the optimization on the worker.
120
+ # ' Called in the worker loop.
121
+ # ' The functions must have two arguments named `callback` and `context`.
122
+ # ' @param on_tuning_result_begin (`function()`)\cr
123
+ # ' Stage called at the beginning of the result writing.
124
+ # ' Called in `TuningInstance*$assign_result()`.
125
+ # ' The functions must have two arguments named `callback` and `context`.
126
+ # ' The arguments of `$assign_result(xdt, y, learner_param_vals, extra)` are available in `context`.
127
+ # ' @param on_result_begin (`function()`)\cr
128
+ # ' Stage called at the beginning of the result writing.
129
+ # ' Called in `OptimInstance$assign_result()`.
130
+ # ' The functions must have two arguments named `callback` and `context`.
131
+ # ' The arguments of `$.assign_result(xdt, y, extra)` are available in the `context`.
132
+ # ' @param on_result_end (`function()`)\cr
133
+ # ' Stage called after the result is written.
134
+ # ' Called in `OptimInstance$assign_result()`.
135
+ # ' The functions must have two arguments named `callback` and `context`.
136
+ # ' The final result `instance$result` is available in the `context`.
101
137
# ' @param on_result (`function()`)\cr
102
- # ' Stage called after the result is written.
103
- # ' Called in `OptimInstance$assign_result()`.
138
+ # ' Deprecated.
139
+ # ' Use `on_result_end` instead.
140
+ # ' Stage called after the result is written.
141
+ # ' Called in `OptimInstance$assign_result()`.
104
142
# ' @param on_optimization_end (`function()`)\cr
105
143
# ' Stage called at the end of the optimization.
106
144
# ' Called in `Optimizer$optimize()`.
@@ -118,6 +156,9 @@ callback_async_tuning = function(
118
156
on_eval_before_archive = NULL ,
119
157
on_optimizer_after_eval = NULL ,
120
158
on_worker_end = NULL ,
159
+ on_tuning_result_begin = NULL ,
160
+ on_result_begin = NULL ,
161
+ on_result_end = NULL ,
121
162
on_result = NULL ,
122
163
on_optimization_end = NULL
123
164
) {
@@ -130,6 +171,9 @@ callback_async_tuning = function(
130
171
on_eval_before_archive ,
131
172
on_optimizer_after_eval ,
132
173
on_worker_end ,
174
+ on_tuning_result_begin ,
175
+ on_result_begin ,
176
+ on_result_end ,
133
177
on_result ,
134
178
on_optimization_end ),
135
179
c(
@@ -141,10 +185,45 @@ callback_async_tuning = function(
141
185
" on_eval_before_archive" ,
142
186
" on_optimizer_after_eval" ,
143
187
" on_worker_end" ,
188
+ " on_tuning_result_begin" ,
189
+ " on_result_begin" ,
190
+ " on_result_end" ,
144
191
" on_result" ,
145
192
" on_optimization_end" )), is.null )
193
+
194
+ if (" on_result" %in% names(stages )) {
195
+ .Deprecated(old = " on_result" , new = " on_result_end" )
196
+ stages $ on_result_end = stages $ on_result
197
+ stages $ on_result = NULL
198
+ }
199
+
146
200
walk(stages , function (stage ) assert_function(stage , args = c(" callback" , " context" )))
147
201
callback = CallbackAsyncTuning $ new(id , label , man )
148
202
iwalk(stages , function (stage , name ) callback [[name ]] = stage )
149
203
callback
150
204
}
205
+
206
+
207
+ # ' @title Assertions for Callbacks
208
+ # '
209
+ # ' @description
210
+ # ' Assertions for [CallbackAsyncTuning] class.
211
+ # '
212
+ # ' @param callback ([CallbackAsyncTuning]).
213
+ # ' @param null_ok (`logical(1)`)\cr
214
+ # ' If `TRUE`, `NULL` is allowed.
215
+ # '
216
+ # ' @return [CallbackAsyncTuning | List of [CallbackAsyncTuning]s.
217
+ # ' @export
218
+ assert_async_tuning_callback = function (callback , null_ok = FALSE ) {
219
+ if (null_ok && is.null(callback )) return (invisible (NULL ))
220
+ assert_class(callback , " CallbackAsyncTuning" )
221
+ invisible (callback )
222
+ }
223
+
224
+ # ' @export
225
+ # ' @param callbacks (list of [CallbackAsyncTuning]).
226
+ # ' @rdname assert_async_tuning_callback
227
+ assert_async_tuning_callbacks = function (callbacks ) {
228
+ invisible (lapply(callbacks , assert_callback ))
229
+ }
0 commit comments