def predict( self, parameter_values_pandas_frame, t=None, context_values_pandas_frame=None, objective_name=None) -> Prediction: # pylint: disable=unused-argument # TODO: make this streaming and/or using arrow. # if context_values_pandas_frame is not None: raise NotImplementedError( "Context not currently supported on remote optimizers") feature_values_dict = parameter_values_pandas_frame.to_dict( orient='list') prediction_request = OptimizerMonitoringService_pb2.PredictRequest( OptimizerHandle=self. optimizer_handle_for_optimizer_monitoring_service, Features=OptimizerMonitoringService_pb2.Features( FeaturesJsonString=json.dumps(feature_values_dict))) prediction_response = self._optimizer_monitoring_stub.Predict( prediction_request) # To be compliant with the OptimizerBase, we need to recover a single Prediction object and return it. # objective_predictions_pb2 = prediction_response.ObjectivePredictions assert len(objective_predictions_pb2) == 1 only_prediction_pb2 = objective_predictions_pb2[0] objective_name = only_prediction_pb2.ObjectiveName valid_predictions_df = Prediction.dataframe_from_json( only_prediction_pb2.PredictionDataFrameJsonString) prediction = Prediction.create_prediction_from_dataframe( objective_name=objective_name, dataframe=valid_predictions_df) return prediction
def encode_optimization_problem( optimization_problem: OptimizationProblem ) -> OptimizerMonitoringService_pb2.OptimizationProblem: return OptimizerMonitoringService_pb2.OptimizationProblem( ParameterSpace=OptimizerMonitoringServiceEncoder.encode_hypergrid( optimization_problem.parameter_space), ObjectiveSpace=OptimizerMonitoringServiceEncoder.encode_hypergrid( optimization_problem.objective_space), Objectives=[ OptimizerMonitoringService_pb2.Objective( Name=objective.name, Minimize=objective.minimize) for objective in optimization_problem.objectives ], ContextSpace=None if optimization_problem.context_space is None else OptimizerMonitoringServiceEncoder.encode_hypergrid( optimization_problem.context_space))
def encode_empty_dimension( dimension: EmptyDimension ) -> OptimizerMonitoringService_pb2.EmptyDimension: assert isinstance(dimension, EmptyDimension) return OptimizerMonitoringService_pb2.EmptyDimension( Name=dimension.name, DimensionType=OptimizerMonitoringServiceEncoder. dimension_types_to_pb2_types[dimension.type])
def encode_subgrid( subgrid: SimpleHypergrid.JoinedSubgrid ) -> OptimizerMonitoringService_pb2.GuestSubgrid: assert isinstance(subgrid, SimpleHypergrid.JoinedSubgrid) return OptimizerMonitoringService_pb2.GuestSubgrid( Subgrid=OptimizerMonitoringServiceEncoder.encode_hypergrid( subgrid.subgrid), ExternalPivotDimension=OptimizerMonitoringServiceEncoder. encode_dimension(subgrid.join_dimension))
def encode_categorical_dimension( dimension: CategoricalDimension ) -> OptimizerMonitoringService_pb2.CategoricalDimension: assert isinstance(dimension, CategoricalDimension) return OptimizerMonitoringService_pb2.CategoricalDimension( Name=dimension.name, Values=[ OptimizerMonitoringServiceEncoder.encode_primitive_value(value) for value in dimension.values ])
def encode_continuous_dimension( dimension: ContinuousDimension ) -> OptimizerMonitoringService_pb2.ContinuousDimension: assert isinstance(dimension, ContinuousDimension) return OptimizerMonitoringService_pb2.ContinuousDimension( Name=dimension.name, Min=dimension.min, Max=dimension.max, IncludeMin=dimension.include_min, IncludeMax=dimension.include_max)
def encode_primitive_value( value: Union[int, float, bool, str] ) -> OptimizerMonitoringService_pb2.PrimitiveValue: assert isinstance(value, (int, float, bool, str)) if isinstance(value, bool): return OptimizerMonitoringService_pb2.PrimitiveValue( BoolValue=value) if isinstance(value, int): return OptimizerMonitoringService_pb2.PrimitiveValue( IntValue=value) if isinstance(value, float): return OptimizerMonitoringService_pb2.PrimitiveValue( DoubleValue=value) if isinstance(value, str): return OptimizerMonitoringService_pb2.PrimitiveValue( StringValue=value) raise TypeError( f"{value} is of type: {type(value)} but must be one of (int, float, bool, str)" )
def encode_ordinal_dimension( dimension: OrdinalDimension ) -> OptimizerMonitoringService_pb2.OrdinalDimension: assert isinstance(dimension, OrdinalDimension) return OptimizerMonitoringService_pb2.OrdinalDimension( Name=dimension.name, Ascending=dimension.ascending, OrderedValues=[ OptimizerMonitoringServiceEncoder.encode_primitive_value(value) for value in dimension.values ])
def encode_dimension( dimension: Dimension) -> OptimizerMonitoringService_pb2.Dimension: if isinstance(dimension, EmptyDimension): return OptimizerMonitoringService_pb2.Dimension( EmptyDimension=OptimizerMonitoringServiceEncoder. encode_empty_dimension(dimension)) if isinstance(dimension, ContinuousDimension): return OptimizerMonitoringService_pb2.Dimension( ContinuousDimension=OptimizerMonitoringServiceEncoder. encode_continuous_dimension(dimension)) if isinstance(dimension, DiscreteDimension): return OptimizerMonitoringService_pb2.Dimension( DiscreteDimension=OptimizerMonitoringServiceEncoder. encode_discrete_dimension(dimension)) if isinstance(dimension, OrdinalDimension): return OptimizerMonitoringService_pb2.Dimension( OrdinalDimension=OptimizerMonitoringServiceEncoder. encode_ordinal_dimension(dimension)) if isinstance(dimension, CategoricalDimension): return OptimizerMonitoringService_pb2.Dimension( CategoricalDimension=OptimizerMonitoringServiceEncoder. encode_categorical_dimension(dimension)) if isinstance(dimension, CompositeDimension): return OptimizerMonitoringService_pb2.Dimension( CompositeDimension=OptimizerMonitoringServiceEncoder. encode_composite_dimension(dimension)) raise TypeError(f"Unsupported dimension type: {type(dimension)}")
def encode_composite_dimension( dimension: CompositeDimension ) -> OptimizerMonitoringService_pb2.CompositeDimension: assert isinstance(dimension, CompositeDimension) encoded_chunks = [] for chunk in dimension.enumerate_chunks(): if dimension.chunks_type is ContinuousDimension: encoded_chunks.append( OptimizerMonitoringService_pb2.Dimension( ContinuousDimension=OptimizerMonitoringServiceEncoder. encode_continuous_dimension(chunk))) elif dimension.chunks_type is DiscreteDimension: encoded_chunks.append( OptimizerMonitoringService_pb2.Dimension( DiscreteDimension=OptimizerMonitoringServiceEncoder. encode_discrete_dimension(chunk))) elif dimension.chunks_type is OrdinalDimension: encoded_chunks.append( OptimizerMonitoringService_pb2.Dimension( OrdinalDimension=OptimizerMonitoringServiceEncoder. encode_ordinal_dimension(chunk))) elif dimension.chunks_type is CategoricalDimension: encoded_chunks.append( OptimizerMonitoringService_pb2.Dimension( CategoricalDimension=OptimizerMonitoringServiceEncoder. encode_categorical_dimension(chunk))) else: raise TypeError( f"Unsupported chunk type: {dimension.chunks_type.__name__}" ) return OptimizerMonitoringService_pb2.CompositeDimension( Name=dimension.name, ChunkType=OptimizerMonitoringServiceEncoder. dimension_types_to_pb2_types[dimension.chunks_type], Chunks=encoded_chunks)
def encode_hypergrid( hypergrid: SimpleHypergrid ) -> OptimizerMonitoringService_pb2.SimpleHypergrid: assert isinstance(hypergrid, SimpleHypergrid) encoded_subgrids = [] for _, subgrids in hypergrid.joined_subgrids_by_pivot_dimension.items( ): for subgrid in subgrids: encoded_subgrid = OptimizerMonitoringServiceEncoder.encode_subgrid( subgrid) encoded_subgrids.append(encoded_subgrid) return OptimizerMonitoringService_pb2.SimpleHypergrid( Name=hypergrid.name, Dimensions=[ OptimizerMonitoringServiceEncoder.encode_dimension(dimension) for dimension in hypergrid.root_dimensions ], GuestSubgrids=encoded_subgrids)
def encode_discrete_dimension( dimension: DiscreteDimension ) -> OptimizerMonitoringService_pb2.DiscreteDimension: assert isinstance(dimension, DiscreteDimension) return OptimizerMonitoringService_pb2.DiscreteDimension( Name=dimension.name, Min=dimension.min, Max=dimension.max)
def optimizer_handle_for_optimizer_monitoring_service(self): return OptimizerMonitoringService_pb2.OptimizerHandle(Id=self.id)