Пример #1
0
    def minimize(
        self,
        problem: Problem,
        x0: np.ndarray,
        id: str,
        history_options: HistoryOptions = None,
    ) -> OptimizerResult:

        lb = problem.lb
        ub = problem.ub
        objective = problem.objective

        if dlib is None:
            raise ImportError(
                "This optimizer requires an installation of dlib.")

        if not objective.has_fun:
            raise Exception("For this optimizer, the objective must "
                            "be able to return function values.")

        # dlib requires variable length arguments
        def get_fval_vararg(*x):
            return objective.get_fval(x)

        dlib.find_min_global(
            get_fval_vararg,
            list(lb),
            list(ub),
            int(self.options['maxiter']),
            0.002,
        )

        optimizer_result = OptimizerResult()

        return optimizer_result
Пример #2
0
    def main(self, piezo_scan_range=Q_(30,'V'), maxevals=50, initial_current=Q_(220,'mA'), initial_feedforward=Q_(-0.2, 'mA/V'), max_retries =2):
        #Set the optimization parametters
        sigma0 = 1
        x0 = [
            (initial_current.to('mA')/self.CONSTS['current_scaling'].to('mA')).m,
            initial_feedforward.to('mA / V').m,
        ]

        bounds = [[self.CONSTS['current_bounds'][0] / self.CONSTS['current_scaling'].to('mA').m, self.CONSTS['feedfoward_bounds'][0]],
                  [self.CONSTS['current_bounds'][1] / self.CONSTS['current_scaling'].to('mA').m, self.CONSTS['feedfoward_bounds'][1]]]

        cmaopts = {
            'tolfun':self.CONSTS['error_threshold'],
            'tolx': 1e-2,
            'maxfevals': maxevals,
            'bounds': bounds,
            'verb_disp':self.CONSTS['verbose'],# No printing of the optimization results on the console
        }

        for retry_idx in self.progress(range(max_retries)):
            #Set to passive mode
            self.fp.active_mode = False
            self.dlc.scan_enabled = True
            self.dlc.piezo_external_input_enabled = True

            #Run the optimization
            self.inner_progress = iter(self.progress(range(maxevals)))
            if self.CONSTS['optimizer'] == 'cma':
                result = cma.fmin(self.cost_func, x0, sigma0, options=cmaopts)
                xopt = result[0]
            elif self.CONSTS['optimizer'] == 'dlib':
                _f = lambda current, ff: self.cost_func([current, ff])
                result = dlib.find_min_global(_f, bounds[0], bounds[1], maxevals)
                xopt = result[0]
            
            #Set the optimal parameters
            err = self.cost_func(xopt)

            #Verify the results
            self.fp.refresh()
            peak_mags = self.fp.peak_magnitudes()
            restart = False

            # If there is too many peak => Restart
            if len(peak_mags) > self.num_peak_threshold:
                restart = True
                if self.CONSTS['verbose']:  print('CTROptimize Retry #{}: Too many peaks...'.format(retry_idx))

            # If the sum of square error metric is to high => Restart
            if err > self.CONSTS['error_threshold']:
                restart = True
                if self.CONSTS['verbose']:  print('CTROptimize Retry #{}: err is too high ({})...'.format(retry_idx,err))

            # Break the loop if we are done
            if not restart:
                break

        if self.CONSTS['verbose']: print('CTROptimize terminated with values {} and err of {}'.format(xopt, err))
Пример #3
0
    def minimize(
        self,
        problem: Problem,
        x0: np.ndarray,
        id: str,
        history_options: HistoryOptions = None,
        optimize_options: OptimizeOptions = None,
    ) -> OptimizerResult:
        """Perform optimization. Parameters: see `Optimizer` documentation."""
        lb = problem.lb
        ub = problem.ub
        check_finite_bounds(lb, ub)
        objective = problem.objective

        if dlib is None:
            raise ImportError(
                "This optimizer requires an installation of dlib. You can "
                "install dlib via `pip install dlib`.")

        if not objective.has_fun:
            raise ValueError("For this optimizer, the objective must "
                             "be able to return function values.")

        # dlib requires variable length arguments
        def get_fval_vararg(*x):
            return objective.get_fval(x)

        dlib.find_min_global(
            get_fval_vararg,
            list(lb),
            list(ub),
            int(self.options['maxiter']),
            0.002,
        )

        optimizer_result = OptimizerResult()

        return optimizer_result
Пример #4
0
    def compute(
        self,
        x_min: Union[T, Iterable[T]],
        x_max: Union[T, Iterable[T]],
        num_evals: int,
        epsilon: Optional[float] = 0,
    ) -> Tuple[T, Union[int, float]]:

        return dlib.find_min_global(
            self._evaluate_objective,
            x_min if isinstance(x_min, list) else [x_min],
            x_max if isinstance(x_max, list) else [x_max],
            num_evals,
            solver_epsilon=epsilon,
        )
