class NullOptimizerTest(OptimalLearningTestCase): """Test the NullOptimizer on a simple objective. NullOptimizer should do nothing. Multistarting it should be the same as a 'dumb' search over points. """ @T.class_setup def base_setup(self): """Set up a test case for optimizing a simple quadratic polynomial.""" self.dim = 3 domain_bounds = [ClosedInterval(-1.0, 1.0)] * self.dim self.domain = TensorProductDomain(domain_bounds) maxima_point = numpy.full(self.dim, 0.5) current_point = numpy.zeros(self.dim) self.polynomial = QuadraticFunction(maxima_point, current_point) self.null_optimizer = NullOptimizer(self.domain, self.polynomial) def test_null_optimizer(self): """Test that null optimizer does not change current_point.""" current_point_old = self.null_optimizer.objective_function.current_point self.null_optimizer.optimize() current_point_new = self.null_optimizer.objective_function.current_point self.assert_vector_within_relative(current_point_old, current_point_new, 0.0) def test_multistarted_null_optimizer(self): """Test that multistarting null optimizer just evalutes the function and indentifies the max.""" num_points = 15 points = self.domain.generate_uniform_random_points_in_domain( num_points) truth = numpy.empty(num_points) for i, point in enumerate(points): self.null_optimizer.objective_function.current_point = point truth[ i] = self.null_optimizer.objective_function.compute_objective_function( ) best_index = numpy.argmax(truth) truth_best_point = points[best_index, ...] test_best_point, test_values = multistart_optimize( self.null_optimizer, starting_points=points) self.assert_vector_within_relative(test_best_point, truth_best_point, 0.0) self.assert_vector_within_relative(test_values, truth, 0.0)
class NullOptimizerTest(OptimalLearningTestCase): """Test the NullOptimizer on a simple objective. NullOptimizer should do nothing. Multistarting it should be the same as a 'dumb' search over points. """ @T.class_setup def base_setup(self): """Set up a test case for optimizing a simple quadratic polynomial.""" self.dim = 3 domain_bounds = [ClosedInterval(-1.0, 1.0)] * self.dim self.domain = TensorProductDomain(domain_bounds) maxima_point = numpy.full(self.dim, 0.5) current_point = numpy.zeros(self.dim) self.polynomial = QuadraticFunction(maxima_point, current_point) self.null_optimizer = NullOptimizer(self.domain, self.polynomial) def test_null_optimizer(self): """Test that null optimizer does not change current_point.""" current_point_old = self.null_optimizer.objective_function.current_point self.null_optimizer.optimize() current_point_new = self.null_optimizer.objective_function.current_point self.assert_vector_within_relative(current_point_old, current_point_new, 0.0) def test_multistarted_null_optimizer(self): """Test that multistarting null optimizer just evalutes the function and indentifies the max.""" num_points = 15 points = self.domain.generate_uniform_random_points_in_domain(num_points) truth = numpy.empty(num_points) for i, point in enumerate(points): self.null_optimizer.objective_function.current_point = point truth[i] = self.null_optimizer.objective_function.compute_objective_function() best_index = numpy.argmax(truth) truth_best_point = points[best_index, ...] test_best_point, test_values = multistart_optimize(self.null_optimizer, starting_points=points) self.assert_vector_within_relative(test_best_point, truth_best_point, 0.0) self.assert_vector_within_relative(test_values, truth, 0.0)