<commit_before>import numpy as np

import time

from socket import timeout

from pysmac.smacrunner import SMACRunner
from pysmac.smacremote import SMACRemote


def check_param_dimensions(x0, xmin, xmax):
    assert(x0.shape == xmin.shape), "shape of x0 and xmin don't agree"
    assert(x0.shape == xmax.shape), "shape of x0 and xmax don't agree"
    return True

def format_params(x0, xmin, xmax, nptype):
    x0 = np.asarray(x0, dtype=nptype)
    xmin = np.asarray(xmin, dtype=nptype)
    xmax = np.asarray(xmax, dtype=nptype)
    return x0, xmin, xmax

def check_categorical_params(params):
    assert isinstance(params, dict), "Categorical parameters must be a dict of lists."
    for key, value in params.iteritems():
        assert isinstance(value, list), "Categorical parameters must be a dict of lists."

def fmin(objective,
         x0=[], xmin=[], xmax=[],
         x0_int=[], xmin_int=[], xmax_int=[],
         x_categorical={},
         custom_args={},
         max_evaluations=100, seed=1,
         update_status_every=500,
         smac_rf_num_trees=100,
         smac_rf_full_tree_bootstrap=True,
         smac_intensification_percentage=0.0
         ):
    """
        min_x f(x) s.t. xmin < x < xmax

        objective: The objective function that should be optimized.
                   Designed for objective functions that are:
                   costly to calculate + don't have a derivative available.
        x0: initial guess
        xmin: minimum values 
        xmax: maximum values
        x0_int: initial guess of integer params
        xmin_int: minimum values of integer params
        xmax_int: maximum values of integer params
        x_categorical: dictionary of categorical parameters
        custom_args: a dict of custom arguments to the objective function
        max_evaluations: the maximum number of evaluations to execute
        seed: the seed that SMAC is initialized with
        update_status_every: the number of iterations, between status updates

        Advanced
        --------
        smac_rf_num_trees: number of trees to create in random forest.
        smac_rf_full_tree_bootstrap: bootstrap all data points into trees.
        smac_intensification_percentage: percent of time to spend intensifying versus model learning.

        returns: best parameters found
    """
    x0, xmin, xmax = format_params(x0, xmin, xmax, np.float)
    check_param_dimensions(x0, xmin, xmax)

    x0_int, xmin_int, xmax_int = format_params(x0_int, xmin_int, xmax_int, np.int)
    check_param_dimensions(x0_int, xmin_int, xmax_int)

    check_categorical_params(x_categorical)

    smacremote = SMACRemote()

    smacrunner = SMACRunner(x0, xmin, xmax,
                            x0_int, xmin_int, xmax_int,
                            x_categorical,
                            smacremote.port, max_evaluations, seed,
                            smac_rf_num_trees,
                            smac_rf_full_tree_bootstrap,
                            smac_intensification_percentage)
    current_fmin = None
    iteration = 0
    while not smacrunner.is_finished():
        iteration += 1
        try:
            params = smacremote.get_next_parameters()
        except timeout:
            #Timeout, check if the runner is finished
            continue

        start = time.clock()
        assert all([param not in custom_args.keys() for param in params.keys()]), ("Naming collision between"
                                                                                   "parameters and custom arguments")
        function_args = {}
        function_args.update(params)
        function_args.update(custom_args)

        performance = objective(**function_args)

        assert performance is not None, ("objective function did not return "
            "a result for parameters %s" % str(function_args))
        if current_fmin is None or performance < current_fmin:
            current_fmin = performance
            fmin_changed = True
        else:
            fmin_changed = False

        if fmin_changed or iteration % update_status_every == 0:
            print "Iteration %d/%d, current fmin: %f" % (iteration, max_evaluations, current_fmin)
        runtime = time.clock() - start

        smacremote.report_performance(performance, runtime)
<commit_msg>status message<commit_after>import numpy as np

import time

from socket import timeout

from pysmac.smacrunner import SMACRunner
from pysmac.smacremote import SMACRemote


