File size: 13,453 Bytes
2a79cd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random

from botorch.models import SingleTaskGP
from botorch.utils.multi_objective.hypervolume import Hypervolume
from botorch.utils.multi_objective.pareto import is_non_dominated
import numpy as np
import torch

from .model import build_and_optimize_model


def explorative_run(surrogate_model, q, objective_weights,idx_test, test_x_torch):
    """

    Fully explorative acquisition function solely for benchmarking purpose.

    Input:

        surrogate_model: surrogate model object

            The surrogate model to be used.

        q: int

            batch size

        objective_weights: list of floats

            lists of the weights to be used for the scalarization in multi-obj. optimization

        idx_test: list

            list of test indices

        test_x_torch: tensor

            torch tensor of the test data features



    Returns the indices of the selected samples, their data, and their list position in the test data lists.

    """

    # get the variance of the test samples from the surrogate model
    variance = surrogate_model.posterior(test_x_torch).variance

    # scalarization in case of multi-objective optimization
    if type(variance[0].detach().tolist()) is list:  # for mono-objective, the type would be float
        # use the provided weights or otherwise average the predicted variance
        if objective_weights is not None:
            objective_weights = torch.tensor(objective_weights).to(**tkwargs).double()
            variance = (variance * objective_weights).sum(dim=-1)
        else:  # average if no weights are provided
            variance = variance.mean(dim=-1)

    # convert to numpy array
    variance_np = variance.detach().numpy()

    # sort the posterior variance
    sorted_variance = variance_np.tolist().copy()
    sorted_variance.sort(reverse=True)

    # only keep the top scores (= batch size)
    selected_variance = sorted_variance[:q]

    # determine the list indices that belong to these variance scores
    list_positions_selected_variance = []
    for current_variance in selected_variance:
        position = [i for i,x in enumerate(list(variance_np)) if x == current_variance]

        # add the positions of all list occurances of the selected variances to the position list
        for k in range(len(position)):
                if len(list_positions_selected_variance) < q:
                    list_positions_selected_variance.append(position[k])

    samples = []
    best_samples=[]
    # get the indices for these samples in idx_test
    for position in list_positions_selected_variance:
        best_samples.append(idx_test[position])
        samples.append(test_x_torch.detach().numpy().tolist()[position])

    return best_samples, samples, list_positions_selected_variance


def greedy_run(surrogate_model, q, objective_weights, idx_test, test_x_torch):
    """

    Fully exploitative acqusition function.

    If used for multi-objective optimization, the objectives are scalarized using provided weights or by averaging.

    Input:

        surrogate_model: surrogate model object

            The surrogate model to be used.

        q: int

            batch size

        objective_weights: list of float

            list with weights for the objectives in the scalarization (only multi-obj. opt.)

        idx_test: list

            list of test indices

        test_x_torch: tensor

            torch tensor of the test data features



    Returns the indices of the selected samples, their data, and their list position in the test data lists.

    """

    tkwargs = {"dtype": torch.double, "device": torch.device("cpu")}

    # get the surrogate means for the test samples
    means = surrogate_model.posterior(test_x_torch).mean

    # scalarization in case of multi-objective optimization
    if type(means[0].detach().tolist()) is list:  # for mono-objective, the type would be float
        # use the provided weights or otherwise average the predicted means
        if objective_weights is not None:
            objective_weights = torch.tensor(objective_weights).to(**tkwargs).double()
            means = (means * objective_weights).sum(dim=-1)
        else:  # average if no weights are provided
            means = means.mean(dim=-1)

    # convert to numpy array
    means_np = means.detach().numpy()

    # sort the posterior means
    sorted_means = means_np.tolist().copy()
    sorted_means.sort(reverse=True)

    # only keep the top scores (= batch size)
    selected_means = sorted_means[:q]

    # determine the list indices that belong to these mean scores
    list_positions_selected_means = []
    for current_means in selected_means:
        position = [i for i,x in enumerate(list(means_np)) if x == current_means]

        # add the positions of all list occurances of the selected means to the position list
        for k in range(len(position)):
                if len(list_positions_selected_means) < q:
                    list_positions_selected_means.append(position[k])

    samples = []
    best_samples=[]
    # get the indices for these samples in idx_test
    for position in list_positions_selected_means:
        best_samples.append(idx_test[position])
        samples.append(test_x_torch.detach().numpy().tolist()[position])

    return best_samples, samples, list_positions_selected_means