Пример #5
0
def optimize_inverse_uvl_coords(xyz_coords,
                                rotate,
                                layer_extents,
                                pop_layers,
                                optiter=100):
    import dlib
    f_uvl_distance = make_uvl_distance(xyz_coords, rotate=rotate)
    for layer, count in viewitems(pop_layers):
        if count > 0:
            min_extent = layer_extents[layer][0]
            max_extent = layer_extents[layer][1]
            uvl_coords, dist = dlib.find_min_global(f_uvl_distance, min_extent,
                                                    max_extent, optiter)
            if uvl_in_bounds(uvl_coords, layer_extents, {layer: count}):
                return uvl_coords
    return None
Пример #6
0
    def dlib_default_cube(objective, n_trials, n_dim, with_count=False):
        global feval_count
        feval_count = 0

        lb = [0. for _ in range(n_dim)]
        ub = [1. for _ in range(n_dim)]

        def _objective(*args) -> float:
            global feval_count
            feval_count += 1
            return objective(list(args))

        best_x, best_val = find_min_global(_objective, lb, ub, n_trials)

        return (best_val, best_x, feval_count) if with_count else (best_val,
                                                                   best_x)
Пример #7
0
def test_global_optimization_nargs():
    w0 = find_max_global(lambda *args: sum(args), [0, 0, 0], [1, 1, 1], 10)
    w1 = find_min_global(lambda *args: sum(args), [0, 0, 0], [1, 1, 1], 10)
    assert w0 == ([1, 1, 1], 3)
    assert w1 == ([0, 0, 0], 0)

    w2 = find_max_global(lambda a, b, c, *args: a + b + c - sum(args), [0, 0, 0], [1, 1, 1], 10)
    w3 = find_min_global(lambda a, b, c, *args: a + b + c - sum(args), [0, 0, 0], [1, 1, 1], 10)
    assert w2 == ([1, 1, 1], 3)
    assert w3 == ([0, 0, 0], 0)

    with raises(Exception):
        find_max_global(lambda a, b: 0, [0, 0, 0], [1, 1, 1], 10)
    with raises(Exception):
        find_min_global(lambda a, b: 0, [0, 0, 0], [1, 1, 1], 10)
    with raises(Exception):
        find_max_global(lambda a, b, c, d, *args: 0, [0, 0, 0], [1, 1, 1], 10)
    with raises(Exception):
        find_min_global(lambda a, b, c, d, *args: 0, [0, 0, 0], [1, 1, 1], 10)
Пример #8
0
def test_global_optimization_nargs():
    w0 = find_max_global(lambda *args: sum(args), [0, 0, 0], [1, 1, 1], 10)
    w1 = find_min_global(lambda *args: sum(args), [0, 0, 0], [1, 1, 1], 10)
    assert w0 == ([1, 1, 1], 3)
    assert w1 == ([0, 0, 0], 0)

    w2 = find_max_global(lambda a, b, c, *args: a + b + c - sum(args),
                         [0, 0, 0], [1, 1, 1], 10)
    w3 = find_min_global(lambda a, b, c, *args: a + b + c - sum(args),
                         [0, 0, 0], [1, 1, 1], 10)
    assert w2 == ([1, 1, 1], 3)
    assert w3 == ([0, 0, 0], 0)

    with raises(Exception):
        find_max_global(lambda a, b: 0, [0, 0, 0], [1, 1, 1], 10)
    with raises(Exception):
        find_min_global(lambda a, b: 0, [0, 0, 0], [1, 1, 1], 10)
    with raises(Exception):
        find_max_global(lambda a, b, c, d, *args: 0, [0, 0, 0], [1, 1, 1], 10)
    with raises(Exception):
        find_min_global(lambda a, b, c, d, *args: 0, [0, 0, 0], [1, 1, 1], 10)
Пример #9
0
def method_dlib():
  import dlib
  x = dlib.find_min_global(fdlib, *list(map(list, zip(*bounds))), args.opt_iters)
  return x[1], x[0]
    options.lambda_param = lambda_param
    options.num_threads = 4
    options.be_verbose = True

    print("start training")
    print(options)
    sys.stdout.flush()
    dlib.train_shape_predictor(training_xml_path, "bbr_predictor.dat", options)
    print("\nTraining error: ",
          dlib.test_shape_predictor(training_xml_path, "bbr_predictor.dat"))
    err = dlib.test_shape_predictor(testing_xml_path, "bbr_predictor.dat")
    print("\nTesting error: ", err)
    sys.stdout.flush()
    return err


lower = [5, -0.2, 0.001, 2, 100, 0.01, 0]
upper = [25, 0.2, 0.20, 5, 1000, 0.99, 0.3]
isint = [True, False, False, True, True, False, False]

x, y = dlib.find_min_global(test_params,
                            bound1=lower,
                            bound2=upper,
                            is_integer_variable=isint,
                            num_function_calls=100)

print("optimal inputs: {}".format(x))
print("optimal output: {}".format(y))

test_params(x[0], x[1], x[2], x[3], x[4], x[5], x[6])
Пример #11
0
#   root folder and run:
#       python setup.py install
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake and boost-python installed.  On Ubuntu, this can be done easily by
#   running the command:
#       sudo apt-get install libboost-python-dev cmake
#

