forked from Snishikant/CreditRating-FeatureSelection-GAW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragonFlyOptimizer.py
211 lines (163 loc) · 7.34 KB
/
DragonFlyOptimizer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Import from stdlib
import logging
# Import modules
import random
import numpy as np
# Import from package
from pyswarms.backend import compute_pbest
from pyswarms.backend.topology import Ring
from pyswarms.base import DiscreteSwarmOptimizer
from pyswarms.utils.console_utils import cli_print
class DragonFlyOptimizer(DiscreteSwarmOptimizer):
def assertions(self):
if self.velocity_clamp is not None:
if not isinstance(self.velocity_clamp, tuple):
raise TypeError("Parameter `velocity_clamp` must be a tuple")
if not len(self.velocity_clamp) == 2:
raise IndexError(
"Parameter `velocity_clamp` must be of " "size 2"
)
if not self.velocity_clamp[0] < self.velocity_clamp[1]:
raise ValueError(
"Make sure that velocity_clamp is in the "
"form (v_min, v_max)"
)
def __init__(
self,
n_particles,
dimensions,
options={},
init_pos=None,
velocity_clamp=None,
ftol=-np.inf,
):
self.logger = logging.getLogger(__name__)
super().__init__(
n_particles=n_particles,
dimensions=dimensions,
options=options,
binary=True,
init_pos=init_pos,
velocity_clamp=velocity_clamp,
ftol=ftol,
)
self.food_fitness = np.inf
self.enemy_fitness = -np.inf
self.food_pos = np.empty(0)
self.enemy_pos = np.empty(0)
self.assertions()
# Initialize the resettable attributes
self.reset()
# Initialize the topology
self.top = Ring(static=False)
def compute_pworst(self, swarm): # Compute enemy position and cost
try:
# Infer dimensions from positions
dimensions = swarm.dimensions
# Create a 1-D and 2-D mask based from comparisons
mask_cost = swarm.current_cost > swarm.pbest_cost
mask_pos = np.repeat(mask_cost[:, np.newaxis], dimensions, axis=1)
# Apply masks
new_pworst_pos = np.where(~mask_pos, swarm.pbest_pos, swarm.position)
new_pworst_cost = np.where(
~mask_cost, swarm.pbest_cost, swarm.current_cost
)
except AttributeError:
msg = "Please pass a Swarm class. You passed {}".format(type(swarm))
self.logger.error(msg)
raise
else:
return (new_pworst_pos, new_pworst_cost)
def _transfer_function(self, v):
"""Helper method for the transfer function
Parameters
----------
x : numpy.ndarray
Input vector for sigmoid computation
Returns
-------
numpy.ndarray
Output transfer function computation
"""
return abs(v / np.sqrt(v ** 2 + 1))
def compute_position(self, velocity):
return np.random.random_sample(size=self.dimensions) < self._transfer_function(velocity)
def optimize(self, objective_func, iters, print_step=1, verbose=1, **kwargs):
ub = 1
lb = 0
for i in range(iters):
w = 0.9 - i * ((0.9 - 0.4) / iters)
my_c = 0.1 - i * ((0.1 - 0) / (iters / 2))
if my_c < 0:
my_c = 0
# print(my_c)
s = 2 * random.random() * my_c # Seperation weight
a = 2 * random.random() * my_c # Alignment weight
c = 2 * random.random() * my_c # Cohesion weight
f = 2 * random.random() # Food attraction weight
e = my_c # Enemy distraction weight
# Compute cost for current position and personal best
self.swarm.current_cost = objective_func(self.swarm.position, **kwargs)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos, **kwargs)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
self.swarm.pworst_pos, self.swarm.pworst_cost = self.compute_pworst(self.swarm)
pmin_cost_idx = np.argmin(self.swarm.pbest_cost)
pmax_cost_idx = np.argmax(self.swarm.pworst_cost)
# pmax_cost_idx = np.argmax(self.swarm.pbest_cost)
# Update gbest from neighborhood
# self.swarm.best_cost = np.min(self.swarm.pbest_cost)
# self.swarm.pbest_pos = self.swarm.pbest_pos[np.argmin(self.swarm.pbest_cost)]
# best_cost_yet_found = np.min(self.swarm.best_cost)
self.swarm.best_pos, self.swarm.best_cost = self.top.compute_gbest(
self.swarm, 2, self.n_particles
)
# Updating Food position
if self.swarm.pbest_cost[pmin_cost_idx] < self.food_fitness:
self.food_fitness = self.swarm.pbest_cost[pmin_cost_idx]
self.food_pos = self.swarm.pbest_pos[pmin_cost_idx]
# Updating Enemy position
if self.swarm.pworst_cost[pmax_cost_idx] > self.enemy_fitness:
self.enemy_fitness = self.swarm.pworst_cost[pmax_cost_idx]
self.enemy_pos = self.swarm.pworst_pos[pmax_cost_idx]
# best_cost_yet_found = np.min(self.swarm.best_cost)
for j in range(self.n_particles):
S = np.zeros(self.dimensions)
A = np.zeros(self.dimensions)
C = np.zeros(self.dimensions)
F = np.zeros(self.dimensions)
E = np.zeros(self.dimensions)
# Calculating Separation(S)
for k in range(self.n_particles):
S += (self.swarm.position[k] - self.swarm.position[j])
S = -S
# Calculating Allignment(A)
for k in range(self.n_particles):
A += self.swarm.velocity[k]
A = (A / self.n_particles)
# Calculating Cohesion
for k in range(self.n_particles):
C += self.swarm.position[k]
C = (C / self.n_particles) - self.swarm.position[j]
F = self.food_pos - self.swarm.position[j] # Calculating Food postion
E = self.enemy_pos - self.swarm.position[j] # Calculating Enemy position
self.swarm.velocity[j] = (s * S + a * A + c * C + f * F + e * E) + w * self.swarm.velocity[j]
self.swarm.position[j] = self.compute_position(self.swarm.velocity[j])
# Print to console
if i % print_step == 0:
cli_print(
"Iteration {}/{}, cost: {}".format(i + 1, iters, np.min(self.swarm.best_cost)),
verbose,
2,
logger=self.logger,
)
# Obtain the final best_cost and the final best_position
# final_best_cost = np.min(self.swarm.pbest_cost)
# final_best_pos = self.swarm.pbest_pos[np.argmin(self.swarm.pbest_cost)]
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.best_pos.copy()
print("==============================\nOptimization finished\n")
print("Final Best Cost : ", final_best_cost, "\nBest Value : ", final_best_pos)
# end_report(
# final_best_cost, final_best_pos, verbose, logger=self.logger
# )
return (final_best_cost, final_best_pos)