-
Notifications
You must be signed in to change notification settings - Fork 3
/
bo_functions.py
118 lines (88 loc) · 2.91 KB
/
bo_functions.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
import numpy as np
import abc
import tensorflow as tf
import gpflowSlim as gfs
FLOAT_TYPE = gfs.settings.float_type
def stybtang(x):
if len(x.shape) == 1:
return 0.5 * tf.reduce_sum(tf.pow(x, 4) - 16 * tf.pow(x, 2) + 5 * x)
else:
return 0.5 * tf.reduce_sum(tf.pow(x, 4) - 16 * tf.pow(x, 2) + 5 * x, axis=1)
def michalewicz(x):
assert len(x.shape) == 2, 'x input must be 2 dimensional array'
indx = tf.constant(list(range(1, 1 + int(x.shape[1]))), dtype=FLOAT_TYPE)
indx = tf.expand_dims(indx, 0)
return -tf.reduce_sum(tf.sin(x) * tf.sin(x * indx / np.pi) ** (2 * 10), axis=-1)
def michalewicz(x):
assert len(x.shape) == 2, 'x input must be 2 dimensional array'
indx = tf.constant(list(range(1, 1 + int(x.shape[1]))), dtype=FLOAT_TYPE)
indx = tf.expand_dims(indx, 0)
return -tf.reduce_sum(tf.sin(x) * tf.sin(x**2 * indx / np.pi) ** (2 * 10), axis=-1)
class Function:
@abc.abstractclassmethod
def min(self):
pass
@abc.abstractclassmethod
def max(self):
pass
@abc.abstractclassmethod
def func(self, x):
pass
@abc.abstractclassmethod
def name(self):
pass
class Stybtang(Function):
def __init__(self, num_dims):
self.num_dims = num_dims
@property
def min(self):
return -4. * np.ones([self.num_dims], dtype=FLOAT_TYPE)
@property
def max(self):
return 4. * np.ones([self.num_dims], dtype=FLOAT_TYPE)
def func(self, x):
return stybtang(x)
@property
def name(self):
return 'Stybtang'
class Michalewicz(Function):
def __init__(self, num_dims):
self.num_dims = num_dims
@property
def min(self):
return 0. * np.ones([self.num_dims], dtype=FLOAT_TYPE)
@property
def max(self):
return np.pi * np.ones([self.num_dims], dtype=FLOAT_TYPE)
def func(self, x):
return michalewicz(x)
@property
def name(self):
return 'Michalewicz'
class Stybtang_transform(Function):
def __init__(self, num_dims, dim_groups):
self.num_dims = num_dims
Q = np.zeros((num_dims, num_dims), dtype=FLOAT_TYPE)
for group in dim_groups:
d = len(group)
if d > 1:
A = np.random.randn(d, d).astype(FLOAT_TYPE)
Q_d, _ = np.linalg.qr(A)
Q[np.ix_(group, group)] = Q_d
else:
Q[group[0], group[0]] = 1.
self.Q = Q
global_opt = -2.903534 * np.ones([num_dims, 1], dtype=FLOAT_TYPE)
self.inverse_opt = np.matmul(np.transpose(Q), global_opt)
self.inverse_opt = np.squeeze(self.inverse_opt)
@property
def min(self):
return self.inverse_opt - 3
@property
def max(self):
return self.inverse_opt + 3
def func(self, x):
return stybtang(tf.matmul(x, tf.transpose(self.Q)))
@property
def name(self):
return 'Stybtang_transform'