Skip to content

Commit

Permalink
Removed unnecessary imports
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrKlanovets committed Nov 2, 2020
1 parent d2db0b2 commit 7e2bcf9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
13 changes: 6 additions & 7 deletions models/single_objective/bat_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import random
import math
from collections import namedtuple
from models.swarm_algorithm import SwarmAlgorithm
Expand All @@ -26,15 +25,15 @@ class BatSearch(SwarmAlgorithm):
params : BatSearchParams
Model behavioral parameters.
bounds : ndarray
A 2 by D matrix containing lower and upper bounds of the search space
A 2 by D matrix containing lower and upper bounds of the search space
for each dimension.
seed : int, optional, default=None
Random generator seed.
max_iter : int, optional, default=100
Maximum number of iterations (generations).
stag_iter : int, optional, default=100
Specifies the allowed number of iterations without solution improvement
by equal or more than a given tolerance. If the number is exceeded,
by equal or more than a given tolerance. If the number is exceeded,
the optimization process stagnations occurs and the algorithm stops.
e : float, optional, default=1e-5
Tolerance.
Expand All @@ -61,7 +60,7 @@ class BatSearch(SwarmAlgorithm):
An array of size N representing the value of the fitness function
for each particle.
gbest : ndarray
A D-dimensional vector representing the position of the current
A D-dimensional vector representing the position of the current
global best particle.
gbest_score : float
The value of the fitness function for the current global best particle.
Expand Down Expand Up @@ -148,7 +147,7 @@ def __local_search(self, new_solutions):
No value.
'''
apply_indices = np.random.rand(self.N) > self.rates
apply_size = np.sum(apply_indices)
apply_size = np.sum(apply_indices)

rand_coefs = np.random.normal(size=(apply_size, self.D))
new_solutions[apply_indices] = np.copy(self.gbest)
Expand All @@ -168,7 +167,7 @@ def optimize(self):
-------
ndarray
The coordinates of the global best particle at the end of
the optimization process.
the optimization process.
'''
i = 0
stag_count = 0
Expand All @@ -188,7 +187,7 @@ def optimize(self):
# Increase rate and reduce loudness.
self.loudness[apply_indices] *= self.alpha
self.rates[apply_indices] = self.r_0 * (1 - math.exp(-self.gamma * (i + 1)))

self.update_best()
self.eval_num += self.N
i += 1
Expand Down
15 changes: 7 additions & 8 deletions models/single_objective/cuckoo_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class CuckooSearch(SwarmAlgorithm):
params : CuckooSearchParams
Model behavioral parameters.
bounds : ndarray
A 2 by D matrix containing lower and upper bounds of the search space
A 2 by D matrix containing lower and upper bounds of the search space
for each dimension.
seed : int, optional, default=None
Random generator seed.
max_iter : int, optional, default=100
Maximum number of iterations (generations).
stag_iter : int, optional, default=100
Specifies the allowed number of iterations without solution improvement
by equal or more than a given tolerance e. If the number is exceeded,
by equal or more than a given tolerance e. If the number is exceeded,
the optimization process stagnation occurs and the algorithm stops.
e : float, optional, default=1e-5
Tolerance.
Expand All @@ -50,7 +50,7 @@ class CuckooSearch(SwarmAlgorithm):
An array of size N representing the value of the fitness function
for each particle.
gbest : ndarray
A D-dimensional vector representing the position of the current
A D-dimensional vector representing the position of the current
global best particle.
gbest_score : float
The value of the fitness function for the current global best particle.
Expand All @@ -59,12 +59,12 @@ class CuckooSearch(SwarmAlgorithm):
'''
def __init__(self, D, N, fit_func, params, bounds, seed=None, max_iter=100,
stag_iter=100, e=0.00001):
super().__init__(D, N, fit_func, params, bounds, seed, max_iter,
super().__init__(D, N, fit_func, params, bounds, seed, max_iter,
stag_iter, e)

# Levy flight's coefficient calculation.
self.beta = 1.5
self.sigma = math.gamma(1 + self.beta)
self.sigma = math.gamma(1 + self.beta)
self.sigma *= math.sin(math.pi * self.beta / 2)
# math.gamma((1 + self.beta) / 2) * self.beta * 2 ** ((self.beta - 1) / 2)
denominator = math.gamma((1 + self.beta) / 2)
Expand All @@ -83,7 +83,6 @@ def set_params(self, new_params):
Returns
-------
No value.
'''
self.xi = new_params.xi
self.alpha = new_params.alpha
Expand All @@ -100,7 +99,7 @@ def optimize(self):
-------
ndarray
The coordinates of the global best particle at the end of
the optimization process.
the optimization process.
'''
i = 0
# Initialize stagnating iterations counter.
Expand Down Expand Up @@ -172,7 +171,7 @@ def __get_new_solutions(self):

def __empty_nests(self):
'''
Replaces a fraction of nests (determined by xi) by generating
Replaces a fraction of nests (determined by xi) by generating
new solutions.
Parameters
Expand Down
26 changes: 12 additions & 14 deletions models/single_objective/firefly_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import math
from collections import namedtuple
from models.swarm_algorithm import SwarmAlgorithm

