Tuner / Hyperparameter Tuning

We build a dedicated class, Tuner, that can help you tune your algorithm’s parameters.

Please head to examples folder to learn more about this Tuner-Examples

Below is a simple example with Tuner class

from opfunu.cec_based.cec2017 import F52017
from mealpy import FloatVar, BBO, Tuner     # We will use this Tuner utility


f1 = F52017(30, f_bias=0)

p1 = {
    "bounds": FloatVar(lb=f1.lb, ub=f1.ub),
    "obj_func": f1.evaluate,
    "minmax": "min",
    "name": "F5",
    "log_to": "console",
}

paras_bbo_grid = {
    "epoch": [10],
    "pop_size": [10],
    "n_elites": [2, 3, 4, 5],
    "p_m": [0.01, 0.02, 0.05]
}

term = {
    "max_epoch": 200,
    "max_time": 20,
    "max_fe": 10000
}

if __name__ == "__main__":
        ### Define model and parameter grid of the model (just like ParameterGrid / GridSearchCV)
    model = BBO.OriginalBBO()
    tuner = Tuner(model, paras_bbo_grid)

    ## Try to run this optimizer on this problem 5 times (n_trials = 5).
    ## Get the best model by mean value of all trials
    ## Distribute to 4 CPU to run (n_jobs=4)
    tuner.execute(problem=p1, termination=term, n_trials=5, n_jobs=4, verbose=True)

    print(tuner.best_row)
    print(tuner.best_score)
    print(tuner.best_params)
    print(type(tuner.best_params))

    print(tuner.best_algorithm)
    ## Better to save the tuning results to CSV for later usage
    tuner.export_results()
    tuner.export_figures()

        ## Now we can even re-train the algorithm with the best parameter by calling resolve() function
    ## Resolve() function will call the solve() function in algorithm with default problem parameter is removed.
    ##    other parameters of solve() function is keeped and can be used.
    g_best = tuner.resolve(mode="thread", n_workers=4, termination=term)

    ## Print out the best score of the best parameter
    print(g_best.solution, g_best.target.fitness)

    print(tuner.algorithm.problem.get_name())

    ## Print out the algorithm with the best parameter
    print(tuner.best_algorithm.get_name())