-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.py
More file actions
69 lines (58 loc) · 2.36 KB
/
sol.py
File metadata and controls
69 lines (58 loc) · 2.36 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
__author__ = 'Thurston Sexton'
import sys
sys.path.insert(0, '../')
from ego import Kriging
from preprocess import Preprocess
from reg import CovarianceEstimate
import numpy as np
import json
# #
# # get data from the game
# # delete the parameters if performing first-time or new player.
# # Parameters are there to speed up after saving a pkl.
pre = Preprocess(pca_model='../eco_full_pca.pkl', all_dat='../all_games.pkl')
# pre = Preprocess()
# pre.get_json('alluser_control.json') # uncomment this to create the pkl file needed!!
# pre.train_pca()
X, y = pre.ready_player_one(3)
from sklearn.preprocessing import StandardScaler, MinMaxScaler
# scale = StandardScaler()
scale = MinMaxScaler((-1., 1.))
X = scale.fit_transform(X)
#
bounds = np.array(30*[[-1., 1.]])
# # get sigma estimate that maximizes the sum of expected improvements
soln = CovarianceEstimate(X, y, bounds=bounds)
[obj_set, sigma_set] = soln.solve()
# # pick the best solution
obj = obj_set.min(axis=0)
sigma = sigma_set[obj_set.argmin(axis=0), :]
print obj, sigma
# # load bounds
# from numpy import loadtxt
# bounds = loadtxt("ego_bounds.txt", comments="#", delimiter=",", unpack=False)
#
# # store sigma for simulation
# # TODO: need to specify file name based on settings, e.g., optimization algorithm and input data source (best player?)
file_address = 'p3_slsqp_sigma_oldICA.json'
with open(file_address, 'w') as f:
# pickle.dump([obj_set, sigma_set], f)
json.dump([obj, sigma.tolist()], f, sort_keys=True, indent=4, ensure_ascii=False)
f.close()
# store all pcs to a json
from sklearn.externals import joblib
temp = joblib.load('eco_full_pca.pkl')
file_address = 'ica.json'
with open(file_address, 'w') as f:
json.dump(temp.components_.tolist(), f, sort_keys=True, indent=4, ensure_ascii=False)
f.close()
with open('p3_range_transform.json', 'w') as outfile:
json.dump({'range':scale.scale_.tolist(), 'min':scale.min_.tolist()},
outfile, sort_keys = True, indent = 4, ensure_ascii=False)
with open('p3_ICA_transform.json', 'w') as outfile:
json.dump({'mix':pre.pca.mixing_.tolist(), 'unmix':pre.pca.components_.tolist(), 'mean':pre.pca.mean_.tolist()},
outfile, sort_keys = True, indent = 4, ensure_ascii=False)
# A = pre.pca.components_
# Std_inv = np.diag(1/scale.std_)
# vis = A.T.dot(Std_inv.dot(np.diag(sigma).dot(Std_inv.dot(A))))
# np.savetxt('visualize_this.txt', vis)