hill_climbing_01
Hill Climbing algorithm (see theory).
df_all, df_best, delta_time, report = hill_climbing_01(settings)
This function does not perform more than one repetition. To perform multiple repetitions, use the metaheuristic_optimizer function.
Input variables
Name | Description | Type |
---|---|---|
settings | Algorithm settings: [0] setup, [1] initial population, [2] seeds | List |
settings[0] \(=\) setup | Algorithm setup | Dictionary |
setup keys | ||
'number of population' | Number of population | Integer |
'number of iterations' | Number of iterations | Integer |
'number of dimensions' | Problem dimension | Integer |
'x pop lower limit' | Lower limit of the design variables | List |
'x upper lower limit' | Upper limit of the design variables | List |
'none variable' | None variable. Default is None. User can use this variable in objective function | None, list, float, dictionary, str or any |
'objective function' | Objective function. The Metapy user defined this function | Py function (def) |
'algorithm parameters' | Algorithm parameters | Dictionary |
'algorithm parameters' keys | ||
'mutation' | Mutation parameters | Dictionary |
settings[1] \(=\) initial population | Users can inform the initial population or use initial population functions | List or METApy function |
settings[2] \(=\) seed | Random seed. Use None for random seed | None or Integer |
Output variables
df_all | All data of the population | Dataframe |
df_best | Best data of the population | Dataframe |
delta_time | Time of the algorithm execution in seconds | Float |
report | Report of the algorithm execution | String |
Mutation parameters
'mutation': {
'cov (%)': 20,
'pdf': 'gaussian'
}
Name | Description | Type |
---|---|---|
'cov (%)' | Coefficient of variation in percentage | Float |
'pdf' | Probability density function used in random generator. Options: 'gaussian' or 'uniform' | String |
Example 1
Use the Hill Climbing optimization method to optimize the 2D sphere function. Use a total of 100 iterations to perform the optimization. Consider the limits \(\mathbf{x}_L = [-5.0, -5.0]\) and \(\mathbf{x}_U = [5.0, 5.0]\) for the problem design variables. Consider the initial guess (two agents) \(\mathbf{pop}_0 = [-0.74, 1.25]\) and \(\mathbf{pop}_1 = [3.58, -3.33]\). Use \(cov = 20\%\) and Gaussian random generator.
"""Object Function: of_file.py"""
def my_function(x, none_variable):
x_0 = x[0]
x_1 = x[1]
of = x_0 ** 2 + x_1 ** 2
return of
"""Run optimization: your_problem.py or your_problem.ipynb"""
# import libray
# pip install metapy-toolbox or pip install --upgrade metapy-toolbox
from metapy_toolbox import hill_climbing_01
from of_file import my_function # External .py file with your objective function
# Algorithm setup
setup = {
'number of iterations': 100,
'number of population': 2,
'number of dimensions': 2,
'x pop lower limit': [-5, -5],
'x pop upper limit': [5, 5],
'none variable': None,
'objective function': my_obj_function,
'algorithm parameters': {
'mutation': {
'cov (%)': 20,
'pdf': 'gaussian'
}
},
}
# Initial guess
init_pop = [[-0.74, 1.25],
[3.58, -3.33]]
"""
# or random initial guess (real design variables)
from metapy_toolbox import initial_population_01
init_pop = initial_population_01(setup['number of population'],
setup['number of dimensions'],
setup['x pop lower limit'],
setup['x pop upper limit'])
"""
# Seed
seed = None
# Call function
settings = [setup, init_pop, seed]
df_all_results, df_resume, time_cost, report = hill_climbing_01(settings)