<commit_before>import time
import logging
import numpy as np


from robo.initial_design.init_random_uniform import init_random_uniform
from robo.solver.base_solver import BaseSolver
from robo.incumbent.best_observation import BestObservation


logger = logging.getLogger(__name__)


class BayesianOptimization(BaseSolver):

    def __init__(self,
            acquisition_func,
            model,
            maximize_func,
            task,
            save_dir=None,
            initial_design=None,
            initial_points=3,
            incumbent_estimation=None,
            num_save=1,
            train_intervall=1,
            n_restarts=1):
        """
        Implementation of the standard Bayesian optimization loop that uses
        an acquisition function and a model to optimize a given task.
        This module keeps track of additional information such as runtime,
        optimization overhead, evaluated points and saves the output
        in a csv file.

        Parameters
        ----------
        acquisition_func: AcquisitionFunctionObject
            The acquisition function which will be maximized.
        model: ModelObject
            Model (i.e. GaussianProcess, RandomForest) that models our current
            believe of the objective function.
        task: TaskObject
            Task object that contains the objective function and additional
            meta information such as the lower and upper bound of the search
            space.
        save_dir: String
            Output path
        initial_design: function
            Function that returns some points which will be evaluated before
            the Bayesian optimization loop is started. This allows to
            initialize the model.
        initial_points: int
            Defines the number of initial points that are evaluated before the
            actual Bayesian optimization.
        incumbent_estimation: IncumbentEstimationObject,
            Object to estimate the incumbent based on the current model. The
            incumbent is the current best guess of the global optimum and is
            estimated in each iteration.
        num_save: int
            Defines after how many iteration the output is saved.
        train_intervall: int
            Specifies after how many iterations the model is retrained.
        n_restarts: int
            How often the incumbent estimation is repeated.
        """

        super(BayesianOptimization, self).__init__(acquisition_func,
                                                    model,
                                                    maximize_func,
                                                    task,
                                                    save_dir)
        self.start_time = time.time()

        if initial_design == None:
            self.initial_design = init_random_uniform
        else:
            self.initial_design = initial_design

        self.X = None
        self.Y = None
        self.time_func_eval = None
        self.time_overhead = None
        self.train_intervall = train_intervall

        self.num_save = num_save

        self.model_untrained = True
        if incumbent_estimation is None:
            self.estimator = BestObservation(self.model,
                                             self.task.X_lower,
                                             self.task.X_upper)
        else:
            self.estimator = incumbent_estimation
        self.incumbent = None
        self.incumbents = []
        self.incumbent_values = []
        self.n_restarts = n_restarts
        self.init_points = initial_points
        self.runtime = []

    def run(self, num_iterations=10, X=None, Y=None):
        """
        The main Bayesian optimization loop

        Parameters
        ----------
        num_iterations: int
            The number of iterations
        X: np.ndarray(N,D)
            Initial points that are already evaluated
        Y: np.ndarray(N,1)
            Function values of the already evaluated points

        Returns
        -------
        np.ndarray(1,D)
            Incumbent
        np.ndarray(1,1)
            (Estimated) function value of the incumbent
        """
        # Save the time where we start the Bayesian optimization procedure
        self.time_start = time.time()

        if X is None and Y is None:
            self.time_func_eval = np.zeros([self.init_points])
            self.time_overhead = np.zeros([self.init_points])
            self.X = np.zeros([1, self.task.n_dims])
            self.Y = np.zeros([1, 1])

            start_time_overhead = time.time()
            init = self.initial_design(self.task.X_lower,
                                       self.task.X_upper,
                                       N=self.init_points)

            for i, x in enumerate(init):
                x = x[np.newaxis, :]
                self.time_overhead[i] = time.time() - start_time_overhead

                logger.info("Evaluate: %s" % x)

                start_time = time.time()
                y = self.task.evaluate(x)
                self.time_func_eval[i] = time.time() - start_time

                if i == 0:
                    self.X[i] = x[0, :]
                    self.Y[i] = y[0, :]
                else:
                    self.X = np.append(self.X, x, axis=0)
                    self.Y = np.append(self.Y, y, axis=0)

                logger.info("Configuration achieved a performance"
                    "of %f in %f seconds" %
                    (self.Y[i], self.time_func_eval[i]))

                # Use best point seen so far as incumbent
                best_idx = np.argmin(self.Y)<commit_msg>fix time computations<commit_after>import time
import logging
import numpy as np


from robo.initial_design.init_random_uniform import init_random_uniform
from robo.solver.base_solver import BaseSolver
from robo.incumbent.best_observation import BestObservation