def hypervolume_improvement(surrogate_model, q, objective_weights, cumulative_train_y, idx_test, test_x_torch):
    """

    Multiobjective greedy acquisition function. Exploit the improvement of the current hypervolume.

    Input:

        surrogate_model: surrogate model object

            The surrogate model to be used.

        q: int

            batch size

        objective_weights: list of float

            list with weights for the objectives in the scalarization (only multi-obj. opt.)

        cumulative_train_y: np.array

            array of the training data labels

        idx_test: list

            list of test indices

        test_x_torch: tensor

            torch tensor of the test data features



    Returns the indices of the selected samples, their data, and their list position in the test data lists.

    """
     
    tkwargs = {"dtype": torch.double, "device": torch.device("cpu")}

    # Calculate the reference point for the hypervolume (minimum value for each objective that was seen so far).
    ref_mins = np.min(cumulative_train_y, axis=0)
    ref_point = torch.tensor(ref_mins).double().to(**tkwargs)

    # Get the surrogate means for the test samples
    means = surrogate_model.posterior(test_x_torch).mean
    
    # Get the current Pareto front
    train_y_torch = torch.tensor(cumulative_train_y).to(**tkwargs).double()
    pareto_mask = is_non_dominated(train_y_torch)  # Check which points are non-dominated
    pareto_y = train_y_torch[pareto_mask]  # The Pareto front is the collection of non-dominated points.

    # Apply weights to the predicted means and Pareto front if requested.
    if objective_weights is not None:
        objective_weights = torch.tensor(objective_weights).to(**tkwargs).double()
        means = means * objective_weights
        pareto_y = pareto_y * objective_weights

    # Calculate the hypervolume of the current Pareto front.
    hv = Hypervolume(ref_point=ref_point)  # initialize hypervolume object.
    initial_hv = hv.compute(pareto_y)  # calculate hypervolume
    
    # Calculate the improvement of the hypervolume for each test set point
    hv_improvements = []
    for i in range(means.shape[0]):  # loop through all test set samples
        current_front = torch.cat([pareto_y, means[i].unsqueeze(0)], dim=0)  # add the test sample to the current pareto front
        current_hv = hv.compute(current_front[is_non_dominated(current_front)])  # calculate the hv for that front
        improvement = current_hv - initial_hv  # get the hypervolume improvement
        hv_improvements.append(improvement)

    # Get the indices of the top q values in hv_improvements (q is the batch size)
    top_q_indices = np.array(hv_improvements).argsort()[-q:][::-1]

    # Remove indices for which the hv_improvement was 0 (in case not enough led to improvement)
    list_positions_samples = [idx for idx in top_q_indices if hv_improvements[idx] > 0]

    # get the samples from the test data
    test_x_list = test_x_torch.detach().numpy().tolist()
    samples = [test_x_list[position] for position in list_positions_samples]
    best_samples= [idx_test[position] for position in list_positions_samples]

    # use the greedy acq fct to calculate the remaining samples in case that
    # less samples than requested improved the hypervolume
    # this acq fct uses simple scalarization of the objectives instead of hypervolume improvement
    remaining_q = q - len(list_positions_samples)
    if remaining_q != 0:
        # remove the just selected samples from idx_test and test_x_torch to avoid duplicate selections
        updated_idx_test = np.delete(idx_test, list_positions_samples)
        for position in list_positions_samples:
            test_x_list.pop(position)
        idx_greedy, samples_greedy, list_positions_greedy = greedy_run(
            surrogate_model=surrogate_model, q=remaining_q, objective_weights=objective_weights,
            idx_test=updated_idx_test, test_x_torch=torch.tensor(test_x_list).to(**tkwargs).double())
        
        # Apped these results to the results from the hypervolume acq fct
        for idx in idx_greedy:
            best_samples.append(idx)
        for sample in samples_greedy:
            samples.append(sample)
        for position in list_positions_greedy:
            list_positions_samples.append(position)

    return best_samples, samples, list_positions_samples


def random_run(q, idx_test, seed):
    """

    Acquisition function using seeded random selection solely for benchmarking purpose.

    Input:

        q: int

            batch size

        idx_test: list

            list of test indices

        seed: int

            random seed for reproducibility

        

    Returns the indices of the selected samples and their list position in the test data lists.

    """

    # Set the random seed.
    random.seed(seed)

    # Select q datapoints.
    best_samples = random.sample(list(idx_test),q)

    # Get the corresponding list positions.
    list_positions_selected_samples = []
    for sample in best_samples:
        list_positions_selected_samples.append(list(idx_test).index(sample))

    return best_samples, list_positions_selected_samples


def low_variance_selection(batch, idx_test, cumulative_test_x, cumulative_train_x, cumulative_train_y):
    """

    Acqusition function using selection of the points with the lowest variance for benchmarking purposes (no batch fantasy). 

    Only works for single objectives.



    Returns the indices of the selected samples.



    Input:

        batch: int

            batch size

        idx_test: list

            list of test indices

        cumulative_test_x: np.array

            array of the test data features

        cumulative_train_x: np.array

            array of the training data features

        cumulative_train_y: np.array

            array of the training data labels

    """

    tkwargs = {
        "dtype": torch.double,
        "device": torch.device("cpu")}

    # Tensors for input data.
    train_x_torch = torch.tensor(cumulative_train_x).to(**tkwargs).double()
    test_x_torch = torch.tensor(cumulative_test_x).double().to(**tkwargs)
    train_y_torch = torch.tensor(cumulative_train_y).double().to(**tkwargs)
    
    # Optimize GP model using function from model.py.
    gp, likelihood = build_and_optimize_model(train_x=train_x_torch, train_y=train_y_torch)

    # Creating a single task GP model for the objective and storing it in individual_models list.
    surrogate_model = SingleTaskGP(train_X=train_x_torch, train_Y=train_y_torch,
                        covar_module=gp.covar_module, likelihood=likelihood)
    
    # delete variables that are not required anymore to save memory
    del gp
    del likelihood

    # get the variance of the test samples from the surrogate model
    variance = surrogate_model.posterior(test_x_torch).variance.detach().numpy()

    # sort the posterior variance
    sorted_variance = variance.tolist().copy()
    sorted_variance.sort(reverse=False)  # ascending order

    # only keep the lowest scores (= batch size)
    selected_variance = sorted_variance[:batch]

    # determine the list indices that belong to these variance scores
    position = None
    list_positions_selected_variance = []

    for j in range(len(selected_variance)):
        position = [i for i,x in enumerate(list(variance)) if x == selected_variance[j]]

        # add the positions of all list occurances of the selected variance to the position list
        for k in range(len(position)):
                if len(list_positions_selected_variance) < batch:
                    list_positions_selected_variance.append(position[k])

    # get the indices for these samples in idx_test
    best_samples = idx_test[list_positions_selected_variance]

    return best_samples