Exemple #1
0
    def _build_matrix(self, run_dict, runhistory, instances=None, par_factor=1):
        '''
            builds X,y matrixes from selected runs from runhistory

            Parameters
            ----------
            run_dict: dict: RunKey -> RunValue
                dictionary from RunHistory.RunKey to RunHistory.RunValue
            runhistory: RunHistory
                runhistory object
            instances: list
                list of instances
            par_factor: int
                penalization factor for censored runtime data

            Returns
            -------
            X: np.ndarray, Y:np.ndarray

        '''
        # First build nan-matrix of size #configs x #params+1
        n_rows = len(run_dict)
        n_cols = self.num_params
        X = np.ones([n_rows, n_cols + self.n_feats]) * np.nan
        y = np.ones([n_rows, 1])

        # Then populate matrix
        for row, (key, run) in enumerate(run_dict.items()):
            # Scaling is automatically done in configSpace
            conf = runhistory.ids_config[key.config_id]
            conf = impute_inactive_values(conf)
            if self.n_feats:
                feats = self.instance_features[key.instance_id]
                X[row, :] = np.hstack((conf.get_array(), feats))
            else:
                X[row, :] = conf.get_array()
            # run_array[row, -1] = instances[row]
            if self.scenario.run_obj == "runtime":
                if run.status != StatusType.SUCCESS:
                    y[row, 0] = run.time * par_factor
                else:
                    y[row, 0] = run.time
            else:
                y[row, 0] = run.cost

        return X, y
Exemple #2
0
    def _build_matrix(self, run_list, runhistory, instances=None):
        # First build nan-matrix of size #configs x #params+1
        n_rows = len(run_list)
        n_cols = self.num_params
        X = np.ones([n_rows, n_cols + self.n_feats]) * np.nan
        y = np.ones([n_rows, 1])

        # Then populate matrix
        for row, (key, run) in enumerate(run_list.items()):
            # Scaling is automatically done in configSpace
            conf = runhistory.ids_config[key.config_id]
            conf = impute_inactive_values(conf)
            if self.n_feats:
                feats = self.instance_features[key.instance_id]
                X[row, :] = np.hstack((conf.get_array(), feats))
            else:
                X[row, :] = conf.get_array()
            # run_array[row, -1] = instances[row]
            y[row, 0] = run.cost

        return X, y
Exemple #3
0
    def maximize(self, start_point, *args):
        """
        Starts a local search from the given startpoint and quits
        if either the max number of steps is reached or no neighbor
        with an higher improvement was found.

        Parameters:
        ----------

        start_point:  np.array(1, D):
            The point from where the local search starts
        *args :
            Additional parameters that will be passed to the
            acquisition function

        Returns:
        -------

        incumbent np.array(1, D):
            The best found configuration
        acq_val_incumbent np.array(1,1) :
            The acquisition value of the incumbent

        """
        incumbent = start_point
        # Compute the acquisition value of the incumbent
        incumbent_ = impute_inactive_values(incumbent)
        acq_val_incumbent = self.acquisition_function(incumbent_.get_array(),
                                                      *args)

        local_search_steps = 0
        neighbors_looked_at = 0
        time_n = []
        while True:

            local_search_steps += 1
            if local_search_steps % 1000 == 0:
                self.logger.warn("Local search took already %d iterations." \
                "Is it maybe stuck in a infinite loop?", local_search_steps)

            # Get neighborhood of the current incumbent
            # by randomly drawing configurations
            changed_inc = False

            all_neighbors = get_one_exchange_neighbourhood(
                incumbent, seed=self.rng.seed())
            self.rng.shuffle(all_neighbors)

            for neighbor in all_neighbors:
                s_time = time.time()
                neighbor_ = impute_inactive_values(neighbor)
                n_array = neighbor_.get_array()

                acq_val = self.acquisition_function(n_array, *args)

                neighbors_looked_at += 1

                time_n.append(time.time() - s_time)

                if acq_val > acq_val_incumbent + self.epsilon:
                    self.logger.debug("Switch to one of the neighbors")
                    incumbent = neighbor
                    acq_val_incumbent = acq_val
                    changed_inc = True
                    break

            if (not changed_inc) or (self.max_iterations != None
                                     and local_search_steps
                                     == self.max_iterations):
                self.logger.debug(
                    "Local search took %d steps and looked at %d configurations. "
                    "Computing the acquisition value for one "
                    "configuration took %f seconds on average.",
                    local_search_steps, neighbors_looked_at, np.mean(time_n))
                break

        return incumbent, acq_val_incumbent