import dlib
from math import sin, cos, pi, exp, sqrt


# This is a standard test function for these kinds of optimization problems.
# It has a bunch of local minima, with the global minimum resulting in
# holder_table()==-19.2085025679.
def holder_table(x0, x1):
    return -abs(sin(x0) * cos(x1) * exp(abs(1 - sqrt(x0 * x0 + x1 * x1) / pi)))


# Find the optimal inputs to holder_table().  The print statements that follow
# show that find_min_global() finds the optimal settings to high precision.
x, y = dlib.find_min_global(
    holder_table,
    [-10, -10],  # Lower bound constraints on x0 and x1 respectively
    [10, 10],  # Upper bound constraints on x0 and x1 respectively
    80)  # The number of times find_min_global() will call holder_table()

print("optimal inputs: {}".format(x))
print("optimal output: {}".format(y))
Пример #12
0
    def run_hyperparameter_search(self):
        hyperparam_results_list = []
        hyper_time = time.time()
        self.parameter_dict['training_early_stop'] = self.parameter_dict[
            'early_stop_cf']
        training_batch_handler_cache = {}
        validation_batch_handler_cache = {}

        def hyper_training_helper(hyper_learning_rate, hyper_rnn_size,
                                  hyper_reg_embedding_beta, hyper_reg_l2_beta,
                                  hyper_learning_rate_decay,
                                  hyper_learning_rate_min,
                                  padding_loss_logit_weight,
                                  padding_loss_mixture_weight):
            """ 
            Function used to wrap the hyperparameters and settings such that it fits the format used by dlib.
            Some variables need to be side-loaded, mostly reporting values.
            """
            ############# SELECT NEW PARAMS
            self.parameter_dict['learning_rate'] = 10**hyper_learning_rate
            self.parameter_dict['rnn_size'] = int(hyper_rnn_size)
            self.parameter_dict[
                'reg_embedding_beta'] = 10**hyper_reg_embedding_beta
            self.parameter_dict['l2_reg_beta'] = 10**hyper_reg_l2_beta
            self.parameter_dict[
                'learning_rate_decay_factor'] = hyper_learning_rate_decay
            self.parameter_dict['learning_rate_min'] = \
                (10 ** hyper_learning_rate_min) * self.parameter_dict['learning_rate']
            self.parameter_dict['embedding_size'] = self.parameter_dict[
                'rnn_size']
            self.parameter_dict[
                'padding_loss_logit_weight'] = padding_loss_logit_weight
            self.parameter_dict[
                'padding_loss_mixture_weight'] = padding_loss_mixture_weight

            # Update Cutoffs
            self.parameter_dict['long_training_time'] = self.parameter_dict[
                'early_stop_cf']
            self.parameter_dict['long_training_steps'] = self.parameter_dict[
                'hyper_search_step_cutoff']
            ######### / PARAMS
            print 'learning_rate               ' + str(10**hyper_learning_rate)
            print 'rnn_size                    ' + str(hyper_rnn_size)
            print 'reg_embedding_beta          ' + str(
                10**hyper_reg_embedding_beta)
            print 'l2_reg_beta                 ' + str(10**hyper_reg_l2_beta)
            print 'learning_rate_decay_factor  ' + str(
                hyper_learning_rate_decay)
            print 'padding_loss_logit_weight   ' + str(
                padding_loss_logit_weight)
            print 'padding_loss_mixture_weight ' + str(
                padding_loss_mixture_weight)

            cf_fold = -1
            # I should call this outside the crossfold, so it occurs once
            # This way all the crossfolds for the same hyperparameters are adjacent in the checkpoint dirs
            log_file_time = str(time.time())
            cf_results_list = []

            for train_pool, val_pool in self.cf_pool:
                cf_fold += 1
                log_file_name = log_file_time + "-cf-" + str(cf_fold)

                print "Starting crossfold"
                # Collect batch_handlers, and check if they've been cached.
                try:
                    training_batch_handler = training_batch_handler_cache[hash(
                        tuple(np.sort(train_pool.uniqueId.unique())))]
                except KeyError:
                    training_batch_handler = BatchHandler.BatchHandler(
                        train_pool, self.parameter_dict, True)
                except AttributeError:
                    print 'This should not be attainable, as crossfold==2 is invalid'

                try:
                    validation_batch_handler = validation_batch_handler_cache[
                        hash(tuple(np.sort(val_pool.uniqueId.unique())))]
                except KeyError:
                    validation_batch_handler = BatchHandler.BatchHandler(
                        val_pool, self.parameter_dict, False)
                except AttributeError:
                    print 'This should not be attainable, as crossfold==2 is invalid'

                # Add input_size, num_classes
                self.parameter_dict[
                    'input_size'] = training_batch_handler.get_input_size()
                self.parameter_dict[
                    'num_classes'] = training_batch_handler.get_num_classes()

                netManager = NetworkManager.NetworkManager(
                    self.parameter_dict, log_file_name)
                netManager.build_model(self.encoder_means, self.encoder_stddev)

                try:
                    cf_results = self.train_network(netManager,
                                                    training_batch_handler,
                                                    validation_batch_handler,
                                                    hyper_search=True)
                except tf.errors.InvalidArgumentError:
                    print "**********************caught error, probably gradients have exploded"
                    return 99999999  # HUGE LOSS --> this was caused by bad init conditions, so it should be avoided.
                # Now assign the handlers to the cache IF AND ONLY IF the training was successful.
                # If it dies before the first pool sort in the training, the whole thing falls over.
                validation_batch_handler_cache[hash(
                    tuple(np.sort(val_pool.uniqueId.unique()))
                )] = validation_batch_handler
                training_batch_handler_cache[hash(
                    tuple(np.sort(train_pool.uniqueId.unique()))
                )] = training_batch_handler

                cf_results['crossfold_number'] = cf_fold
                # As pandas does not like lists when adding a list to a row of a dataframe, set to None (the lists are
                # a large amount of redundant data). This is why I copy out parameters.py
                for key, value in cf_results.iteritems():
                    if (type(value) is list or type(value) is np.ndarray
                            or type(value) is tuple):
                        cf_results[key] = pd.Series([value], dtype=object)
                cf_results_list.append(pd.DataFrame(cf_results, index=[0]))

                # plot
                print "Drawing html graph"
                if self.parameter_dict['model_type'] == 'categorical':
                    netManager.draw_categorical_html_graphs(
                        validation_batch_handler)
                else:
                    netManager.draw_generative_html_graphs(
                        validation_batch_handler, multi_sample=1)
                    netManager.draw_generative_html_graphs(
                        validation_batch_handler, multi_sample=20)

                # Here we have a fully trained model, but we are still in the cross fold.

                # FIXME Only do 1 fold per hyperparams. Its not neccessary to continue
                break

            # Run reportwriter here and return all_tracks..... euclidean loss?
            val_acc, val_loss, report_df =\
                self.test_network(netManager, validation_batch_handler)

            cf_df = pd.concat(cf_results_list)
            # Condense results from cross fold (Average, best, worst, whatever selection method)
            hyperparam_results = copy.copy(self.parameter_dict)
            #hyperparam_results['input_columns'] = ",".join(hyperparam_results['input_columns'])
            hyperparam_results['eval_accuracy'] = np.min(
                cf_df['eval_accuracy'])
            hyperparam_results['final_learning_rate'] = np.min(
                cf_df['final_learning_rate'])
            hyperparam_results['training_accuracy'] = np.min(
                cf_df['training_accuracy'])
            hyperparam_results['training_loss'] = np.average(
                cf_df['training_loss'])
            hyperparam_results['validation_accuracy'] = np.average(
                cf_df['validation_accuracy'])
            hyperparam_results['validation_loss'] = np.average(
                cf_df['validation_loss'])

            track_scores = ReportWriter.ReportWriter.score_model_on_metric(
                self.parameter_dict, report_df)
            hyperparam_results['euclidean_err_sum'] = sum(
                track_scores['euclidean'])
            hyperparam_results['crossfold_number'] = -1
            #FIXME What is this line doing?
            hyperparam_results['network_chkpt_dir'] = (cf_df.sort_values(
                'eval_accuracy',
                ascending=False).iloc[[0]]['network_chkpt_dir'])
            hyperparam_results['cf_summary'] = True
            for key, value in hyperparam_results.iteritems():
                if (type(value) is list or type(value) is np.ndarray
                        or type(value) is tuple):
                    hyperparam_results[key] = pd.Series(
                        [value], dtype=object)  # str(cf_results[key])
            hyperparam_results_list.append(
                pd.DataFrame(hyperparam_results, index=[0]))
            hyperparam_results_list.append(cf_df)
            #Write results and hyperparams to hyperparameter_results_dataframe

            return hyperparam_results['euclidean_err_sum']

        ################################

        import dlib
        #  http://blog.dlib.net/2017/12/a-global-optimization-algorithm-worth.html
        lowers = [
            min(self.parameter_dict['hyper_learning_rate_args']),
            min(self.parameter_dict['hyper_rnn_size_args']),
            min(self.parameter_dict['hyper_reg_embedding_beta_args']),
            min(self.parameter_dict['hyper_reg_l2_beta_args']),
            min(self.parameter_dict['hyper_learning_rate_decay_args']),
            min(self.parameter_dict['hyper_learning_rate_min_args']),
            min(self.parameter_dict['hyper_padding_loss_logit_weight_args']),
            min(self.parameter_dict['hyper_padding_loss_mixture_weight_args'])
        ]
        uppers = [
            max(self.parameter_dict['hyper_learning_rate_args']),
            max(self.parameter_dict['hyper_rnn_size_args']),
            max(self.parameter_dict['hyper_reg_embedding_beta_args']),
            max(self.parameter_dict['hyper_reg_l2_beta_args']),
            max(self.parameter_dict['hyper_learning_rate_decay_args']),
            max(self.parameter_dict['hyper_learning_rate_min_args']),
            max(self.parameter_dict['hyper_padding_loss_logit_weight_args']),
            max(self.parameter_dict['hyper_padding_loss_mixture_weight_args'])
        ]
        x, y = dlib.find_min_global(
            hyper_training_helper,
            lowers,
            uppers,
            [False, True, False, False, False, False, False, False
             ],  # Is integer Variable
            self.parameter_dict['hyper_search_folds'])

        hyper_df = pd.concat(hyperparam_results_list, ignore_index=True)
        hyper_df.to_csv(
            os.path.join(self.parameter_dict['master_dir'],
                         self.hyper_results_logfile))
        summary_df = hyper_df[hyper_df['cf_summary'] == True]

        # Distance at which the classifier can make a sound judgement, lower is better
        if self.parameter_dict['evaluation_metric_type'] == 'perfect_distance':
            best_params = summary_df.sort_values(
                'perfect_distance', ascending=True).iloc[0].to_dict()
        if self.parameter_dict[
                'evaluation_metric_type'] == 'validation_accuracy':  # Higher better
            best_params = summary_df.sort_values(
                'validation_accuracy', ascending=False).iloc[0].to_dict()
        if self.parameter_dict[
                'evaluation_metric_type'] == 'validation_loss':  # Lower better
            best_params = summary_df.sort_values(
                'validation_loss', ascending=True).iloc[0].to_dict()
        if self.parameter_dict[
                'evaluation_metric_type'] == 'euclidean_err_sum':  # Lower better
            best_params = summary_df.sort_values(
                'euclidean_err_sum', ascending=True).iloc[0].to_dict()
        # TODO eval_metric_type_reportwriter?
        return best_params