logger = logging.getLogger(__name__)


class BayesianOptimization(BaseSolver):

    def __init__(self,
            acquisition_func,
            model,
            maximize_func,
            task,
            save_dir=None,
            initial_design=None,
            initial_points=3,
            incumbent_estimation=None,
            num_save=1,
            train_intervall=1,
            n_restarts=1):
        """
        Implementation of the standard Bayesian optimization loop that uses
        an acquisition function and a model to optimize a given task.
        This module keeps track of additional information such as runtime,
        optimization overhead, evaluated points and saves the output
        in a csv file.

        Parameters
        ----------
        acquisition_func: AcquisitionFunctionObject
            The acquisition function which will be maximized.
        model: ModelObject
            Model (i.e. GaussianProcess, RandomForest) that models our current
            believe of the objective function.
        task: TaskObject
            Task object that contains the objective function and additional
            meta information such as the lower and upper bound of the search
            space.
        save_dir: String
            Output path
        initial_design: function
            Function that returns some points which will be evaluated before
            the Bayesian optimization loop is started. This allows to
            initialize the model.
        initial_points: int
            Defines the number of initial points that are evaluated before the
            actual Bayesian optimization.
        incumbent_estimation: IncumbentEstimationObject,
            Object to estimate the incumbent based on the current model. The
            incumbent is the current best guess of the global optimum and is
            estimated in each iteration.
        num_save: int
            Defines after how many iteration the output is saved.
        train_intervall: int
            Specifies after how many iterations the model is retrained.
        n_restarts: int
            How often the incumbent estimation is repeated.
        """

        super(BayesianOptimization, self).__init__(acquisition_func,
                                                    model,
                                                    maximize_func,
                                                    task,
                                                    save_dir)
        self.start_time = time.time()

        if initial_design == None:
            self.initial_design = init_random_uniform
        else:
            self.initial_design = initial_design

        self.X = None
        self.Y = None
        self.time_func_eval = None
        self.time_overhead = None
        self.train_intervall = train_intervall

        self.num_save = num_save

        self.model_untrained = True
        if incumbent_estimation is None:
            self.estimator = BestObservation(self.model,
                                             self.task.X_lower,
                                             self.task.X_upper)
        else:
            self.estimator = incumbent_estimation
        self.incumbent = None
        self.incumbents = []
        self.incumbent_values = []
        self.n_restarts = n_restarts
        self.init_points = initial_points
        self.runtime = []

    def run(self, num_iterations=10, X=None, Y=None):
        """
        The main Bayesian optimization loop

        Parameters
        ----------
        num_iterations: int
            The number of iterations
        X: np.ndarray(N,D)
            Initial points that are already evaluated
        Y: np.ndarray(N,1)
            Function values of the already evaluated points

        Returns
        -------
        np.ndarray(1,D)
            Incumbent
        np.ndarray(1,1)
            (Estimated) function value of the incumbent
        """
        # Save the time where we start the Bayesian optimization procedure
        self.time_start = time.time()

        if X is None and Y is None:
            self.time_func_eval = np.zeros([self.init_points])
            self.time_overhead = np.zeros([self.init_points])
            self.X = np.zeros([1, self.task.n_dims])
            self.Y = np.zeros([1, 1])

            init = self.initial_design(self.task.X_lower,
                                       self.task.X_upper,
                                       N=self.init_points)

            for i, x in enumerate(init):
                x = x[np.newaxis, :]

                logger.info("Evaluate: %s" % x)

                start_time = time.time()
                y = self.task.evaluate(x)

                if i == 0:
                    self.X[i] = x[0, :]
                    self.Y[i] = y[0, :]
                    self.time_func_eval[i] = time.time() - start_time
                    self.time_overhead[i] = 0.0
                else:
                    self.X = np.append(self.X, x, axis=0)
                    self.Y = np.append(self.Y, y, axis=0)

                    time_feval = np.array([time.time() - start_time])
                    self.time_func_eval = np.append(self.time_func_eval,
                                                    time_feval, axis=0)
                    self.time_overhead = np.append(self.time_overhead,
                                                   np.array([0]), axis=0)

                logger.info("Configuration achieved a performance"
                    "of %f in %f seconds" %
                    (self.Y[i], self.time_func_eval[i]))

                # Use best point seen so far as incumbent
                best_idx = np.argmin(self.Y)
{'fraction_non_alphanumeric': 0.06567778444654034, 'fraction_numerical': 0.003515390551316128, 'mean_word_length': 1.4748567791215785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6943197', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2787, 'n_words': 847}