-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.py
359 lines (309 loc) · 13.7 KB
/
optimize.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import warnings
from typing import Any, Dict, List, Literal, Optional, Union
import numpy as np
import pandas as pd
import torch
from botorch.acquisition.monte_carlo import MCAcquisitionFunction
from botorch.acquisition.multi_objective.monte_carlo import (
qExpectedHypervolumeImprovement as qEHVI,
)
from botorch.acquisition.multi_objective.monte_carlo import (
qNoisyExpectedHypervolumeImprovement as qNEHVI,
)
from botorch import fit_gpytorch_mll
from botorch.models import SingleTaskGP
from botorch.models.model import Model
from botorch.models.model_list_gp_regression import ModelListGP
from botorch.models.transforms.input import FilterFeatures
from botorch.optim import optimize_acqf
from botorch.sampling.base import MCSampler
from botorch.sampling.normal import SobolQMCNormalSampler
from gpytorch.mlls import SumMarginalLogLikelihood
from summit import *
dtype = torch.double
class CategoricalqNEHVI(qNEHVI):
def __init__(
self,
model: Model,
ref_point: Union[List[float], torch.Tensor],
X_baseline: torch.Tensor,
domain: Domain,
descriptor_tensors: Optional[Dict[str, torch.Tensor]] = None,
sampler: Optional[MCSampler] = None,
**kwargs,
) -> None:
super().__init__(model, ref_point, X_baseline, sampler, **kwargs)
self._domain = domain
self.skip = True if domain.num_categorical_variables() == 0 else False
self.descriptor_tensors = descriptor_tensors
def forward(self, X): # this is do the rounding for categ vars in acquisition function!!
if not self.skip:
# print("before round X=", X) # test before rounding
X = self.round_categorical(X, self._domain, self.descriptor_tensors)
# print("after round X=", X) # test after rounding
return super().forward(X)
@staticmethod
def round_categorical(
X, domain: Domain, descriptor_tensors: Optional[Dict[str, torch.Tensor]] = None
):
"""Round all categorical variables to a one-hot encoding"""
num_experiments = X.shape[1]
X = X.clone()
for q in range(num_experiments):
c = 0
for v in domain.input_variables:
if isinstance(v, CategoricalVariable) and v.ds is None:
n_levels = len(v.levels)
levels_selected = X[:, q, c : c + n_levels].argmax(axis=1)
X[:, q, c : c + n_levels] = 0
for j, l in zip(range(X.shape[0]), levels_selected):
X[j, q, int(c + l)] = 1
check = int(X[:, q, c : c + n_levels].sum()) == X.shape[0]
if not check:
raise ValueError(
(
f"Rounding to a one-hot encoding is not properly working. Please report this bug at "
f"https://github.com/sustainable-processes/summit/issues. Tensor: \n {X[:, :, c : c + n_levels]}"
)
)
c += n_levels
# Choose the closest option by Euclidean distance
elif isinstance(v, CategoricalVariable) and v.ds is not None:
# Get embeddings
num_descriptors = v.ds.shape[1]
embeddings = X[:, q, c : c + num_descriptors]
option_embeddings = descriptor_tensors[v.name]
# Calculated distances
distances = torch.cdist(embeddings, option_embeddings)
# Select the closest option
levels_selected = distances.argmin(axis=1)
# Update embeddings
new_embeddings = option_embeddings[levels_selected, :]
X[:, q, c : c + num_descriptors] = new_embeddings
else:
c += 1
return X
class MOBO(Strategy):
"""Multiobjective Bayesian Optimisation using BOtorch
Parameters
----------
domain : :class:`~summit.domain.Domain`
The domain of the optimization
transform : :class:`~summit.strategies.base.Transform`, optional
A transform object. By default no transformation will be done
on the input variables or objectives.
Examples
--------
>>> from summit.domain import Domain, ContinuousVariable
>>> from summit.strategies import NelderMead
>>> domain = Domain()
>>> domain += ContinuousVariable(name='temperature', description='reaction temperature in celsius', bounds=[0, 1])
>>> domain += ContinuousVariable(name='flowrate_a', description='flow of reactant a in mL/min', bounds=[0, 1])
>>> domain += ContinuousVariable(name="yld", description='relative conversion to xyz', bounds=[0,100], is_objective=True, maximize=True)
>>> strategy = STBO(domain)
>>> next_experiments = strategy.suggest_experiments()
>>> print(next_experiments)
NAME temperature flowrate_a strategy
TYPE DATA DATA METADATA
0 0.500 0.500 Nelder-Mead Simplex
1 0.625 0.500 Nelder-Mead Simplex
2 0.500 0.625 Nelder-Mead Simplex
"""
def __init__(
self,
domain: Domain,
transform: Transform = None,
input_groups: Optional[Dict[str, List[str]]] = None,
**kwargs,
):
Strategy.__init__(self, domain, transform, **kwargs)
if len(self.domain.output_variables) < 2:
raise DomainError("MOBO only works with multi-objective problems")
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Acqusition function and reference point
self.static_reference_point = {}
for v in self.domain.output_variables:
self.static_reference_point[v.name] = (
v.bounds[1] if v.maximize else -v.bounds[0]
)
# Input grouping
self.input_groups = input_groups
if self.input_groups:
variable_names = [v.name for v in self.domain.input_variables]
self.input_group_indices = {
output_name: [variable_names.index(input) for input in g]
for output_name, g in input_groups.items()
}
else:
self.input_group_indices = None
self.reset()
def suggest_experiments(self, num_experiments, prev_res: DataSet = None, **kwargs):
# Suggest lhs initial design or append new experiments to previous experiments
if prev_res is None and self.all_experiments is None:
lhs = LHS(self.domain, transform=self.transform)
self.iterations += 1
k = num_experiments if num_experiments > 1 else 2
conditions = lhs.suggest_experiments(k)
for v in self.domain.input_variables:
if isinstance(v, CategoricalVariable) and v.ds is not None:
indices = conditions[v.name].values
descriptors = v.ds.loc[indices]
descriptors.index = conditions.index
conditions = conditions.join(descriptors, how="inner")
var_descriptor_names = v.ds.data_columns
for var in var_descriptor_names:
descriptor = conditions[var].copy()
conditions = conditions.drop(columns=var, level=0)
conditions[var, "METADATA"] = descriptor
return conditions
elif prev_res is not None and self.all_experiments is None:
self.all_experiments = prev_res
elif prev_res is not None and self.all_experiments is not None:
self.all_experiments = pd.concat([self.all_experiments, prev_res], axis=0)
self.iterations += 1
data = self.all_experiments
# Get inputs (decision variables) and outputs (objectives)
inputs, output = self.transform.transform_inputs_outputs(
data,
min_max_scale_inputs=True,
standardize_outputs=True,
categorical_method="mixed",
)
# Make it always a maximization problem
for v in self.domain.output_variables:
if not v.maximize:
output[v.name] = -output[v.name]
# Convert to torch tensors
X = torch.tensor(
inputs.data_to_numpy().astype(float),
device=self.device,
dtype=dtype,
)
y = torch.tensor(
output.data_to_numpy().astype(float),
device=self.device,
dtype=dtype,
)
# Fit independent GP models
self.model = self.fit_model(X, y)
# Optimize acquisition function
reference_point = self._get_transformed_reference_point(output)
results = self.optimize_acquisition_function(
self.model,
X=X,
num_experiments=num_experiments,
reference_point=reference_point,
num_restarts=kwargs.get("num_restarts", 100),
raw_samples=kwargs.get("raw_samples", 2000),
# mc_samples=kwargs.get("mc_samples", 128),
)
# Convert result to datset
result = DataSet(
results.cpu().detach().numpy(),
columns=inputs.data_columns,
)
# Untransform
result = self.transform.un_transform(
result,
min_max_scale_inputs=True,
standardized_outputs=True,
categorical_method="mixed",
)
# Add metadata
result[("strategy", "METADATA")] = "MOBO"
return result
def fit_model(self, X, y):
models = []
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for i, v in enumerate(self.domain.output_variables):
transform = None
if self.input_group_indices:
feature_indices = torch.tensor(
self.input_group_indices[v.name], device=self.device
)
transform = FilterFeatures(feature_indices=feature_indices)
model = SingleTaskGP(X, y[:, [i]], input_transform=transform) # X: conti vars normalise to (0,1) categ vars keep one-hot; y standarise using mean
models.append(model)
model = ModelListGP(*models)
mll = SumMarginalLogLikelihood(model.likelihood, model)
fit_gpytorch_mll(mll, max_retries=20)
return model
def optimize_acquisition_function(
self,
model,
X: torch.Tensor,
reference_point: torch.Tensor,
num_experiments: int,
num_restarts: int = 100,
raw_samples: int = 2000,
mc_samples: int = 128,
):
# Sampler
sampler = SobolQMCNormalSampler(sample_shape=torch.Size([mc_samples]))
# Normalize any descriptor dfs
descriptors_tensors = {}
for v in self.domain.input_variables:
if isinstance(v, CategoricalVariable) and v.ds is not None:
new_ds = v.ds.copy()
for descriptor in new_ds.data_columns:
var_max = v.ds[descriptor].max()
var_min = v.ds[descriptor].min()
new_ds[descriptor, "DATA"] = (new_ds[descriptor] - var_min) / (
var_max - var_min
)
descriptors_tensors[v.name] = torch.tensor(
new_ds.to_numpy(), device=self.device, dtype=dtype
)
# Acquisition function
acq = CategoricalqNEHVI(
model=model,
ref_point=reference_point, # -1.6, -4.2, every time will slightly change
domain=self.domain,
sampler=sampler,
X_baseline=X, # conti vars normalise to (0,1), categ vars keep one-hot encoding
prune_baseline=True,
descriptor_tensors=descriptors_tensors,
)
# Optimize acquisition function
results, _ = optimize_acqf(
acq_function=acq,
bounds=self._get_input_bounds(), # all range from 0-1
num_restarts=num_restarts,
q=num_experiments,
raw_samples=raw_samples,
)
return results
def _get_input_bounds(self):
bounds = []
for v in self.domain.input_variables:
if isinstance(v, ContinuousVariable):
# Because of min max scaling
bounds += [[0, 1]]
elif isinstance(v, CategoricalVariable) and v.ds is None:
# Because of one-hot encoding
bounds += [[0, 1] for _ in v.levels]
elif isinstance(v, CategoricalVariable) and v.ds is not None:
# Because of min-max scaling
bounds += [[0, 1] for _ in v.ds.columns]
return torch.tensor(np.array(bounds), dtype=dtype, device=self.device).T
def _get_transformed_reference_point(self, output: DataSet):
transformed_reference_point = []
for v in self.domain.output_variables:
# Get the worst point in each output
# Should always be transformed to a maximization problem
val = output.sort_values(v.name, ascending=True)[v.name].iloc[0]
transformed_reference_point.append(val)
return torch.tensor(
np.array(transformed_reference_point), dtype=dtype, device=self.device
)
def reset(self):
"""Reset MTBO state"""
self.all_experiments = None
self.iterations = 0
self.fbest = (
float("inf") if self.domain.output_variables[0].maximize else -float("inf")
)
def to_dict(self, **strategy_params):
strategy_params.update({"input_groups": self.input_groups})
return super().to_dict(**strategy_params)