def GetOptimizerConvergenceState(self, request, context): with self.exclusive_optimizer(optimizer_id=request.Id) as optimizer: serialized_convergence_state = serialize_to_bytes_string( optimizer.get_optimizer_convergence_state()) return OptimizerConvergenceState( OptimizerHandle=OptimizerHandle(Id=request.Id), SerializedOptimizerConvergenceState=serialized_convergence_state)
def GetOptimizerInfo(self, request: OptimizerHandle, context): # TODO: Learn about and leverage gRPC's error handling model for a case # TODO: when the handle is invalid. optimizer = self._optimizers_by_id[request.Id] return OptimizerInfo( OptimizerHandle=OptimizerHandle(Id=request.Id), OptimizerConfigJsonString=optimizer.optimizer_config.to_json(), OptimizationProblem=optimizer.optimization_problem.to_protobuf())
def GetOptimizerInfo(self, request: OptimizerHandle, context): # TODO: Learn about and leverage gRPC's error handling model for a case # TODO: when the handle is invalid. optimizer_id = request.Id optimizer = self._bayesian_optimizer_store.get_optimizer(optimizer_id) return OptimizerInfo( OptimizerHandle=OptimizerHandle(Id=request.Id), OptimizerConfigJsonString=optimizer.optimizer_config.to_json(), OptimizationProblem=OptimizerServiceEncoder. encode_optimization_problem(optimizer.optimization_problem))
def get_optimizer_by_id(self, optimizer_id): """Returns a proxy to an optimizer with a specified Id. :param optimizer_id: :return: """ optimizer_handle = OptimizerHandle(Id=optimizer_id) optimizer_info = self._optimizer_service_stub.GetOptimizerInfo(optimizer_handle) optimizer_proxy = self._optimizer_factory.connect_to_existing_remote_optimizer(optimizer_info) return optimizer_proxy
def ListExistingOptimizers(self, request: Empty, context): optimizers_info = [] for optimizer_id in self._ordered_ids: optimizer = self._optimizers_by_id[optimizer_id] optimizers_info.append(OptimizerInfo( OptimizerHandle=OptimizerHandle(Id=optimizer_id), OptimizerConfigJsonString=optimizer.optimizer_config.to_json(), OptimizationProblem=optimizer.optimization_problem.to_protobuf() )) return OptimizerList(Optimizers=optimizers_info)
def CreateOptimizer(self, request: CreateOptimizerRequest, context): # pylint: disable=unused-argument self.logger.info("Creating Optimizer") optimization_problem = OptimizerServiceDecoder.decode_optimization_problem( optimization_problem_pb2=request.OptimizationProblem) optimizer_config_json = request.OptimizerConfig if optimizer_config_json is not None and len( optimizer_config_json) > 0: optimizer_config = Point.from_json(optimizer_config_json) else: optimizer_config = bayesian_optimizer_config_store.default optimizer = BayesianOptimizer( optimization_problem=optimization_problem, optimizer_config=optimizer_config) optimizer_id = self._bayesian_optimizer_store.get_next_optimizer_id() self._bayesian_optimizer_store.add_optimizer(optimizer_id=optimizer_id, optimizer=optimizer) self.logger.info( f"Created optimizer {optimizer_id} with config: {optimizer.optimizer_config.to_json(indent=2)}" ) return OptimizerHandle(Id=optimizer_id)