@@ -75,25 +75,19 @@ def _generate_mesh(lb, ub, num_points=10):
7575 return points
7676
7777
78- def _update_gp_and_eval_var ( all_x , all_y , x_for_var , test_points , persis_info ):
78+ def _eval_var ( my_gp , all_x , all_y , x_for_var , test_points , persis_info ):
7979 """
80- Update the GP using the points in all_x and their function values in
81- all_y. (We are assuming deterministic values in all_y, so we set the noise
82- to be 1e-8 when build the GP.) Then evaluates the posterior covariance at
83- points in x_for_var. If we have test points, calculate mean square error
84- at those points.
80+ Evaluate the posterior covariance at points in x_for_var.
81+ If we have test points, calculate mean square error at those points.
8582 """
86- my_gp2S = GP (all_x , all_y , noise_variances = 1e-12 * np .ones (len (all_y )))
87- my_gp2S .train ()
88-
8983 # Obtain covariance in groups to prevent memory overload.
9084 n_rows = x_for_var .shape [0 ]
9185 var_vals = []
9286 group_size = 1000
9387
9488 for start_idx in range (0 , n_rows , group_size ):
9589 end_idx = min (start_idx + group_size , n_rows )
96- var_vals_group = my_gp2S .posterior_covariance (x_for_var [start_idx :end_idx ], variance_only = True )["v(x)" ]
90+ var_vals_group = my_gp .posterior_covariance (x_for_var [start_idx :end_idx ], variance_only = True )["v(x)" ]
9791 var_vals .extend (var_vals_group )
9892
9993 assert len (var_vals ) == n_rows , "Something wrong with the grouping"
@@ -102,13 +96,14 @@ def _update_gp_and_eval_var(all_x, all_y, x_for_var, test_points, persis_info):
10296 persis_info .setdefault ("mean_variance" , []).append (np .mean (var_vals ))
10397
10498 if test_points is not None :
105- f_est = my_gp2S .posterior_mean (test_points ["x" ])["f(x)" ]
99+ f_est = my_gp .posterior_mean (test_points ["x" ])["f(x)" ]
106100 mse = np .mean ((f_est - test_points ["f" ]) ** 2 )
107101 persis_info .setdefault ("mean_squared_error" , []).append (mse )
102+
108103 return np .array (var_vals )
109104
110105
111- def calculate_grid_distances (lb , ub , num_points ):
106+ def _calculate_grid_distances (lb , ub , num_points ):
112107 """Calculate minimum and maximum distances between points in grid"""
113108 num_points = [num_points ] * len (lb )
114109 spacings = [(ub [i ] - lb [i ]) / (num_points [i ] - 1 ) for i in range (len (lb ))]
@@ -117,7 +112,7 @@ def calculate_grid_distances(lb, ub, num_points):
117112 return min_distance , max_distance
118113
119114
120- def is_point_far_enough (point , eligible_points , r ):
115+ def _is_point_far_enough (point , eligible_points , r ):
121116 """Check if point is at least r distance away from all points in eligible_points."""
122117 for ep in eligible_points :
123118 if np .linalg .norm (point - ep ) < r :
@@ -138,7 +133,7 @@ def _find_eligible_points(x_for_var, sorted_indices, r, batch_size):
138133 eligible_points = []
139134 for idx in sorted_indices :
140135 point = x_for_var [idx ]
141- if is_point_far_enough (point , eligible_points , r ):
136+ if _is_point_far_enough (point , eligible_points , r ):
142137 eligible_points .append (point )
143138 if len (eligible_points ) == batch_size :
144139 break
@@ -156,29 +151,32 @@ def persistent_gpCAM_simple(H_in, persis_info, gen_specs, libE_info):
156151 `test_gpCAM.py <https://github.com/Libensemble/libensemble/blob/develop/libensemble/tests/regression_tests/test_gpCAM.py>`_
157152 """ # noqa
158153 U = gen_specs ["user" ]
154+ my_gp = None
155+ noise = 1e-12
159156
160157 test_points = _read_testpoints (U )
161158
162159 batch_size , n , lb , ub , all_x , all_y , ps = _initialize_gpcAM (U , libE_info )
163160
164161 # Send batches until manager sends stop tag
165162 tag = None
166- persis_info [ "max_variance" ] = []
163+ var_vals = None
167164
168165 if U .get ("use_grid" ):
169166 num_points = 10
170167 x_for_var = _generate_mesh (lb , ub , num_points )
171- r_low_init , r_high_init = calculate_grid_distances (lb , ub , num_points )
168+ r_low_init , r_high_init = _calculate_grid_distances (lb , ub , num_points )
169+ else :
170+ x_for_var = persis_info ["rand_stream" ].uniform (lb , ub , (10 * batch_size , n ))
172171
173172 while tag not in [STOP_TAG , PERSIS_STOP ]:
174173 if all_x .shape [0 ] == 0 :
175174 x_new = persis_info ["rand_stream" ].uniform (lb , ub , (batch_size , n ))
176175 else :
177176 if not U .get ("use_grid" ):
178177 x_for_var = persis_info ["rand_stream" ].uniform (lb , ub , (10 * batch_size , n ))
179- var_vals = _update_gp_and_eval_var (all_x , all_y , x_for_var , test_points , persis_info )
180-
181- if U .get ("use_grid" ):
178+ x_new = x_for_var [np .argsort (var_vals )[- batch_size :]]
179+ else :
182180 r_high = r_high_init
183181 r_low = r_low_init
184182 x_new = []
@@ -190,13 +188,12 @@ def persistent_gpCAM_simple(H_in, persis_info, gen_specs, libE_info):
190188 if len (x_new ) < batch_size :
191189 r_high = r_cand
192190 r_cand = (r_high + r_low ) / 2.0
193- else :
194- x_new = x_for_var [np .argsort (var_vals )[- batch_size :]]
195191
196192 H_o = np .zeros (batch_size , dtype = gen_specs ["out" ])
197193 H_o ["x" ] = x_new
198194 tag , Work , calc_in = ps .send_recv (H_o )
199195
196+ # This works with or without final_gen_send
200197 if calc_in is not None :
201198 y_new = np .atleast_2d (calc_in ["f" ]).T
202199 nan_indices = [i for i , fval in enumerate (y_new ) if np .isnan (fval )]
@@ -205,12 +202,15 @@ def persistent_gpCAM_simple(H_in, persis_info, gen_specs, libE_info):
205202 all_x = np .vstack ((all_x , x_new ))
206203 all_y = np .vstack ((all_y , y_new ))
207204
208- # If final points are sent with PERSIS_STOP, update model and get final var_vals
209- if calc_in is not None :
210- # H_o not updated by default - is persis_info
211- if not U .get ("use_grid" ):
212- x_for_var = persis_info ["rand_stream" ].uniform (lb , ub , (10 * batch_size , n ))
213- var_vals = _update_gp_and_eval_var (all_x , all_y , x_for_var , test_points , persis_info )
205+ if my_gp is None :
206+ my_gp = GP (all_x , all_y , noise_variances = noise * np .ones (len (all_y )))
207+ else :
208+ my_gp .tell (all_x , all_y , noise_variances = noise * np .ones (len (all_y )))
209+ my_gp .train ()
210+
211+ if not U .get ("use_grid" ):
212+ x_for_var = persis_info ["rand_stream" ].uniform (lb , ub , (10 * batch_size , n ))
213+ var_vals = _eval_var (my_gp , all_x , all_y , x_for_var , test_points , persis_info )
214214
215215 return H_o , persis_info , FINISHED_PERSISTENT_GEN_TAG
216216
@@ -242,15 +242,15 @@ def persistent_gpCAM_ask_tell(H_in, persis_info, gen_specs, libE_info):
242242
243243 if first_call :
244244 # Initialize GP
245- my_gp2S = GP (all_x , all_y , noise_variances = 1e-8 * np .ones (len (all_y )))
245+ my_gp = GP (all_x , all_y , noise_variances = 1e-8 * np .ones (len (all_y )))
246246 first_call = False
247247 else :
248- my_gp2S .tell (all_x , all_y , noise_variances = 1e-8 * np .ones (len (all_y )))
248+ my_gp .tell (all_x , all_y , noise_variances = 1e-8 * np .ones (len (all_y )))
249249
250- my_gp2S .train ()
250+ my_gp .train ()
251251
252252 start = time .time ()
253- x_new = my_gp2S .ask (
253+ x_new = my_gp .ask (
254254 bounds = np .column_stack ((lb , ub )),
255255 n = batch_size ,
256256 pop_size = batch_size ,
0 commit comments