def plot1D(filename='GP_results/test1.txt', magnet_list=['h13', 'v13', 'h31', 'v31']): ''' Plots at every iteration GP. Requires some manual hardcoded interaction ''' reader = np.asmatrix(np.loadtxt(filename)) x_observed = np.asarray(reader[:, 0]) # Hardcoded!!!!!!!!! f_observed = np.asarray(reader[:, -1]) # Hardcoded!!!!!!!!! n_rows = math.ceil(len(f_observed) / 5) f_mean, sub_mean = plt.subplots(n_rows, 5, sharex=True, sharey=True) f_mean.tight_layout() # to adjust spacing between subplots f_acq, sub_acq = plt.subplots(n_rows, 5, sharex=True, sharey=True) f_acq.tight_layout() # to adjust spacing between subplots num_points = 1000 X_grid = np.linspace(-10, 10, num_points)[:, None] #X_grid = np.linspace(-15, 15, num_points)[:,None] for i in range(n_rows): j = 0 while len(f_observed) > 5 * i + j and j < 5: X = x_observed[0:(5 * i + j + 1)] Y = f_observed[0:(5 * i + j + 1)] mean, Cov, variance, m = GP_analysis(X, Y, X_grid) sub_mean[i, j].plot(X_grid, mean) sub_mean[i, j].fill_between(X_grid[:, 0], (mean.T + variance.T).T[:, 0], (mean.T - variance.T).T[:, 0], facecolor="gray", alpha=0.15) sub_mean[i, j].scatter(X, Y) model = GPModel(optimize_restarts=1, verbose=True) model.model = m space = Design_space([{ 'name': 'var1', 'type': 'continuous', 'domain': (-10, 10) }]) acq = AcquisitionLCB(model, space, exploration_weight=1) # Hardcoded!!!!!!!!! alpha_full = acq.acquisition_function(X_grid) sub_acq[i, j].plot(X_grid, alpha_full) j = j + 1 timestamp = (datetime.datetime.now()).strftime("%m-%d_%H-%M-%S") f_mean.subplots_adjust(wspace=0.3, top=None, bottom=None) f_mean.savefig(f'GP_results/dis_mean_M1-{timestamp}.pdf') f_acq.subplots_adjust(wspace=0.3, top=None, bottom=None) f_acq.savefig(f'GP_results/dis_acq_M1-{timestamp}.pdf') #plt.show() plt.close() return (None)
m.Gaussian_noise.variance.unconstrain_positive() m.Gaussian_noise.variance.set_prior(GPy.priors.Gaussian(10, 10)) m.optimize('bfgs', max_iters=100) # Hyper-parameters are optimized here print(m) f = open(f"GP_results/opt_params_{timestamp}.txt", "a+") ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]') text = ansi_escape.sub('', str(m)) f.write(text + '\n') f.close() # Find next point model = GPModel(optimize_restarts=1, verbose=True) model.model = m #acq = AcquisitionEI(model, space, jitter = 1) acq = AcquisitionLCB( model, space, exploration_weight=0.5) # Hardcoded HYPER_PARAMETER!!!!!!!! alpha_full = acq.acquisition_function(X_grid) magnet_values = X_grid[np.argmin(alpha_full), :] print("Min LCB: ", np.argmin(alpha_full), min(alpha_full), X_grid[np.argmin(alpha_full), :]) print("Max LCB: ", np.argmax(alpha_full), max(alpha_full), X_grid[np.argmax(alpha_full), :]) ''' if (len(magnet_list)==1): gp.plot1D(f'GP_results/correctorValues_Distance_{timestamp}.txt') elif (len(magnet_list)==2):
def plot2D(filename='GP_results/test2.txt', magnet_list=['h13', 'v13', 'h31', 'v31']): ''' Plots at every iteration GP. Requires some manual hardcoded interaction ''' reader = np.asmatrix(np.loadtxt(filename)) xy_observed = np.asarray(reader[:, 0:2]) # Hardcoded!!!!!!!!! f_observed = np.asarray(reader[:, -1]) # Hardcoded!!!!!!!!! n_rows = math.ceil(len(f_observed) / 5) + 1 f_mean, sub_mean = plt.subplots(n_rows, 5, sharex=True, sharey=True) f_mean.tight_layout() # to adjust spacing between subplots f_sigma, sub_sigma = plt.subplots(n_rows, 5, sharex=True, sharey=True) f_sigma.tight_layout() # to adjust spacing between subplots f_acq, sub_acq = plt.subplots(n_rows, 5, sharex=True, sharey=True) f_acq.tight_layout() # to adjust spacing between subplots num_points = 100 XY_grid = np.mgrid[-10:10:0.3, -10:10:0.3].reshape(2, -1).T # Hardcoded!!!!!!!!! for i in range(n_rows - 1): j = 0 while len(f_observed) > 5 * i + j and j < 5: XY = xy_observed[0:(5 * i + j + 1)] Z = f_observed[0:(5 * i + j + 1)] mean, Cov, variance, m = GP_analysis(XY, Z, XY_grid) xx = np.asarray(XY_grid[:, 0]) yy = np.asarray(XY_grid[:, 1]) xo = np.asarray(XY[:, 0]).reshape(-1) yo = np.asarray(XY[:, 1]).reshape(-1) sub_mean[i, j].scatter(xx, yy, c=mean.T[0], vmin=min(mean.T[0]), vmax=max(mean.T[0]), edgecolors='none', cmap='GnBu') sub_mean[i, j].scatter(xo, yo, c='k', marker='s') sub_sigma[i, j].scatter(xx, yy, c=variance, vmin=min(variance), vmax=max(variance), edgecolors='none') sub_sigma[i, j].scatter(xo, yo, c='white') model = GPModel(optimize_restarts=1, verbose=True) model.model = m space = Design_space([{ 'name': 'var1', 'type': 'continuous', 'domain': (-10, 10) }, { 'name': 'var2', 'type': 'continuous', 'domain': (-10, 10) }]) acq = AcquisitionLCB(model, space, exploration_weight=1) # Hardcoded!!!!!!!!! alpha_full = acq.acquisition_function(XY_grid) sub_acq[i, j].scatter(xx, yy, c=alpha_full.T[0], vmin=min(alpha_full.T[0]), vmax=max(alpha_full.T[0]), edgecolors='none', cmap='GnBu') sub_acq[i, j].scatter(xo, yo, c='k', marker='s') minXY = XY_grid[np.argmin(alpha_full)] sub_acq[i, j].scatter(minXY[0], minXY[1], marker='P') j = j + 1 timestamp = (datetime.datetime.now()).strftime("%m-%d_%H-%M-%S") f_mean.subplots_adjust(wspace=0.3, top=None, bottom=None) f_mean.savefig(f'GP_results/dis_mean_M1_M2-{timestamp}.pdf') f_sigma.subplots_adjust(wspace=0.3, top=None, bottom=None) f_sigma.savefig(f'GP_results/dis_sigma_M1_M2-{timestamp}.pdf') f_acq.subplots_adjust(wspace=0.3, top=None, bottom=None) f_acq.savefig(f'GP_results/dis_acq_M1_M2-{timestamp}.pdf') #plt.show() plt.close()