Ejemplo n.º 1
0
    def _optimize(self, fun_negative_acquisition, str_initial_method,
                  int_samples):
        list_next_point = []
        if self.str_optimizer_method_bo == 'L-BFGS-B':
            list_bounds = self._get_bounds()
            arr_initials = self.get_initial(
                str_initial_method,
                fun_objective=fun_negative_acquisition,
                int_samples=int_samples)
            for arr_initial in arr_initials:
                next_point = minimize(fun_negative_acquisition,
                                      x0=arr_initial,
                                      bounds=list_bounds,
                                      method=self.str_optimizer_method_bo,
                                      options={'disp': False})
                next_point_x = next_point.x
                list_next_point.append(next_point_x)
                if self.debug:
                    print(
                        '[DEBUG] _optimize in bo.py: optimized point for acq',
                        next_point_x)
        elif self.str_optimizer_method_bo == 'DIRECT':  # pragma: no cover
            list_bounds = self._get_bounds()
            next_point = directminimize(
                fun_negative_acquisition,
                bounds=list_bounds,
            )
            next_point_x = next_point.x
            list_next_point.append(next_point_x)
        elif self.str_optimizer_method_bo == 'CMA-ES':
            list_bounds = self._get_bounds()
            list_bounds = np.array(list_bounds)

            def fun_wrapper(f):
                def g(bx):
                    return f(bx)[0]

                return g

            arr_initials = self.get_initial(
                str_initial_method,
                fun_objective=fun_negative_acquisition,
                int_samples=1)
            next_point_x = cma.fmin(fun_wrapper(fun_negative_acquisition),
                                    arr_initials[0],
                                    0.5,
                                    options={
                                        'bounds':
                                        [list_bounds[:, 0], list_bounds[:, 1]],
                                        'verbose':
                                        -1
                                    })[0]
            list_next_point.append(next_point_x)

        next_points = np.array(list_next_point)
        next_point = get_best_acquisition(next_points,
                                          fun_negative_acquisition)
        return next_point.flatten(), next_points
Ejemplo n.º 2
0
    def _optimize(self, fun_negative_acquisition: constants.TYPING_CALLABLE,
                  str_sampling_method: str,
                  num_samples: int) -> constants.TYPING_TUPLE_TWO_ARRAYS:
        """
        It optimizes `fun_negative_function` with `self.str_optimizer_method_bo`.
        `num_samples` examples are determined by `str_sampling_method`, to
        start acquisition function optimization.

        :param fun_objective: negative acquisition function.
        :type fun_objective: callable
        :param str_sampling_method: the name of sampling method.
        :type str_sampling_method: str.
        :param num_samples: the number of samples.
        :type num_samples: int.

        :returns: tuple of next point to evaluate and all candidates
            determined by acquisition function optimization.
            Shape: ((d, ), (`num_samples`, d)).
        :rtype: (numpy.ndarray, numpy.ndarray)

        """

        list_next_point = []
        if self.str_optimizer_method_bo == 'L-BFGS-B':
            list_bounds = self._get_bounds()
            initials = self.get_samples(str_sampling_method,
                                        fun_objective=fun_negative_acquisition,
                                        num_samples=num_samples)

            for arr_initial in initials:
                next_point = minimize(fun_negative_acquisition,
                                      x0=arr_initial,
                                      bounds=list_bounds,
                                      method=self.str_optimizer_method_bo,
                                      options={'disp': False})
                next_point_x = next_point.x
                list_next_point.append(next_point_x)
                if self.debug:
                    self.logger.debug('acquired sample: %s',
                                      utils_logger.get_str_array(next_point_x))
        elif self.str_optimizer_method_bo == 'DIRECT':  # pragma: no cover
            self.logger.debug('num_samples is ignored.')

            list_bounds = self._get_bounds()
            next_point = directminimize(
                fun_negative_acquisition,
                bounds=list_bounds,
                maxf=88888,
            )
            next_point_x = next_point.x
            list_next_point.append(next_point_x)
        elif self.str_optimizer_method_bo == 'CMA-ES':
            self.logger.debug('num_samples is ignored.')

            list_bounds = self._get_bounds()
            list_bounds = np.array(list_bounds)

            def fun_wrapper(f):
                def g(bx):
                    return f(bx)[0]

                return g

            initials = self.get_samples(str_sampling_method,
                                        fun_objective=fun_negative_acquisition,
                                        num_samples=1)
            cur_sigma0 = np.mean(list_bounds[:, 1] - list_bounds[:, 0]) / 4.0
            next_point_x = cma.fmin(fun_wrapper(fun_negative_acquisition),
                                    initials[0],
                                    cur_sigma0,
                                    options={
                                        'bounds':
                                        [list_bounds[:, 0], list_bounds[:, 1]],
                                        'verbose':
                                        -1,
                                        'maxfevals':
                                        1e5
                                    })[0]
            list_next_point.append(next_point_x)

        next_points = np.array(list_next_point)
        next_point = utils_bo.get_best_acquisition_by_evaluation(
            next_points, fun_negative_acquisition)[0]
        return next_point, next_points