Пример #13
0
    db = PcbDB.kicadPcbDataBase(kpfile)
    placer = PcbPlacer.GridBasedPlacer(db)
    placer.set_bb_ec(False)
    placer.set_rtree(True)
    placer.set_two_sided(True)
    placer.set_base_lam(lamtemp)
    placer.set_lamtemp_update(lam_temperature_update)
    placer.set_lambda_schedule(lamb)
    wl = placer.test_placer_flow()
    db.printKiCad("./cache")
    subprocess.call('mv ./cache ./' + fname + '_lambsched_' + str(lamb) +
                    '_lamrate_' + str(lamtemp) + '_lam_temp_' +
                    str(lam_temperature_update),
                    shell=True)
    subprocess.call('mkdir ./cache', shell=True)
    subprocess.call('mkdir ./cache/img', shell=True)
    out = (str(lamb), str(lamtemp), str(lam_temperature_update), str(wl))

    with open('./' + fname + '_log.txt', 'a+') as f:
        f.write(out[0] + ',' + out[1] + ',' + out[2] + ',' + out[3] + '\n')
    return wl


x, wl = dlib.find_min_global(
    place, [lamrates[0], temperatures[0], lambda_schedules[0]],
    [lamrates[1], temperatures[1], lambda_schedules[1]], is_integer_variable,
    100)

