-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBayesianOptimization.py
More file actions
65 lines (56 loc) · 2.83 KB
/
BayesianOptimization.py
File metadata and controls
65 lines (56 loc) · 2.83 KB
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
import numpy as np
import math
from typing import Callable, Optional
from DifferentiableFunction import IDifferentiableFunction
from Set import AffineSpace, MultidimensionalInterval
from GP import GP
from SQP import SQP
class BO(object):
def __init__(self):
super().__init__()
def Minimize(self, function: IDifferentiableFunction, iterations: int = 50, x: Optional[np.array] = None, y: Optional[np.array] = None) -> np.array:
"""
Minimize the given function using Bayesian Optimization.
:param function: The function to minimize.
:param iterations: The number of iterations to run the optimization.
:param x: The data_x to start the optimization with.
:param y: The data_y to start the optimization with.
:return: The point with the lowest function value found during the optimization.
"""
domain = function.domain
d = domain._ambient_dimension
# Initialize data_x and data_y with provided data or empty arrays
if x is not None and y is not None:
# check that x and y is not empty
assert x.shape[0] > 0, "The data_x must not be empty."
assert y.shape[0] > 0, "The data_y must not be empty."
# data_x dimension must be equal to the domain dimension
assert x.shape[1] == d, "The data_x must have the same dimension as the domain."
# check that the number of rows in x and y is equal
assert x.shape[0] == y.shape[0], "The number of rows in data_x and data_y must be equal."
assert domain.contains(x), "The data_x must be in the domain of the function."
data_x = x
data_y = y
else:
data_x = np.empty((0, d))
data_y = np.empty((0,))
# Mögliche Kernels: GP.Linear(), GP.MaternCovariance(), GP.RBF()
# gp = GP(data_x=data_x, data_y=data_y)
# gp = GP(data_x=data_x, data_y=data_y, kernel=GP.Linear())
gp = GP(data_x=data_x, data_y=data_y, kernel=GP.MaternCovariance())
sqp = SQP()
for step in range(iterations):
# UCB with 2 sigma, added with 0 times the original function to get the domain
acquisition_function = gp.PosteriorMean(
)-2*gp.PosteriorStandardDeviation() + 0*function
# new measurement point
startingpoint = domain.point()
x = sqp.Minimize(acquisition_function,
startingpoint=startingpoint)
# measure
y = function.evaluate(x)
# update data
data_x = np.concatenate((data_x, x.reshape(1, -1)), axis=0)
data_y = np.concatenate((data_y, y), axis=0)
gp = GP(data_x=data_x, data_y=data_y)
return data_x[np.argmin(data_y), :]