def cma_optimizer(func, ranges, sigma=0.5, **kwargs): """Applies the CMA optimizer upon a given function on the grid described in the ranges argument. The function used for performing optimization in the function available in the cma package. Args: func (function): The function to optimize using CMA. ranges (numpy array of numpy arrays): The parameter grid on which to optimize the function. sigma (float): The value for the sigma (the step size) Returns: float: The minimum of the function. """ # compute the minimum and the maximum of each dimension of the grid mins = list() maxs = list() for _range in ranges: mins.append(min(_range)) maxs.append(max(_range)) bound = (mins, maxs) x_0 = uniform_random_draw(1, ranges) evolution_strategy = cma.CMAEvolutionStrategy(x_0, sigma, {"bounds": bound}) evolution_strategy.optimize(func) return evolution_strategy.result.xbest
def test_uniform_random_draw_except(self): """ Tests that the uniform random draw works as expected when given a range 1D parametric space. """ number_of_parameters = 2 parameter_space = np.array([[[1, 2, 3], [4, 5, 6, 7], [8, 9, 10]]]) expected_result = np.array([[1, 4, 10], [2, 6, 8]]) actual_result = uniform_random_draw(number_of_parameters, parameter_space) assert_array_equal(actual_result, expected_result)
def test_uniform_random_draw_array(self): """ Tests that the uniform random draw functions behaves as expected when the parameter space is described by arrays. """ number_of_parameters = 2 parameter_space = np.array([[1, 2, 3], [4, 5, 6, 7], [8, 9, 10]]) expected_result = np.array([[1, 4, 10], [2, 6, 8]]) actual_result = uniform_random_draw(number_of_parameters, parameter_space) assert_array_equal(actual_result, expected_result)
def test_hybrid_lhs_uniform_sampling_large(self): """ Tests that the latin hypercube sampling with random uniform works properly. """ number_of_parameters = 8 parameter_space = np.array([np.arange(1, 10), np.arange(1, 5)]) actual_result = hybrid_lhs_uniform_sampling(number_of_parameters, parameter_space) np.random.seed(2) lhs = latin_hypercube_sampling(4, parameter_space) ur = uniform_random_draw(4, parameter_space) assert_array_equal(actual_result, np.append(lhs, ur, axis=0))
def test_uniform_random_draw_range(self): """ Tests that the uniform random draw works as expected when given a range as a parametric space. """ number_of_parameters = 2 parameter_space = np.array( [np.arange(1, 10), np.arange(10, 20), np.arange(20, 30)]) expected_result = np.array([[9, 16, 28], [9, 12, 27]]) actual_result = uniform_random_draw(number_of_parameters, parameter_space) assert_array_equal(actual_result, expected_result)
def l_bfgs_b_minimizer(func, ranges, **kwargs): """Apply L-BFGS-B algorithm on a function, constrained by the bounds in the range argument. The function used in the one implemented in the numpy.optimize package. The initialization of the algorithm is performed by a random choice on the grid. Args: func (function): The function to optimize. ranges (numpy array of numpy arrays): The parameter space. Returns: float: The minimum found of the function. """ bounds = [(min(range_), max(range_)) for range_ in ranges] x_0 = uniform_random_draw(1, ranges) min_ = minimize(func, x0=x_0, method="L-BFGS-B", bounds=bounds) return min_.x