forked from automl/SMAC3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmbo.py
327 lines (286 loc) · 12.6 KB
/
smbo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import itertools
import logging
import numpy as np
import random
import time
import typing
import math
from smac.configspace import Configuration
from smac.epm.rf_with_instances import RandomForestWithInstances
from smac.initial_design.initial_design import InitialDesign
from smac.intensification.intensification import Intensifier
from smac.optimizer import pSMAC
from smac.optimizer.acquisition import AbstractAcquisitionFunction
from smac.optimizer.ei_optimization import InterleavedLocalAndRandomSearch, \
AcquisitionFunctionMaximizer, RandomSearch
from smac.runhistory.runhistory import RunHistory
from smac.runhistory.runhistory2epm import AbstractRunHistory2EPM
from smac.scenario.scenario import Scenario
from smac.stats.stats import Stats
from smac.tae.execute_ta_run import FirstRunCrashedException
from smac.utils.io.traj_logging import TrajLogger
from smac.utils.validate import Validator
__author__ = "Aaron Klein, Marius Lindauer, Matthias Feurer"
__copyright__ = "Copyright 2015, ML4AAD"
__license__ = "3-clause BSD"
class SMBO(object):
"""Interface that contains the main Bayesian optimization loop
Attributes
----------
logger
incumbent
scenario
config_space
stats
initial_design
runhistory
rh2EPM
intensifier
aggregate_func
num_run
model
acq_optimizer
acquisition_func
rng
"""
def __init__(self,
scenario: Scenario,
stats: Stats,
initial_design: InitialDesign,
runhistory: RunHistory,
runhistory2epm: AbstractRunHistory2EPM,
intensifier: Intensifier,
aggregate_func: callable,
num_run: int,
model: RandomForestWithInstances,
acq_optimizer: AcquisitionFunctionMaximizer,
acquisition_func: AbstractAcquisitionFunction,
rng: np.random.RandomState,
restore_incumbent: Configuration=None):
"""
Interface that contains the main Bayesian optimization loop
Parameters
----------
scenario: smac.scenario.scenario.Scenario
Scenario object
stats: Stats
statistics object with configuration budgets
initial_design: InitialDesign
initial sampling design
runhistory: RunHistory
runhistory with all runs so far
runhistory2epm : AbstractRunHistory2EPM
Object that implements the AbstractRunHistory2EPM to convert runhistory
data into EPM data
intensifier: Intensifier
intensification of new challengers against incumbent configuration
(probably with some kind of racing on the instances)
aggregate_func: callable
how to aggregate the runs in the runhistory to get the performance of a
configuration
num_run: int
id of this run (used for pSMAC)
model: RandomForestWithInstances
empirical performance model (right now, we support only
RandomForestWithInstances)
acq_optimizer: AcquisitionFunctionMaximizer
Optimizer of acquisition function.
acquisition_function : AcquisitionFunction
Object that implements the AbstractAcquisitionFunction (i.e., infill
criterion for acq_optimizer)
restore_incumbent: Configuration
incumbent to be used from the start. ONLY used to restore states.
rng: np.random.RandomState
Random number generator
"""
self.logger = logging.getLogger(
self.__module__ + "." + self.__class__.__name__)
self.incumbent = restore_incumbent
self.scenario = scenario
self.config_space = scenario.cs
self.stats = stats
self.initial_design = initial_design
self.runhistory = runhistory
self.rh2EPM = runhistory2epm
self.intensifier = intensifier
self.aggregate_func = aggregate_func
self.num_run = num_run
self.model = model
self.acq_optimizer = acq_optimizer
self.acquisition_func = acquisition_func
self.rng = rng
self._random_search = RandomSearch(
acquisition_func, self.config_space, rng
)
def start(self):
"""Starts the Bayesian Optimization loop.
Detects whether we the optimization is restored from previous state.
"""
self.stats.start_timing()
# Initialization, depends on input
if self.stats.ta_runs == 0 and self.incumbent is None:
try:
self.incumbent = self.initial_design.run()
except FirstRunCrashedException as err:
if self.scenario.abort_on_first_run_crash:
raise
elif self.stats.ta_runs > 0 and self.incumbent is None:
raise ValueError("According to stats there have been runs performed, "
"but the optimizer cannot detect an incumbent. Did "
"you set the incumbent (e.g. after restoring state)?")
elif self.stats.ta_runs == 0 and self.incumbent is not None:
raise ValueError("An incumbent is specified, but there are no runs "
"recorded in the Stats-object. If you're restoring "
"a state, please provide the Stats-object.")
else:
# Restoring state!
self.logger.info("State Restored! Starting optimization with "
"incumbent %s", self.incumbent)
self.logger.info("State restored with following budget:")
self.stats.print_stats()
def run(self):
"""Runs the Bayesian optimization loop
Returns
----------
incumbent: np.array(1, H)
The best found configuration
"""
self.start()
# Main BO loop
while True:
if self.scenario.shared_model:
pSMAC.read(run_history=self.runhistory,
output_dirs=self.scenario.input_psmac_dirs,
configuration_space=self.config_space,
logger=self.logger)
start_time = time.time()
X, Y = self.rh2EPM.transform(self.runhistory)
self.logger.debug("Search for next configuration")
# get all found configurations sorted according to acq
challengers = self.choose_next(X, Y)
time_spent = time.time() - start_time
time_left = self._get_timebound_for_intensification(time_spent)
self.logger.debug("Intensify")
self.incumbent, inc_perf = self.intensifier.intensify(
challengers=challengers,
incumbent=self.incumbent,
run_history=self.runhistory,
aggregate_func=self.aggregate_func,
time_bound=max(self.intensifier._min_time, time_left))
if self.scenario.shared_model:
pSMAC.write(run_history=self.runhistory,
output_directory=self.scenario.output_dir_for_this_run)
logging.debug("Remaining budget: %f (wallclock), %f (ta costs), %f (target runs)" % (
self.stats.get_remaing_time_budget(),
self.stats.get_remaining_ta_budget(),
self.stats.get_remaining_ta_runs()))
if self.stats.is_budget_exhausted():
break
self.stats.print_stats(debug_out=True)
return self.incumbent
def choose_next(self, X: np.ndarray, Y: np.ndarray,
incumbent_value: float=None):
"""Choose next candidate solution with Bayesian optimization. The
suggested configurations depend on the argument ``acq_optimizer`` to
the ``SMBO`` class.
Parameters
----------
X : (N, D) numpy array
Each row contains a configuration and one set of
instance features.
Y : (N, O) numpy array
The function values for each configuration instance pair.
incumbent_value: float
Cost value of incumbent configuration
(required for acquisition function);
if not given, it will be inferred from runhistory;
if not given and runhistory is empty,
it will raise a ValueError
Returns
-------
Iterable
"""
if X.shape[0] == 0:
# Only return a single point to avoid an overly high number of
# random search iterations
return self._random_search.maximize(
runhistory=self.runhistory, stats=self.stats, num_points=1
)
self.model.train(X, Y)
if incumbent_value is None:
if self.runhistory.empty():
raise ValueError("Runhistory is empty and the cost value of "
"the incumbent is unknown.")
incumbent_value = self.runhistory.get_cost(self.incumbent)
self.acquisition_func.update(model=self.model, eta=incumbent_value)
challengers = self.acq_optimizer.maximize(
self.runhistory, self.stats, 5000
)
return challengers
def validate(self, config_mode='inc', instance_mode='train+test',
repetitions=1, use_epm=False, n_jobs=-1, backend='threading'):
"""Create validator-object and run validation, using
scenario-information, runhistory from smbo and tae_runner from intensify
Parameters
----------
config_mode: str or list<Configuration>
string or directly a list of Configuration
str from [def, inc, def+inc, wallclock_time, cpu_time, all]
time evaluates at cpu- or wallclock-timesteps of:
[max_time/2^0, max_time/2^1, max_time/2^3, ..., default]
with max_time being the highest recorded time
instance_mode: string
what instances to use for validation, from [train, test, train+test]
repetitions: int
number of repetitions in nondeterministic algorithms (in
deterministic will be fixed to 1)
use_epm: bool
whether to use an EPM instead of evaluating all runs with the TAE
n_jobs: int
number of parallel processes used by joblib
Returns
-------
runhistory: RunHistory
runhistory containing all specified runs
"""
traj_fn = os.path.join(self.scenario.output_dir_for_this_run, "traj_aclib2.json")
trajectory = TrajLogger.read_traj_aclib_format(fn=traj_fn, cs=self.scenario.cs)
new_rh_path = os.path.join(self.scenario.output_dir_for_this_run, "validated_runhistory.json")
validator = Validator(self.scenario, trajectory, self.rng)
if use_epm:
new_rh = validator.validate_epm(config_mode=config_mode,
instance_mode=instance_mode,
repetitions=repetitions,
runhistory=self.runhistory,
output=new_rh_path)
else:
new_rh = validator.validate(config_mode, instance_mode, repetitions,
n_jobs, backend, self.runhistory,
self.intensifier.tae_runner,
output=new_rh_path)
return new_rh
def _get_timebound_for_intensification(self, time_spent):
"""Calculate time left for intensify from the time spent on
choosing challengers using the fraction of time intended for
intensification (which is specified in
scenario.intensification_percentage).
Parameters
----------
time_spent : float
Returns
-------
time_left : float
"""
frac_intensify = self.scenario.intensification_percentage
if frac_intensify <= 0 or frac_intensify >= 1:
raise ValueError("The value for intensification_percentage-"
"option must lie in (0,1), instead: %.2f" %
(frac_intensify))
total_time = time_spent / (1 - frac_intensify)
time_left = frac_intensify * total_time
self.logger.debug("Total time: %.4f, time spent on choosing next "
"configurations: %.4f (%.2f), time left for "
"intensification: %.4f (%.2f)" %
(total_time, time_spent, (1 - frac_intensify), time_left, frac_intensify))
return time_left