print('params', x)
print('wl', wl)
Пример #14
0
#
#   Alternatively, if you want to compile dlib yourself then go into the dlib
#   root folder and run:
#       python setup.py install
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake and boost-python installed.  On Ubuntu, this can be done easily by
#   running the command:
#       sudo apt-get install libboost-python-dev cmake
#

import dlib
from math import sin,cos,pi,exp,sqrt

# This is a standard test function for these kinds of optimization problems.
# It has a bunch of local minima, with the global minimum resulting in
# holder_table()==-19.2085025679. 
def holder_table(x0,x1):
    return -abs(sin(x0)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))

# Find the optimal inputs to holder_table().  The print statements that follow
# show that find_min_global() finds the optimal settings to high precision.
x,y = dlib.find_min_global(holder_table, 
                           [-10,-10],  # Lower bound constraints on x0 and x1 respectively
                           [10,10],    # Upper bound constraints on x0 and x1 respectively
                           80)         # The number of times find_min_global() will call holder_table()

print("optimal inputs: {}".format(x));
print("optimal output: {}".format(y));

Пример #15
0
def test_on_holder_table():
    x, y = find_min_global(holder_table, [-10, -10], [10, 10], 200)
    assert (y - -19.2085025679) < 1e-7
Пример #16
0
# Create an observer to obtain the beam sizes
observer = SigmaObserver()


# Create the optimization routine
def parametric_tracking(kq1, kq2):
    manzoni_input.sequence[1].K1 = kq1 / _.m**2
    manzoni_input.sequence[3].K1 = kq2 / _.m**2
    manzoni_input.sequence[5].K1 = kq2 / _.m**2
    track(beamline=manzoni_input, beam=beam, observer=observer)
    if np.isnan(observer.data[-1][1]) or np.isnan(observer.data[-1][3]):
        return 1000.0
    return observer.data[-1][1]**2 + observer.data[-1][3]


iters = []
v = []
for i in [5, 10, 25, 50, 75, 100, 125]:
    x, y = dlib.find_min_global(
        parametric_tracking,
        [0, -3.5],  # Lower bound constraints
        [3.5, 0.0],  # Upper bound constraints
        i)
    v.append(y)
    print(y)
    iters.append(i)