Ejemplo n.º 3
0
    def _optimize(self, fun_negative_acquisition, str_initial_method,
                  int_samples):
        """
        It optimizes `fun_negative_function` with `self.str_optimizer_method_bo`.
        `int_samples` examples are determined by `str_initial_method`, to start acquisition function optimization.

        :param fun_objective: negative acquisition function.
        :type fun_objective: function
        :param str_initial_method: the name of sampling method.
        :type str_initial_method: str.
        :param int_samples: the number of samples.
        :type int_samples: int.

        :returns: tuple of next point to evaluate and all candidates determined by acquisition function optimization. Shape: ((d, ), (`int_samples`, d)).
        :rtype: (numpy.ndarray, numpy.ndarray)

        """

        list_next_point = []
        if self.str_optimizer_method_bo == 'L-BFGS-B':
            list_bounds = self._get_bounds()
            arr_initials = self.get_initial(
                str_initial_method,
                fun_objective=fun_negative_acquisition,
                int_samples=int_samples)
            for arr_initial in arr_initials:
                next_point = minimize(fun_negative_acquisition,
                                      x0=arr_initial,
                                      bounds=list_bounds,
                                      method=self.str_optimizer_method_bo,
                                      options={'disp': False})
                next_point_x = next_point.x
                list_next_point.append(next_point_x)
                if self.debug:
                    logger.debug('acquired sample: {}'.format(
                        utils_logger.get_str_array(next_point_x)))
        elif self.str_optimizer_method_bo == 'DIRECT':  # pragma: no cover
            list_bounds = self._get_bounds()
            next_point = directminimize(
                fun_negative_acquisition,
                bounds=list_bounds,
                maxf=88888,
            )
            next_point_x = next_point.x
            list_next_point.append(next_point_x)
        elif self.str_optimizer_method_bo == 'CMA-ES':
            list_bounds = self._get_bounds()
            list_bounds = np.array(list_bounds)

            def fun_wrapper(f):
                def g(bx):
                    return f(bx)[0]

                return g

            arr_initials = self.get_initial(
                str_initial_method,
                fun_objective=fun_negative_acquisition,
                int_samples=1)
            cur_sigma0 = np.mean(list_bounds[:, 1] - list_bounds[:, 0]) / 4.0
            next_point_x = cma.fmin(fun_wrapper(fun_negative_acquisition),
                                    arr_initials[0],
                                    cur_sigma0,
                                    options={
                                        'bounds':
                                        [list_bounds[:, 0], list_bounds[:, 1]],
                                        'verbose':
                                        -1,
                                        'maxfevals':
                                        1e5
                                    })[0]
            list_next_point.append(next_point_x)

        next_points = np.array(list_next_point)
        next_point = get_best_acquisition(next_points,
                                          fun_negative_acquisition)
        return next_point.flatten(), next_points