From 80a327908753636bfb22cb2785e52b2061fe83c8 Mon Sep 17 00:00:00 2001 From: IHIaadj Date: Tue, 20 Jun 2023 08:09:13 +0200 Subject: [PATCH] complete bayesian optimization --- analogainas/search_algorithms/bo.py | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/analogainas/search_algorithms/bo.py b/analogainas/search_algorithms/bo.py index 02204446..8d7bfc2b 100644 --- a/analogainas/search_algorithms/bo.py +++ b/analogainas/search_algorithms/bo.py @@ -22,8 +22,46 @@ def acquisition(self, X, Xsamples, model): probs = (mu - best) / (std+1E-9) return probs + def maximize(self): + for _ in range(self.n_iter): + x_next = self.propose_next_point() + y_next = self.evaluate_func(x_next) + + self.X.append(x_next) + self.y.append(y_next) + + best_idx = np.argmax(self.y) + best_x = self.X[best_idx] + best_y = self.y[best_idx] + return best_x, best_y + + def propose_next_point(self): + x_candidates = self.random_state.uniform( + low=self.search_space[:, 0], + high=self.search_space[:, 1], + size=(100, self.search_space.shape[0]) + ) + + best_x = None + best_acquisition = float('-inf') + + for x in x_candidates: + acquisition = self.acquisition(x) + if acquisition > best_acquisition: + best_x = x + best_acquisition = acquisition + + return best_x + + def gaussian_process_regression(self): + # Define your surrogate model (Gaussian Process) and fit it to the data + # Example: Mean of 0, Standard Deviation of 1 + mean = 0.0 + std = 1.0 + return mean, std + # optimize the acquisition function - def bayesian_search(self, X, y, model): + def run(self, X, y, model): # random search, generate random samples Xsamples = self.rs_search(100) Xsamples = Xsamples.reshape(len(Xsamples), 1)