# Path: robo/maximizers/differential_evolution.py
# class DifferentialEvolution(BaseMaximizer):
# 
#     def __init__(self, objective_function, lower, upper, n_iters=20, rng=None):
#         """
# 
#         Parameters
#         ----------
#         objective_function: acquisition function
#             The acquisition function which will be maximized
#         lower: np.ndarray (D)
#             Lower bounds of the input space
#         upper: np.ndarray (D)
#             Upper bounds of the input space
#         n_iters: int
#             Number of iterations
#         """
#         self.n_iters = n_iters
#         super(DifferentialEvolution, self).__init__(objective_function, lower, upper, rng)
# 
#     def _acquisition_fkt_wrapper(self, acq_f):
#         def _l(x):
#             a = -acq_f(np.array([np.clip(x, self.lower, self.upper)]))
#             if np.any(np.isinf(a)):
#                 return sys.float_info.max
#             return a
# 
#         return _l
# 
#     def maximize(self):
#         """
#         Maximizes the given acquisition function.
# 
#         Returns
#         -------
#         np.ndarray(N,D)
#             Point with highest acquisition value.
#         """
# 
#         bounds = list(zip(self.lower, self.upper))
# 
#         res = sp.optimize.differential_evolution(self._acquisition_fkt_wrapper(self.objective_func),
#                                                  bounds, maxiter=self.n_iters)
# 
#         return np.clip(res["x"], self.lower, self.upper)


import unittest
import numpy as np
from robo.maximizers.grid_search import GridSearch
from robo.maximizers.random_sampling import RandomSampling
from robo.maximizers.scipy_optimizer import SciPyOptimizer
from robo.maximizers.differential_evolution import DifferentialEvolution
from robo.acquisition_functions.base_acquisition import BaseAcquisitionFunction
from test.dummy_model import DemoQuadraticModel
        return np.array([y])


class TestMaximizers1D(unittest.TestCase):

    def setUp(self):

        self.lower = np.array([0])
        self.upper = np.array([1])
        self.objective_function = DemoAcquisitionFunction()

    def test_grid_search(self):
        maximizer = GridSearch(self.objective_function, self.lower, self.upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert len(x.shape) == 1
        assert np.all(x >= self.lower)
        assert np.all(x <= self.upper)

    def test_random_sampling(self):
        maximizer = RandomSampling(self.objective_function, self.lower, self.upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert len(x.shape) == 1
        assert np.all(x >= self.lower)
        assert np.all(x <= self.upper)

    def test_differential_evolution(self):

# Path: robo/maximizers/differential_evolution.py
# class DifferentialEvolution(BaseMaximizer):
# 
#     def __init__(self, objective_function, lower, upper, n_iters=20, rng=None):
#         """
# 
#         Parameters
#         ----------
#         objective_function: acquisition function
#             The acquisition function which will be maximized
#         lower: np.ndarray (D)
#             Lower bounds of the input space
#         upper: np.ndarray (D)
#             Upper bounds of the input space
#         n_iters: int
#             Number of iterations
#         """
#         self.n_iters = n_iters
#         super(DifferentialEvolution, self).__init__(objective_function, lower, upper, rng)
# 
#     def _acquisition_fkt_wrapper(self, acq_f):
#         def _l(x):
#             a = -acq_f(np.array([np.clip(x, self.lower, self.upper)]))
#             if np.any(np.isinf(a)):
#                 return sys.float_info.max
#             return a
# 
#         return _l
# 
#     def maximize(self):
#         """
#         Maximizes the given acquisition function.
# 
#         Returns
#         -------
#         np.ndarray(N,D)
#             Point with highest acquisition value.
#         """
# 
#         bounds = list(zip(self.lower, self.upper))
# 
#         res = sp.optimize.differential_evolution(self._acquisition_fkt_wrapper(self.objective_func),
#                                                  bounds, maxiter=self.n_iters)
# 
#         return np.clip(res["x"], self.lower, self.upper)


# Path: test/test_maximizer/test_maximizers_one_dim.py
import unittest
import numpy as np
from robo.maximizers.grid_search import GridSearch
from robo.maximizers.random_sampling import RandomSampling
from robo.maximizers.scipy_optimizer import SciPyOptimizer
from robo.maximizers.differential_evolution import DifferentialEvolution
from robo.acquisition_functions.base_acquisition import BaseAcquisitionFunction
from test.dummy_model import DemoQuadraticModel

        return np.array([y])


class TestMaximizers1D(unittest.TestCase):

    def setUp(self):

        self.lower = np.array([0])
        self.upper = np.array([1])
        self.objective_function = DemoAcquisitionFunction()

    def test_grid_search(self):
        maximizer = GridSearch(self.objective_function, self.lower, self.upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert len(x.shape) == 1
        assert np.all(x >= self.lower)
        assert np.all(x <= self.upper)

    def test_random_sampling(self):
        maximizer = RandomSampling(self.objective_function, self.lower, self.upper)
        x = maximizer.maximize()

        assert x.shape[0] == 1
        assert len(x.shape) == 1
        assert np.all(x >= self.lower)
        assert np.all(x <= self.upper)

    def test_differential_evolution(self):

        maximizer = DifferentialEvolution(self.objective_function, self.lower, self.upper, n_iters=10)