/pyMetaheuristic

A python library for the following Metaheuristics: Adaptive Random Search,

Primary LanguagePythonOtherNOASSERTION

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

  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 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)
  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 all the people that helped to improve or correct the code. Thank you very much!