├── BatAlgorithm.py ├── LICENSE ├── README.md ├── run.py └── setup.py /BatAlgorithm.py: -------------------------------------------------------------------------------- 1 | import random 2 | import numpy as np 3 | 4 | class BatAlgorithm(): 5 | def __init__(self, D, NP, N_Gen, A, r, Qmin, Qmax, Lower, Upper, function): 6 | self.D = D #dimension 7 | self.NP = NP #population size 8 | self.N_Gen = N_Gen #generations 9 | self.A = A #loudness 10 | self.r = r #pulse rate 11 | self.Qmin = Qmin #frequency min 12 | self.Qmax = Qmax #frequency max 13 | self.Lower = Lower #lower bound 14 | self.Upper = Upper #upper bound 15 | 16 | self.f_min = 0.0 #minimum fitness 17 | 18 | self.Lb = [0] * self.D #lower bound 19 | self.Ub = [0] * self.D #upper bound 20 | self.Q = [0] * self.NP #frequency 21 | 22 | self.v = [[0 for i in range(self.D)] for j in range(self.NP)] #velocity 23 | self.Sol = [[0 for i in range(self.D)] for j in range(self.NP)] #population of solutions 24 | self.Fitness = [0] * self.NP #fitness 25 | self.best = [0] * self.D #best solution 26 | self.Fun = function 27 | 28 | 29 | def best_bat(self): 30 | i = 0 31 | j = 0 32 | for i in range(self.NP): 33 | if self.Fitness[i] < self.Fitness[j]: 34 | j = i 35 | for i in range(self.D): 36 | self.best[i] = self.Sol[j][i] 37 | self.f_min = self.Fitness[j] 38 | 39 | def init_bat(self): 40 | for i in range(self.D): 41 | self.Lb[i] = self.Lower 42 | self.Ub[i] = self.Upper 43 | 44 | for i in range(self.NP): 45 | self.Q[i] = 0 46 | for j in range(self.D): 47 | rnd = np.random.uniform(0, 1) 48 | self.v[i][j] = 0.0 49 | self.Sol[i][j] = self.Lb[j] + (self.Ub[j] - self.Lb[j]) * rnd 50 | self.Fitness[i] = self.Fun(self.D, self.Sol[i]) 51 | self.best_bat() 52 | 53 | def simplebounds(self, val, lower, upper): 54 | if val < lower: 55 | val = lower 56 | if val > upper: 57 | val = upper 58 | return val 59 | 60 | def move_bat(self): 61 | S = [[0.0 for i in range(self.D)] for j in range(self.NP)] 62 | 63 | self.init_bat() 64 | 65 | for t in range(self.N_Gen): 66 | for i in range(self.NP): 67 | rnd = np.random.uniform(0, 1) 68 | self.Q[i] = self.Qmin + (self.Qmax - self.Qmin) * rnd 69 | for j in range(self.D): 70 | self.v[i][j] = self.v[i][j] + (self.Sol[i][j] - 71 | self.best[j]) * self.Q[i] 72 | S[i][j] = self.Sol[i][j] + self.v[i][j] 73 | 74 | S[i][j] = self.simplebounds(S[i][j], self.Lb[j], 75 | self.Ub[j]) 76 | 77 | rnd = np.random.random_sample() 78 | 79 | if rnd > self.r: 80 | for j in range(self.D): 81 | S[i][j] = self.best[j] + 0.001 * random.gauss(0, 1) 82 | S[i][j] = self.simplebounds(S[i][j], self.Lb[j], 83 | self.Ub[j]) 84 | 85 | Fnew = self.Fun(self.D, S[i]) 86 | 87 | rnd = np.random.random_sample() 88 | 89 | if (Fnew <= self.Fitness[i]) and (rnd < self.A): 90 | for j in range(self.D): 91 | self.Sol[i][j] = S[i][j] 92 | self.Fitness[i] = Fnew 93 | 94 | if Fnew <= self.f_min: 95 | for j in range(self.D): 96 | self.best[j] = S[i][j] 97 | self.f_min = Fnew 98 | 99 | print(self.f_min) 100 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bat Algorithm in Python 2 | 3 | ## Objective 4 | The main objective is to create an implementation of bat algorithm in Python programming language. 5 | 6 | ## Installation 7 | 8 | pip install BatAlgorithm 9 | 10 | ### Example 11 | The following example presents a simple use of bat algorithm. `Fun()` denotes the objective function that may be changed by the user. Control parameters should be defined within `BatAlgorithm()` constructor. Order of parameters is as 12 | follows: `BatAlgorithm(D, NP, N_Gen, A, r, Qmin, Qmax, Lower, Upper, function)` where: 13 | 14 | - `D` denotes dimension of the problem, 15 | - `NP` denotes population size, 16 | - `N_Gen` denotes number of generations (iterations), 17 | - `A` parameter denotes loudness, 18 | - `r` parameter denotes pulse rate, 19 | - `Qmin` parameter denotes frequency minimum, 20 | - `Qmax` parameter denotes frequency maximum, 21 | - `Lower` denotes lower bound, 22 | - `Upper` denotes upper bound and 23 | - `function` passes objective function. 24 | 25 | ## CODE EXAMPLE: 26 | 27 | ```python 28 | import random 29 | from BatAlgorithm import * 30 | 31 | def Fun(D, sol): 32 | val = 0.0 33 | for i in range(D): 34 | val = val + sol[i] * sol[i] 35 | return val 36 | 37 | # For reproducive results 38 | #random.seed(5) 39 | 40 | for i in range(10): 41 | Algorithm = BatAlgorithm(10, 40, 1000, 0.5, 0.5, 0.0, 2.0, -10.0, 10.0, Fun) 42 | Algorithm.move_bat() 43 | ``` 44 | 45 | ## Bugs 46 | Bugs and extension should be send via Github. 47 | 48 | ## Authors 49 | Iztok Fister Jr. and Marko Burjek 50 | 51 | ## References 52 | 53 | Yang, X.-S. "A new metaheuristic bat-inspired algorithm." Nature inspired cooperative strategies for optimization (NICSO 2010). Springer 54 | Berlin Heidelberg, 2010. 65-74. 55 | 56 | Fister, I. Jr., Fister, I., Yang, X.-S., Fong, S., Zhuang, Y. "Bat algorithm: Recent advances." IEEE 15th International Symposium on Computational Intelligence and Informatics (CINTI), IEEE, 2014. 163-167. 57 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | import random 3 | from BatAlgorithm import * 4 | 5 | def Fun(D, sol): 6 | val = 0.0 7 | for i in range(D): 8 | val = val + sol[i] * sol[i] 9 | return val 10 | 11 | # For reproducive results 12 | #random.seed(5) 13 | 14 | for i in range(10): 15 | Algorithm = BatAlgorithm(10, 40, 1000, 0.5, 0.5, 0.0, 2.0, -10.0, 10.0, Fun) 16 | Algorithm.move_bat() 17 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | import os 3 | import sys 4 | 5 | if sys.version_info[0] < 3: 6 | with open('README.md') as f: 7 | long_description = f.read() 8 | else: 9 | with open('README.md', encoding='utf-8') as f: 10 | long_description = f.read() 11 | 12 | setup(name='BatAlgorithm', 13 | description='Bat algorithm implementation', 14 | long_description_content_type="text/markdown", 15 | long_description=long_description, 16 | author='Iztok Fister Jr. and Marko Burjek', 17 | version='0.3.1', 18 | license='MIT', 19 | classifiers=[ 20 | 'Development Status :: 5 - Production/Stable', 21 | 'Intended Audience :: Developers', 22 | 'Intended Audience :: Science/Research', 23 | 'Natural Language :: English', 24 | 'Operating System :: OS Independent', 25 | 'Programming Language :: Python', 26 | 'Programming Language :: Python :: 2', 27 | 'Programming Language :: Python :: 2.7', 28 | 'Programming Language :: Python :: 3', 29 | 'Programming Language :: Python :: 3.6', 30 | 'Programming Language :: Python :: 3.7', 31 | 'Topic :: Scientific/Engineering', 32 | 'Topic :: Software Development' 33 | ], 34 | include_package_data=True, 35 | url='https://github.com/buma/BatAlgorithm', 36 | py_modules=['BatAlgorithm'] 37 | ) 38 | 39 | --------------------------------------------------------------------------------