pyMetaheuristic
Introduction
A python library for the following Metaheuristics: Adaptive Random Search, Ant Lion Optimizer, Arithmetic Optimization Algorithm, Artificial Bee Colony Optimization, Artificial Fish Swarm Algorithm, Bat Algorithm, Biogeography Based Optimization, Cross-Entropy Method, Crow Search Algorithm, Cuckoo Search, Differential Evolution, Dispersive Flies Optimization, Dragonfly Algorithm, Firefly Algorithm, Flow Direction Algorithm, Flower Pollination Algorithm, Genetic Algorithm, Grasshopper Optimization Algorithm, Gravitational Search Algorithm, Grey Wolf Optimizer, Harris Hawks Optimization, Improved Grey Wolf Optimizer, Improved Whale Optimization Algorithm, Jaya, Jellyfish Search Optimizer, Krill Herd Algorithm, Memetic Algorithm, Moth Flame Optimization, Multiverse Optimizer, Pathfinder Algorithm, Particle Swarm Optimization, Random Search, Salp Swarm Algorithm, Simulated Annealing, Sine Cosine Algorithm, Student Psychology Based Optimization; Symbiotic Organisms Search; Teaching Learning Based Optimization, Whale Optimization Algorithm.
Usage
- Install
pip install pyMetaheuristic
- 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 Own Custom Function. The function input should be a list of values,
# each value represents a dimenstion (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
}
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)
- Colab Demo
Try it in Colab:
- Adaptive Random Search ( Colab Demo ) ( Original Paper )
- Ant Lion Optimizer ( Colab Demo ) ( Original Paper )
- Arithmetic Optimization Algorithm ( Colab Demo ) ( Original Paper )
- Artificial Bee Colony Optimization ( Colab Demo ) ( Original Paper )
- Artificial Fish Swarm Algorithm ( Colab Demo ) ( Original Paper )
- Bat Algorithm ( Colab Demo ) ( Original Paper )
- Biogeography Based Optimization ( Colab Demo ) ( Original Paper )
- Cross-Entropy Method ( Colab Demo ) ( Original Paper )
- Crow Search Algorithm ( Colab Demo ) ( Original Paper )
- Cuckoo Search ( Colab Demo ) ( Original Paper )
- Differential Evolution ( Colab Demo ) ( Original Paper )
- Dispersive Flies Optimization ( Colab Demo ) ( Original Paper )
- Dragonfly Algorithm ( Colab Demo ) ( Original Paper )
- Firefly Algorithm ( Colab Demo ) ( Original Paper )
- Flow Direction Algorithm ( Colab Demo ) ( Original Paper )
- Flower Pollination Algorithm ( Colab Demo ) ( Original Paper )
- Genetic Algorithm ( Colab Demo ) ( Original Paper )
- Grey Wolf Optimizer ( Colab Demo ) ( Original Paper )
- Grasshopper Optimization Algorithm ( Colab Demo ) ( Original Paper )
- Gravitational Search Algorithm ( Colab Demo ) ( Original Paper )
- Harris Hawks Optimization ( Colab Demo ) ( Original Paper )
- Improved Grey Wolf Optimizer ( Colab Demo ) ( Original Paper )
- Improved Whale Optimization Algorithm ( Colab Demo ) ( Original Paper )
- Jaya ( Colab Demo ) ( Original Paper )
- Jellyfish Search Optimizer ( Colab Demo ) ( Original Paper )
- Krill Herd Algorithm ( Colab Demo ) ( Original Paper )
- Memetic Algorithm ( Colab Demo ) ( Original Paper )
- Moth Flame Optimization ( Colab Demo ) ( Original Paper )
- Multiverse Optimizer ( Colab Demo ) ( Original Paper )
- Pathfinder Algorithm ( Colab Demo ) ( Original Paper )
- Particle Swarm Optimization ( Colab Demo ) ( Original Paper )
- Random Search ( Colab Demo ) ( Original Paper )
- Salp Swarm Algorithm ( Colab Demo ) ( Original Paper )
- Simulated Annealing ( Colab Demo ) ( Original Paper )
- Sine Cosine Algorithm ( Colab Demo ) ( Original Paper )
- Student Psychology Based Optimization ( Colab Demo ) ( Original Paper )
- Symbiotic Organisms Search ( Colab Demo ) ( Original Paper )
- Teaching Learning Based Optimization ( Colab Demo ) ( Original Paper )
- Whale Optimization Algorithm ( Colab Demo ) ( Original Paper )
- Test Functions
- Available Test Functions: https://bit.ly/3KyluPp
- Test Functions and their Optimal Solutions with 2D or 3D plots ( Colab Demo )
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 all the people that helped to improve or correct the code. Thank you very much!
- Raiser (01.MARCH.2022) - https://github.com/mpraiser - University of Chinese Academy of Sciences (China)