-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrey_Wolves.py
141 lines (102 loc) · 5.15 KB
/
Grey_Wolves.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import numpy as np
import random
def find_alpha_beta_delta(pop_weights_vector, errors):
# we are looking for solutions that minimize the error
# hence we take the solutions with least 3 errors
idx1, idx2, idx3 = np.argsort(errors)[:3]
x_alpha_score, x_beta_score, x_delta_score = np.sort(errors)[:3]
X_alpha = pop_weights_vector[idx1]
X_beta = pop_weights_vector[idx2]
X_delta = pop_weights_vector[idx3]
return X_alpha, X_beta, X_delta, x_alpha_score, x_beta_score, x_delta_score
def GWO(population_vectors, X_alpha, X_beta, X_delta, a):
num_solutions, num_dimensions = population_vectors.shape
X_new = np.zeros((num_solutions, num_dimensions))
for i in range(num_solutions):
A1 = a * (2 * np.random.random(num_dimensions) - 1)
A2 = a * (2 * np.random.random(num_dimensions) - 1)
A3 = a * (2 * np.random.random(num_dimensions) - 1)
C1 = 2 * np.random.random(num_dimensions)
C2 = 2 * np.random.random(num_dimensions)
C3 = 2 * np.random.random(num_dimensions)
X1 = X_alpha - (A1 * np.abs((C1 * X_alpha) - population_vectors[i]))
X2 = X_beta - (A2 * np.abs((C2 * X_beta) - population_vectors[i]))
X3 = X_delta - (A3 * np.abs((C3 * X_delta) - population_vectors[i]))
pos_new = (X1 + X2 + X3) / 3.0
# clip the new solution
pos_new = np.clip(pos_new, -10, 10)
X_new[i] = pos_new
return X_new
def CGWO(population_vectors, X_alpha, X_beta, X_delta, a,
ran1, ran2, ran3, ran4, ran5, ran6):
num_solutions, num_dimensions = population_vectors.shape
X_new = np.zeros((num_solutions, num_dimensions))
for i in range(num_solutions):
A1 = a * (2 * ran1[i, :] - 1)
A2 = a * (2 * ran2[i, :] - 1)
A3 = a * (2 * ran3[i, :] - 1)
C1 = 2 * ran4[i, :]
C2 = 2 * ran5[i, :]
C3 = 2 * ran6[i, :]
X1 = X_alpha - (A1 * np.abs((C1 * X_alpha) - population_vectors[i]))
X2 = X_beta - (A2 * np.abs((C2 * X_beta) - population_vectors[i]))
X3 = X_delta - (A3 * np.abs((C3 * X_delta) - population_vectors[i]))
pos_new = (X1 + X2 + X3) / 3.0
# clip the new solution
# pos_new = np.clip(pos_new, -10, 10)
X_new[i] = pos_new
return X_new
def IGWO(population_vectors, X_alpha, X_beta, X_delta, a_alpha, a_delta):
# Improved GWO
num_solutions, num_dimensions = population_vectors.shape
X_new = np.zeros((num_solutions, num_dimensions))
a_beta = (a_alpha + a_delta) * 0.5
for i in range(num_solutions):
A1 = a_alpha * (2 * np.random.random(num_dimensions) - 1)
A2 = a_beta * (2 * np.random.random(num_dimensions) - 1)
A3 = a_delta * (2 * np.random.random(num_dimensions) - 1)
C1 = 2 * np.random.random(num_dimensions)
C2 = 2 * np.random.random(num_dimensions)
C3 = 2 * np.random.random(num_dimensions)
X1 = X_alpha - (A1 * np.abs((C1 * X_alpha) - population_vectors[i]))
X2 = X_beta - (A2 * np.abs((C2 * X_beta) - population_vectors[i]))
X3 = X_delta - (A3 * np.abs((C3 * X_delta) - population_vectors[i]))
pos_new = (X1 + X2 + X3) / 3.0
X_new[i] = pos_new
return X_new
def HGWO(population_vectors, X_alpha, X_beta, X_delta, a_alpha, a_beta, a_delta):
# Hyperbolic GWO
num_solutions, num_dimensions = population_vectors.shape
X_new = np.zeros((num_solutions, num_dimensions))
for i in range(num_solutions):
A1 = a_alpha * (2 * np.random.random(num_dimensions) - 1)
A2 = a_beta * (2 * np.random.random(num_dimensions) - 1)
A3 = a_delta * (2 * np.random.random(num_dimensions) - 1)
C1 = 2 * np.random.random(num_dimensions)
C2 = 2 * np.random.random(num_dimensions)
C3 = 2 * np.random.random(num_dimensions)
X1 = X_alpha - (A1 * np.abs((C1 * X_alpha) - population_vectors[i]))
X2 = X_beta - (A2 * np.abs((C2 * X_beta) - population_vectors[i]))
X3 = X_delta - (A3 * np.abs((C3 * X_delta) - population_vectors[i]))
pos_new = (X1 + X2 + X3) / 3.0
# pos_new = np.clip(pos_new, -10, 10)
X_new[i] = pos_new
return X_new
def MHGWO(population_vectors, X_alpha, X_beta, X_delta, a_alpha, a_beta, a_delta, R1, R2, R3):
# Modified Search with Hyperbolic GWO
num_solutions, num_dimensions = population_vectors.shape
X_new = np.zeros((num_solutions, num_dimensions))
for i in range(num_solutions):
A1 = a_alpha * (2 * np.random.rand(num_dimensions) - 1)
A2 = a_beta * (2 * np.random.rand(num_dimensions) - 1)
A3 = a_delta * (2 * np.random.rand(num_dimensions) - 1)
C1 = 2 * np.random.rand(num_dimensions)
C2 = 2 * np.random.rand(num_dimensions)
C3 = 2 * np.random.rand(num_dimensions)
X1 = X_alpha - (A1 * np.abs((C1 * X_alpha) - population_vectors[i]))
X2 = X_beta - (A2 * np.abs((C2 * X_beta) - population_vectors[i]))
X3 = X_delta - (A3 * np.abs((C3 * X_delta) - population_vectors[i]))
pos_new = (R1 * X1) + (R2 * X2) + (R3 * X3)
# pos_new = np.clip(pos_new, -10, 10)
X_new[i] = pos_new
return X_new