def check_param_dimensions(x0, xmin, xmax):
    assert(x0.shape == xmin.shape), "shape of x0 and xmin don't agree"
    assert(x0.shape == xmax.shape), "shape of x0 and xmax don't agree"
    return True

def format_params(x0, xmin, xmax, nptype):
    x0 = np.asarray(x0, dtype=nptype)
    xmin = np.asarray(xmin, dtype=nptype)
    xmax = np.asarray(xmax, dtype=nptype)
    return x0, xmin, xmax

def check_categorical_params(params):
    assert isinstance(params, dict), "Categorical parameters must be a dict of lists."
    for key, value in params.iteritems():
        assert isinstance(value, list), "Categorical parameters must be a dict of lists."

def fmin(objective,
         x0=[], xmin=[], xmax=[],
         x0_int=[], xmin_int=[], xmax_int=[],
         x_categorical={},
         custom_args={},
         max_evaluations=100, seed=1,
         update_status_every=500,
         smac_rf_num_trees=100,
         smac_rf_full_tree_bootstrap=True,
         smac_intensification_percentage=0.01
         ):
    """
        min_x f(x) s.t. xmin < x < xmax

        objective: The objective function that should be optimized.
                   Designed for objective functions that are:
                   costly to calculate + don't have a derivative available.
        x0: initial guess
        xmin: minimum values 
        xmax: maximum values
        x0_int: initial guess of integer params
        xmin_int: minimum values of integer params
        xmax_int: maximum values of integer params
        x_categorical: dictionary of categorical parameters
        custom_args: a dict of custom arguments to the objective function
        max_evaluations: the maximum number of evaluations to execute
        seed: the seed that SMAC is initialized with
        update_status_every: the number of num_evaluationss, between status updates

        Advanced
        --------
        smac_rf_num_trees: number of trees to create in random forest.
        smac_rf_full_tree_bootstrap: bootstrap all data points into trees.
        smac_intensification_percentage: percent of time to spend intensifying versus model learning.

        returns: best parameters found
    """
    x0, xmin, xmax = format_params(x0, xmin, xmax, np.float)
    check_param_dimensions(x0, xmin, xmax)

    x0_int, xmin_int, xmax_int = format_params(x0_int, xmin_int, xmax_int, np.int)
    check_param_dimensions(x0_int, xmin_int, xmax_int)

    check_categorical_params(x_categorical)

    smacremote = SMACRemote()

    smacrunner = SMACRunner(x0, xmin, xmax,
                            x0_int, xmin_int, xmax_int,
                            x_categorical,
                            smacremote.port, max_evaluations, seed,
                            smac_rf_num_trees,
                            smac_rf_full_tree_bootstrap,
                            smac_intensification_percentage)
    current_fmin = None
    num_evaluations = 0
    while not smacrunner.is_finished():
        num_evaluations += 1
        try:
            params = smacremote.get_next_parameters()
        except timeout:
            #Timeout, check if the runner is finished
            continue

        start = time.clock()
        assert all([param not in custom_args.keys() for param in params.keys()]), ("Naming collision between"
                                                                                   "parameters and custom arguments")
        function_args = {}
        function_args.update(params)
        function_args.update(custom_args)

        performance = objective(**function_args)

        assert performance is not None, ("objective function did not return "
            "a result for parameters %s" % str(function_args))
        if current_fmin is None or performance < current_fmin:
            current_fmin = performance
            fmin_changed = True
        else:
            fmin_changed = False

        if fmin_changed or num_evaluations % update_status_every == 0:
            print "Number of evaluations %d, current fmin: %f" % (num_evaluations, current_fmin)
        runtime = time.clock() - start

        smacremote.report_performance(performance, runtime)

{'fraction_non_alphanumeric': 0.07190829087540528, 'fraction_numerical': 0.008452987494210282, 'mean_word_length': 2.3219230769230768, 'pattern_counts': {'":': 0, '<': 9, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '763610', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2452, 'n_words': 726}