plt.plot(iters, v, '*-')
plt.show()
Пример #17
0
def test_on_holder_table():
    x,y = find_min_global(holder_table, 
                            [-10,-10],  
                            [10,10],   
                            200)       
    assert (y - -19.2085025679) < 1e-7
Пример #18
0
def calculate_optimizer_metrics(problems, n_runs, plot_traces=False):
    """
    Let the global optimizers rt_opt, scipy's differential_evolution, scipy's dual_annealing,
    and dlib's LIPO minimize a bunch of test functions and collect performance metrics.

    :param problems: [list<TestProblem instance>] List of test problems to be used
    :param n_runs: [int] How often each problem is solved by the different optimizers
    :param plot_traces: [bool] Whether to plot bacteria traces for the rt_opt
    :return: Performance metrics [dict]
    """

    optimizer_results = {
        'Run-and-Tumble': {},
        'Differential Evolution': {},
        'Dual Annealing': {},
        'LIPO': {}
    }

    if plot_traces:
        n_plots = len(problems)
        n_cols = int(np.ceil(np.sqrt(n_plots)))
        n_rows = int(np.ceil(n_plots / n_cols))
        fig, axs = plt.subplots(n_rows, n_cols)

    n_total_steps = len(problems) * len(optimizer_results) * n_runs

    print('Collecting optimizer statistics...')
    with tqdm(total=n_total_steps) as pbar:
        for n, problem in enumerate(problems):
            name = problem.__class__.__name__
            if problem.bounds.lower is not None:
                bounds_lower = problem.bounds.lower
            else:
                bounds_lower = np.repeat(-5, problem.ndims)
            if problem.bounds.upper is not None:
                bounds_upper = problem.bounds.upper
            else:
                bounds_upper = np.repeat(5, problem.ndims)

            # Run-and-tumble algorithm
            bounds = np.vstack((bounds_lower, bounds_upper)).T
            optimizer_results['Run-and-Tumble'][name] = {
                'runtime': [],
                'nfev': [],
                'x': [],
                'f': []
            }
            for m in range(n_runs):
                start = time.time()
                ret = optimize(problem.f, bounds=bounds)
                end = time.time()
                runtime = end - start
                pbar.update(1)
                optimizer_results['Run-and-Tumble'][name]['runtime'].append(
                    runtime)
                optimizer_results['Run-and-Tumble'][name]['nfev'].append(
                    ret.nfev)
                optimizer_results['Run-and-Tumble'][name]['x'].append(ret.x)
                optimizer_results['Run-and-Tumble'][name]['f'].append(ret.fun)

                if plot_traces and m == 0:  # Plot bacteria traces only once
                    if problem.ndims != 2:
                        raise NotImplementedError(
                            'Currently, plotting bacteria traces is ' +
                            'supported for 2D problems only.')
                    # noinspection PyUnboundLocalVariable
                    plot_row = n // n_cols
                    # noinspection PyUnboundLocalVariable
                    plot_col = n % n_cols
                    X, Y, Z = gridmap2d(
                        problem.f, (bounds_lower[0], bounds_upper[0], 100),
                        (bounds_lower[1], bounds_upper[1], 100))
                    # noinspection PyUnboundLocalVariable
                    cp = axs[plot_row, plot_col].contourf(X, Y, Z, levels=20)
                    axs[plot_row,
                        plot_col].set_xlim([bounds_lower[0], bounds_upper[0]])
                    axs[plot_row,
                        plot_col].set_ylim([bounds_lower[1], bounds_upper[1]])
                    # noinspection PyUnboundLocalVariable
                    fig.colorbar(cp, ax=axs[plot_row, plot_col])
                    for single_trace in ret.trace.transpose(1, 0, 2):
                        axs[plot_row, plot_col].plot(single_trace[:, 0],
                                                     single_trace[:, 1],
                                                     'o',
                                                     c='white',
                                                     ms=0.4)
                    axs[plot_row, plot_col].set_xlabel('x')
                    axs[plot_row, plot_col].set_ylabel('y')
                    axs[plot_row, plot_col].set_title(f'Problem: {name}',
                                                      fontsize=10)

            # Differential Evolution algorithm
            bounds = np.vstack((bounds_lower, bounds_upper)).T
            optimizer_results['Differential Evolution'][name] = {
                'runtime': [],
                'nfev': [],
                'x': [],
                'f': []
            }
            for m in range(n_runs):
                start = time.time()
                ret = differential_evolution(problem.f, bounds)
                end = time.time()
                runtime = end - start
                pbar.update(1)
                optimizer_results['Differential Evolution'][name][
                    'runtime'].append(runtime)
                optimizer_results['Differential Evolution'][name][
                    'nfev'].append(ret.nfev)
                optimizer_results['Differential Evolution'][name]['x'].append(
                    ret.x)
                optimizer_results['Differential Evolution'][name]['f'].append(
                    ret.fun)

            # Dual Annealing algorithm
            bounds = np.vstack((bounds_lower, bounds_upper)).T
            optimizer_results['Dual Annealing'][name] = {
                'runtime': [],
                'nfev': [],
                'x': [],
                'f': []
            }
            for m in range(n_runs):
                start = time.time()
                ret = dual_annealing(problem.f, bounds)
                end = time.time()
                runtime = end - start
                pbar.update(1)
                optimizer_results['Dual Annealing'][name]['runtime'].append(
                    runtime)
                optimizer_results['Dual Annealing'][name]['nfev'].append(
                    ret.nfev)
                optimizer_results['Dual Annealing'][name]['x'].append(ret.x)
                optimizer_results['Dual Annealing'][name]['f'].append(ret.fun)

            # LIPO algorithm
            nfev = 500 * problem.ndims
            lipoFun = LipoWrapper(problem.f)
            optimizer_results['LIPO'][name] = {
                'runtime': [],
                'nfev': [],
                'x': [],
                'f': []
            }
            for m in range(n_runs):
                start = time.time()
                ret_lp = dlib.find_min_global(lipoFun.f, bounds_lower.tolist(),
                                              bounds_upper.tolist(), nfev)
                end = time.time()
                runtime = end - start
                pbar.update(1)
                optimizer_results['LIPO'][name]['runtime'].append(runtime)
                optimizer_results['LIPO'][name]['nfev'].append(nfev)
                optimizer_results['LIPO'][name]['x'].append(ret_lp[0])
                optimizer_results['LIPO'][name]['f'].append(ret_lp[1])

    if plot_traces:
        # Finalize bacteria traces plot
        fig.subplots_adjust(wspace=0.3, hspace=0.4)
        fig.suptitle('Run-and-tumble bacteria traces', fontsize=14)
        fig_manager = plt.get_current_fig_manager()
        fig_manager.window.showMaximized()
        fig.set_size_inches(25.6, 14.4)
        plt.savefig(join(SAVE_DIR, 'bacteria_traces.png'),
                    bbox_inches='tight',
                    dpi=150)
        plt.show()

    return optimizer_results
