From c5f00a40695f910225b6a4f964579a6fb3e2dd2f Mon Sep 17 00:00:00 2001 From: Niko Date: Tue, 30 May 2023 15:49:49 +0200 Subject: [PATCH] added random, regular, max acquisition & improvement metric --- .../BayesianOptimization.py | 4 ++- .../active_bo_ros/active_bo_topic.py | 35 +++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/active_bo_ros/active_bo_ros/BayesianOptimization/BayesianOptimization.py b/src/active_bo_ros/active_bo_ros/BayesianOptimization/BayesianOptimization.py index 72a456e..9780090 100644 --- a/src/active_bo_ros/active_bo_ros/BayesianOptimization/BayesianOptimization.py +++ b/src/active_bo_ros/active_bo_ros/BayesianOptimization/BayesianOptimization.py @@ -86,7 +86,7 @@ class BayesianOptimization: self.Y[i] = reward - self.GP = GaussianProcessRegressor(Matern(nu=1.5)) + self.GP = GaussianProcessRegressor(Matern(nu=1.5, length_scale_bounds=(1e-8, 1e5)), n_restarts_optimizer=5, ) self.GP.fit(self.X, self.Y) def next_observation(self): @@ -147,6 +147,8 @@ class BayesianOptimization: self.X = np.vstack((self.X, x_new)) self.Y = np.vstack((self.Y, reward)) + self.GP.fit(self.X, self.Y) + if self.episode == 0: self.best_reward[0] = max(self.Y) else: diff --git a/src/active_bo_ros/active_bo_ros/active_bo_topic.py b/src/active_bo_ros/active_bo_ros/active_bo_topic.py index a996e32..d66e77d 100644 --- a/src/active_bo_ros/active_bo_ros/active_bo_topic.py +++ b/src/active_bo_ros/active_bo_ros/active_bo_topic.py @@ -132,12 +132,20 @@ class ActiveBOTopic(Node): def active_rl_callback(self, msg): if self.active_rl_pending: - self.get_logger().info('Active Reinforcement Learning response pending!') - self.active_rl_pending = False + self.get_logger().info('Active Reinforcement Learning response received!') self.rl_weights = msg.weights self.rl_final_step = msg.final_step self.rl_reward = msg.reward + try: + self.BO.add_new_observation(self.rl_reward, self.rl_weights) + self.get_logger().info('Active Reinforcement Learning added new observation!') + except Exception as e: + self.get_logger().error(f'Active Reinforcement Learning failed to add new observation: {e}') + + self.active_rl_pending = False + self.reset_rl_response() + def mainloop_callback(self): if self.active_bo_pending: @@ -181,18 +189,26 @@ class ActiveBOTopic(Node): else: if self.active_rl_pending: pass - elif self.rl_weights is not None and not self.active_rl_pending: - try: - self.BO.add_new_observation(self.rl_reward, self.rl_weights) - self.reset_rl_response() - except Exception as e: - self.get_logger().error(f'Active Reinforcement Learning failed to add new observation: {e}') else: if self.current_episode < self.bo_episodes: # metrics - if self.bo_metric == "RandomQuery": + if self.bo_metric == "random": user_query = RandomQuery(self.bo_metric_parameter) + elif self.bo_metric == "regular": + user_query = RegularQuery(self.bo_metric_parameter) + + elif self.bo_metric == "max acquisition": + user_query = MaxAcqQuery(self.bo_metric_parameter, + self.BO.GP, + 100, + self.bo_nr_weights, + acq=self.bo_acq_fcn, + X=self.BO.X) + + elif self.bo_metric == "improvement": + user_query = ImprovementQuery(self.bo_metric_parameter, 10) + else: raise NotImplementedError @@ -234,7 +250,6 @@ class ActiveBOTopic(Node): self.get_logger().info(f'Current Run: {self.current_run}') - def main(args=None): rclpy.init(args=args)