Valdecy / pyMetaheuristic

pyMetaheuristic: A Comprehensive Python Library for Optimization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyMetaheuristic

Introduction

pyMetaheuristic is a robust Python Library crafted to provide a wide range of metaheuristic algorithms, ideal for tackling complex optimization tasks. It encompasses a diverse mix of algorithms, from traditional to modern methods. For a detailed list of these metaheuristics and their demonstrations, refer to Section 3. The library is also equipped with a selection of test functions, useful for benchmarking and evaluating algorithm performance. Details on these functions can be found in Section 4. Getting started with pyMetaheuristic is straightforward. Install the package using pip and begin exploring the available algorithms and test functions, as outlined in Sections 1 and 2. Whether you're addressing intricate optimization problems or experimenting with different algorithms, pyMetaheuristic is your essential toolkit.

Each metaheuristic includes two parameters: 'start_init' and 'target_value'. By default, 'start_init' is set to None, but it can be assigned an initial guess in the form of a NumPy array. The default value of 'target_value' is also None. However, you can specify a continuous value for it. If during the iterations, the objective function reaches a value that is equal to or less than this specified 'target_value', the iterations will halt.

Usage

  1. Install
pip install pyMetaheuristic
  1. Import
# Import PSO
from pyMetaheuristic.algorithm import particle_swarm_optimization

# Import a Test Function. Available Test Functions: https://bit.ly/3KyluPp
from pyMetaheuristic.test_function import easom

# OR Define your Custom Function. The function input should be a list of values, 
# each value representing a dimension (x1, x2, ...xn) of the problem.
import numpy as np
def easom(variables_values = [0, 0]):
    x1, x2     = variables_values
    func_value = -np.cos(x1)*np.cos(x2)*np.exp(-(x1 - np.pi)**2 - (x2 - np.pi)**2)
    return func_value

# Run PSO
parameters = {
    'swarm_size': 250,
    'min_values': (-5, -5),
    'max_values': (5, 5),
    'iterations': 500,
    'decay': 0,
    'w': 0.9,
    'c1': 2,
    'c2': 2,
    'verbose': True,
    'start_init': None,
    'target_value': None
}
pso = particle_swarm_optimization(target_function = easom, **parameters)

# Print Solution
variables = pso[:-1]
minimum   = pso[ -1]
print('Variables: ', np.around(variables, 4) , ' Minimum Value Found: ', round(minimum, 4) )

# Plot Solution
from pyMetaheuristic.utils import graphs
plot_parameters = {
    'min_values': (-5, -5),
    'max_values': (5, 5),
    'step': (0.1, 0.1),
    'solution': [variables],
    'proj_view': '3D',
    'view': 'browser'
}
graphs.plot_single_function(target_function = easom, **plot_parameters)
  1. Colab Demo

Try it in Colab:

  1. Test Functions

Multiobjective Optimization or Many Objectives Optimization

For Multiobjective Optimization or Many Objectives Optimization try pyMultiobjective

TSP (Travelling Salesman Problem)

For Travelling Salesman Problems, try pyCombinatorial

Acknowledgement

This section is dedicated to everyone who helped improve or correct the code. Thank you very much!

About

pyMetaheuristic: A Comprehensive Python Library for Optimization

License:Other


Languages

Language:Python 100.0%