Пример #19
0
from math import sin, cos, exp, sqrt, pi

import dlib
from tuning.metaheuristic.function_utils import *


def fitness(*args):
    # print(args)

    res = 0
    for a in args:
        res += a**2
    return res


def holder_table(x0, x1):
    return -abs(sin(x0) * cos(x1) * exp(abs(1 - sqrt(x0 * x0 + x1 * x1) / pi)))


x, y = dlib.find_min_global(
    fitness,
    [-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10
     ],  # Lower bound constraints on x0 and x1 respectively
    [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
     ],  # Upper bound constraints on x0 and x1 respectively
    10)

print("++++++++++++++++++++++++++++++=")
print(y)
print(x)
Пример #20
0
def minimizable(weights):
    results = []
    t0 = time.time()
    for i, seed in zip(range(runs), seeds):
        task_queue.put((weights, seed))

    while not len(results) == runs:
        results.append(result_queue.get())
        if len(results) % 10 == 0:
            print(weights, sum(results), len(results), 'w/r',
                  sum(results) / len(results))

    print(weights,
          sum(results) / len(results), (time.time() - t0) / len(results))
    return -sum(results) / len(results)


starting_values = [0.02, 0.01, 0.05, 0.1, 0.01]
bounds = [(0.00001, 0.9)] * len(starting_values)

# Find the optimal inputs to holder_table().  The print statements that follow
# show that find_min_global() finds the optimal settings to high precision.
print(
    dlib.find_min_global(
        minimizable,
        [x[0]
         for x in bounds],  # Lower bound constraints on x0 and x1 respectively
        [x[1]
         for x in bounds],  # Upper bound constraints on x0 and x1 respectively
        80))  # The number of times find_min_global() will call holder_table()
