-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreg.py
More file actions
180 lines (155 loc) · 7.33 KB
/
reg.py
File metadata and controls
180 lines (155 loc) · 7.33 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
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
__author__ = 'Yi Ren'
"""
This contains the class for fitting a sigma for a human player onto EGO.
"""
from ego import Kriging
# from bayes_opt import BayesianOptimization
import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt
# from tqdm import tqdm
class CovarianceEstimate:
'''
Instantiates meta-problem, i.e., prepares and solves the minimization problem
to fit a Covariance matrix that maximizes the sum of expected improvement over
all observed human plays, to fit a Kriging response surface. Currently
optimizing using EGO. Input is reduced 30-dim. (standardized) PCA of original
18k-length signal
Parameters:
X: entire data-set of a Person's plays (n by 30-dim standardized)
y: array-like of player's scores
Methods:
solve(self): actual solver, currently EGO, that finds Sigma[1:30]
that maximize total expected improvement
'''
def __init__(self, X, y, bounds, xbounds, initial_guess, l_INI, sample_size=10000, num_ini_guess=2, alpha=10.):
self.n = X.shape[1]
self.sigma_inv = np.ones(self.n) # default value
self.model = Kriging(self.sigma_inv, xbounds, num_ini_guess, sample_size) # Fit Kriging on the data.
self.model.fit(X, y)
self.input = X
self.rem_eng = y
self.sct = None
self.alpha = alpha
self.l_INI = l_INI
self.initial_guess = initial_guess
self.bounds = bounds
# self.pbar = None
# self.fig = None
# self.ax = None
# self.Nfeval = 0
def callbackF(self, Xi):
# feval = self.model.obj(Xi)
# print '{0:4d} {1: 3.6f}'.format(self.Nfeval, feval)
# if np.any(Xi > 1000.) or np.any(Xi < 0.):
# print 'OoB'
# if feval > 1000.:
# print Xi
# self.Nfeval += 1
# self.sct = plt.gca.plot(self.model.recent_path, 'b')
if len(plt.gca().lines) > 0:
del plt.gca().lines[0]
self.sct = plt.gca().plot(self.model.recent_path, 'b')
plt.gca().set_xlabel('play number')
plt.gca().set_ylabel('Expected Improvement Path')
plt.pause(0.0001)
# if self.pbar is not None:
# self.pbar.update()
# self.ax.set_title('Point Jacobi approximation after '+str(k)+' iterations')
# plt.pause(.002)
# print 'Hi!'
def ego_func(self, x0, x1, x2, x3, x4, x5, x6,
x7, x8, x9, x10, x11, x12, x13, x14, x15,
x16, x17, x18, x19, x20, x21, x22, x23,
x24, x25, x26, x27, x28, x29):
'''
Had to take in n individual variables...
:return: objective function
'''
x = np.zeros(self.n)
args = [x0, x1, x2, x3, x4, x5, x6,
x7, x8, x9, x10, x11, x12, x13, x14, x15,
x16, x17, x18, x19, x20, x21, x22, x23,
x24, x25, x26, x27, x28, x29]
for n, i in enumerate(args):
print i
x[n] = i
return self.model.obj(x)
def solve(self, plot=False):
'''
MUST HAVE ego_bds.p and ego_explore.p in directory. This is a list of desired search ranch,
and the positions to evaluate the model to instantiate EGO. Do not confuse with ego_bounds.txt,
which is a list of viable ranges for each variable in later simulation (the range of each PC).
:return: best sigma
'''
test_scale = np.arange(-2.,1.,0.5)
# test_scale = np.array([0.7])
result_x = np.zeros((test_scale.shape[0], self.n))
result_f = np.zeros(test_scale.shape)
# pbar = tqdm(total=test_scale.size)
for i, s in enumerate(test_scale):
# self.pbar=tqdm()
if plot:
self.sct = None
ax = None
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ion()
plt.show()
# x0 = np.zeros(30)+1e-15
# x0[7] = np.exp(2.5)
# x0 = np.ones(30)
# -->
# x0 = [1.50205418e-01, 6.76120529e-12, 1.84195280e-11, 7.83697468e-12,
# 1.88757328e-12, 3.95820958e-11, 1.19620582e-11, 1.19440742e-11,
# 1.87089886e-11, 4.58049765e+00, 4.91223856e-12, 2.84647880e-12,
# 5.69667522e-12, 2.30425680e-11, 1.14781061e-11, 3.45560046e-11,
# 9.35167355e-12, 7.56675789e-12, 1.92011109e-03, 1.98174635e-11,
# 7.22949300e-12, 3.86415689e-12, 3.32313328e-11, 6.81567135e-12,
# 9.75048750e-12, 1.13677645e-11, 5.17722412e-12, 1.53569991e-03,
# 2.38163239e-11, 4.80720115e-12] # -6.28698665444
# x0 = np.ones(30)*0.1
# -->
# [ 2.00829797e-03 1.90314686e-04 4.95525647e-08 5.72888703e-08
# 5.05644909e-08 4.96489353e-08 4.86581148e-08 5.14023421e-08
# 5.30146723e-02 4.28454169e-08 2.13781365e-09 3.81019074e-08
# 1.58936468e-08 1.02135824e-08 4.68306047e-08 4.19927866e-08
# 5.23404844e-08 4.40914737e-08 4.98378832e-08 7.80431707e-09
# 4.60839075e-08 5.09895031e-08 4.21929293e-08 2.49207806e-02
# 1.62240194e-08 4.79799229e-08 2.22741405e-08 4.69940438e-01
# 2.09332766e-09 5.85302709e-08] -6.34599415271
# [ 0. 0. 0. 0. 0. 0. 0.
# 0. 0. 0. 0. 0. 0. 0.
# 0. 0. 0. 0. 0. 0. 0.
# 0. 0. 0. 0. 0. 0.
# 0.86766298 0. 0. ] -6.752578221
# x0 = np.ones(self.n)*s
# x0 = np.random.random(30)*5.
x0 = self.initial_guess
func = lambda x: -self.model.obj(x, alpha=self.alpha, l_INI= self.l_INI) # use this if switching from EGO, maximize the log likelihood
# lb = 0.01
# ub = 100.
# bounds = [(lb, ub)]*self.n
bounds = self.bounds
# print bounds
# these are some alternative functions, which use 'callbackF for verbosity'
# print self.model.obj(x0)
print 'Initializing at '+str(s)
res = opt.minimize(func, x0=x0, bounds=bounds, method='L-BFGS-B',
options={'eps': 1e-3, 'iprint': 2, 'disp': True, 'maxiter': 100},
callback=self.callbackF)
# pbar.update(1)
# res = opt.differential_evolution(func, bounds, disp=True, popsize=10)
# res = opt.basinhopping(func, x0=x0, disp=True)
# bounds = pickle.load(open("ego_bds.p", "rb")) # load sigma boundaries
# bo = BayesianOptimization(self.ego_func, pbounds=bounds) # create optimizer object
# explore = pickle.load(open("ego_explore.p", "r")) # load points to check
# bo.explore(explore) #initiate
# bo.maximize(init_points=15, n_iter=25)
# print bo.res['max']
print res.x, res.fun
# return bo.res['max']
result_f[i] = res.fun
result_x[i] = 10**(res.x)
# self.pbar.close()
return result_f, result_x