Expand All @@ -25,15 +24,15 @@ class FireflyAlgorithm(SwarmAlgorithm):
params : FireflyAlgorithmParams
Model behavioral parameters.
bounds : ndarray
A 2 by D matrix containing lower and upper bounds of the search space
A 2 by D matrix containing lower and upper bounds of the search space
for each dimension.
seed : int, optional, default=None
Random generator seed.
max_iter : int, optional, default=100
Maximum number of iterations (generations).
stag_iter : int, optional, default=100
Specifies the allowed number of iterations without solution improvement
by equal or more than a given tolerance. If the number is exceeded,
by equal or more than a given tolerance. If the number is exceeded,
the optimization process stagnations occurs and the algorithm stops.
e : float, optional, default=1e-5
Tolerance.
Expand All @@ -52,15 +51,15 @@ class FireflyAlgorithm(SwarmAlgorithm):
Light absorption coefficient.
lambd : float
Randomization coefficient determining the weight of the third component
of the update rule, which coordinates the movement towards
of the update rule, which coordinates the movement towards
the global best solution.
particles : ndarray
An N by D array representing the swarm of N particles.
scores : ndarray
An array of size N representing the value of the fitness function
for each particle.
gbest : ndarray
A D-dimensional vector representing the position of the current
A D-dimensional vector representing the position of the current
global best particle.
gbest_score : float
The value of the fitness function for the current global best particle.
Expand All @@ -69,7 +68,7 @@ class FireflyAlgorithm(SwarmAlgorithm):
'''
def __init__(self, D, N, fit_func, params, bounds, seed=None, max_iter=100,
stag_iter=100, e=0.0001):
super().__init__(D, N, fit_func, params, bounds, seed, max_iter,
super().__init__(D, N, fit_func, params, bounds, seed, max_iter,
stag_iter, e)

def reset(self):
Expand Down Expand Up @@ -98,7 +97,6 @@ def set_params(self, new_params):
Returns
-------
No value.
'''
self.beta_0 = new_params.beta_0
self.alpha_0 = new_params.alpha_0
Expand All @@ -118,7 +116,7 @@ def optimize(self):
-------
ndarray
The coordinates of the global best particle at the end of
the optimization process.
the optimization process.
'''
i = 0
# Initialize stagnating iterations counter.
Expand Down Expand Up @@ -148,7 +146,7 @@ def optimize(self):
stag_count += 1
elif stag_count > 0:
stag_count = 0

prev_best_score = self.gbest_score
return self.gbest

Expand All @@ -163,7 +161,7 @@ def __move_first_to_second(self, f1, f2):
A vector representing firefly to move.
f2 : ndarray
A vector representing firefly to move to.
Returns
-------
No value.
Expand All @@ -174,17 +172,17 @@ def __move_first_to_second(self, f1, f2):
# Get the attractiveness of the firefly we want to fly to.
# beta = self.beta_0 * math.exp(-self.gamma * r ** 2)
beta = self.beta_0 / (1 + self.gamma * r ** 2)

diff = f2 - f1
urand_1 = np.random.randn(self.D)

# Update coordinates according to the flight formula.
f1 += beta * diff + self.alpha * urand_1

# Modifications:
# Add an extra term to the flight formula, which uses global best
# Add an extra term to the flight formula, which uses global best
# solution to improve the efficiency.
urand_2 = np.random.randn(self.D)
urand_2 = np.random.randn(self.D)
f1 += self.lambd * urand_2 * (self.gbest - f1)

def __move_all(self):
Expand All @@ -211,7 +209,7 @@ def __move_all(self):

def __reduce_alpha(self, iteration):
'''
Reduces alfa as the number of iterations increases.
Reduces alfa as the number of iterations increases.
Optional modification.
Parameters
Expand Down

0 comments on commit 7e2bcf9

Please sign in to comment.