The METApy optimization toolbox is an easy-to-use environment for applying metaheuristic optimization methods. The platform has several optimization methods and functions for generating charts and statistical analysis of the results. |
If you have any suggestions or error reports regarding the algorithm’s functioning, please inform us by email:
wanderlei_junior@ufcat.edu.br
. We will be happy to improve the framework.
Requirements and install
Use the command below to install the framework.
pip install metapy-toolbox
The METApy is available for installation and use in Google Collaboratoy, Jupyter Notebook or other Python development environments.
Files structure
Let's build an example optimization problem using the METApy framework. The basic file structure of the library should be as follows:
.
└── problem_directory
└── of_file.py # Contain objective function (format Python def)
└── your_problem.ipynb # Metapy function (or can use .py file too)
└── other files
Build objective function in another .py file for good algorithm work. METApy uses parallel processing, and Python documentation recommends separating files when using a .ipynb file.
Quick start
The purpose of this example is the optimization (minimization) of the well-known sphere function.
your_problem
"""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 metaheuristic_optimizer
from of_file import my_function # External .py file with your objective function
# Algorithm settings
algorithm_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'
}
},
}
# METApy settings
general_setup = {
'number of repetitions': 30,
'type code': 'real code',
'initial pop. seed': [None] * 30,
'algorithm': 'hill_climbing_01',
}
# Run algorithm
df_all_reps, df_resume_all_reps, reports, status = metaheuristic_optimizer(algorithm_setup, general_setup)
of_file
"""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
Analysis of results
See the details repetition \(id = 0\). df_resume_all_reps
contains history details the best particle in \(id = 0\) repetition.
print(df_resume_all_reps[0])
To see all population history in repetition \(id = 0\) use:
print(df_all_reps[0])
See best repetition \(id\) and best dataset:
print(status)
print(df_resume_all_reps[status])
See complete report about best repetition:
# Report details
arq = "report_example.txt"
# Writing report
with open(arq, "w") as file:
file.write(report[status])
See section metaheuristic_optimizer for more details.