Skip to content

Commit

Permalink
No noise HO
Browse files Browse the repository at this point in the history
  • Loading branch information
TadaSanar committed Apr 14, 2023
1 parent 80bcd23 commit 2c1cc69
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
35 changes: 22 additions & 13 deletions hper_bo.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,13 @@ def determine_data_fusion_points(data_fusion_XZ_accum,
# Pick new points for which the surrogate model has a high gradient, no
# matter if there is an earlier data fusion point nearby.
new_df_points_x_g = x_next[k][grad_max_s_next > gradient_limit]


print('\nGradients of points surviving gradient limit:\n',
grad_max_s_next[grad_max_s_next > gradient_limit])

print('\nGradients of points not surviving gradient limit:\n',
grad_max_s_next[grad_max_s_next < gradient_limit])

# THIS OPTION IS NOT IN USE.
# Pick new points for which the surrogate model does not have
# a high gradient but they are located far away from the
Expand Down Expand Up @@ -691,7 +697,8 @@ def bo_sim_target(bo_ground_truth_model_path='./Source_data/C2a_GPR_model_with_u
init_points=None, batch_size=1,
acquisition_function='EI', acq_fun_params=None,
df_data_coll_params=None, no_plots=False,
results_folder='./Results/'):
results_folder='./Results/', noise_df = False,
noise_target = False):
'''
Simulates a Bayesian Optimization cycle using the Gaussian Process
Regression model given in bo_ground_truth_model_path as the ground truth for
Expand Down Expand Up @@ -878,9 +885,9 @@ def bo_sim_target(bo_ground_truth_model_path='./Source_data/C2a_GPR_model_with_u

if df_data_coll_params is not None:

#print('Data fusion requests round ' +
# str(k-1) + ':', data_fusion_x_next[k-1])
#print('Target requests round ' + str(k-1) + ':', x_next[k-1])
print('Data fusion requests round ' +
str(k-1) + ':', data_fusion_x_next[k-1])
print('Target requests round ' + str(k-1) + ':', x_next[k-1])

# Do data fusion.
if data_fusion_XZ_rounds[k] is None:
Expand All @@ -890,7 +897,7 @@ def bo_sim_target(bo_ground_truth_model_path='./Source_data/C2a_GPR_model_with_u
data_fusion_XZ_rounds = query_data_fusion_XZ_this_round(
data_fusion_XZ_rounds, data_fusion_x_next,
df_data_coll_params, acq_fun_params, k,
data_fusion_gt_model, noise=False)
data_fusion_gt_model, noise = noise_df)
# TO DO: Add noise to the simulated prediction! Confirm proper
# scaling in GP once more. Divide the function so that
# experimental treatment is fully before BO loop and simulated
Expand Down Expand Up @@ -918,12 +925,10 @@ def bo_sim_target(bo_ground_truth_model_path='./Source_data/C2a_GPR_model_with_u
# and update X_rounds, Y_rounds, X_accum, Y_accum in place.
X_rounds, Y_rounds, X_accum, Y_accum = query_target_data_from_model(
k, X_rounds, Y_rounds, X_accum, Y_accum, init_points, x_next,
stability_model, rounds, materials, noise=False)

#print('Target data round ' + str(k) + ':', X_accum[k], Y_accum[k])

#print('X_step and Y_step for round ' + str(k) + ':', X_step[k], Y_step[k])
stability_model, rounds, materials, noise = noise_target)

print('Target data round ' + str(k) + ':', X_accum[k], Y_accum[k])

# Define and fit BO object.
# f=None because this code will be adapted in future for experimental
# BO cycle.
Expand All @@ -937,7 +942,11 @@ def bo_sim_target(bo_ground_truth_model_path='./Source_data/C2a_GPR_model_with_u
evaluator_type='local_penalization',
batch_size=batch_size,
acquisition_jitter=jitter,
acq_fun_params=acq_fun_params
acq_fun_params=acq_fun_params,
noise_var = 0.1,
optimize_restarts = 10,
max_iters = 10000,
exact_feval = (not noise_target)
)

# Suggest next points (samples to prepare).
Expand All @@ -960,7 +969,7 @@ def bo_sim_target(bo_ground_truth_model_path='./Source_data/C2a_GPR_model_with_u
current_surrogate_model_params = {'lengthscale': lengthscales[k],
'variance': variances[k],
'max_gradient': max_gradients[k]}

if df_data_coll_params is not None:

# Do data fusion.
Expand Down
18 changes: 11 additions & 7 deletions hper_repetitions_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def modify_filename_nreps(filename, new_value, param_to_modify_str = '_nreps'):

def repeated_tests(m):

c_eig = [0.1, 1, 2, 5] # Expected information gain.
c_eig = [0.05, 0.1, 1, 2, 5, 8, 10] # Expected information gain.
c_exclz = [1, 5, 10, 20] # Size of the exclusion zone in percentage points (max. 100)
c_g = [0.063, 0.253, 0.674, 1.282, 1.96]#list(cg(np.array([0, 0.01, 0.05, 0.2, 0.5, 0.8, 0.95, 0.99, 1]))) # Gradient limit. 0.05#, 0.07, 0.1, 0.2, 0.5, 0.75
c_g = [0, 0.063, 0.253, 0.674, 1.282, 1.96]#list(cg(np.array([0, 0.01, 0.05, 0.2, 0.5, 0.8, 0.95, 0.99, 1]))) # Gradient limit. 0.05#, 0.07, 0.1, 0.2, 0.5, 0.75

hyperparams_eig = []
hyperparams_exclz = []
Expand All @@ -148,11 +148,11 @@ def repeated_tests(m):

hyperparams_eig.append((c_g[i], c_eig[j]))

folder = './Results/20230413-jitter01-noiseless/'
folder = './Results/20230414-jitter01-noiseless-HO/'
ground_truth = [0.17, 0.03, 0.80] # From C2a paper

bo_params = {'n_repetitions': 25,
'n_rounds': 100,
'n_rounds': 15,
'n_init': 2,
'batch_size': 1,
'materials': ['CsPbI', 'MAPbI', 'FAPbI']
Expand All @@ -162,8 +162,11 @@ def repeated_tests(m):
fetch_old_results = False
# Give False if you don't want to save the figures.
save_figs = True
# Choose if noisy queries are being used or exact.
noise_df= False
noise_target = False

if (m > 1): # TO DO change
if (m > -1):

if m == 0:

Expand Down Expand Up @@ -298,7 +301,8 @@ def repeated_tests(m):
acquisition_function = acquisition_function,
acq_fun_params = afp,
df_data_coll_params = ddcp,
no_plots=no_plots, results_folder = triangle_folder)
no_plots=no_plots, results_folder = triangle_folder,
noise_df = noise_df, noise_target = noise_target)

# All ouputs saved only from the first two repetitions to save
# disk space.
Expand Down Expand Up @@ -486,7 +490,7 @@ def repeated_tests(m):

print(os.getcwd())

m_total = 42
m_total = 68

###############################################################################
# get number of cpus available to job
Expand Down
16 changes: 10 additions & 6 deletions hper_repetitions_parallel_temp_noiseless.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def modify_filename_nreps(filename, new_value, param_to_modify_str = '_nreps'):

def repeated_tests(m):

c_eig = [0.1, 1, 2, 5] # Expected information gain.
c_eig = [0.05, 0.1, 1, 2, 5, 8, 10] # Expected information gain.
c_exclz = [1, 5, 10, 20] # Size of the exclusion zone in percentage points (max. 100)
c_g = [0.063, 0.253, 0.674, 1.282, 1.96]#list(cg(np.array([0, 0.01, 0.05, 0.2, 0.5, 0.8, 0.95, 0.99, 1]))) # Gradient limit. 0.05#, 0.07, 0.1, 0.2, 0.5, 0.75
c_g = [0, 0.063, 0.253, 0.674, 1.282, 1.96]#list(cg(np.array([0, 0.01, 0.05, 0.2, 0.5, 0.8, 0.95, 0.99, 1]))) # Gradient limit. 0.05#, 0.07, 0.1, 0.2, 0.5, 0.75

hyperparams_eig = []
hyperparams_exclz = []
Expand All @@ -148,11 +148,11 @@ def repeated_tests(m):

hyperparams_eig.append((c_g[i], c_eig[j]))

folder = './Results/20230413-jitter001-noiseless/'
folder = './Results/20230414-jitter01-noiseless-HO/'
ground_truth = [0.17, 0.03, 0.80] # From C2a paper

bo_params = {'n_repetitions': 25,
'n_rounds': 100,
'n_rounds': 15,
'n_init': 2,
'batch_size': 1,
'materials': ['CsPbI', 'MAPbI', 'FAPbI']
Expand All @@ -162,6 +162,9 @@ def repeated_tests(m):
fetch_old_results = False
# Give False if you don't want to save the figures.
save_figs = True
# Choose if noisy queries are being used or exact.
noise_df= False
noise_target = False

if (m > -1):

Expand Down Expand Up @@ -298,7 +301,8 @@ def repeated_tests(m):
acquisition_function = acquisition_function,
acq_fun_params = afp,
df_data_coll_params = ddcp,
no_plots=no_plots, results_folder = triangle_folder)
no_plots=no_plots, results_folder = triangle_folder,
noise_df = noise_df, noise_target = noise_target)

# All ouputs saved only from the first two repetitions to save
# disk space.
Expand Down Expand Up @@ -486,7 +490,7 @@ def repeated_tests(m):

print(os.getcwd())

m_total = 42
m_total = 68

###############################################################################
# get number of cpus available to job
Expand Down

0 comments on commit 2c1cc69

Please sign in to comment.