-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
181 lines (148 loc) · 6.11 KB
/
funcs.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
import sys
import numpy as np
import models
import torch
import math
import gpytorch
import matplotlib.pyplot as plt
from torch.nn import Module
import torch.nn as nn
from target_functions import Branin, Hartmann, Levy
class quad_target(object):
def __init__(self):
self.fidelity = 2
self.x_dim = 1
self.a = torch.tensor([0.5, -0.5])
self.b = torch.tensor([0.5, 0.5])
self.bounds = torch.tensor([[-1.], [1.]])
self.s = 1
self.f = 32
def noise_level(self, tr_x, index_x):
if len(tr_x.shape) == 2:
tr_x = tr_x[:, 0]
return (self.a[index_x] * tr_x + self.b[index_x]) * torch.sin(self.f*torch.pi*tr_x)
def query_ground_truth(self, tr_x, index_x):
if len(tr_x.shape) == 2:
tr_x = tr_x[:, 0]
tr_y_gt = - (self.s*tr_x ** 2 - 1)*torch.cos(torch.pi*3*tr_x)
return tr_y_gt
def query(self, tr_x, index_x):
return self.query_ground_truth(tr_x, index_x) + self.noise_level(tr_x, index_x)
class sin_target(object):
def __init__(self, fidelity_fix=None, bias=False):
if fidelity_fix is None:
self.fidelity = 2
else:
self.fidelity = 1
self.fidelity_fix = fidelity_fix
self.x_dim = 1
self.a = torch.tensor([0.5, -0.5])
self.b = torch.tensor([0, 0.5])
self.bounds = torch.tensor([[0.], [1.]])
self.bias = bias
def noise_level(self, tr_x, index_x):
if self.fidelity == 1:
index_x = self.fidelity_fix * torch.ones_like(index_x, dtype=torch.long)
if len(tr_x.shape) == 2:
tr_x = tr_x[:, 0]
return self.a[index_x] * tr_x + self.b[index_x]
def query_ground_truth(self, tr_x, index_x):
if len(tr_x.shape) == 2:
tr_x = tr_x[:, 0]
tr_y_gt = torch.sin(tr_x * (2 * math.pi))
tr_y_gt[torch.logical_or((tr_x < 0), (tr_x > 1))] = 0
return tr_y_gt
def query(self, tr_x, index_x):
bias_sets = torch.tensor([0.5, -0.5])
if self.fidelity == 1:
index_x = self.fidelity_fix * torch.ones(tr_x.shape[0], dtype=torch.long)
noise_level = self.noise_level(tr_x, index_x)
bias_value = self.bias*bias_sets[index_x]
return self.query_ground_truth(tr_x, index_x) + torch.randn(tr_x.shape[0])*noise_level + bias_value
class band_gap_target(object):
def __init__(self, dir, follow, cost=None):
if cost is None:
cost = [1, 1]
self.fidelity = 2
self.Z = torch.load(dir+'/Z'+follow+'.ts')
self.Y = torch.load(dir+'/Y'+follow+'.ts')
self.Y_0 = torch.load(dir+'/Y_0'+follow+'.ts')
self.Y_1 = torch.load(dir+'/Y_1'+follow+'.ts')+0.9
self.size = self.Z.shape[0]
self.Y_low = [self.Y_0, self.Y_1]
self.cost = cost
def input_by_num(self, num_x):
return self.Z[num_x, :]
def query_ground_truth_by_num(self, num_x):
return self.Y[num_x, 0]
def query_by_num(self, num_x, index_x):
output = torch.ones([num_x.shape[0]])
for i in range(num_x.shape[0]):
output[i] = self.Y_low[index_x[i]][num_x[i], 0]
return output
def query_by_value(self, value, index_x):
closest_index = torch.argmin(torch.sum((self.Z - value)**2, dim=1)).unsqueeze(0)
return self.query_by_num(closest_index, index_x)
def query_ground_truth_by_value(self, value):
closest_index = torch.argmin(torch.sum((self.Z - value)**2, dim=1)).unsqueeze(0)
return self.query_ground_truth_by_num(closest_index)
class band_gap_target_three(object):
def __init__(self, dir, follow, cost=None):
if cost is None:
cost = [1, 1, 1]
self.fidelity = 3
self.Z = torch.load(dir+'/Z'+follow+'.ts')
self.Y = torch.load(dir+'/Y'+follow+'.ts')
self.Y_0 = torch.load(dir+'/Y_0'+follow+'.ts')
self.Y_1 = torch.load(dir+'/Y_1'+follow+'.ts')+0.9
self.Y_2 = torch.load(dir + '/Y_2' + follow + '.ts')
self.size = self.Z.shape[0]
self.Y_low = [self.Y_0, self.Y_1, self.Y_2]
self.cost = cost
def input_by_num(self, num_x):
return self.Z[num_x, :]
def query_ground_truth_by_num(self, num_x):
return self.Y[num_x, 0]
def query_by_num(self, num_x, index_x):
output = torch.ones([num_x.shape[0]])
for i in range(num_x.shape[0]):
output[i] = self.Y_low[index_x[i]][num_x[i], 0]
return output
def query_by_value(self, value, index_x):
closest_index = torch.argmin(torch.sum((self.Z - value)**2, dim=1)).unsqueeze(0)
return self.query_by_num(closest_index, index_x)
def query_ground_truth_by_value(self, value):
closest_index = torch.argmin(torch.sum((self.Z - value)**2, dim=1)).unsqueeze(0)
return self.query_ground_truth_by_num(closest_index)
class br_target(object):
def __init__(self, fidelity_fix=None):
if fidelity_fix is None:
self.fidelity = 2
else:
self.fidelity = 1
self.fidelity_fix = fidelity_fix
self.x_dim = 1
self.a = torch.tensor([[[0.5, 0.5]], [[-0.5, -0.50]]])
self.b = torch.tensor([[0], [1]])
#
# self.bounds = torch.tensor([[0.], [1.]])
def noise_level(self, tr_x, index_x):
if self.fidelity == 1:
index_x = self.fidelity_fix * torch.ones_like(index_x, dtype=torch.long)
if len(tr_x.shape) == 2:
tr_x = tr_x[:, 0]
return self.a[index_x] * tr_x + self.b[index_x]
def query_ground_truth(self, tr_x, index_x):
if len(tr_x.shape) == 2:
tr_x = tr_x[:, 0]
tr_y_gt = torch.sin(tr_x * (2 * math.pi))
tr_y_gt[torch.logical_or((tr_x < 0), (tr_x > 1))] = 0
return tr_y_gt
def query(self, tr_x, index_x):
if self.fidelity == 1:
index_x = self.fidelity_fix * torch.ones(tr_x.shape[0], dtype=torch.long)
noise_level = self.noise_level(tr_x, index_x)
return self.query_ground_truth(tr_x, index_x) + torch.randn(tr_x.shape[0])*noise_level
if __name__ == '__main__':
aaa = band_gap_target("./real_experiement", '')
print(aaa.Y.max())