Пример #21
0
def icp_transform(comm,
                  geometry_config,
                  volume,
                  make_surface,
                  soma_coords,
                  projection_ls,
                  population_extents,
                  rotate=None,
                  populations=None,
                  icp_iter=1000,
                  opt_iter=100):
    """
    Uses the iterative closest point (ICP) algorithm of the PCL library to transform soma coordinates onto a surface for a particular L value.
    http://pointclouds.org/documentation/tutorials/iterative_closest_point.php#iterative-closest-point

    """

    import dlib, pcl

    rank = comm.rank
    size = comm.size

    if populations is None:
        populations = list(soma_coords.keys())

    srf_resample = 25

    layer_extents = geometry_config['Parametric Surface']['Layer Extents']

    (extent_u, extent_v, extent_l) = get_total_extents(layer_extents)

    min_u, max_u = extent_u
    min_v, max_v = extent_v
    min_l, max_l = extent_l

    ## This parameter is used to expand the range of L and avoid
    ## situations where the endpoints of L end up outside of the range
    ## of the distance interpolant
    safety = 0.01

    extent_u = (min_u - safety, max_u + safety)
    extent_v = (min_v - safety, max_v + safety)

    projection_ptclouds = []
    for obs_l in projection_ls:
        srf = make_surface(extent_u, extent_v, obs_l, rotate=rotate)
        U, V = srf._resample_uv(srf_resample, srf_resample)
        meshpts = srf.ev(U, V)
        projection_ptcloud = pcl.PointCloud()
        projection_ptcloud.from_array(meshpts)
        projection_ptclouds.append(projection_ptcloud)

    soma_coords_dict = {}
    for pop in populations:
        coords_dict = soma_coords[pop]
        if rank == 0:
            logger.info('Computing point transformation for population %s...' %
                        pop)
        count = 0
        xyz_coords = []
        gids = []
        for gid, coords in viewitems(coords_dict):
            if gid % size == rank:
                soma_u, soma_v, soma_l = coords
                xyz_coords.append(volume(soma_u, soma_v, soma_l,
                                         rotate=rotate))
                gids.append(gid)
        xyz_pts = np.vstack(xyz_coords)

        cloud_in = pcl.PointCloud()
        cloud_in.from_array(xyz_pts)

        icp = cloud_in.make_IterativeClosestPoint()

        all_est_xyz_coords = []
        all_est_uvl_coords = []
        all_interp_err = []

        for (k, cloud_prj) in enumerate(projection_ls):
            k_est_xyz_coords = np.zeros((len(gids), 3))
            k_est_uvl_coords = np.zeros((len(gids), 3))
            interp_err = np.zeros((len(gids), ))
            converged, transf, estimate, fitness = icp.icp(cloud_in,
                                                           cloud_prj,
                                                           max_iter=icp_iter)
            logger.info('Transformation of population %s has converged: ' %
                        (pop) + str(converged) + ' score: %f' % (fitness))
            for i, gid in zip(list(range(0, estimate.size)), gids):
                est_xyz_coords = estimate[i]
                k_est_xyz_coords[i, :] = est_xyz_coords
                f_uvl_distance = make_uvl_distance(est_xyz_coords,
                                                   rotate=rotate)
                uvl_coords, err = dlib.find_min_global(f_uvl_distance,
                                                       limits[0], limits[1],
                                                       opt_iter)
                k_est_uvl_coords[i, :] = uvl_coords
                interp_err[i, ] = err
                if rank == 0:
                    logger.info(
                        'gid %i: u: %f v: %f l: %f' %
                        (gid, uvl_coords[0], uvl_coords[1], uvl_coords[2]))
            all_est_xyz_coords.append(k_est_xyz_coords)
            all_est_uvl_coords.append(k_est_uvl_coords)
            all_interp_err.append(interp_err)

        coords_dict = {}
        for (i, gid) in enumerate(gids):
            coords_dict[gid] = {
                'X Coordinate':
                np.asarray([col[i, 0] for col in all_est_xyz_coords],
                           dtype='float32'),
                'Y Coordinate':
                np.asarray([col[i, 1] for col in all_est_xyz_coords],
                           dtype='float32'),
                'Z Coordinate':
                np.asarray([col[i, 2] for col in all_est_xyz_coords],
                           dtype='float32'),
                'U Coordinate':
                np.asarray([col[i, 0] for col in all_est_uvl_coords],
                           dtype='float32'),
                'V Coordinate':
                np.asarray([col[i, 1] for col in all_est_uvl_coords],
                           dtype='float32'),
                'L Coordinate':
                np.asarray([col[i, 2] for col in all_est_uvl_coords],
                           dtype='float32'),
                'Interpolation Error':
                np.asarray([err[i] for err in all_interp_err], dtype='float32')
            }

        soma_coords_dict[pop] = coords_dict

    return soma_coords_dict
# lower range, upper range, and is/is not integer boolean, respectively
params = OrderedDict([("tree_depth", (2, 5, True)),
                      ("nu", (0.001, 0.2, False)),
                      ("cascade_depth", (4, 25, True)),
                      ("feature_pool_size", (100, 1000, True)),
                      ("num_test_splits", (20, 300, True)),
                      ("oversampling_amount", (1, 40, True)),
                      ("oversampling_translation_jitter", (0.0, 0.3, False)),
                      ("feature_pool_region_padding", (-0.2, 0.2, False)),
                      ("lambda_param", (0.01, 0.99, False))])

# Use the ordered dictionary to easily extract the lower and upper boundaries of the hyperparamter range,
# include whether or not the parameter is an integer or not
lower = [v[0] for (k, v) in params.items()]
upper = [v[1] for (k, v) in params.items()]
isint = [v[2] for (k, v) in params.items()]

# Utilize dlib to optimize our shape predictor hyperparameters
(bestParams,
 bestLoss) = dlib.find_min_global(test_shape_predictor_params,
                                  bound1=lower,
                                  bound2=upper,
                                  is_integer_variable=isint,
                                  num_function_calls=config.MAX_FUNC_CALLS)

# Display the optimal hyperparameters so we can reuse them in our training script
print("[INFO] Optimal Parameters: {}".format(bestParams))
print("[INFO] Optimal Error: {}".format(bestLoss))

# delete the temporary model file
os.remove(config.TEMP_MODEL_PATH)