def evaluate_point(self, point: Point) -> Point: # If evaluate_point is not implemented in the subclass, we can make it work like so: # point_df = point.to_dataframe() values_df = self.evaluate_dataframe(point_df) values_point = Point.from_dataframe(values_df) return values_point
def _unproject_point(self, point: Point) -> Point: """ Projects a given point from target hypergrid to adaptee hypergrid. If the subclass does not implement this method, we can do it automatically. The hand-written projection logic would likely be way more efficient, but we should also consider the programmer-time vs. cpu-time trade off. :param point: :return: """ original_dataframe = point.to_dataframe() unprojected_dataframe = self.unproject_dataframe(original_dataframe) unprojected_point = Point.from_dataframe(unprojected_dataframe) return unprojected_point
def suggest(self, random=False, context: Point = None): if self.optimization_problem.context_space is not None: if context is None: raise ValueError( "Context required by optimization problem but not provided." ) assert context in self.optimization_problem.context_space random = random or self.num_observed_samples < self.optimizer_config.min_samples_required_for_guided_design_of_experiments context_values = context.to_dataframe( ) if context is not None else None suggested_config = self.experiment_designer.suggest( random=random, context_values_dataframe=context_values) assert suggested_config in self.optimization_problem.parameter_space return suggested_config
def evaluate_point(self, point: Point) -> Point: y = self._polynomial_function.evaluate(point.to_dataframe().to_numpy()) return Point(y=y[0])
def evaluate_point(self, point: Point) -> Point: point = Point(**{dim_name: point[dim_name] for dim_name in self._parameter_space.dimension_names}) y = self._polynomial_function.evaluate(point.to_dataframe().to_numpy()) return Point(y=y[0])
def test_registering_multiple_objectives(self): input_space = SimpleHypergrid(name='input', dimensions=[ ContinuousDimension(name="x_1", min=0, max=10), ContinuousDimension(name="x_2", min=0, max=10) ]) output_space = SimpleHypergrid(name='output', dimensions=[ ContinuousDimension(name="y_1", min=0, max=10), ContinuousDimension(name="y_2", min=0, max=10) ]) optimization_problem = OptimizationProblem( parameter_space=input_space, objective_space=output_space, objectives=[Objective(name='y_1', minimize=True)]) optimizer = self.bayesian_optimizer_factory.create_local_optimizer( optimization_problem=optimization_problem) for _ in range(100): input = optimizer.suggest() output = Point(y_1=input.x_1, y_2=input.x_2) optimizer.register(input.to_dataframe(), output.to_dataframe()) num_predictions = 100 prediction = optimizer.predict( parameter_values_pandas_frame=optimization_problem.parameter_space. random_dataframe(num_predictions)) prediction_df = prediction.get_dataframe() assert len(prediction_df.index) == num_predictions # Let's test invalid observations. # input = input_space.random() input_df = input.to_dataframe() # We should only remember the valid dimensions. # output_with_extra_dimension = Point(y_1=input.x_1, y_2=input.x_2, invalid_dimension=42) output_with_extra_dimension_df = output_with_extra_dimension.to_dataframe( ) optimizer.register(input_df, output_with_extra_dimension_df) # Let's make sure that the invalid_dimension was not remembered. # all_inputs_df, all_outputs_df, _ = optimizer.get_all_observations() assert all(column in {'y_1', 'y_2'} for column in all_outputs_df.columns) # We should accept inputs with missing output dimensions, as long as at least one is specified. # output_with_missing_dimension = Point(y_1=input.x_1) output_with_missing_dimension_df = output_with_missing_dimension.to_dataframe( ) optimizer.register(input_df, output_with_missing_dimension_df) all_inputs_df, all_outputs_df, _ = optimizer.get_all_observations() # Let's make sure the missing dimension ends up being a null. # last_observation = all_outputs_df.iloc[[-1]] assert last_observation['y_2'].isnull().values.all() # Inserting an observation with no valid dimensions should fail. # empty_output = Point() empty_output_df = empty_output.to_dataframe() with pytest.raises(ValueError): optimizer.register(input_df, empty_output_df) only_invalid_outputs = Point(invalid_col1=0, invalid_col2=2) only_invalid_outputs_df = only_invalid_outputs.to_dataframe() with pytest.raises(ValueError): optimizer.register(input_df, only_invalid_outputs_df)