-
Notifications
You must be signed in to change notification settings - Fork 16
/
BatAlgorithm.py
99 lines (79 loc) · 3.32 KB
/
BatAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import random
import numpy as np
class BatAlgorithm():
def __init__(self, D, NP, N_Gen, A, r, Qmin, Qmax, Lower, Upper, function):
self.D = D #dimension
self.NP = NP #population size
self.N_Gen = N_Gen #generations
self.A = A #loudness
self.r = r #pulse rate
self.Qmin = Qmin #frequency min
self.Qmax = Qmax #frequency max
self.Lower = Lower #lower bound
self.Upper = Upper #upper bound
self.f_min = 0.0 #minimum fitness
self.Lb = [0] * self.D #lower bound
self.Ub = [0] * self.D #upper bound
self.Q = [0] * self.NP #frequency
self.v = [[0 for i in range(self.D)] for j in range(self.NP)] #velocity
self.Sol = [[0 for i in range(self.D)] for j in range(self.NP)] #population of solutions
self.Fitness = [0] * self.NP #fitness
self.best = [0] * self.D #best solution
self.Fun = function
def best_bat(self):
i = 0
j = 0
for i in range(self.NP):
if self.Fitness[i] < self.Fitness[j]:
j = i
for i in range(self.D):
self.best[i] = self.Sol[j][i]
self.f_min = self.Fitness[j]
def init_bat(self):
for i in range(self.D):
self.Lb[i] = self.Lower
self.Ub[i] = self.Upper
for i in range(self.NP):
self.Q[i] = 0
for j in range(self.D):
rnd = np.random.uniform(0, 1)
self.v[i][j] = 0.0
self.Sol[i][j] = self.Lb[j] + (self.Ub[j] - self.Lb[j]) * rnd
self.Fitness[i] = self.Fun(self.D, self.Sol[i])
self.best_bat()
def simplebounds(self, val, lower, upper):
if val < lower:
val = lower
if val > upper:
val = upper
return val
def move_bat(self):
S = [[0.0 for i in range(self.D)] for j in range(self.NP)]
self.init_bat()
for t in range(self.N_Gen):
for i in range(self.NP):
rnd = np.random.uniform(0, 1)
self.Q[i] = self.Qmin + (self.Qmax - self.Qmin) * rnd
for j in range(self.D):
self.v[i][j] = self.v[i][j] + (self.Sol[i][j] -
self.best[j]) * self.Q[i]
S[i][j] = self.Sol[i][j] + self.v[i][j]
S[i][j] = self.simplebounds(S[i][j], self.Lb[j],
self.Ub[j])
rnd = np.random.random_sample()
if rnd > self.r:
for j in range(self.D):
S[i][j] = self.best[j] + 0.001 * random.gauss(0, 1)
S[i][j] = self.simplebounds(S[i][j], self.Lb[j],
self.Ub[j])
Fnew = self.Fun(self.D, S[i])
rnd = np.random.random_sample()
if (Fnew <= self.Fitness[i]) and (rnd < self.A):
for j in range(self.D):
self.Sol[i][j] = S[i][j]
self.Fitness[i] = Fnew
if Fnew <= self.f_min:
for j in range(self.D):
self.best[j] = S[i][j]
self.f_min = Fnew
print(self.f_min)