Exemple #1
0
    def _get_candidate(self, parameters, results, lower_is_better,
                       min_candidates=10):
        """
        Samples candidates parameters from the top 33% of population. If less than min_candidates
        then use a random sample

        Returns
        dict: parameter dictionary.
        """
        if results.shape[0] > 0:
            population = results.loc[results['Status'] != 'INTERMEDIATE',
                         :]  # select only completed trials
        else:
            population = None
        if population is None or population.shape[0] < min_candidates:
            trial_param_values = {}
            for parameter_object in parameters:
                trial_param_values[
                    parameter_object.name] = parameter_object.sample()
            return trial_param_values
        population = population.sort_values(by='Objective',
                                            ascending=lower_is_better)
        idx = sherpa_rng.randint(low=0, high=population.shape[
                                                   0] // 3)  # pick randomly among top 33%
        trial_all_values = population.iloc[
            idx].to_dict()  # extract the trial values on results table
        trial_param_values = {param.name: trial_all_values[param.name] for param
                              in parameters}  # Select only parameter values
        return trial_param_values
Exemple #2
0
    def _truncation_selection(self, parameters, results, lower_is_better):
        """
        Continues the top 80% of the generation, resamples the rest from the
        top 20% and perturbs.

        Returns
            dict: parameter dictionary.
        """
        # Select correct generation and sort generation members
        completed = results.loc[results['Status'] == 'COMPLETED', :]


        if (self.count - 1) % self.population_size / self.population_size < 0.8:
            # Go through top 80% of generation
            generation_df = completed.loc[(completed.generation
                                           == self.generation - 1), :] \
                                     .sort_values(by='Objective',
                                                  ascending=lower_is_better)
            d = generation_df.iloc[(self.count - 1) % self.population_size].to_dict()
        else:
            # For the rest, sample from top 20% of the last generation
            target_generations = [self.generation - 1]
            generation_df = completed.loc[(completed.generation.isin(
                target_generations)), :] \
                                     .sort_values(by='Objective',
                                                  ascending=lower_is_better)
            idx = sherpa_rng.randint(low=0, high=self.population_size*len(target_generations)//5)
            d = generation_df.iloc[idx].to_dict()
            d = self._perturb(candidate=d, parameters=parameters)
        trial = {param.name: d[param.name] for param in parameters}
        for key in ['load_from', 'save_to', 'lineage']:
            trial[key] = d[key]
        return trial